Skip to content

Commit c64f3ca

Browse files
committed
commit: add GetFullCommitID
1 parent 483e663 commit c64f3ca

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

commit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,19 @@ func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) {
303303
func (c *Commit) FileStatus() (*CommitFileStatus, error) {
304304
return GetCommitFileStatus(c.repo.Path, c.ID.String())
305305
}
306+
307+
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
308+
func GetFullCommitID(repoPath, shortID string) (string, error) {
309+
if len(shortID) >= 40 {
310+
return shortID, nil
311+
}
312+
313+
commitID, err := NewCommand("rev-parse", shortID).RunInDir(repoPath)
314+
if err != nil {
315+
if strings.Contains(err.Error(), "exit status 128") {
316+
return "", ErrNotExist{shortID, ""}
317+
}
318+
return "", err
319+
}
320+
return strings.TrimSpace(commitID), nil
321+
}

git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111
)
1212

13-
const _VERSION = "0.6.5"
13+
const _VERSION = "0.6.6"
1414

1515
func Version() string {
1616
return _VERSION

repo_commit.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,10 @@ func (repo *Repository) getCommit(id sha1) (*Commit, error) {
126126

127127
// GetCommit returns commit object of by ID string.
128128
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
129-
if len(commitID) != 40 {
130-
var err error
131-
commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path)
132-
if err != nil {
133-
if strings.Contains(err.Error(), "exit status 128") {
134-
return nil, ErrNotExist{commitID, ""}
135-
}
136-
return nil, err
137-
}
129+
var err error
130+
commitID, err = GetFullCommitID(repo.Path, commitID)
131+
if err != nil {
132+
return nil, fmt.Errorf("GetCommitFullID: %v", err)
138133
}
139134
id, err := NewIDFromString(commitID)
140135
if err != nil {

0 commit comments

Comments
 (0)