Skip to content

ListBranches should handle empty case #21910

Closed
@wizpresso-steve-cy-fan

Description

@wizpresso-steve-cy-fan

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/[email protected]/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/[email protected]/mux.go:71 (0x1ac8ef4)
	/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:314 (0x1aca8fb)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/go-chi/chi/[email protected]/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/[email protected]/middleware/strip.go:30 (0x1fb9178)
	/usr/local/go/src/net/http/server.go:2109 (0x95068e)
	/go/pkg/mod/github.com/chi-middleware/[email protected]/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/[email protected]/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions