Skip to content

api: create branches and tags - #8566

Merged
unknwon merged 8 commits into
mainfrom
api-create-branch-tag
Nov 18, 2026
Merged

unknwon merged 8 commits into
mainfrom
api-create-branch-tag

Conversation

@unknwon

@unknwon unknwon commented Nov 15, 2026

Copy link
Copy Markdown
Member

Describe the pull request

Stacked on #8454.

Adds two endpoints, both requiring write access:

  • POST /repos/{owner}/{repo}/branches
  • POST /repos/{owner}/{repo}/tags

Both take {"name": "...", "target": "..."}. target is a branch, tag (annotated tags are peeled) or commit SHA, and defaults to the default branch. They respond 201 with the same body as getting the branch or listing the tag.

The reference is pushed as the user from a temporary repository, the same way the web editor commits. So the repository's hooks run: the activity feed shows the new branch or tag, and the create and push webhooks fire as they would for a git push.

Errors:

  • 422: invalid name (git check-ref-format, plus tag names starting with - or equal to HEAD), name already taken, or target not found.
  • 403: mirror repository, or no write access.

The API reference documents both endpoints. This supersedes #8212 by @Meldiron, which was closed for inactivity.

Link to the issue: Closes #8213, closes #8209

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code or have provided the test plan.
  • I have added an entry to CHANGELOG.

Test plan

Added TestRepository_IsValidTagName. Ran a local server with SQLite and a webhook receiver for create and push:

Request Result
Branch with no target, from a commit SHA, nested/name from a branch 201, refs created on the default branch's commit
Tag on a branch, from an annotated tag, named like an existing branch 201, lightweight tags on the peeled commit
Same branch or tag again 422 already exists
Branch -x, a..b, HEAD, x.lock, @{-1}; tag -x, HEAD, v1/, a b 422 invalid name
Target nope, --output=/tmp/pwned, a missing SHA 422 target does not exist, no file written
Empty repository with no target 422 target main does not exist
Mirror repository 403
User with read access, then write access 403, then 201

GET /branches and GET /tags list the new references. Every creation added a feed entry (create branch, or push tag) for the right user. The receiver got create with ref_type branch or tag for each, plus push for branches. No errors were logged. moon run gogs:lint reports 0 issues.

This was referenced Nov 15, 2026
Base automatically changed from api-v1-flamego to main November 17, 2026 01:50
@mintlify

mintlify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
Gogs 🟢 Ready View Preview Nov 18, 2026, 4:16 AM

💡 Tip: Enable Automations to automatically generate PRs for you.

unknwon and others added 3 commits November 18, 2026 04:15
POST /repos/{owner}/{repo}/branches and /repos/{owner}/{repo}/tags create a
branch or lightweight tag named "name" at "target", which is a branch, tag or
commit and defaults to the default branch. The reference is pushed as the
user, so the hooks of the repository record the activity and fire the create
and push webhooks like a "git push" would.
Meldiron proposed and implemented creating tags through the API in #8212.

Co-authored-by: Meldiron <[email protected]>
… to branches first

Creating a branch or tag whose name clashes with an existing reference path,
e.g., "feature" next to "feature/base", and concurrent requests for the same
name now respond 422 instead of 500 or recording the creation more than once.
A target names a branch before a tag of the same name, like the rest of the
API, and a range such as "a..b" is not a target.
The push reports a reference that is taken, so creating one needs no lock or
check beforehand, and a reference created by a Git push in the meantime is no
longer recorded twice. Git before 2.51 gives no specific reason for a conflict,
so the reference is checked after such a failed push.
@unknwon
unknwon force-pushed the api-create-branch-tag branch from 5788ced to 0d998b4 Compare November 18, 2026 04:16
@unknwon
unknwon merged commit 1860c9e into main Nov 18, 2026
14 checks passed
@unknwon
unknwon deleted the api-create-branch-tag branch November 18, 2026 04:20

This branch was successfully deployed

1 active deployment
staging - docs — 0d998b49 Deployed Nov 18, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Public API: Create branch Public API: Create tag

1 participant