diff options
author | zeripath <art27@cantab.net> | 2019-08-05 21:39:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-05 21:39:39 +0100 |
commit | 7ad67109d732bd560c8da0356aa555be467d786c (patch) | |
tree | 7c7a35761b01e2eec6a823f0caf40748c3b7f327 /modules/git/repo_commit.go | |
parent | 1d8915ad5d9889c02dd98ab2c2f29aa8f5ee4dfa (diff) | |
download | gitea-7ad67109d732bd560c8da0356aa555be467d786c.tar.gz gitea-7ad67109d732bd560c8da0356aa555be467d786c.zip |
Be more strict with git arguments (#7715)
* Be more strict with git arguments
* fix-up commit test
* use bindings for branch name
Diffstat (limited to 'modules/git/repo_commit.go')
-rw-r--r-- | modules/git/repo_commit.go | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 733991612a..28374298c1 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -117,20 +117,26 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) { return commit, nil } -// GetCommit returns commit object of by ID string. -func (repo *Repository) GetCommit(commitID string) (*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", commitID).RunInDir(repo.Path) + actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path) if err != nil { - if strings.Contains(err.Error(), "unknown revision or path") { - return nil, ErrNotExist{commitID, ""} + if strings.Contains(err.Error(), "unknown revision or path") || + strings.Contains(err.Error(), "fatal: Needed a single revision") { + return SHA1{}, ErrNotExist{commitID, ""} } - return nil, err + return SHA1{}, err } commitID = actualCommitID } - id, err := NewIDFromString(commitID) + return NewIDFromString(commitID) +} + +// GetCommit returns commit object of by ID string. +func (repo *Repository) GetCommit(commitID string) (*Commit, error) { + id, err := repo.ConvertToSHA1(commitID) if err != nil { return nil, err } @@ -243,6 +249,7 @@ func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) { } // FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2 +// You must ensure that id1 and id2 are valid commit ids. func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) { stdout, err := NewCommand("diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path) if err != nil { |