ListBranches should handle empty case #9877

Closed
opened 2025-11-02 08:52:05 -06:00 by GiteaMirror · 0 comments
Owner

Originally created by @wizpresso-steve-cy-fan on GitHub (Nov 23, 2022).

Description

We are trying to add Automatic Branch Protection on master branch because #2529 is not going away soon because #20825 is still a work in progress, we are doing it ourselves via Webhook. I know that I should do it on the very first commit but I have noticed there is a panic when calling ListBranches on an empty, totally new Gitea repo:

2022/11/23 08:51:24 [637ddf0c-8] router: completed GET /api/v1/repos/Wizpresso/test/topics for 10.244.199.45:59068, 200 OK in 12.5ms @ repo/topic.go:21(repo.ListTopics)
2022/11/23 08:51:24 ...common/middleware.go:71:1() [E] [637ddf0c-9] PANIC: runtime error: invalid memory address or nil pointer dereference
	/usr/local/go/src/runtime/panic.go:260 (0x459635)
	/usr/local/go/src/runtime/signal_unix.go:835 (0x459605)
	/go/src/code.gitea.io/gitea/modules/git/repo_branch_nogogit.go:66 (0xfaeabd)
	/go/src/code.gitea.io/gitea/modules/git/repo_branch.go:112 (0xfac1e4)
	/go/src/code.gitea.io/gitea/routers/api/v1/repo/branch.go:257 (0x1fd0fca)
	/go/src/code.gitea.io/gitea/modules/web/wrap_convert.go:63 (0x1ef0d33)
	/go/src/code.gitea.io/gitea/modules/web/wrap.go:41 (0x1eef404)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 (0x1acb135)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/modules/web/wrap.go:98 (0x1eefe8c)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/modules/web/wrap.go:98 (0x1eefe8c)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/modules/context/api.go:277 (0x1ad6831)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/routers/api/v1/api.go:1192 (0x202c913)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:71 (0x1ac8ef4)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:314 (0x1aca8fb)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 (0x1acb135)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/routers/common/middleware.go:79 (0x1fbb8e2)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/modules/web/routing/logger_manager.go:123 (0x1eeb113)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30 (0x1fb9178)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/chi-middleware/proxy@v1.1.1/middleware.go:37 (0x1fb5916)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/src/code.gitea.io/gitea/routers/common/middleware.go:32 (0x1fbb731)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:88 (0x1ac8eaf)
	/go/src/code.gitea.io/gitea/modules/web/route.go:200 (0x1eee82d)
	/usr/local/go/src/net/http/server.go:2947 (0x953a8b)
	/usr/local/go/src/net/http/server.go:1991 (0x94f166)
	/usr/local/go/src/runtime/asm_amd64.s:1594 (0x477300)
	
2022/11/23 08:51:24 [637ddf0c-9] router: failed    GET /api/v1/repos/Wizpresso/test/branches for 10.244.199.45:59068, panic in 19.2ms @ repo/branch.go:226(repo.ListBranches), err=runtime error: invalid memory address or nil pointer dereference

Which starts from here:
044c754ea5/routers/api/v1/repo/branch.go (L257-L261)

We are using it to detect main/master and it seems like rather than returning [] it assumed a "Git" repo would exist but it is not yet because the first commit is not set.

Gitea Version

866f567a3

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

I built this version of my own PR here: https://github.com/go-gitea/gitea/pull/21780

Database

SQLite

Originally created by @wizpresso-steve-cy-fan on GitHub (Nov 23, 2022). ### Description We are trying to add Automatic Branch Protection on master branch because #2529 is not going away soon because #20825 is still a work in progress, we are doing it ourselves via Webhook. I know that I should do it on the very first commit but I have noticed there is a panic when calling ListBranches on an empty, totally new Gitea repo: ``` 2022/11/23 08:51:24 [637ddf0c-8] router: completed GET /api/v1/repos/Wizpresso/test/topics for 10.244.199.45:59068, 200 OK in 12.5ms @ repo/topic.go:21(repo.ListTopics) 2022/11/23 08:51:24 ...common/middleware.go:71:1() [E] [637ddf0c-9] PANIC: runtime error: invalid memory address or nil pointer dereference /usr/local/go/src/runtime/panic.go:260 (0x459635) /usr/local/go/src/runtime/signal_unix.go:835 (0x459605) /go/src/code.gitea.io/gitea/modules/git/repo_branch_nogogit.go:66 (0xfaeabd) /go/src/code.gitea.io/gitea/modules/git/repo_branch.go:112 (0xfac1e4) /go/src/code.gitea.io/gitea/routers/api/v1/repo/branch.go:257 (0x1fd0fca) /go/src/code.gitea.io/gitea/modules/web/wrap_convert.go:63 (0x1ef0d33) /go/src/code.gitea.io/gitea/modules/web/wrap.go:41 (0x1eef404) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 (0x1acb135) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/modules/web/wrap.go:98 (0x1eefe8c) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/modules/web/wrap.go:98 (0x1eefe8c) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/modules/context/api.go:277 (0x1ad6831) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/routers/api/v1/api.go:1192 (0x202c913) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:71 (0x1ac8ef4) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:314 (0x1aca8fb) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 (0x1acb135) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/routers/common/middleware.go:79 (0x1fbb8e2) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/modules/web/routing/logger_manager.go:123 (0x1eeb113) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/middleware/strip.go:30 (0x1fb9178) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/chi-middleware/proxy@v1.1.1/middleware.go:37 (0x1fb5916) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/src/code.gitea.io/gitea/routers/common/middleware.go:32 (0x1fbb731) /usr/local/go/src/net/http/server.go:2109 (0x95068e) /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:88 (0x1ac8eaf) /go/src/code.gitea.io/gitea/modules/web/route.go:200 (0x1eee82d) /usr/local/go/src/net/http/server.go:2947 (0x953a8b) /usr/local/go/src/net/http/server.go:1991 (0x94f166) /usr/local/go/src/runtime/asm_amd64.s:1594 (0x477300) 2022/11/23 08:51:24 [637ddf0c-9] router: failed GET /api/v1/repos/Wizpresso/test/branches for 10.244.199.45:59068, panic in 19.2ms @ repo/branch.go:226(repo.ListBranches), err=runtime error: invalid memory address or nil pointer dereference ``` Which starts from here: https://github.com/go-gitea/gitea/blob/044c754ea53f5b81f451451df53aea366f6f700a/routers/api/v1/repo/branch.go#L257-L261 We are using it to detect `main/master` and it seems like rather than returning `[]` it assumed a "Git" repo would exist but it is not yet because the first commit is not set. ### Gitea Version 866f567a3 ### Can you reproduce the bug on the Gitea demo site? No ### Log Gist _No response_ ### Screenshots _No response_ ### Git Version _No response_ ### Operating System _No response_ ### How are you running Gitea? I built this version of my own PR here: https://github.com/go-gitea/gitea/pull/21780 ### Database SQLite
GiteaMirror added the type/bug label 2025-11-02 08:52:05 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#9877