Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasPreviousCommit causes recursive load of commits unnecessarily #14598

Merged
29 changes: 17 additions & 12 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bufio"
"bytes"
"container/list"
"errors"
"fmt"
"image"
"image/color"
Expand All @@ -17,6 +18,7 @@ import (
_ "image/png" // for processing png images
"io"
"net/http"
"os/exec"
"strconv"
"strings"
)
Expand Down Expand Up @@ -264,23 +266,26 @@ func (c *Commit) CommitsBefore() (*list.List, error) {

// HasPreviousCommit returns true if a given commitHash is contained in commit's parents
func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
for i := 0; i < c.ParentCount(); i++ {
commit, err := c.Parent(i)
if err := CheckGitVersionAtLeast("1.8"); err != nil {
_, err := NewCommand("merge-base", "--ancestor", commitHash.String(), c.ID.String()).RunInDir(c.repo.Path)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false, err
}
if commit.ID == commitHash {
return true, nil
}
commitInParentCommit, err := commit.HasPreviousCommit(commitHash)
if err != nil {
return false, err
}
if commitInParentCommit {
return true, nil
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
return false, nil
}
}
return false, err
}
return false, nil

result, err := NewCommand("rev-list", "-n1", commitHash.String()+".."+c.ID.String(), "--").RunInDir(c.repo.Path)
if err != nil {
return false, err
}

return len(strings.TrimSpace(result)) > 0, nil
}

// CommitsBeforeLimit returns num commits before current revision
Expand Down