fix: parse HEAD ref (#38088)

fix #38086
This commit is contained in:
wxiaoguang
2026-06-13 02:27:38 +08:00
committed by GitHub
parent 15ae1bfc8c
commit ae49f65692
3 changed files with 15 additions and 3 deletions
+1 -1
View File
@@ -168,7 +168,7 @@ func (ref RefName) ShortName() string {
if ref.IsFor() { if ref.IsFor() {
return ref.ForBranchName() 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 // RefGroup returns the group type of the reference
+5 -1
View File
@@ -8,6 +8,7 @@ import (
"strings" "strings"
"gitea.dev/modules/git/gitcmd" "gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/setting"
"gitea.dev/modules/util" "gitea.dev/modules/util"
) )
@@ -86,8 +87,11 @@ func (repo *Repository) UnstableGuessRefByShortName(shortName string) RefName {
commit, err := repo.GetCommit(shortName) commit, err := repo.GetCommit(shortName)
if err == nil { if err == nil {
commitIDString := commit.ID.String() 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) return RefName(commitIDString)
} else {
setting.PanicInDevOrTesting("abuse of UnstableGuessRefByShortName, queried %s, got %s", shortName, commitIDString)
} }
} }
return "" return ""
+9 -1
View File
@@ -33,9 +33,17 @@ func TestCompareTag(t *testing.T) {
// A dropdown for both base and head. // A dropdown for both base and head.
assert.Lenf(t, selection.Nodes, 2, "The template has changed") 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") req = NewRequest(t, "GET", "/user2/repo1/compare/invalid").SetHeader("Accept", "text/html")
resp = session.MakeRequest(t, req, http.StatusNotFound) 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) // Compare with inferred default branch (master)