@san-siva/gitsy 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/bin/g-wa +85 -45
  2. package/bin/utils +8 -3
  3. package/package.json +1 -1
package/bin/g-wa CHANGED
@@ -78,7 +78,7 @@ check_branch_in_worktree() {
78
78
  local branch=$1
79
79
  local step_number=$2
80
80
 
81
- print_message "${BLUE}Checking if branch exists in worktrees...${NC}" $step_number
81
+ print_message "${BLUE}Checking if ${NC}${branch}${BLUE} branch exists...${NC}" $step_number
82
82
 
83
83
  # Check if branch is already checked out in another worktree
84
84
  local existing_worktree=$(git worktree list | grep "\[${branch}\]" | awk '{print $1}')
@@ -92,7 +92,6 @@ check_branch_in_worktree() {
92
92
 
93
93
  generate_worktree_path() {
94
94
  local target_branch=$1
95
- local step_number=$2
96
95
 
97
96
  # Determine worktree directory based on current location
98
97
  local worktree_dir
@@ -142,7 +141,7 @@ checkout_or_create_branch() {
142
141
  # Check if branch exists locally
143
142
  if git show-ref --verify --quiet "refs/heads/${target_branch}"; then
144
143
  try_fetching_branch
145
- print_message "${BLUE}Creating worktree for branch ${NC}${target_branch}${BLUE} at ${NC}${path}${NC}" $((step_number + 1))
144
+ print_message "${BLUE}Creating ${NC}${target_branch}${BLUE} worktree...${NC}" $((step_number + 1))
146
145
  create_worktree "${target_branch}" "${path}"
147
146
  return
148
147
  fi
@@ -153,7 +152,7 @@ checkout_or_create_branch() {
153
152
  if git ls-remote --heads origin "${target_branch}" | grep -q "${target_branch}"; then
154
153
  print_message "${GREEN}Branch available on remote.${NC}"
155
154
  try_fetching_branch
156
- print_message "${BLUE}Creating worktree for branch ${NC}${target_branch}${BLUE} at ${NC}${path}${NC}" $((step_number + 2))
155
+ print_message "${BLUE}Creating ${NC}${target_branch}${BLUE} worktree...${NC}" $((step_number + 2))
157
156
  create_worktree "${target_branch}" "${path}"
158
157
  return
159
158
  fi
@@ -162,7 +161,7 @@ checkout_or_create_branch() {
162
161
 
163
162
  create_new_branch=$(prompt_user true "Create new branch?" $((step_number + 2)))
164
163
  if [ "${create_new_branch}" = "y" ]; then
165
- print_message "${BLUE}Creating worktree for branch ${NC}${target_branch}${BLUE} at ${NC}${path}${NC}" $((step_number + 3))
164
+ print_message "${BLUE}Creating ${NC}${target_branch}${BLUE} worktree...${NC}" $((step_number + 3))
166
165
  create_worktree "${target_branch}" "${path}" true
167
166
  return
168
167
  fi
@@ -177,6 +176,7 @@ prompt_restructure_confirmation() {
177
176
  local repo_name=$2
178
177
  local default_branch=$3
179
178
  local step_number=$4
179
+ local target_branch=$5
180
180
 
181
181
  # Save the current branch before restructuring
182
182
  local original_branch=$(get_current_branch)
@@ -197,13 +197,24 @@ prompt_restructure_confirmation() {
197
197
 
198
198
  # If not on default branch, mention worktree will be created
199
199
  if [ "$original_branch" != "$default_branch" ]; then
200
- local sanitized_name=$(sanitize_branch_name "$original_branch")
200
+ local sanitized_original=$(sanitize_branch_name "$original_branch")
201
+ local sanitized_target=$(sanitize_branch_name "$target_branch")
201
202
  print_message "" 0 >&2
202
203
  print_message "Note: You're currently on ${BLUE}${original_branch}${NC} (not ${BLUE}${default_branch}${NC})" 0 >&2
203
- print_message "Two directories will be created:" 0 >&2
204
- print_message "" 0 >&2
205
- print_message "${BLUE}${repo_name}/main${NC} ${BLUE}${default_branch}${NC} branch" 0 >&2
206
- print_message " ${BLUE}${repo_name}/worktrees/${sanitized_name}${NC} ${BLUE}${original_branch}${NC} branch" 0 >&2
204
+
205
+ # Check if target branch is the same as original branch
206
+ if [ "$target_branch" = "$original_branch" ]; then
207
+ print_message "Two directories will be created:" 0 >&2
208
+ print_message "" 0 >&2
209
+ print_message "• ${BLUE}${repo_name}/main${NC} → ${BLUE}${default_branch}${NC} branch" 0 >&2
210
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_original}${NC} → ${BLUE}${original_branch}${NC} branch" 0 >&2
211
+ else
212
+ print_message "Three directories will be created:" 0 >&2
213
+ print_message "" 0 >&2
214
+ print_message "• ${BLUE}${repo_name}/main${NC} → ${BLUE}${default_branch}${NC} branch" 0 >&2
215
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_original}${NC} → ${BLUE}${original_branch}${NC} branch" 0 >&2
216
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_target}${NC} → ${BLUE}${target_branch}${NC} branch (cut from ${BLUE}${original_branch}${NC})" 0 >&2
217
+ fi
207
218
  fi
208
219
 
209
220
  # Prompt user for confirmation
@@ -266,7 +277,7 @@ checkout_main_in_main_branch() {
266
277
  local step_number=$4
267
278
 
268
279
  # Checkout the default branch into main
269
- print_message "${BLUE}Checking out branch ${NC}${default_branch}${BLUE} into main directory...${NC}" $step_number
280
+ print_message "${BLUE}Creating main worktree...${NC}" $step_number
270
281
  if ! git -c color.ui=always checkout "$default_branch" 2>&1 | indent; then
271
282
  print_message "" -1
272
283
  print_message "${RED}Failed to checkout ${NC}${default_branch}${RED} branch. [Fail]${NC}" -1
@@ -276,16 +287,48 @@ checkout_main_in_main_branch() {
276
287
  print_message "${GREEN}Repository restructured successfully to ${NC}${repo_name}/main/${GREEN}. [DONE]${NC}"
277
288
  }
278
289
 
279
- should_trigger_from_default_branch() {
280
- local default_branch=$1
281
- local original_branch=$2
290
+ checkout_current_branch() {
291
+ local step_number=$1
292
+ local default_branch=$2
293
+ local original_branch=$3
282
294
 
283
- if [ "$original_branch" != "$default_branch" ]; then
295
+ if [ -z "$step_number" ]; then
296
+ step_number=0
297
+ fi
298
+
299
+ if [ -z "$default_branch" ]; then
300
+ print_message "" -1
301
+ print_message "${RED}Default branch is not set. [Fail]${NC}" -1
302
+ exit 1
303
+ fi
304
+
305
+ if [ "$default_branch" = "$original_branch" ]; then
306
+ return 0
307
+ fi
308
+
309
+ local path=$(generate_worktree_path "$original_branch" $step_number)
310
+ print_message "${BLUE}Creating ${NC}${original_branch}${BLUE} worktree...${NC}" $step_number
311
+ create_worktree "$original_branch" "$path"
312
+
313
+ # Navigate to the created worktree
314
+ if ! navigate_to_dir "$path"; then
315
+ print_message "" -1
316
+ print_message "${RED}Failed to navigate to ${original_branch} worktree. [Fail]${NC}" -1
317
+ exit 1
318
+ fi
319
+ }
320
+
321
+ already_restructured() {
322
+ local current_dir=$(pwd)
323
+ if ! navigate_to_main_dir "false"; then
324
+ return 1
325
+ fi
326
+ if ! navigate_to_dir "$current_dir"; then
284
327
  print_message "" -1
285
- print_message "${RED}Warning: We only support creating worktrees from the default branch.${NC}" -1
286
- print_message "${RED}Commit or stash changes, checkout to ${NC}${default_branch}${RED}, and try again.${NC}" -1
328
+ print_message "${RED}Failed to navigate back to original directory. [Fail]${NC}" -1
287
329
  exit 1
288
330
  fi
331
+ return 0
289
332
  }
290
333
 
291
334
  create_main_dir_if_necessary() {
@@ -301,17 +344,15 @@ create_main_dir_if_necessary() {
301
344
  local parent_dir=$(basename "$(dirname "$git_root")")
302
345
  local repo_name=$(get_repo_name)
303
346
 
304
- # Check if already in correct structure (repo_name/main/.git)
305
- if [ "$current_dir" = "main" ] && [ "$parent_dir" = "$repo_name" ]; then
347
+ if already_restructured; then
306
348
  return 0
307
349
  fi
308
350
 
351
+ # Save original branch before restructuring
352
+ local original_branch=$(get_current_branch)
353
+
309
354
  # Prompt user for restructuring
310
- prompt_restructure_confirmation "$git_root" "$repo_name" "$default_branch" "$step_number"
311
- local prompt_exit_code=$?
312
- if [ $prompt_exit_code -ne 0 ]; then
313
- exit $prompt_exit_code
314
- fi
355
+ prompt_restructure_confirmation "$git_root" "$repo_name" "$default_branch" "$step_number" "$target_branch"
315
356
 
316
357
  # Get parent directory path
317
358
  local parent_path=$(dirname "$git_root")
@@ -329,18 +370,11 @@ create_main_dir_if_necessary() {
329
370
 
330
371
  # Checkout the default branch into main directory
331
372
  checkout_main_in_main_branch "$repo_name" "$main_dir" "$default_branch" $((step_number + 2))
332
- }
333
373
 
334
- create_target_dir_if_necessary() {
335
- # Generate worktree path
336
- local path=$(generate_worktree_path "${target_branch}" $step_number)
374
+ # Create worktree for original branch and navigate to it (if different from default)
375
+ checkout_current_branch $((step_number + 3)) "$default_branch" "$original_branch"
337
376
 
338
- # Check if branch exists locally
339
- if git show-ref --verify --quiet "refs/heads/${target_branch}"; then
340
- try_fetching_branch
341
- create_worktree "${target_branch}" "${path}"
342
- return
343
- fi
377
+ return 1
344
378
  }
345
379
 
346
380
  main() {
@@ -369,8 +403,6 @@ main() {
369
403
  local default_branch=$(get_default_branch)
370
404
  local current_branch=$(get_current_branch)
371
405
 
372
- should_trigger_from_default_branch "$default_branch" "$current_branch"
373
-
374
406
  local step_number=2
375
407
  if [ "$stash" = true ]; then
376
408
  step_number=3
@@ -381,19 +413,27 @@ main() {
381
413
  local current_dir=$(basename "$git_root")
382
414
  local parent_dir=$(basename "$(dirname "$git_root")")
383
415
  local repo_name=$(get_repo_name)
384
- local needs_restructure=false
385
-
386
- if [ "$current_dir" != "main" ] || [ "$parent_dir" != "$repo_name" ]; then
387
- needs_restructure=true
388
- fi
389
-
390
416
  create_main_dir_if_necessary "$default_branch" $step_number
417
+ local needs_restructure=$?
391
418
 
392
- # Calculate next step number
393
419
  local next_step=$((step_number + 1))
394
- if [ "$needs_restructure" = true ]; then
395
- # Restructuring uses 3 steps: prompt, move, checkout
420
+ if [ $needs_restructure -eq 1 ]; then
396
421
  next_step=$((step_number + 3))
422
+ else
423
+ # Notify user that worktree will be created
424
+ local sanitized_target=$(sanitize_branch_name "$target_branch")
425
+ print_message "" -1
426
+ print_message "${BLUE}Will create ${NC}${target_branch}${BLUE} worktree (cut from ${NC}${current_branch}${BLUE})${NC}" -1
427
+ print_message "Location: ${BLUE}${repo_name}/worktrees/${sanitized_target}${NC}" -1
428
+ print_message "" -1
429
+
430
+ # Prompt user for confirmation
431
+ local proceed=$(prompt_user false "Proceed with creating worktree?" "$next_step")
432
+ if [ "$proceed" != "y" ]; then
433
+ print_message "" -1
434
+ print_message "${RED}Worktree creation cancelled by user.${NC}" -1
435
+ exit 1
436
+ fi
397
437
  fi
398
438
 
399
439
  checkout_or_create_branch $next_step
package/bin/utils CHANGED
@@ -468,6 +468,7 @@ navigate_to_main_dir() {
468
468
  local current_dir=$(pwd)
469
469
  local current_dirname=$(basename "$current_dir")
470
470
  local parent_dirname=$(basename "$(dirname "$current_dir")")
471
+ local should_exit_on_failure=$1
471
472
 
472
473
  # Already in main directory (but not nested main/main)
473
474
  if [ "$current_dirname" = "main" ] && [ "$parent_dirname" != "main" ]; then
@@ -488,9 +489,13 @@ navigate_to_main_dir() {
488
489
  fi
489
490
  fi
490
491
 
491
- print_message "" -1
492
- print_message "${RED}Failed to find main directory. [Fail]${NC}" -1
493
- exit 1
492
+ if [ "$should_exit_on_failure" = "true" ]; then
493
+ print_message "" -1
494
+ print_message "${RED}Failed to find main directory. [Fail]${NC}" -1
495
+ exit 1
496
+ fi
497
+
498
+ return 1
494
499
  }
495
500
 
496
501
  check_uncommitted_changes() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@san-siva/gitsy",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "A set of bash utilities for managing Git repositories with ease",
5
5
  "keywords": [
6
6
  "git",