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
+25 -14
knotserver
+20 -12
knotserver/git/post_receive.go
··· 3 3 import ( 4 4 "bufio" 5 5 "context" 6 + "errors" 6 7 "fmt" 7 8 "io" 9 + "log" 8 10 "strings" 9 11 "time" 10 12 ··· 57 59 ByEmail map[string]int 58 60 } 59 61 60 - func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { 62 + func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) (RefUpdateMeta, error) { 63 + var errs error 64 + 61 65 commitCount, err := g.newCommitCount(line) 62 - if err != nil { 63 - // TODO: log this 64 - } 66 + errors.Join(errs, err) 65 67 66 68 isDefaultRef, err := g.isDefaultBranch(line) 67 - if err != nil { 68 - // TODO: log this 69 - } 69 + errors.Join(errs, err) 70 70 71 71 ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) 72 72 defer cancel() 73 73 breakdown, err := g.AnalyzeLanguages(ctx) 74 - if err != nil { 75 - // TODO: log this 76 - } 74 + errors.Join(errs, err) 77 75 78 76 return RefUpdateMeta{ 79 77 CommitCount: commitCount, 80 78 IsDefaultRef: isDefaultRef, 81 79 LangBreakdown: breakdown, 82 - } 80 + }, errs 83 81 } 84 82 85 83 func (g *GitRepo) newCommitCount(line PostReceiveLine) (CommitCount, error) { ··· 95 93 args := []string{fmt.Sprintf("--max-count=%d", 100)} 96 94 97 95 if line.OldSha.IsZero() { 98 - // just git rev-list <newsha> 96 + // git rev-list <newsha> ^other-branches --not ^this-branch 99 97 args = append(args, line.NewSha.String()) 98 + 99 + branches, _ := g.Branches() 100 + for _, b := range branches { 101 + if !strings.Contains(line.Ref, b.Name) { 102 + args = append(args, fmt.Sprintf("^%s", b.Name)) 103 + } 104 + } 105 + 106 + args = append(args, "--not") 107 + args = append(args, fmt.Sprintf("^%s", line.Ref)) 100 108 } else { 101 109 // git rev-list <oldsha>..<newsha> 102 110 args = append(args, fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String()))
+5 -2
knotserver/internal.go
··· 3 3 import ( 4 4 "context" 5 5 "encoding/json" 6 + "errors" 6 7 "fmt" 7 8 "log/slog" 8 9 "net/http" ··· 145 146 return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) 146 147 } 147 148 148 - meta := gr.RefUpdateMeta(line) 149 + var errs error 150 + meta, err := gr.RefUpdateMeta(line) 151 + errors.Join(errs, err) 149 152 150 153 metaRecord := meta.AsRecord() 151 154 ··· 169 172 EventJson: string(eventJson), 170 173 } 171 174 172 - return h.db.InsertEvent(event, h.n) 175 + return errors.Join(errs, h.db.InsertEvent(event, h.n)) 173 176 } 174 177 175 178 func (h *InternalHandle) triggerPipeline(clientMsgs *[]string, line git.PostReceiveLine, gitUserDid, repoDid, repoName string, pushOptions PushOptions) error {