Skip to content

Commit

Permalink
Fix for bad commitID to show up in error (go-gitea#148)
Browse files Browse the repository at this point in the history
* Fix for bad commitID to show up in error

* Adds test for repo_commit GetCommit

* Adds test if commitID is valid branch
  • Loading branch information
richmahn authored and techknowlogick committed Mar 8, 2019
1 parent 8983773 commit 6cba05a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion repo_commit.go
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions repo_commit_test.go
Expand Up @@ -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)
Expand All @@ -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: ]")
}

0 comments on commit 6cba05a

Please sign in to comment.