Skip to content

Commit

Permalink
Do not assume all 40 char strings are SHA1s (go-gitea#14624)
Browse files Browse the repository at this point in the history
GetCommit() assumes that all 40 char strings are SHA1s. This leads to an
error if you try to do a PR on a branch which is 40 characters long.

This PR attempts the SHA first - and if it fails will switch to using rev-parse.

Fix go-gitea#14470

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath authored Feb 10, 2021
1 parent f9abf94 commit 0a23079
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) {

// ConvertToSHA1 returns a Hash object from a potential ID string
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
if len(commitID) != 40 {
var err error
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
if err != nil {
if strings.Contains(err.Error(), "unknown revision or path") ||
strings.Contains(err.Error(), "fatal: Needed a single revision") {
return SHA1{}, ErrNotExist{commitID, ""}
}
return SHA1{}, err
if len(commitID) == 40 {
sha1, err := NewIDFromString(commitID)
if err == nil {
return sha1, nil
}
commitID = actualCommitID
}
return NewIDFromString(commitID)

actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
if err != nil {
if strings.Contains(err.Error(), "unknown revision or path") ||
strings.Contains(err.Error(), "fatal: Needed a single revision") {
return SHA1{}, ErrNotExist{commitID, ""}
}
return SHA1{}, err
}

return NewIDFromString(actualCommitID)
}

// GetCommit returns commit object of by ID string.
Expand Down

0 comments on commit 0a23079

Please sign in to comment.