From 83ce45b186274154203c9dea629f156ed3eb34a5 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 10 Jan 2026 02:59:03 +0800 Subject: [PATCH] Fix some trivial problems (#36336) (#36337) Partially backport #36336 1. correctly parse git protocol's "OldCommit NewCommit RefName" line, it should be explicitly split by space 2. trim space for the "commit status context name" to follow the same behavior of git_model.NewCommitStatus --- cmd/hook.go | 24 +++++++++++++----------- cmd/hook_test.go | 14 ++++++++++++++ services/actions/commit_status.go | 2 ++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cmd/hook.go b/cmd/hook.go index 2f866dd396b..37fdb927703 100644 --- a/cmd/hook.go +++ b/cmd/hook.go @@ -163,6 +163,14 @@ func (n *nilWriter) WriteString(s string) (int, error) { return len(s), nil } +func parseGitHookCommitRefLine(line string) (oldCommitID, newCommitID string, refFullName git.RefName, ok bool) { + fields := strings.Split(line, " ") + if len(fields) != 3 { + return "", "", "", false + } + return fields[0], fields[1], git.RefName(fields[2]), true +} + func runHookPreReceive(ctx context.Context, c *cli.Command) error { if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal { return nil @@ -228,14 +236,11 @@ Gitea or set your environment appropriately.`, "") continue } - fields := bytes.Fields(scanner.Bytes()) - if len(fields) != 3 { + oldCommitID, newCommitID, refFullName, ok := parseGitHookCommitRefLine(scanner.Text()) + if !ok { continue } - oldCommitID := string(fields[0]) - newCommitID := string(fields[1]) - refFullName := git.RefName(fields[2]) total++ lastline++ @@ -378,16 +383,13 @@ Gitea or set your environment appropriately.`, "") continue } - fields := bytes.Fields(scanner.Bytes()) - if len(fields) != 3 { + var ok bool + oldCommitIDs[count], newCommitIDs[count], refFullNames[count], ok = parseGitHookCommitRefLine(scanner.Text()) + if !ok { continue } fmt.Fprintf(out, ".") - oldCommitIDs[count] = string(fields[0]) - newCommitIDs[count] = string(fields[1]) - refFullNames[count] = git.RefName(fields[2]) - commitID, _ := git.NewIDFromString(newCommitIDs[count]) if refFullNames[count] == git.BranchPrefix+"master" && !commitID.IsZero() && count == total { masterPushed = true diff --git a/cmd/hook_test.go b/cmd/hook_test.go index 86cd4834f20..fefc33c01c9 100644 --- a/cmd/hook_test.go +++ b/cmd/hook_test.go @@ -39,3 +39,17 @@ func TestPktLine(t *testing.T) { assert.NoError(t, err) assert.Equal(t, []byte("0007a\nb"), w.Bytes()) } + +func TestParseGitHookCommitRefLine(t *testing.T) { + oldCommitID, newCommitID, refName, ok := parseGitHookCommitRefLine("a b c") + assert.True(t, ok) + assert.Equal(t, "a", oldCommitID) + assert.Equal(t, "b", newCommitID) + assert.Equal(t, "c", string(refName)) + + _, _, _, ok = parseGitHookCommitRefLine("a\tb\tc") + assert.False(t, ok) + + _, _, _, ok = parseGitHookCommitRefLine("a b") + assert.False(t, ok) +} diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index ef241e5091a..228c60b437a 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "path" + "strings" actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" @@ -91,6 +92,7 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er runName = wfs[0].Name } ctxname := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event) + ctxname = strings.TrimSpace(ctxname) // git_model.NewCommitStatus also trims spaces state := toCommitStatus(job.Status) if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll); err == nil { for _, v := range statuses {