knotserver: count commits from initial push #312

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

commit-count calculation for initial ref-creation pushes failed silently because the underlying call to rev-list would fail.

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

Changed files
+27 -18
knotserver
+27 -18
knotserver/git/post_receive.go
···
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 <newsha>
+
args = append(args, line.NewSha.String())
+
} else {
+
// git rev-list <oldsha>..<newsha>
+
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