From 57cb5eaf41dc08b388cdebf1b6bab9ff61fd2f4d Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 5 Aug 2024 20:53:19 +0200 Subject: [PATCH] [BUG] Allow 4 charachter SHA in `/src/commit` - Adjust the `RepoRefByType` middleware to allow for commit SHAs that are as short as 4 characters (the minium that Git requires). - Integration test added. - Follow up to 4d76bbeda71f585e9212adb13f3ce7b73e583004 - Resolves #4781 (cherry picked from commit b967fce25d4debfdd00ab65ec67e8f8284b22b6b) --- services/context/repo.go | 4 ++-- tests/integration/repo_test.go | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/services/context/repo.go b/services/context/repo.go index e4cacbc53c..9d2fd2ddbe 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -897,7 +897,7 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string { case RepoRefCommit: parts := strings.Split(path, "/") - if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= repo.GetObjectFormat().FullLength() { + if len(parts) > 0 && len(parts[0]) >= 4 && len(parts[0]) <= repo.GetObjectFormat().FullLength() { repo.TreePath = strings.Join(parts[1:], "/") return parts[0] } @@ -1021,7 +1021,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context return cancel } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if len(refName) >= 7 && len(refName) <= ctx.Repo.GetObjectFormat().FullLength() { + } else if len(refName) >= 4 && len(refName) <= ctx.Repo.GetObjectFormat().FullLength() { ctx.Repo.IsViewCommit = true ctx.Repo.CommitID = refName diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go index c1d2f327b4..2637d9b4d8 100644 --- a/tests/integration/repo_test.go +++ b/tests/integration/repo_test.go @@ -710,6 +710,13 @@ func TestCommitView(t *testing.T) { doc := NewHTMLParser(t, resp.Body) commitTitle := doc.Find(".commit-summary").Text() assert.Contains(t, commitTitle, "Initial commit") + + req = NewRequest(t, "GET", "/user2/repo1/src/commit/65f1") + resp = MakeRequest(t, req, http.StatusOK) + + doc = NewHTMLParser(t, resp.Body) + commitTitle = doc.Find(".shortsha").Text() + assert.Contains(t, commitTitle, "65f1bf27bc") }) t.Run("Full commit ID", func(t *testing.T) {