Summary
Both PUT /repos/:owner/:repo/contents/* (create/update a file) and GET /repos/:owner/:repo/branches return a generic 500 error on a repository that has no commits yet (e.g. created via POST /user/repos with auto_init: false), instead of a meaningful error or a graceful empty result.
Reproduction
- Create a repository without an initial commit (
auto_init: false, or any other means of creating an empty repo).
- Call
PUT /repos/:owner/:repo/contents/README.md with a minimal body ({"message": "init", "content": "aGVsbG8="}).
- Observe:
500 {"message":"Something went wrong, please check the server logs for more information.","url":"https://github.com/gogs/docs-api"}
- Similarly,
GET /repos/:owner/:repo/branches on the same repository also returns a generic 500.
Expected behavior
PUT contents: either successfully create the first commit, or return a clear, typed error (e.g. a 4xx with an explanatory message) instead of a generic 500.
GET branches: return an empty array [] for a repository with no branches, rather than erroring.
Likely cause
UpdateRepoFile (internal/database/repo_editor.go) calls r.UpdateLocalCopyBranch(opts.OldBranch), which assumes OldBranch (the repository's default branch) already exists. On a repository with zero commits there is no such branch, so the underlying git operation fails and the error surfaces as a generic 500 rather than a handled/typed error.
Environment
Summary
Both
PUT /repos/:owner/:repo/contents/*(create/update a file) andGET /repos/:owner/:repo/branchesreturn a generic500error on a repository that has no commits yet (e.g. created viaPOST /user/reposwithauto_init: false), instead of a meaningful error or a graceful empty result.Reproduction
auto_init: false, or any other means of creating an empty repo).PUT /repos/:owner/:repo/contents/README.mdwith a minimal body ({"message": "init", "content": "aGVsbG8="}).GET /repos/:owner/:repo/brancheson the same repository also returns a generic500.Expected behavior
PUT contents: either successfully create the first commit, or return a clear, typed error (e.g. a 4xx with an explanatory message) instead of a generic 500.GET branches: return an empty array[]for a repository with no branches, rather than erroring.Likely cause
UpdateRepoFile(internal/database/repo_editor.go) callsr.UpdateLocalCopyBranch(opts.OldBranch), which assumesOldBranch(the repository's default branch) already exists. On a repository with zero commits there is no such branch, so the underlying git operation fails and the error surfaces as a generic 500 rather than a handled/typed error.Environment