Skip to content

Commit 7c5857d

Browse files
committed
refactor: break up cmd_create into smaller functions (#117)
Extract _post_create_copy() and _post_create_next_steps() from cmd_create, reducing it from 268 to ~200 lines. The copy block and next-steps collision detection are now self-contained functions.
1 parent f05a1c2 commit 7c5857d

1 file changed

Lines changed: 72 additions & 60 deletions

File tree

bin/gtr

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,76 @@ main() {
130130
}
131131

132132
# Create command
133+
# Copy files and directories to newly created worktree
134+
# Usage: _post_create_copy repo_root worktree_path
135+
_post_create_copy() {
136+
local repo_root="$1"
137+
local worktree_path="$2"
138+
139+
local includes excludes file_includes
140+
includes=$(cfg_get_all gtr.copy.include copy.include)
141+
excludes=$(cfg_get_all gtr.copy.exclude copy.exclude)
142+
143+
# Read .worktreeinclude file if exists
144+
file_includes=$(parse_pattern_file "$repo_root/.worktreeinclude")
145+
146+
# Merge patterns (newline-separated)
147+
if [ -n "$file_includes" ]; then
148+
if [ -n "$includes" ]; then
149+
includes="$includes"$'\n'"$file_includes"
150+
else
151+
includes="$file_includes"
152+
fi
153+
fi
154+
155+
if [ -n "$includes" ]; then
156+
log_step "Copying files..."
157+
copy_patterns "$repo_root" "$worktree_path" "$includes" "$excludes"
158+
fi
159+
160+
# Copy directories (typically git-ignored dirs like node_modules, .venv)
161+
local dir_includes dir_excludes
162+
dir_includes=$(cfg_get_all gtr.copy.includeDirs copy.includeDirs)
163+
dir_excludes=$(cfg_get_all gtr.copy.excludeDirs copy.excludeDirs)
164+
165+
if [ -n "$dir_includes" ]; then
166+
log_step "Copying directories..."
167+
copy_directories "$repo_root" "$worktree_path" "$dir_includes" "$dir_excludes"
168+
fi
169+
}
170+
171+
# Show next steps after worktree creation (resolves collision for --folder overrides)
172+
# Usage: _post_create_next_steps branch_name folder_name folder_override repo_root base_dir prefix
173+
_post_create_next_steps() {
174+
local branch_name="$1" folder_name="$2" folder_override="$3"
175+
local repo_root="$4" base_dir="$5" prefix="$6"
176+
177+
local next_steps_id
178+
if [ -n "$folder_override" ]; then
179+
# Check if folder_name would resolve to main repo (collision with current branch)
180+
local resolve_result
181+
if resolve_result=$(resolve_target "$folder_name" "$repo_root" "$base_dir" "$prefix" 2>/dev/null); then
182+
unpack_target "$resolve_result"
183+
if [ "$_ctx_is_main" = "1" ]; then
184+
# Collision: folder name matches current branch, use branch name instead
185+
next_steps_id="$branch_name"
186+
else
187+
next_steps_id="$folder_name"
188+
fi
189+
else
190+
next_steps_id="$folder_name"
191+
fi
192+
else
193+
next_steps_id="$branch_name"
194+
fi
195+
196+
echo ""
197+
echo "Next steps:"
198+
echo " git gtr editor $next_steps_id # Open in editor"
199+
echo " git gtr ai $next_steps_id # Start AI tool"
200+
echo " cd \"\$(git gtr go $next_steps_id)\" # Navigate to worktree"
201+
}
202+
133203
cmd_create() {
134204
local branch_name=""
135205
local from_ref=""
@@ -287,38 +357,7 @@ cmd_create() {
287357

288358
# Copy files based on patterns
289359
if [ "$skip_copy" -eq 0 ]; then
290-
local includes excludes file_includes
291-
# Pass .gtrconfig keys as second argument for file-based config
292-
includes=$(cfg_get_all gtr.copy.include copy.include)
293-
excludes=$(cfg_get_all gtr.copy.exclude copy.exclude)
294-
295-
# Read .worktreeinclude file if exists
296-
file_includes=$(parse_pattern_file "$repo_root/.worktreeinclude")
297-
298-
# Merge patterns (newline-separated)
299-
if [ -n "$file_includes" ]; then
300-
if [ -n "$includes" ]; then
301-
includes="$includes"$'\n'"$file_includes"
302-
else
303-
includes="$file_includes"
304-
fi
305-
fi
306-
307-
if [ -n "$includes" ]; then
308-
log_step "Copying files..."
309-
copy_patterns "$repo_root" "$worktree_path" "$includes" "$excludes"
310-
fi
311-
312-
# Copy directories (typically git-ignored dirs like node_modules, .venv)
313-
local dir_includes dir_excludes
314-
# Pass .gtrconfig keys as second argument for file-based config
315-
dir_includes=$(cfg_get_all gtr.copy.includeDirs copy.includeDirs)
316-
dir_excludes=$(cfg_get_all gtr.copy.excludeDirs copy.excludeDirs)
317-
318-
if [ -n "$dir_includes" ]; then
319-
log_step "Copying directories..."
320-
copy_directories "$repo_root" "$worktree_path" "$dir_includes" "$dir_excludes"
321-
fi
360+
_post_create_copy "$repo_root" "$worktree_path"
322361
fi
323362

324363
# Run post-create hooks (unless --no-hooks)
@@ -363,34 +402,7 @@ cmd_create() {
363402

364403
# Show next steps only if no auto-launch flags were used
365404
if [ "$open_editor" -eq 0 ] && [ "$start_ai" -eq 0 ]; then
366-
# Determine identifier to show in next steps
367-
# Use custom folder when --folder specified, otherwise use original branch name
368-
# Guard against collision where folder name resolves to main repo
369-
local next_steps_id
370-
if [ -n "$folder_override" ]; then
371-
# Check if folder_name would resolve to main repo (collision with current branch)
372-
local resolve_result
373-
if resolve_result=$(resolve_target "$folder_name" "$repo_root" "$base_dir" "$prefix" 2>/dev/null); then
374-
unpack_target "$resolve_result"
375-
if [ "$_ctx_is_main" = "1" ]; then
376-
# Collision: folder name matches current branch, use branch name instead
377-
next_steps_id="$branch_name"
378-
else
379-
next_steps_id="$folder_name"
380-
fi
381-
else
382-
# resolve_target failed (shouldn't happen for new worktree), fall back to folder_name
383-
next_steps_id="$folder_name"
384-
fi
385-
else
386-
next_steps_id="$branch_name"
387-
fi
388-
389-
echo ""
390-
echo "Next steps:"
391-
echo " git gtr editor $next_steps_id # Open in editor"
392-
echo " git gtr ai $next_steps_id # Start AI tool"
393-
echo " cd \"\$(git gtr go $next_steps_id)\" # Navigate to worktree"
405+
_post_create_next_steps "$branch_name" "$folder_name" "$folder_override" "$repo_root" "$base_dir" "$prefix"
394406
fi
395407
}
396408

0 commit comments

Comments
 (0)