Skip to content

Contents API (PUT) and branch listing return generic 500 on repositories with zero commits #8396

Description

@f-3-l-i-x

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

  1. Create a repository without an initial commit (auto_init: false, or any other means of creating an empty repo).
  2. Call PUT /repos/:owner/:repo/contents/README.md with a minimal body ({"message": "init", "content": "aGVsbG8="}).
  3. Observe:
    500 {"message":"Something went wrong, please check the server logs for more information.","url":"https://github.com/gogs/docs-api"}
    
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions