Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListBranches should handle empty case #21910

Closed
wizpresso-steve-cy-fan opened this issue Nov 23, 2022 · 0 comments · Fixed by #21921
Closed

ListBranches should handle empty case #21910

wizpresso-steve-cy-fan opened this issue Nov 23, 2022 · 0 comments · Fixed by #21921
Labels
Milestone

Comments

@wizpresso-steve-cy-fan
Copy link

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:

branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
return
}

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

866f567

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: #21780

Database

SQLite

@lunny lunny added this to the 1.17.4 milestone Nov 23, 2022
zeripath pushed a commit that referenced this issue Dec 4, 2022
Fix #21910

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
lunny added a commit to lunny/gitea that referenced this issue Dec 4, 2022
Fix go-gitea#21910

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
lunny added a commit to lunny/gitea that referenced this issue Dec 4, 2022
Fix go-gitea#21910

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
lafriks pushed a commit that referenced this issue Dec 4, 2022
Fix #21910
Backport #21921

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
techknowlogick pushed a commit that referenced this issue Dec 4, 2022
Fix #21910
Backport #21921

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants