Skip to content
Open
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ sudo mv wtp /usr/local/bin/ # or add to PATH
# Automatically tracks remote branch if not found locally
wtp add feature/auth

# Create worktree with new branch
# Create worktree with new branch from HEAD
# → Creates worktree at ../worktrees/feature/new-feature
wtp add -b feature/new-feature

# Create worktree from default_branch specified in .wtp.yml
wtp add -b feature/new-feature

# Create new branch from specific commit
# → Creates worktree at ../worktrees/hotfix/urgent
wtp add -b hotfix/urgent abc1234
Expand Down Expand Up @@ -194,6 +197,9 @@ version: "1.0"
defaults:
# Base directory for worktrees (relative to project root)
base_dir: "../worktrees"
# Optional default branch for new worktrees created with -b
# If omitted, git uses the current HEAD
default_branch: "main"

hooks:
post_create:
Expand Down
6 changes: 4 additions & 2 deletions cmd/wtp/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func addCommandWithCommandExecutor(
}

// Build git worktree command using the new command builder
worktreeCmd := buildWorktreeCommand(cmd, workTreePath, branchName, resolvedTrack)
worktreeCmd := buildWorktreeCommand(cmd, workTreePath, branchName, resolvedTrack, cfg)

// Execute the command
result, err := cmdExec.Execute([]command.Command{worktreeCmd})
Expand Down Expand Up @@ -122,7 +122,7 @@ func addCommandWithCommandExecutor(

// buildWorktreeCommand builds a git worktree command using the new command package
func buildWorktreeCommand(
cmd *cli.Command, workTreePath, _, resolvedTrack string,
cmd *cli.Command, workTreePath, _, resolvedTrack string, cfg *config.Config,
) command.Command {
opts := command.GitWorktreeAddOptions{
Branch: cmd.String("branch"),
Expand Down Expand Up @@ -151,6 +151,8 @@ func buildWorktreeCommand(
if opts.Branch != "" && cmd.Args().Len() > 1 {
commitish = cmd.Args().Get(1)
}
} else if opts.Branch != "" && cfg != nil && cfg.Defaults.DefaultBranch != "" {
commitish = cfg.Defaults.DefaultBranch
}

return command.GitWorktreeAdd(workTreePath, commitish, opts)
Expand Down
39 changes: 33 additions & 6 deletions cmd/wtp/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@

func TestAddCommand_CommandConstruction(t *testing.T) {
tests := []struct {
name string
flags map[string]any
args []string
expectedCommands []command.Command
expectError bool
}{
name string

Check failure on line 331 in cmd/wtp/add_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gofmt)
flags map[string]any
args []string
defaultBranch string
expectedCommands []command.Command
expectError bool
}{
{
name: "basic worktree creation",
flags: map[string]any{
Expand All @@ -358,6 +359,31 @@
}},
expectError: false,
},
{
name: "new branch uses configured default branch",
flags: map[string]any{
"branch": "new-feature",
},
args: []string{},
defaultBranch: "develop",
expectedCommands: []command.Command{{
Name: "git",
Args: []string{"worktree", "add", "-b", "new-feature", "/test/worktrees/new-feature", "develop"},
}},
expectError: false,
},
{
name: "new branch without default branch keeps git HEAD",
flags: map[string]any{
"branch": "new-feature",
},
args: []string{},
expectedCommands: []command.Command{{
Name: "git",
Args: []string{"worktree", "add", "-b", "new-feature", "/test/worktrees/new-feature"},
}},
expectError: false,
},
}

for _, tt := range tests {
Expand All @@ -369,6 +395,7 @@
cfg := &config.Config{
Defaults: config.Defaults{
BaseDir: "/test/worktrees",
DefaultBranch: tt.defaultBranch,
},
}

Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type Config struct {

// Defaults represents default configuration values
type Defaults struct {
// BaseDir is the default directory for new worktrees.
BaseDir string `yaml:"base_dir,omitempty"`
// DefaultBranch is the optional base branch for new worktrees created with -b.
DefaultBranch string `yaml:"default_branch,omitempty"`
}

// Hooks represents the post-create hooks configuration
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
configContent := `version: "1.0"
defaults:
base_dir: "../my-worktrees"
default_branch: "develop"
hooks:
post_create:
- type: copy
Expand Down Expand Up @@ -60,6 +61,10 @@
t.Errorf("Expected base_dir '../my-worktrees', got %s", config.Defaults.BaseDir)
}

if config.Defaults.DefaultBranch != "develop" {
t.Errorf("Expected default_branch 'develop', got %s", config.Defaults.DefaultBranch)
}

if len(config.Hooks.PostCreate) != 3 {
t.Errorf("Expected 3 hooks, got %d", len(config.Hooks.PostCreate))
}
Expand Down Expand Up @@ -108,7 +113,8 @@
config := &Config{
Version: "1.0",
Defaults: Defaults{
BaseDir: "../test-worktrees",

Check failure on line 116 in internal/config/config_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gofmt)
DefaultBranch: "develop",
},
Hooks: Hooks{
PostCreate: []Hook{
Expand Down Expand Up @@ -145,6 +151,10 @@
if loadedConfig.Defaults.BaseDir != config.Defaults.BaseDir {
t.Errorf("Expected base_dir %s, got %s", config.Defaults.BaseDir, loadedConfig.Defaults.BaseDir)
}

if loadedConfig.Defaults.DefaultBranch != config.Defaults.DefaultBranch {
t.Errorf("Expected default_branch %s, got %s", config.Defaults.DefaultBranch, loadedConfig.Defaults.DefaultBranch)
}
}

func TestConfigValidate(t *testing.T) {
Expand Down
Loading