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
This commit is contained in:
Richard Mahn 2019-03-08 00:44:27 -07:00 коммит произвёл techknowlogick
Родитель 8983773ac6
Коммит 6cba05a150
2 изменённых файлов: 12 добавлений и 1 удалений

Просмотреть файл

@ -153,13 +153,14 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
if len(commitID) != 40 {
var err error
commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path)
actualCommitID, err := NewCommand("rev-parse", commitID).RunInDir(repo.Path)
if err != nil {
if strings.Contains(err.Error(), "unknown revision or path") {
return nil, ErrNotExist{commitID, ""}
}
return nil, err
}
commitID = actualCommitID
}
id, err := NewIDFromString(commitID)
if err != nil {

Просмотреть файл

@ -26,6 +26,7 @@ func TestRepository_GetCommitBranches(t *testing.T) {
{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
{"master", []string{"master"}},
}
for _, testCase := range testCases {
commit, err := bareRepo1.GetCommit(testCase.CommitID)
@ -47,3 +48,12 @@ func TestGetTagCommitWithSignature(t *testing.T) {
// test that signature is not in message
assert.Equal(t, "tag", commit.CommitMessage)
}
func TestGetCommitWithBadCommitID(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
commit, err := bareRepo1.GetCommit("bad_branch")
assert.Nil(t, commit)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: bad_branch, rel_path: ]")
}