fix: correctly get stats for API commits (#8756)

- Instead of generating a patch and parsing its contents, use a faster and simple way to get it via `--shortstat`.
- Resolves forgejo/forgejo#8725
- Regression of forgejo/forgejo#7682
- Adds unit test.
- Adds integration test.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8756
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-08-02 13:06:04 +02:00 committed by Gusted
parent e4cd25057f
commit 648a75e687
4 changed files with 115 additions and 7 deletions

View file

@ -183,6 +183,17 @@ func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAddi
return numFiles, totalAdditions, totalDeletions, err
}
// GetCommitStat returns the number of files, total additions and total deletions the commit has.
func (repo *Repository) GetCommitShortStat(commitID string) (numFiles, totalAdditions, totalDeletions int, err error) {
cmd := NewCommand(repo.Ctx, "diff-tree", "--shortstat", "--no-commit-id", "--root").AddDynamicArguments(commitID)
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
return 0, 0, 0, err
}
return parseDiffStat(stdout)
}
// GetDiffShortStat counts number of changed files, number of additions and deletions
func GetDiffShortStat(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
// Now if we call: