diff options
author | zeripath <art27@cantab.net> | 2021-02-13 17:25:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-14 01:25:47 +0800 |
commit | 101fb0d7e2ee90424e7fc27f7d11087a2a480875 (patch) | |
tree | c86686c0a0a67f9cd6dc76c9e327679f855f25da /modules | |
parent | 82637c240aa3b0e0bf290d35b2014953edec5b6e (diff) | |
download | gitea-101fb0d7e2ee90424e7fc27f7d11087a2a480875.tar.gz gitea-101fb0d7e2ee90424e7fc27f7d11087a2a480875.zip |
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_commit.go | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index c9a5efb24e..a00e59edb8 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -129,19 +129,23 @@ 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 - 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 + } + } + + 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, ""} } - commitID = actualCommitID + return SHA1{}, err } - return NewIDFromString(commitID) + + return NewIDFromString(actualCommitID) } // GetCommit returns commit object of by ID string. |