fix(activity): count pulse code stats from the branch's net diff (#40573)

The activity page summed per-commit `git log --numstat`, so a renamed
file counted as an extra `old => new` path and changes that landed
through merge commits were missed. Like GitHub's Pulse, the changed
files, additions and deletions now come from the default branch's net
diff over the period, so git detects renames and changes reverted within
the period no longer count.

`GetCodeActivityStats` on an M3 Max, median of 6 runs:

| Repo | Period | Before | After |
|---|---|---|---|
| gitea | week | 40 ms | 40 ms |
| gitea | year | 2.06 s | 0.51 s |
| golang/go | week | 524 ms | 155 ms |
| golang/go | year | 4.54 s | 0.75 s |

Refs:
https://github.com/go-gitea/gitea/pull/40569#issuecomment-7231088642

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
silverwind
2026-11-28 19:10:44 +00:00
committed by GitHub
parent 16070126c3
commit b08f5fad30
4 changed files with 119 additions and 143 deletions

View File

@@ -15,7 +15,6 @@ import (
"path"
"slices"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
@@ -1497,42 +1496,14 @@ func highlightCodeLines(name, lang string, sections []*DiffSection, isLeft bool,
return lines
}
type DiffShortStat struct {
NumFiles, TotalAddition, TotalDeletion int // these fields are used in templates directly
}
type DiffShortStat = git.DiffShortStat
func GetDiffShortStat(ctx context.Context, gitRepo *git.Repository, opts *DiffCommonOptions) (*DiffShortStat, error) {
_, actualBeforeCommitID, afterCommit, err := prepareDiffCommits(ctx, gitRepo, opts)
if err != nil {
return nil, err
}
stat := &DiffShortStat{}
cmd := gitcmd.NewCommand("diff", "--shortstat").
AddArguments(opts.WhitespaceBehavior...).
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
AddDynamicArguments(actualBeforeCommitID.String(), afterCommit.ID.String())
// output: " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n"
stdout, _, err := cmd.WithRepo(gitRepo).RunStdString(ctx)
if err != nil {
return nil, err
}
stdout = strings.TrimSpace(stdout)
for field := range strings.SplitSeq(stdout, ",") {
field = strings.TrimSpace(field)
num, suffix, ok := strings.Cut(field, " ")
switch {
case strings.Contains(suffix, "file") && strings.Contains(suffix, "change"):
stat.NumFiles, _ = strconv.Atoi(num)
case strings.Contains(suffix, "insertion"):
stat.TotalAddition, _ = strconv.Atoi(num)
case strings.Contains(suffix, "deletion"):
stat.TotalDeletion, _ = strconv.Atoi(num)
case ok:
setting.PanicInDevOrTesting("unexpected diff shortstat output: %s", stdout)
}
}
return stat, nil
return git.GetDiffShortStat(ctx, gitRepo, actualBeforeCommitID.String(), afterCommit.ID.String(), opts.WhitespaceBehavior)
}
// SyncUserSpecificDiff inserts user-specific data such as which files the user has already viewed on the given diff