Do not assume all 40 char strings are SHA1s (#14624) (#14648)

Backport #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 <art27@cantab.net>
pull/14647/head^2
zeripath 4 years ago committed by GitHub
parent 82637c240a
commit 101fb0d7e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      modules/git/repo_commit.go

@ -129,8 +129,13 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, 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
if len(commitID) == 40 {
sha1, err := NewIDFromString(commitID)
if err == nil {
return sha1, nil
}
}
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
if err != nil {
if strings.Contains(err.Error(), "unknown revision or path") ||
@ -139,9 +144,8 @@ func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
}
return SHA1{}, err
}
commitID = actualCommitID
}
return NewIDFromString(commitID)
return NewIDFromString(actualCommitID)
}
// GetCommit returns commit object of by ID string.

Loading…
Cancel
Save