update add cmd to adopt existing branches#101
Open
skarim wants to merge 2 commits into
Open
Conversation
f105d53 to
9a6a247
Compare
1fdd772 to
d522c91
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates gh stack add to allow adopting an existing local git branch (when it’s not already tracked by any stack) instead of rejecting it, aligning add with the adopt-or-create behavior used by gh stack init.
Changes:
- Replace the “branch already exists” hard error with an adopt flow that skips branch creation when the branch already exists in git.
- Update success messaging to distinguish between “created” vs “adopted” branches (with/without commits).
- Add tests covering adoption, rejection when the branch is already in a stack, and adoption combined with commit flags.
Show a summary per file
| File | Description |
|---|---|
| cmd/add.go | Adds adopt logic for existing branches and adjusts branch creation + output paths accordingly. |
| cmd/add_test.go | Adds new tests for adopting existing branches and ensuring stack metadata updates. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (2)
cmd/add.go:207
- When adopting an existing branch,
stageAndValidateruns beforeCheckoutBranch(branchName). That means staging is done on the current branch and can also make the checkout fail due to staged/working-tree changes, so the subsequent commit may not apply cleanly to the adopted branch. Consider checking out the adopted branch first (or moving staging/commit to after checkout unconditionally) so-A/-u/-mreliably operate on the branch being added.
This issue also appears on line 183 of the same file.
// If the branch already exists in git but is not part of any stack,
// adopt it instead of erroring. This mirrors the init command's behavior.
adopted := git.BranchExists(branchName)
// Stage changes before creating the branch so we can fail early if
// there's nothing to commit (avoids leaving an empty orphan branch).
if wantsCommit {
if err := stageAndValidate(cfg, opts); err != nil {
return ErrSilent
}
}
if !adopted {
// Create the new branch from the current HEAD and check it out
if err := git.CreateBranch(branchName, currentBranch); err != nil {
cfg.Errorf("failed to create branch: %s", err)
return ErrSilent
}
}
if err := git.CheckoutBranch(branchName); err != nil {
cfg.Errorf("failed to checkout branch: %s", err)
return ErrSilent
}
cmd/add.go:186
adopted := git.BranchExists(branchName)will adopt any existing local branch, even if it isn’t actually based oncurrentBranch. Previously,addalways created the new layer fromcurrentBranch, which guaranteed a linear “on top of the stack” relationship. It would be safer to validate the adopted branch is a descendant ofcurrentBranch(e.g., usinggit.IsAncestor(currentBranch, branchName)orgit.MergeBase) and error with a clear message if not.
// If the branch already exists in git but is not part of any stack,
// adopt it instead of erroring. This mirrors the init command's behavior.
adopted := git.BranchExists(branchName)
- Files reviewed: 2/2 changed files
- Comments generated: 2
d522c91 to
4f79dfe
Compare
6118a59 to
39f3375
Compare
4f79dfe to
3ac2962
Compare
Previously, `gh stack add` rejected any branch name that already existed in git with a blanket "branch already exists" error. This was overly restrictive — users who create branches ahead of time (e.g. from the GitHub UI or via `git branch`) had no way to incorporate them into a stack without deleting and recreating them. Now, if the specified branch exists in git but is not part of any existing stack, `add` adopts it: it skips branch creation, checks out the existing branch, and appends it to the stack metadata. This mirrors the adopt-or-create pattern already used by `gh stack init`. Branches that belong to another stack are still rejected by the existing `ValidateNoDuplicateBranch` guard, so there is no risk of cross-stack conflicts. Behavioral summary: - Existing branch, not in any stack → adopted (checkout only, no create) - Existing branch, already in a stack → error (unchanged) - Non-existent branch → created (unchanged) - Staging/commit flags (-A, -u, -m) work with adopted branches Tests added: - TestAdd_AdoptsExistingBranch - TestAdd_RejectsExistingBranchInStack - TestAdd_AdoptsExistingBranchWithCommit
3ac2962 to
ae84c7d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allow
gh stack addto adopt existing branchesPreviously,
gh stack addrejected any branch name that already existedin git with a blanket "branch already exists" error. This was overly
restrictive — users who create branches ahead of time (e.g. from the
GitHub UI or via
git branch) had no way to incorporate them into astack without deleting and recreating them.
Now, if the specified branch exists in git but is not part of any
existing stack,
addadopts it: it skips branch creation, checks outthe existing branch, and appends it to the stack metadata. This mirrors
the adopt-or-create pattern already used by
gh stack init.Branches that belong to another stack are still rejected by the existing
ValidateNoDuplicateBranchguard, so there is no risk of cross-stackconflicts.
Behavioral summary:
Tests added:
Stack created with GitHub Stacks CLI • Give Feedback 💬