Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions cmd/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import (
)

var adoptCmd = &cobra.Command{
Use: "adopt [branch]",
Use: "adopt <parent>",
Short: "Start tracking an existing branch",
Long: `Start tracking an existing branch by setting its parent.`,
Args: cobra.MaximumNArgs(1),
RunE: runAdopt,
Long: `Start tracking an existing branch by setting its parent.

By default, adopts the current branch. Use --branch to specify a different branch.`,
Args: cobra.ExactArgs(1),
RunE: runAdopt,
}

var adoptParentFlag string
var adoptBranchFlag string

func init() {
adoptCmd.Flags().StringVar(&adoptParentFlag, "parent", "", "parent branch")
adoptCmd.Flags().StringVar(&adoptBranchFlag, "branch", "", "branch to adopt (default: current branch)")
rootCmd.AddCommand(adoptCmd)
}

Expand All @@ -39,10 +41,13 @@ func runAdopt(cmd *cobra.Command, args []string) error {

g := git.New(cwd)

// Determine branch to adopt
// Parent is the required positional argument
parent := args[0]

// Determine branch to adopt (from flag or current branch)
var branchName string
if len(args) > 0 {
branchName = args[0]
if adoptBranchFlag != "" {
branchName = adoptBranchFlag
} else {
branchName, err = g.CurrentBranch()
if err != nil {
Expand All @@ -60,12 +65,6 @@ func runAdopt(cmd *cobra.Command, args []string) error {
return fmt.Errorf("branch %q is already tracked", branchName)
}

// Determine parent
parent := adoptParentFlag
if parent == "" {
return fmt.Errorf("--parent is required")
}

// Validate parent is trunk or tracked
trunk, err := cfg.GetTrunk()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions e2e/adopt_orphan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func TestAdoptExistingBranch(t *testing.T) {
env.Git("checkout", "-b", "external-branch")
env.CreateCommit("external work")

// Adopt it with explicit parent
env.MustRun("adopt", "external-branch", "--parent", "main")
// Adopt it (current branch with main as parent)
env.MustRun("adopt", "main")

env.AssertStackParent("external-branch", "main")
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/chaos_manual_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestManualBranchCreate(t *testing.T) {
t.Errorf("manual branch should not be tracked, got parent %q", parent)
}

// Can adopt it into the stack
env.MustRun("adopt", "manual-branch", "--parent", "main")
// Can adopt it into the stack (current branch with main as parent)
env.MustRun("adopt", "main")
env.AssertStackParent("manual-branch", "main")

// Now it shows in log
Expand Down
Loading