@@ -12,35 +12,33 @@ DIR_NAME=$(basename "$(pwd)")
1212# Default worktree directory
1313TMP_DIR=" ${HOME} /.wt/worktrees"
1414
15- # Function to run setup script in worktree if it's a new worktree
16- run_setup_if_new () {
17- local worktree_dir=" $1 "
18- local is_new=" $2 "
19- local root_path=" $3 "
15+ # ============================================================================
16+ # Helper Functions
17+ # ============================================================================
2018
21- # Only run setup for newly created worktrees
22- if [ " $is_new " = " true" ]; then
23- # Run setup script if it exists in worktree
24- if [ -f " $worktree_dir /.wt/setup" ]; then
25- echo " Running setup script in worktree..."
26- (cd " $worktree_dir " && ROOT_WORKTREE_PATH=" $root_path " bash .wt/setup)
27- echo " Setup script completed"
28- fi
29- fi
19+ # Sanitize branch name for directory use (replace / and other invalid chars with _)
20+ sanitize_name () {
21+ echo " $1 " | sed ' s/[\\/\\:*?"<>|]/_/g'
3022}
3123
32- case " $1 " in
33- add)
34- if [ -z " $2 " ]; then
35- echo " Usage: $0 add <name>"
36- exit 1
37- fi
24+ # Get worktree directory path
25+ get_worktree_dir () {
26+ local name=" $1 "
27+ local safe_name
28+ safe_name=$( sanitize_name " $name " )
29+ echo " ${TMP_DIR} /${DIR_NAME} -${safe_name} "
30+ }
3831
39- NAME=" $2 "
40- # Sanitize branch name for directory use (replace / and other invalid chars with _)
41- SAFE_NAME=$( echo " $NAME " | sed ' s/[\/\\:*?"<>|]/_/g' )
42- WORKTREE_DIR=" ${TMP_DIR} /${DIR_NAME} -${SAFE_NAME} "
32+ # Get symlink path
33+ get_symlink_path () {
34+ local name=" $1 "
35+ local safe_name
36+ safe_name=$( sanitize_name " $name " )
37+ echo " .wt/worktrees/${safe_name} "
38+ }
4339
40+ # Initialize .wt directory structure
41+ init_wt_directory () {
4442 # Create .wt directory and .gitignore if they don't exist
4543 if [ ! -d " .wt" ]; then
4644 mkdir -p .wt
5452 if [ ! -f " .wt/setup" ]; then
5553 cat > .wt/setup << 'EOF '
5654#! /bin/bash
55+ # wt - Git Worktree Management Tool
56+ # https://github.com/tumf/wt
5757# $ROOT_WORKTREE_PATH is path to the base repository (source tree)
5858if [ -f .wt/setup.local ]; then
5959 source .wt/setup.local
6262 fi
6363
6464 mkdir -p .wt/worktrees
65+ }
6566
66- # Check if worktree already exists
67- if git worktree list --porcelain | grep -q " worktree $WORKTREE_DIR " ; then
68- echo " Worktree already exists at $WORKTREE_DIR "
69- exit 1
70- fi
67+ # Create or add to existing worktree
68+ create_worktree () {
69+ local name=" $1 "
70+ local worktree_dir=" $2 "
7171
7272 # Check if branch exists
73- if git branch --list " $NAME " | grep -q " $NAME " ; then
74- echo " Branch '$NAME ' already exists. Adding worktree without -b flag."
75- git worktree add " $WORKTREE_DIR " " $NAME "
73+ if git branch --list " $name " | grep -q " $name " ; then
74+ echo " Branch '$name ' already exists. Adding worktree without -b flag."
75+ git worktree add " $worktree_dir " " $name "
7676 else
77- echo " Creating new branch '$NAME ' and adding worktree."
78- git worktree add " $WORKTREE_DIR " -b " $NAME "
77+ echo " Creating new branch '$name ' and adding worktree."
78+ git worktree add " $worktree_dir " -b " $name "
79+ fi
80+ }
81+
82+ # Create symlink to worktree
83+ create_symlink () {
84+ local worktree_dir=" $1 "
85+ local symlink_path=" $2 "
86+ local force=" ${3:- false} "
87+
88+ if [ -e " $symlink_path " ]; then
89+ if [ " $force " = true ]; then
90+ echo " Symlink already exists at $symlink_path , removing old symlink"
91+ rm " $symlink_path "
92+ else
93+ echo " Symlink already exists at $symlink_path "
94+ exit 1
95+ fi
96+ fi
97+
98+ ln -s " $worktree_dir " " $symlink_path "
99+ echo " Created symlink $symlink_path -> $worktree_dir "
100+ }
101+
102+ # Run setup script if it exists
103+ run_setup_script () {
104+ if [ -f " .wt/setup" ]; then
105+ echo " Running setup script..."
106+ ROOT_WORKTREE_PATH=" $( pwd) " bash " .wt/setup"
107+ echo " Setup script completed"
108+ fi
109+ }
110+
111+ # Function to run setup script in worktree if it's a new worktree
112+ run_setup_if_new () {
113+ local worktree_dir=" $1 "
114+ local is_new=" $2 "
115+ local root_path=" $3 "
116+
117+ # Only run setup for newly created worktrees
118+ if [ " $is_new " = " true" ]; then
119+ # Run setup script if it exists in worktree
120+ if [ -f " $worktree_dir /.wt/setup" ]; then
121+ echo " Running setup script in worktree..."
122+ (cd " $worktree_dir " && ROOT_WORKTREE_PATH=" $root_path " bash .wt/setup)
123+ echo " Setup script completed"
124+ fi
79125 fi
126+ }
127+
128+ # Check if worktree exists and is valid
129+ worktree_exists () {
130+ local worktree_dir=" $1 "
80131
81- # Create symlink in .wt/worktrees directory
82- SYMLINK_PATH=" .wt/worktrees/${SAFE_NAME} "
83- if [ -e " $SYMLINK_PATH " ]; then
84- echo " Symlink already exists at $SYMLINK_PATH "
132+ if git worktree list --porcelain | grep -q " worktree $worktree_dir " ; then
133+ # Check if it's prunable (directory doesn't exist)
134+ if [ ! -d " $worktree_dir " ]; then
135+ return 1 # Exists but is prunable
136+ fi
137+ return 0 # Exists and is valid
138+ fi
139+ return 1 # Does not exist
140+ }
141+
142+ case " $1 " in
143+ add)
144+ if [ -z " $2 " ]; then
145+ echo " Usage: $0 add <name>"
146+ exit 1
147+ fi
148+
149+ NAME=" $2 "
150+ WORKTREE_DIR=$( get_worktree_dir " $NAME " )
151+ SYMLINK_PATH=$( get_symlink_path " $NAME " )
152+
153+ # Initialize .wt directory structure
154+ init_wt_directory
155+
156+ # Check if worktree already exists
157+ if git worktree list --porcelain | grep -q " worktree $WORKTREE_DIR " ; then
158+ echo " Worktree already exists at $WORKTREE_DIR "
85159 exit 1
86160 fi
87161
88- ln -s " $WORKTREE_DIR " " $SYMLINK_PATH "
162+ # Create worktree
163+ create_worktree " $NAME " " $WORKTREE_DIR "
164+
165+ # Create symlink
166+ create_symlink " $WORKTREE_DIR " " $SYMLINK_PATH "
89167 echo " Created worktree at $WORKTREE_DIR "
90- echo " Created symlink $SYMLINK_PATH -> $WORKTREE_DIR "
91168
92169 # Run setup script if it exists in worktree
93170 ROOT_PATH=" $( pwd) "
@@ -118,9 +195,7 @@ remove | rm)
118195 exit 1
119196 fi
120197
121- # Sanitize branch name for directory use (replace / and other invalid chars with _)
122- SAFE_NAME=$( echo " $NAME " | sed ' s/[\/\\:*?"<>|]/_/g' )
123- SYMLINK_PATH=" .wt/worktrees/${SAFE_NAME} "
198+ SYMLINK_PATH=$( get_symlink_path " $NAME " )
124199
125200 # Debug: show what we're checking
126201 if [ ! -e " $SYMLINK_PATH " ]; then
@@ -188,31 +263,11 @@ go)
188263 fi
189264
190265 NAME=" $2 "
191- # Sanitize branch name for directory use (replace / and other invalid chars with _)
192- SAFE_NAME=$( echo " $NAME " | sed ' s/[\/\\:*?"<>|]/_/g' )
193- WORKTREE_DIR=" ${TMP_DIR} /${DIR_NAME} -${SAFE_NAME} "
194- SYMLINK_PATH=" .wt/worktrees/${SAFE_NAME} "
266+ WORKTREE_DIR=$( get_worktree_dir " $NAME " )
267+ SYMLINK_PATH=$( get_symlink_path " $NAME " )
195268
196- # Create .wt directory and .gitignore if they don't exist
197- if [ ! -d " .wt" ]; then
198- mkdir -p .wt
199- cat > .wt/.gitignore << 'EOF '
200- worktrees
201- *.local
202- EOF
203- fi
204-
205- # Create .wt/setup template if it doesn't exist
206- if [ ! -f " .wt/setup" ]; then
207- cat > .wt/setup << 'EOF '
208- #! /bin/bash
209- # wt - Git Worktree Management Tool
210- # https://github.com/tumf/wt
211- # $ROOT_WORKTREE_PATH is path to the base repository (source tree)
212- EOF
213- fi
214-
215- mkdir -p .wt/worktrees
269+ # Initialize .wt directory structure
270+ init_wt_directory
216271
217272 # Check if worktree already exists
218273 WORKTREE_EXISTS=false
@@ -235,24 +290,12 @@ EOF
235290 if [ " $WORKTREE_EXISTS " = false ] || [ " $IS_PRUNABLE " = true ]; then
236291 echo " Worktree '$NAME ' does not exist, creating it first"
237292
238- # Check if branch exists
239- if git branch --list " $NAME " | grep -q " $NAME " ; then
240- echo " Branch '$NAME ' already exists. Adding worktree without -b flag."
241- git worktree add " $WORKTREE_DIR " " $NAME "
242- else
243- echo " Creating new branch '$NAME ' and adding worktree."
244- git worktree add " $WORKTREE_DIR " -b " $NAME "
245- fi
246-
247- # Create symlink in .wt/worktrees directory
248- if [ -e " $SYMLINK_PATH " ]; then
249- echo " Symlink already exists at $SYMLINK_PATH , removing old symlink"
250- rm " $SYMLINK_PATH "
251- fi
293+ # Create worktree
294+ create_worktree " $NAME " " $WORKTREE_DIR "
252295
253- ln -s " $WORKTREE_DIR " " $SYMLINK_PATH "
296+ # Create symlink (force=true to remove old symlink if exists)
297+ create_symlink " $WORKTREE_DIR " " $SYMLINK_PATH " true
254298 echo " Created worktree at $WORKTREE_DIR "
255- echo " Created symlink $SYMLINK_PATH -> $WORKTREE_DIR "
256299
257300 # Run setup script if it exists in worktree (only for newly created worktrees)
258301 ROOT_PATH=" $( pwd) "
@@ -293,29 +336,12 @@ run)
293336 fi
294337 COMMAND_ARGS=" ${*: 3} "
295338 fi
296- # Sanitize branch name for directory use (replace / and other invalid chars with _)
297- SAFE_NAME=$( echo " $NAME " | sed ' s/[\/\\:*?"<>|]/_/g' )
298- WORKTREE_DIR=" ${TMP_DIR} /${DIR_NAME} -${SAFE_NAME} "
299- SYMLINK_PATH=" .wt/worktrees/${SAFE_NAME} "
300339
301- # Create .wt directory and .gitignore if they don't exist
302- if [ ! -d " .wt" ]; then
303- mkdir -p .wt
304- echo " worktrees" > .wt/.gitignore
305- echo " *.local" > .wt/.gitignore
306- fi
340+ WORKTREE_DIR=$( get_worktree_dir " $NAME " )
341+ SYMLINK_PATH=$( get_symlink_path " $NAME " )
307342
308- # Create .wt/setup template if it doesn't exist
309- if [ ! -f " .wt/setup" ]; then
310- cat > .wt/setup << 'EOF '
311- #! /bin/bash
312- # wt - Git Worktree Management Tool
313- # https://github.com/tumf/wt
314- # $ROOT_WORKTREE_PATH is path to the base repository (source tree)
315- EOF
316- fi
317-
318- mkdir -p .wt/worktrees
343+ # Initialize .wt directory structure
344+ init_wt_directory
319345
320346 # Check if worktree already exists
321347 WORKTREE_EXISTS=false
@@ -338,24 +364,12 @@ EOF
338364 if [ " $WORKTREE_EXISTS " = false ] || [ " $IS_PRUNABLE " = true ]; then
339365 echo " Worktree '$NAME ' does not exist, creating it first"
340366
341- # Check if branch exists
342- if git branch --list " $NAME " | grep -q " $NAME " ; then
343- echo " Branch '$NAME ' already exists. Adding worktree without -b flag."
344- git worktree add " $WORKTREE_DIR " " $NAME "
345- else
346- echo " Creating new branch '$NAME ' and adding worktree."
347- git worktree add " $WORKTREE_DIR " -b " $NAME "
348- fi
349-
350- # Create symlink in .wt/worktrees directory
351- if [ -e " $SYMLINK_PATH " ]; then
352- echo " Symlink already exists at $SYMLINK_PATH , removing old symlink"
353- rm " $SYMLINK_PATH "
354- fi
367+ # Create worktree
368+ create_worktree " $NAME " " $WORKTREE_DIR "
355369
356- ln -s " $WORKTREE_DIR " " $SYMLINK_PATH "
370+ # Create symlink
371+ create_symlink " $WORKTREE_DIR " " $SYMLINK_PATH "
357372 echo " Created worktree at $WORKTREE_DIR "
358- echo " Created symlink $SYMLINK_PATH -> $WORKTREE_DIR "
359373
360374 # Run setup script if it exists in worktree (only for newly created worktrees)
361375 ROOT_PATH=" $( pwd) "
0 commit comments