Skip to content

Commit 0a23079

Browse files
authored
Do not assume all 40 char strings are SHA1s (#14624)
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 #14470 Signed-off-by: Andrew Thornton <[email protected]>
1 parent f9abf94 commit 0a23079

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

modules/git/repo_commit.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) {
3131

3232
// ConvertToSHA1 returns a Hash object from a potential ID string
3333
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
34-
if len(commitID) != 40 {
35-
var err error
36-
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
37-
if err != nil {
38-
if strings.Contains(err.Error(), "unknown revision or path") ||
39-
strings.Contains(err.Error(), "fatal: Needed a single revision") {
40-
return SHA1{}, ErrNotExist{commitID, ""}
41-
}
42-
return SHA1{}, err
34+
if len(commitID) == 40 {
35+
sha1, err := NewIDFromString(commitID)
36+
if err == nil {
37+
return sha1, nil
4338
}
44-
commitID = actualCommitID
4539
}
46-
return NewIDFromString(commitID)
40+
41+
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
42+
if err != nil {
43+
if strings.Contains(err.Error(), "unknown revision or path") ||
44+
strings.Contains(err.Error(), "fatal: Needed a single revision") {
45+
return SHA1{}, ErrNotExist{commitID, ""}
46+
}
47+
return SHA1{}, err
48+
}
49+
50+
return NewIDFromString(actualCommitID)
4751
}
4852

4953
// GetCommit returns commit object of by ID string.

0 commit comments

Comments
 (0)