knotserver: fix commit counting for branch creation #447

merged
opened by oppi.li targeting master from push-ytkuzknmmrmn

rev-list was over-counting

Signed-off-by: oppiliappan me@oppi.li

Changed files
+24 -14
knotserver
+19 -12
knotserver/git/post_receive.go
···
import (
"bufio"
"context"
+
"errors"
"fmt"
"io"
"strings"
···
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) {
···
args := []string{fmt.Sprintf("--max-count=%d", 100)}
if line.OldSha.IsZero() {
-
// just git rev-list <newsha>
+
// git rev-list <newsha> ^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 <oldsha>..<newsha>
args = append(args, fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String()))
+5 -2
knotserver/internal.go
···
import (
"context"
"encoding/json"
+
"errors"
"fmt"
"log/slog"
"net/http"
···
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()
···
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 {