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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ Columns:

### `git-wt migrate`

Bring existing branch worktrees under `git-wt` management.
Bring existing Git worktrees under `git-wt` management.

- Moves the main worktree into `<root>/main/<repo-name>` when it is still a plain clone at `<root>` or on the old layout at `<root>/main`.
- Creates managed worktrees for local branches that do not already have one.
- Renames existing non-managed branch worktrees into the managed path format.
- Does not create worktrees for local branches that do not already have one.

Use `--prompt` | `-p` to review the proposed migrations before applying them.

Expand Down
23 changes: 1 addition & 22 deletions internal/gitwt/gitwt_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ func migrationCandidatesFromRepository(repository *Repository) ([]migrateCandida
return nil, fmt.Errorf("get current directory: %w", err)
}

branchesByWorktree := make(map[string]string, len(porcelainWorktrees))
candidates := make([]migrateCandidate, 0)
candidates := make([]migrateCandidate, 0, len(porcelainWorktrees))
for _, porcelainWorktree := range porcelainWorktrees {
if porcelainWorktree.BranchRef == "" {
continue
Expand All @@ -271,7 +270,6 @@ func migrationCandidatesFromRepository(repository *Repository) ([]migrateCandida
continue
}

branchesByWorktree[branchName] = porcelainWorktree.Path
if filepath.Clean(porcelainWorktree.Path) == filepath.Clean(mainPath) {
continue
}
Expand All @@ -291,25 +289,6 @@ func migrationCandidatesFromRepository(repository *Repository) ([]migrateCandida
})
}

branches, err := repository.localBranches()
if err != nil {
return nil, err
}

for _, branchName := range branches {
if _, ok := branchesByWorktree[branchName]; ok {
continue
}

targetPath := managedWorktreePath(mainPath, branchName)
candidates = append(candidates, migrateCandidate{
Action: "create",
Name: branchName,
TargetPath: targetPath,
DisplayTargetPath: currentRelativePath(currentDirectory, targetPath),
})
}

slices.SortFunc(candidates, func(left, right migrateCandidate) int {
return cmp.Compare(left.Name, right.Name)
})
Expand Down
18 changes: 7 additions & 11 deletions internal/gitwt/gitwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ func TestListFailsWhenBranchHasNoUpstream(t *testing.T) {

testRepository := newTestRepository(t)
testRepository.createLocalBranch(t, branchName)
legacyPath := filepath.Join(testRepository.rootPath, "legacy-no-upstream")
runGitCommand(t, testRepository.mainPath, "worktree", "add", legacyPath, branchName)
testRepository.runGitWT(t, "migrate")

result := testRepository.runGitWT(t, "list")
Expand Down Expand Up @@ -1143,7 +1145,7 @@ func TestMigrateMovesPlainCloneMainIntoNestedLayout(t *testing.T) {
}
}

func TestMigratePlainCloneCreatesBranchWorktreesUnderRoot(t *testing.T) {
func TestMigratePlainCloneLeavesExistingBranchesWithoutWorktrees(t *testing.T) {
const branchName = "dev"

testRepository := newPlainCloneTestRepository(t)
Expand All @@ -1158,13 +1160,9 @@ func TestMigratePlainCloneCreatesBranchWorktreesUnderRoot(t *testing.T) {
}

testRepository.assertPathPresent(t, nestedMainPath)
testRepository.assertPathPresent(t, nestedBranchPath)
testRepository.assertPathMissing(t, nestedBranchPath)
assertCurrentBranchAtPath(t, nestedMainPath, "main")
assertCurrentBranchAtPath(t, nestedBranchPath, branchName)
assertMainWorktreePath(t, nestedMainPath)
if filepath.Dir(filepath.Dir(nestedBranchPath)) != testRepository.rootPath {
t.Fatalf("expected branch worktree under plain clone root %s, got %s", testRepository.rootPath, nestedBranchPath)
}
}

func TestMigrateMovesMainAndOldLayoutFeatureWorktrees(t *testing.T) {
Expand Down Expand Up @@ -1194,7 +1192,7 @@ func TestMigrateMovesMainAndOldLayoutFeatureWorktrees(t *testing.T) {
}
}

func TestMigrateCreatesWorktreesForExistingBranches(t *testing.T) {
func TestMigrateDoesNotCreateWorktreesForExistingBranches(t *testing.T) {
const branchOne = "feature/alpha"
const branchTwo = "feature/beta"

Expand All @@ -1207,10 +1205,8 @@ func TestMigrateCreatesWorktreesForExistingBranches(t *testing.T) {
t.Fatalf("migrate failed: %v\n%s", result.err, result.stderr)
}

testRepository.assertPathPresent(t, testRepository.worktreePath(branchOne))
testRepository.assertPathPresent(t, testRepository.worktreePath(branchTwo))
assertCurrentBranchAtPath(t, testRepository.worktreePath(branchOne), branchOne)
assertCurrentBranchAtPath(t, testRepository.worktreePath(branchTwo), branchTwo)
testRepository.assertPathMissing(t, testRepository.worktreePath(branchOne))
testRepository.assertPathMissing(t, testRepository.worktreePath(branchTwo))
testRepository.assertPathPresent(t, testRepository.mainPath)
assertCurrentBranchAtPath(t, testRepository.mainPath, "main")
}
Expand Down