forked from NYANDEV/forgejo
This PR improves HasPreviousCommit to prevent the automatic and recursive loading of previous commits using git merge-base --is-ancestor and git rev-list Fix #13684 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
f305cffcaf
commit
d3200db041
1 changed files with 28 additions and 16 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"container/list"
|
"container/list"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
_ "image/png" // for processing png images
|
_ "image/png" // for processing png images
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -309,23 +311,33 @@ func (c *Commit) CommitsBefore() (*list.List, error) {
|
||||||
|
|
||||||
// HasPreviousCommit returns true if a given commitHash is contained in commit's parents
|
// HasPreviousCommit returns true if a given commitHash is contained in commit's parents
|
||||||
func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
|
func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
|
||||||
for i := 0; i < c.ParentCount(); i++ {
|
this := c.ID.String()
|
||||||
commit, err := c.Parent(i)
|
that := commitHash.String()
|
||||||
if err != nil {
|
|
||||||
return false, err
|
if this == that {
|
||||||
}
|
|
||||||
if commit.ID == commitHash {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
commitInParentCommit, err := commit.HasPreviousCommit(commitHash)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if commitInParentCommit {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := CheckGitVersionConstraint(">= 1.8.0"); err == nil {
|
||||||
|
_, err := NewCommand("merge-base", "--is-ancestor", that, this).RunInDir(c.repo.Path)
|
||||||
|
if err == nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := NewCommand("rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunInDir(c.repo.Path)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(strings.TrimSpace(result)) > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitsBeforeLimit returns num commits before current revision
|
// CommitsBeforeLimit returns num commits before current revision
|
||||||
|
|
Loading…
Reference in a new issue