Skip to content

Fix #3755: avoid creating orphan branch when push_files fails - #3778

Open
genius-0963 wants to merge 1 commit into
github:mainfrom
genius-0963:fix/issue-3755
Open

genius-0963 wants to merge 1 commit into
github:mainfrom
genius-0963:fix/issue-3755

Conversation

@genius-0963

Copy link
Copy Markdown

Summary

Avoids leaving behind an orphan branch pointing at the default branch's HEAD when push_files fails during tree or commit creation.

Why

Fixes #3755

When push_files pushed to a branch that did not exist, it previously created the new branch reference pointing to the default branch's HEAD commit upfront before creating the tree and commit. If creating the tree (e.g. malformed path component) or creating the commit failed, the new branch reference was not rolled back, leaving behind an orphan branch at the default branch's HEAD.

What changed

  • In pkg/github/repositories.go (PushFiles), deferred branch reference creation for new branches until after the tree and commit objects have been successfully created.
  • Created the new branch reference directly pointing at the new commit SHA via client.Git.CreateRef, eliminating the orphan branch creation when tree or commit operations fail.
  • Removed unused helper createReferenceFromDefaultBranch in pkg/github/repositories_helper.go.
  • Added unit tests in pkg/github/repositories_test.go covering successful push to a new branch, tree creation failure on a new branch (ensuring no branch ref is created), and commit creation failure on a new branch (ensuring no branch ref is created).

MCP impact

  • Tool schema or behavior changed
    push_files behavior is improved so that failed pushes to a non-existent branch leave the repository unchanged without leaving an orphan branch reference. The tool schema and output types remain unchanged.

Prompts tested (tool changes only)

  • "Push file docs/readme.md with content 'hello' to new branch feature-test" (successfully creates the branch directly at the commit)

Security / limits

  • No security or limits impact

Tool renaming

  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed

When push_files targets a non-existent branch, it previously created
the branch reference pointing to the default branch head before creating
the tree and commit. If tree or commit creation subsequently failed (e.g.
due to a malformed path or API error), the newly created branch reference
was left behind at the default branch head.

This commit defers branch reference creation until after the tree and
commit have been successfully created, pointing the new branch reference
directly at the new commit SHA. If tree or commit creation fails, no branch
reference is created in the repository.

Fixes github#3755
@genius-0963
genius-0963 requested a review from a team as a code owner November 9, 2026 17:11
Copilot AI adaptive review requested due to automatic review settings November 9, 2026 17:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change that defers new branch creation until after commit creation.

No open issues 🎉

What changed in this PR

Addresses #3755 by delaying new branch creation in push_files until the tree and commit are successfully created, preventing failed pushes from leaving an orphan branch reference.

Changes:

  • Creates new branch references directly at the completed commit while preserving the update path for existing branches.
  • Removes the unused helper that created branches upfront.
  • Adds tests for successful new-branch pushes and tree or commit creation failures.
File Description
pkg/​github/​repositories.go Defers new branch reference creation until after commit creation.
pkg/​github/​repositories_test.go Adds new-branch success and failure regression tests.
pkg/​github/​repositories_helper.go Removes the obsolete branch-creation helper.

Review effort: Balanced (auto)

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.

push_files leaves an orphan branch at the default branch's head when the push fails

2 participants