[v12.0/forgejo] fix: correctly get stats for API commits (#8758)

**Backport:** https://codeberg.org/forgejo/forgejo/pulls/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.

Co-authored-by: Gusted <postmaster@gusted.xyz>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8758
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2025-08-02 13:46:34 +02:00 committed by Gusted
commit 1ef2c321be
4 changed files with 115 additions and 7 deletions

View file

@ -231,3 +231,22 @@ func TestGetFileHistoryNotOnMaster(t *testing.T) {
assert.Equal(t, "1", resp.Header().Get("X-Total"))
}
func TestAPIReposGitCommit(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441").
AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var apiData api.Commit
DecodeJSON(t, resp, &apiData)
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData.CommitMeta.SHA)
assert.Equal(t, 1, apiData.Stats.Total)
assert.Equal(t, 1, apiData.Stats.Additions)
assert.Equal(t, 0, apiData.Stats.Deletions)
}