diff --git a/modules/git/ref.go b/modules/git/ref.go index 0c9aeae8c0a..7d0bbcbae9b 100644 --- a/modules/git/ref.go +++ b/modules/git/ref.go @@ -168,7 +168,7 @@ func (ref RefName) ShortName() string { if ref.IsFor() { return ref.ForBranchName() } - return string(ref) // usually it is a commit ID + return string(ref) // usually it is a commit ID, or "HEAD" } // RefGroup returns the group type of the reference diff --git a/modules/git/repo_ref.go b/modules/git/repo_ref.go index 5adb1e57353..11235c71b15 100644 --- a/modules/git/repo_ref.go +++ b/modules/git/repo_ref.go @@ -8,6 +8,7 @@ import ( "strings" "gitea.dev/modules/git/gitcmd" + "gitea.dev/modules/setting" "gitea.dev/modules/util" ) @@ -86,8 +87,11 @@ func (repo *Repository) UnstableGuessRefByShortName(shortName string) RefName { commit, err := repo.GetCommit(shortName) if err == nil { commitIDString := commit.ID.String() - if strings.HasPrefix(commitIDString, shortName) { + // make sure the "shortName" is either partial commit ID, or it is HEAD + if strings.HasPrefix(commitIDString, shortName) || shortName == RefNameHead { return RefName(commitIDString) + } else { + setting.PanicInDevOrTesting("abuse of UnstableGuessRefByShortName, queried %s, got %s", shortName, commitIDString) } } return "" diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 6c00b3fa0eb..ac2e014d92f 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -33,9 +33,17 @@ func TestCompareTag(t *testing.T) { // A dropdown for both base and head. assert.Lenf(t, selection.Nodes, 2, "The template has changed") + req = NewRequest(t, "GET", "/user2/repo1/compare/v1.1...HEAD") + resp = session.MakeRequest(t, req, http.StatusOK) + assert.True(t, test.IsNormalPageCompleted(resp.Body.String())) + + req = NewRequest(t, "GET", "/user2/repo1/compare/v1.1...NotExisting").SetHeader("Accept", "text/html") + resp = session.MakeRequest(t, req, http.StatusNotFound) + assert.True(t, test.IsNormalPageCompleted(resp.Body.String())) + req = NewRequest(t, "GET", "/user2/repo1/compare/invalid").SetHeader("Accept", "text/html") resp = session.MakeRequest(t, req, http.StatusNotFound) - assert.True(t, test.IsNormalPageCompleted(resp.Body.String()), "expect 404 page not 500") + assert.True(t, test.IsNormalPageCompleted(resp.Body.String())) } // Compare with inferred default branch (master)