-
Notifications
You must be signed in to change notification settings - Fork 5
feat: expand default worktree base dir with repo placeholders #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
satococoa
wants to merge
6
commits into
main
Choose a base branch
from
codex/update-defaultbasedir-for-worktrees
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c2a41c
feat: expand default base dir with repo name
satococoa 9c14d3d
docs: warn about legacy worktree default
satococoa 6ff7489
test: fix lint regressions
satococoa a950867
fix: update test paths for new DefaultBaseDir and improve path handling
satococoa 3d81425
test: cover default base dir worktree paths
satococoa df8cc63
feat: warn about legacy worktree layout
satococoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/satococoa/wtp/internal/config" | ||
| "github.com/satococoa/wtp/internal/git" | ||
| ) | ||
|
|
||
| const legacyWarningExampleLimit = 3 | ||
|
|
||
| type legacyWorktreeMigration struct { | ||
| currentRel string | ||
| suggestedRel string | ||
| } | ||
|
|
||
| func maybeWarnLegacyWorktreeLayout( | ||
| w io.Writer, | ||
| mainRepoPath string, | ||
| cfg *config.Config, | ||
| worktrees []git.Worktree, | ||
| ) { | ||
| if w == nil { | ||
| w = os.Stderr | ||
| } | ||
|
|
||
| if cfg == nil || mainRepoPath == "" || len(worktrees) == 0 { | ||
| return | ||
| } | ||
|
|
||
| if hasConfigFile(mainRepoPath) { | ||
| return | ||
| } | ||
|
|
||
| migrations := detectLegacyWorktreeMigrations(mainRepoPath, cfg, worktrees) | ||
| if len(migrations) == 0 { | ||
| return | ||
| } | ||
|
|
||
| repoBase := filepath.Base(mainRepoPath) | ||
| fmt.Fprintln(w, "⚠️ Legacy worktree layout detected.") | ||
| fmt.Fprintf(w, " wtp now expects worktrees under '../worktrees/%s/...'\n", repoBase) | ||
| fmt.Fprintln(w, " Move existing worktrees to the new layout (run from the repository root):") | ||
|
|
||
| limit := len(migrations) | ||
| if limit > legacyWarningExampleLimit { | ||
| limit = legacyWarningExampleLimit | ||
| } | ||
| for i := 0; i < limit; i++ { | ||
| migration := migrations[i] | ||
| fmt.Fprintf(w, " git worktree move %s %s\n", migration.currentRel, migration.suggestedRel) | ||
| } | ||
|
|
||
| if len(migrations) > legacyWarningExampleLimit { | ||
| fmt.Fprintf(w, " ... and %d more\n", len(migrations)-legacyWarningExampleLimit) | ||
| } | ||
|
|
||
| fmt.Fprintln(w, " (Alternatively, run 'wtp init' and set defaults.base_dir to keep a custom layout.)") | ||
| fmt.Fprintln(w) | ||
| } | ||
|
|
||
| func detectLegacyWorktreeMigrations( | ||
| mainRepoPath string, | ||
| cfg *config.Config, | ||
| worktrees []git.Worktree, | ||
| ) []legacyWorktreeMigration { | ||
| if cfg == nil || mainRepoPath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| mainRepoPath = filepath.Clean(mainRepoPath) | ||
|
|
||
| newBaseDir := filepath.Clean(cfg.ResolveWorktreePath(mainRepoPath, "")) | ||
| legacyBaseDir := filepath.Clean(filepath.Join(filepath.Dir(mainRepoPath), "worktrees")) | ||
| repoBase := filepath.Base(mainRepoPath) | ||
|
|
||
| if legacyBaseDir == newBaseDir { | ||
| return nil | ||
| } | ||
|
|
||
| var migrations []legacyWorktreeMigration | ||
| for _, wt := range worktrees { | ||
| if wt.IsMain { | ||
| continue | ||
| } | ||
|
|
||
| worktreePath := filepath.Clean(wt.Path) | ||
|
|
||
| if strings.HasPrefix(worktreePath, newBaseDir+string(os.PathSeparator)) || | ||
| worktreePath == newBaseDir { | ||
| continue | ||
| } | ||
|
|
||
| if !strings.HasPrefix(worktreePath, legacyBaseDir+string(os.PathSeparator)) { | ||
| continue | ||
| } | ||
|
|
||
| legacyRel, err := filepath.Rel(legacyBaseDir, worktreePath) | ||
| if err != nil || legacyRel == "." { | ||
| continue | ||
| } | ||
|
|
||
| if strings.HasPrefix(legacyRel, repoBase+string(os.PathSeparator)) { | ||
| // Already under the new structure (worktrees/<repo>/...) | ||
| continue | ||
| } | ||
|
|
||
| suggestedPath := filepath.Join(legacyBaseDir, repoBase, legacyRel) | ||
|
|
||
| currentRel := relativeToRepo(mainRepoPath, worktreePath) | ||
| suggestedRel := relativeToRepo(mainRepoPath, suggestedPath) | ||
|
|
||
| migrations = append(migrations, legacyWorktreeMigration{ | ||
| currentRel: currentRel, | ||
| suggestedRel: suggestedRel, | ||
| }) | ||
| } | ||
|
|
||
| return migrations | ||
| } | ||
|
|
||
| func relativeToRepo(mainRepoPath, targetPath string) string { | ||
| rel, err := filepath.Rel(mainRepoPath, targetPath) | ||
| if err != nil { | ||
| return targetPath | ||
| } | ||
| if !strings.HasPrefix(rel, "..") { | ||
| rel = filepath.Join(".", rel) | ||
| } | ||
| return filepath.Clean(rel) | ||
| } | ||
|
|
||
| func hasConfigFile(mainRepoPath string) bool { | ||
| configPath := filepath.Join(mainRepoPath, config.ConfigFileName) | ||
| _, err := os.Stat(configPath) | ||
| return err == nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote the suggested
git worktree movecommand.If the repo name or worktree path contains spaces (totally valid on macOS/Windows), this suggestion becomes
git worktree move ../worktrees/My Repo/foo ..., which breaks the one-liner the warning asks the user to run. Please emit shell-safe quoting so the guidance works everywhere.This uses Go’s
%qformatter to escape spaces and special characters consistently.📝 Committable suggestion
🤖 Prompt for AI Agents