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
··· 77 ByEmail: byEmail, 78 } 79 80 - if !line.NewSha.IsZero() { 81 - output, err := g.revList( 82 - fmt.Sprintf("--max-count=%d", 100), 83 - fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String()), 84 - ) 85 - if err != nil { 86 - return commitCount, fmt.Errorf("failed to run rev-list: %w", err) 87 - } 88 89 - lines := strings.Split(strings.TrimSpace(string(output)), "\n") 90 - if len(lines) == 1 && lines[0] == "" { 91 - return commitCount, nil 92 - } 93 94 - for _, item := range lines { 95 - obj, err := g.r.CommitObject(plumbing.NewHash(item)) 96 - if err != nil { 97 - continue 98 - } 99 - commitCount.ByEmail[obj.Author.Email] += 1 100 } 101 } 102 103 return commitCount, nil
··· 77 ByEmail: byEmail, 78 } 79 80 + if line.NewSha.IsZero() { 81 + return commitCount, nil 82 + } 83 84 + args := []string{fmt.Sprintf("--max-count=%d", 100)} 85 86 + if line.OldSha.IsZero() { 87 + // just git rev-list <newsha> 88 + args = append(args, line.NewSha.String()) 89 + } else { 90 + // git rev-list <oldsha>..<newsha> 91 + args = append(args, fmt.Sprintf("%s..%s", line.OldSha.String(), line.NewSha.String())) 92 + } 93 + 94 + output, err := g.revList(args...) 95 + if err != nil { 96 + return commitCount, fmt.Errorf("failed to run rev-list: %w", err) 97 + } 98 + 99 + lines := strings.Split(strings.TrimSpace(string(output)), "\n") 100 + if len(lines) == 1 && lines[0] == "" { 101 + return commitCount, nil 102 + } 103 + 104 + for _, item := range lines { 105 + obj, err := g.r.CommitObject(plumbing.NewHash(item)) 106 + if err != nil { 107 + continue 108 } 109 + commitCount.ByEmail[obj.Author.Email] += 1 110 } 111 112 return commitCount, nil