From c747ceb83e63f0f5bbf1433849cef9845d17cd04 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Sun, 10 Aug 2025 23:12:58 +0100 Subject: [PATCH] knotserver: fix commit counting for branch creation Change-Id: ytkuzknmmrmnzunokyqknzzylwtwopmn rev-list was over-counting Signed-off-by: oppiliappan --- knotserver/git/post_receive.go | 31 +++++++++++++++++++------------ knotserver/internal.go | 7 +++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/knotserver/git/post_receive.go b/knotserver/git/post_receive.go index 1b3ea94..e16f416 100644 --- a/knotserver/git/post_receive.go +++ b/knotserver/git/post_receive.go @@ -3,6 +3,7 @@ package git import ( "bufio" "context" + "errors" "fmt" "io" "strings" @@ -57,29 +58,25 @@ type CommitCount struct { ByEmail map[string]int } -func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { +func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) (RefUpdateMeta, error) { + var errs error + commitCount, err := g.newCommitCount(line) - if err != nil { - // TODO: log this - } + errors.Join(errs, err) isDefaultRef, err := g.isDefaultBranch(line) - if err != nil { - // TODO: log this - } + errors.Join(errs, err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) defer cancel() breakdown, err := g.AnalyzeLanguages(ctx) - if err != nil { - // TODO: log this - } + errors.Join(errs, err) return RefUpdateMeta{ CommitCount: commitCount, IsDefaultRef: isDefaultRef, LangBreakdown: breakdown, - } + }, errs } func (g *GitRepo) newCommitCount(line PostReceiveLine) (CommitCount, error) { @@ -95,8 +92,18 @@ func (g *GitRepo) newCommitCount(line PostReceiveLine) (CommitCount, error) { args := []string{fmt.Sprintf("--max-count=%d", 100)} if line.OldSha.IsZero() { - // just git rev-list + // git rev-list ^other-branches --not ^this-branch args = append(args, line.NewSha.String()) + + branches, _ := g.Branches() + for _, b := range branches { + if !strings.Contains(line.Ref, b.Name) { + args = append(args, fmt.Sprintf("^%s", b.Name)) + } + } + + args = append(args, "--not") + args = append(args, fmt.Sprintf("^%s", line.Ref)) } else { // git rev-list .. args = append(args, fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String())) diff --git a/knotserver/internal.go b/knotserver/internal.go index 8fb0bc4..1b5a67b 100644 --- a/knotserver/internal.go +++ b/knotserver/internal.go @@ -3,6 +3,7 @@ package knotserver import ( "context" "encoding/json" + "errors" "fmt" "log/slog" "net/http" @@ -145,7 +146,9 @@ func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, r return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) } - meta := gr.RefUpdateMeta(line) + var errs error + meta, err := gr.RefUpdateMeta(line) + errors.Join(errs, err) metaRecord := meta.AsRecord() @@ -169,7 +172,7 @@ func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, r EventJson: string(eventJson), } - return h.db.InsertEvent(event, h.n) + return errors.Join(errs, h.db.InsertEvent(event, h.n)) } func (h *InternalHandle) triggerPipeline(clientMsgs *[]string, line git.PostReceiveLine, gitUserDid, repoDid, repoName string, pushOptions PushOptions) error { -- 2.43.0