From ceeb8f4dc9ddf75b122d8aab87449289735e32a2 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 14 Apr 2018 23:28:16 +0200 Subject: [PATCH 1/2] GetCommit() returns a ErrNotExist if short commit ID does not exists Currently, GetCommit() returns a generic error if a short commit ID does not exists in a repository. When a commit is not found by git-rev-parse, it returns an errors which contains "fatal: ambiguous argument". GetCommit() now search if the error contains this string, and, if it does, returns an ErrNotExist. The idea is to allow commits to be accessed from gitea with a short commit ID. Without this change, it would return a 500 Internal Server Error when a short ID does not exists in the repository. Signed-off-by: Alban Gruin --- repo_commit.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/repo_commit.go b/repo_commit.go index 56bebd7a3..1ca19c699 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -125,6 +125,9 @@ func (repo *Repository) GetCommit(commitID string) (*Commit, error) { var err error commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path) if err != nil { + if strings.Contains(err.Error(), "fatal: ambiguous argument") { + return nil, ErrNotExist{commitID, ""} + } return nil, err } } From d08a18027bd1b20936968413c0a4dd9051dd6f73 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 21 Apr 2018 19:34:03 +0200 Subject: [PATCH 2/2] GetCommit(): change the comparison for short commit messages `fatal: ambiguous argument` can be the beginning of two errors in git. This changes the comparison to something less ambiguous. Signed-off-by: Alban Gruin --- repo_commit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo_commit.go b/repo_commit.go index 1ca19c699..da6c2b2ed 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -125,7 +125,7 @@ func (repo *Repository) GetCommit(commitID string) (*Commit, error) { var err error commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path) if err != nil { - if strings.Contains(err.Error(), "fatal: ambiguous argument") { + if strings.Contains(err.Error(), "unknown revision or path") { return nil, ErrNotExist{commitID, ""} } return nil, err