go-gitea / gitea

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
https://gitea.com
MIT License
45.06k stars 5.49k forks source link

500 Error in Empty repository #8538

Closed RelicOfTesla closed 5 years ago

RelicOfTesla commented 5 years ago

Description

step1. New/Create Repository step2. http://xxxxx:3000/xxxx/xxxx/issues/new or /xxxx/xxxx/settings/hooks/git response 500 error

...

Screenshots

zeripath commented 5 years ago

It would be helpful to get the log in cases like this, however it appears simple to replicate.

When trying the /issues/new endpoint I get:

2019/10/16 10:20:54 ...les/context/panic.go:35:1() [E] PANIC:: runtime error: invalid memory address or nil pointer dereference
    /usr/lib/go-1.13/src/runtime/panic.go:199 (0x452b4b)
        panicmem: panic(memoryError)
    /usr/lib/go-1.13/src/runtime/signal_unix.go:394 (0x452988)
        sigpanic: panicmem()
    /home/andrew/go/src/code.gitea.io/gitea/modules/git/repo_commit.go:22 (0x954e85)
        (*Repository).GetRefCommitID: ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
    /home/andrew/go/src/code.gitea.io/gitea/modules/git/repo_commit.go:39 (0x9565c8)
        (*Repository).GetBranchCommitID: return repo.GetRefCommitID(BranchPrefix + name)
    /home/andrew/go/src/code.gitea.io/gitea/modules/git/repo_commit.go:149 (0x956571)
        (*Repository).GetBranchCommit: commitID, err := repo.GetBranchCommitID(name)
    /home/andrew/go/src/code.gitea.io/gitea/routers/repo/issue.go:385 (0x125c0d3)
        getFileContentFromDefaultBranch: ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
    /home/andrew/go/src/code.gitea.io/gitea/routers/repo/issue.go:412 (0x125c1cb)
        setTemplateIfExists: content, found := getFileContentFromDefaultBranch(ctx, filename)
    /home/andrew/go/src/code.gitea.io/gitea/routers/repo/issue.go:442 (0x125c717)
        NewIssue: setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
    /usr/lib/go-1.13/src/reflect/value.go:460 (0x49dea5)
    ...

And on /settings/hook/git:

2019/10/16 10:22:31 ...les/context/panic.go:35:1() [E] PANIC:: runtime error: invalid memory address or nil pointer dereference
    /usr/lib/go-1.13/src/runtime/panic.go:199 (0x452b4b)
        panicmem: panic(memoryError)
    /usr/lib/go-1.13/src/runtime/signal_unix.go:394 (0x452988)
        sigpanic: panicmem()
    /home/andrew/go/src/code.gitea.io/gitea/routers/repo/setting.go:685 (0x1297406)
        GitHooks: hooks, err := ctx.Repo.GitRepo.Hooks()
    /home/andrew/go/src/code.gitea.io/gitea/routers/repo/setting.go:685 (0x12973fa)
        GitHooks: hooks, err := ctx.Repo.GitRepo.Hooks()
    /usr/lib/go-1.13/src/reflect/value.go:460 (0x49dea5)
        Value.call: call(frametype, fn, args, uint32(frametype.size), uint32(retOffset))
    /usr/lib/go-1.13/src/reflect/value.go:321 (0x49d663)
        Value.Call: return v.call("Call", in)
    /home/andrew/go/src/code.gitea.io/gitea/vendor/gitea.com/macaron/inject/inject.go:177 (0x9b1d79)
        (*injector).callInvoke: return reflect.ValueOf(f).Call(in), nil
    /home/andrew/go/src/code.gitea.io/gitea/vendor/gitea.com/macaron/inject/inject.go:137 (0x9b1729)
        (*injector).Invoke: return inj.callInvoke(f, t, t.NumIn())
    /home/andrew/go/src/code.gitea.io/gitea/vendor/gitea.com/macaron/macaron/context.go:121 (0x9e18c8)
        (*Context).run: vals, err := c.Invoke(c.handler())
    /home/andrew/go/src/code.gitea.io/gitea/vendor/gitea.com/macaron/macaron/context.go:112 (0x10db6d1)
        (*Context).Next: c.run()
zeripath commented 5 years ago

So the problem is that either ctx.Repo.GitRepo is nil or ctx.Repo is nil.

zeripath commented 5 years ago

https://github.com/go-gitea/gitea/blob/1d9a1a0d456887a458fd17fef417ff9c6bdd58da/modules/context/repo.go#L417-L428

Implies that in an empty repo - ctx.Repo.GitRepo will be nil

zeripath commented 5 years ago

Simplest Solution: