gitea/models
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
..
actions Move jobparser from act repository to Gitea (#36699) 2026-02-22 19:33:01 +00:00
activities Load heatmap data asynchronously (#36622) 2026-02-17 14:03:55 +00:00
admin Don't block site admin's operation if SECRET_KEY is lost (#35721) 2025-10-22 12:35:56 +08:00
asymkey Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
auth Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
avatars Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
db Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
dbfs Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
fixtures Fix get release draft permission check (#36659) 2026-02-22 20:56:46 +00:00
git feat: add branch_count to repository API (#35351) (#36743) 2026-02-27 14:10:01 +00:00
issues Refactor text utility classes to Tailwind CSS (#36703) 2026-02-22 22:56:33 +00:00
migrations Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
organization Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
packages fix(packages/container): data race when uploading container blobs concurrently (#36524) 2026-02-04 00:08:20 +08:00
perm Bump golangci-lint to 2.7.2, enable modernize stringsbuilder (#36180) 2025-12-17 20:50:53 +00:00
project Fix permission check on org project operations (#36318) 2026-01-14 17:29:33 +00:00
pull Fix incorrect viewed files counter if file has changed (#36009) 2025-11-27 14:02:03 +00:00
renderhelper fix: generate IDs for HTML headings without id attribute (#36233) 2026-01-06 05:09:44 +00:00
repo Update tool dependencies and fix new lint issues (#36702) 2026-02-26 19:13:19 +00:00
secret Don't block site admin's operation if SECRET_KEY is lost (#35721) 2025-10-22 12:35:56 +08:00
shared/types Refactor locale&string&template related code (#29165) 2024-02-14 21:48:45 +00:00
system Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
unit Run `gopls modernize` on codebase (#34751) 2025-06-18 01:48:09 +00:00
unittest Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
user Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
webhook Add paging headers (#36521) 2026-02-06 13:12:05 +00:00
main_test.go make writing main test easier (#27270) 2023-09-28 01:38:53 +00:00
repo.go Use db.WithTx/WithTx2 instead of TxContext when possible (#35130) 2025-07-22 10:02:01 -07:00
repo_test.go Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00