Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 6cba05a

Browse files
richmahntechknowlogick
authored andcommitted
Fix for bad commitID to show up in error (#148)
* Fix for bad commitID to show up in error * Adds test for repo_commit GetCommit * Adds test if commitID is valid branch
1 parent 8983773 commit 6cba05a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

repo_commit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,14 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
153153
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
154154
if len(commitID) != 40 {
155155
var err error
156-
commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path)
156+
actualCommitID, err := NewCommand("rev-parse", commitID).RunInDir(repo.Path)
157157
if err != nil {
158158
if strings.Contains(err.Error(), "unknown revision or path") {
159159
return nil, ErrNotExist{commitID, ""}
160160
}
161161
return nil, err
162162
}
163+
commitID = actualCommitID
163164
}
164165
id, err := NewIDFromString(commitID)
165166
if err != nil {

repo_commit_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestRepository_GetCommitBranches(t *testing.T) {
2626
{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
2727
{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
2828
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
29+
{"master", []string{"master"}},
2930
}
3031
for _, testCase := range testCases {
3132
commit, err := bareRepo1.GetCommit(testCase.CommitID)
@@ -47,3 +48,12 @@ func TestGetTagCommitWithSignature(t *testing.T) {
4748
// test that signature is not in message
4849
assert.Equal(t, "tag", commit.CommitMessage)
4950
}
51+
52+
func TestGetCommitWithBadCommitID(t *testing.T) {
53+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
54+
bareRepo1, err := OpenRepository(bareRepo1Path)
55+
commit, err := bareRepo1.GetCommit("bad_branch")
56+
assert.Nil(t, commit)
57+
assert.Error(t, err)
58+
assert.EqualError(t, err, "object does not exist [id: bad_branch, rel_path: ]")
59+
}

0 commit comments

Comments
 (0)