diff options
Diffstat (limited to 'modules/git/repo_commit_gogit.go')
-rw-r--r-- | modules/git/repo_commit_gogit.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/modules/git/repo_commit_gogit.go b/modules/git/repo_commit_gogit.go index 48b0cfe19d..2f9b1c4206 100644 --- a/modules/git/repo_commit_gogit.go +++ b/modules/git/repo_commit_gogit.go @@ -30,6 +30,27 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) { return ref.Hash().String(), nil } +// ConvertToSHA1 returns a Hash object from a potential ID string +func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, 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") || + strings.Contains(err.Error(), "fatal: Needed a single revision") { + return SHA1{}, ErrNotExist{commitID, ""} + } + return SHA1{}, err + } + + return NewIDFromString(actualCommitID) +} + // IsCommitExist returns true if given commit exists in current repository. func (repo *Repository) IsCommitExist(name string) bool { hash := plumbing.NewHash(name) |