From bf7b38532eb98fc09074d2779e0091cc4f7771a5 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Wed, 16 Jul 2025 15:43:27 +0100 Subject: [PATCH] knotserver: count commits from initial push Change-Id: llqkvunvvzyvovnlpqyulzlqyzqlkkqn commit-count calculation for initial ref-creation pushes failed silently because the underlying call to `rev-list` would fail. Signed-off-by: oppiliappan --- knotserver/git/post_receive.go | 45 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/knotserver/git/post_receive.go b/knotserver/git/post_receive.go index 24bbbc1..e2ec8ac 100644 --- a/knotserver/git/post_receive.go +++ b/knotserver/git/post_receive.go @@ -77,27 +77,36 @@ func (g *GitRepo) newCommitCount(line PostReceiveLine) (CommitCount, error) { ByEmail: byEmail, } - if !line.NewSha.IsZero() { - output, err := g.revList( - fmt.Sprintf("--max-count=%d", 100), - fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String()), - ) - if err != nil { - return commitCount, fmt.Errorf("failed to run rev-list: %w", err) - } + if line.NewSha.IsZero() { + return commitCount, nil + } - lines := strings.Split(strings.TrimSpace(string(output)), "\n") - if len(lines) == 1 && lines[0] == "" { - return commitCount, nil - } + args := []string{fmt.Sprintf("--max-count=%d", 100)} - for _, item := range lines { - obj, err := g.r.CommitObject(plumbing.NewHash(item)) - if err != nil { - continue - } - commitCount.ByEmail[obj.Author.Email] += 1 + if line.OldSha.IsZero() { + // just git rev-list + args = append(args, line.NewSha.String()) + } else { + // git rev-list .. + args = append(args, fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String())) + } + + output, err := g.revList(args...) + if err != nil { + return commitCount, fmt.Errorf("failed to run rev-list: %w", err) + } + + lines := strings.Split(strings.TrimSpace(string(output)), "\n") + if len(lines) == 1 && lines[0] == "" { + return commitCount, nil + } + + for _, item := range lines { + obj, err := g.r.CommitObject(plumbing.NewHash(item)) + if err != nil { + continue } + commitCount.ByEmail[obj.Author.Email] += 1 } return commitCount, nil -- 2.43.0