gitea/models/git
James Robinson fde7f7db28
feat: add branch_count to repository API (#35351) (#36743)
Description
This PR adds a branch_count field to the repository API response.
Currently, clients have to fetch all branches via /branches just to
determine the total number of branches. This addition brings Gitea
closer to parity with GitLab's API and improves efficiency for UI/CLI
clients that need this metric.

Linked Issue
Fixes #35351

Changes
API Structs: Added BranchCount field to Repository struct in
modules/structs/repo.go.

Database Logic: Implemented CountBranches in models/git/branch.go using
XORM for efficient counting.

Service Layer: Updated the ToRepo conversion logic in
services/convert/repository.go to populate the new field during API
serialisation.

Tests: Added a new unit test TestCountBranches in
models/git/branch_test.go to verify counts (including handling of
deleted branches).

Screenshots
<img width="196" height="121" alt="Screenshot 2026-02-24 at 21 41 07"
src="https://github.com/user-attachments/assets/cd023e92-f338-448b-9e49-0a5d54cc96c2"
/>

Testing
Manually verified the output using curl against a local Gitea instance.

Verified that adding a branch increments the count and deleting a branch
(soft-delete) decrements it.

Ran backend linting: make lint-backend (Passed).

Ran specific unit test: go test -v -tags "sqlite sqlite_unlock_notify"
./models/git -run TestCountBranches (Passed).

Co-authored-by: silverwind <me@silverwind.io>
2026-02-27 14:10:01 +00:00
..
branch.go feat: add branch_count to repository API (#35351) (#36743) 2026-02-27 14:10:01 +00:00
branch_list.go Fix branch order (#31174) 2024-05-31 09:58:41 +08:00
branch_test.go feat: add branch_count to repository API (#35351) (#36743) 2026-02-27 14:10:01 +00:00
commit_status.go Add quick approve button on PR page (#35678) 2025-10-20 18:46:37 +08:00
commit_status_summary.go Keeping consistent between UI and API about combined commit status state and fix some bugs (#34562) 2025-06-09 04:05:33 +00:00
commit_status_test.go Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
lfs.go Fix bug when do LFS GC (#36500) 2026-02-12 15:27:19 -08:00
lfs_lock.go Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
lfs_lock_list.go Show lock owner instead of repo owner on LFS setting page (#31788) 2024-08-11 14:48:20 +00:00
lfs_lock_test.go LFS locks must belong to the intended repo (#36344) 2026-01-11 12:57:58 +02:00
lfs_test.go Fix bug when do LFS GC (#36500) 2026-02-12 15:27:19 -08:00
main_test.go make writing main test easier (#27270) 2023-09-28 01:38:53 +00:00
protected_branch.go Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
protected_branch_list.go Replace gobwas/glob package (#35478) 2025-09-13 18:01:00 +00:00
protected_branch_list_test.go Enable testifylint rules (#34075) 2025-03-31 01:53:48 -04:00
protected_branch_test.go Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
protected_tag.go Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
protected_tag_test.go Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00