@san-siva/gitsy 1.0.6 → 1.0.8

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.
package/bin/g-db CHANGED
@@ -70,7 +70,8 @@ delete_local_branch() {
70
70
  return 0
71
71
  fi
72
72
 
73
- current_branch=$(get_current_branch)
73
+ local current_branch
74
+ get_current_branch current_branch
74
75
  if [ "${branch}" = "${current_branch}" ]; then
75
76
  print_message "" -1
76
77
  print_message "${RED}Cannot delete the branch you are currently on.${NC}" -1
package/bin/g-diff CHANGED
@@ -96,7 +96,7 @@ format_diff_output() {
96
96
  local skip_next_line=false
97
97
 
98
98
  # Process git diff output line by line
99
- git diff "${source_branch}..origin/${target_branch}" | while IFS= read -r line; do
99
+ git diff "origin/${target_branch}..${source_branch}" | while IFS= read -r line; do
100
100
  if [[ "$line" =~ ^diff\ --git\ a/(.+)\ b/(.+)$ ]]; then
101
101
  # New file section
102
102
  if [ -n "$current_file" ]; then
@@ -105,11 +105,16 @@ format_diff_output() {
105
105
  current_file="${BASH_REMATCH[1]}"
106
106
  echo "${BLUE}file:${NC} ${current_file}"
107
107
 
108
- # Get stats for this file
109
- local adds=$(git diff --numstat "${source_branch}..origin/${target_branch}" -- "$current_file" | awk '{print $1}')
110
- local dels=$(git diff --numstat "${source_branch}..origin/${target_branch}" -- "$current_file" | awk '{print $2}')
111
- if [ -n "$adds" ] && [ -n "$dels" ]; then
112
- echo "${BLUE}stats:${NC} ${GREEN}+${adds}${NC} ${RED}-${dels}${NC}"
108
+ # Get stats for this file
109
+ local adds dels numstat_output
110
+ numstat_output=$(git diff --numstat "origin/${target_branch}..${source_branch}" -- "$current_file" 2>&1)
111
+ local numstat_exit=$?
112
+ if [ $numstat_exit -eq 0 ]; then
113
+ adds=$(echo "$numstat_output" | awk '{print $1}')
114
+ dels=$(echo "$numstat_output" | awk '{print $2}')
115
+ if [ -n "$adds" ] && [ -n "$dels" ]; then
116
+ echo "${BLUE}stats:${NC} ${GREEN}+${adds}${NC} ${RED}-${dels}${NC}"
117
+ fi
113
118
  fi
114
119
  echo "${BLUE}changes:${NC}"
115
120
  echo ""
@@ -143,18 +148,28 @@ format_diff_output() {
143
148
  }
144
149
 
145
150
  has_changes() {
146
- ! git diff --quiet "${source_branch}..origin/${target_branch}" 2>&1
151
+ ! git diff --quiet "origin/${target_branch}..${source_branch}" 2>&1
147
152
  }
148
153
 
149
154
  show_full_diff() {
150
155
  if has_changes; then
151
156
  # Capture and display formatted output
152
- local formatted_output=$(format_diff_output)
157
+ local formatted_output
158
+ formatted_output=$(format_diff_output)
159
+ local format_exit=$?
160
+ if [ $format_exit -ne 0 ]; then
161
+ print_message "" -1
162
+ print_message "${RED}Failed to format diff output. [Fail]${NC}" -1
163
+ return 1
164
+ fi
153
165
  echo "$formatted_output" | indent
154
166
 
155
167
  # Copy formatted output to clipboard (strip color codes)
156
- local clipboard_content=$(sed $'s/\033\[[0-9;]*m//g' <<< "${formatted_output}")
157
- copy_to_clipboard "$clipboard_content" "Formatted diff copied to clipboard."
168
+ local clipboard_content
169
+ clipboard_content=$(sed $'s/\033\[[0-9;]*m//g' <<< "${formatted_output}")
170
+ if [ $? -eq 0 ]; then
171
+ copy_to_clipboard "$clipboard_content" "Formatted diff copied to clipboard."
172
+ fi
158
173
  else
159
174
  print_message "${BLUE}No changes found between ${NC}${source_branch}${BLUE} and ${NC}${target_branch}${BLUE}.${NC}"
160
175
  fi
@@ -162,7 +177,7 @@ show_full_diff() {
162
177
 
163
178
  show_files_only() {
164
179
  local filenames
165
- if ! filenames=$(git diff --name-only "${source_branch}..origin/${target_branch}" 2>&1); then
180
+ if ! filenames=$(git diff --name-only "origin/${target_branch}..${source_branch}" 2>&1); then
166
181
  echo "$filenames" | indent
167
182
  print_message "" -1
168
183
  print_message "${RED}Failed to compare branches.${NC}" -1
@@ -179,7 +194,7 @@ show_files_only() {
179
194
 
180
195
  show_stat_summary() {
181
196
  local result
182
- if ! result=$(git -c color.ui=always diff --stat "${source_branch}..origin/${target_branch}" 2>&1); then
197
+ if ! result=$(git -c color.ui=always diff --stat "origin/${target_branch}..${source_branch}" 2>&1); then
183
198
  echo "$result" | indent
184
199
  print_message "" -1
185
200
  print_message "${RED}Failed to compare branches.${NC}" -1
@@ -190,7 +205,12 @@ show_stat_summary() {
190
205
  echo "$result" | indent
191
206
 
192
207
  # Copy stats to clipboard (strip color codes)
193
- local stats_content=$(git diff --stat "${source_branch}..origin/${target_branch}")
208
+ local stats_content
209
+ if ! stats_content=$(git diff --stat "origin/${target_branch}..${source_branch}" 2>&1); then
210
+ print_message "" -1
211
+ print_message "${RED}Failed to generate stats for clipboard. [Fail]${NC}" -1
212
+ return 1
213
+ fi
194
214
  copy_to_clipboard "$stats_content" "Stats copied to clipboard."
195
215
  else
196
216
  print_message "${BLUE}No changes found between ${NC}${source_branch}${BLUE} and ${NC}${target_branch}${BLUE}.${NC}"
@@ -202,12 +222,7 @@ compare_branches() {
202
222
 
203
223
  if [ -z "$source_branch" ]; then
204
224
  print_message "${BLUE}Source branch not specified, using current branch...${NC}" $step_number
205
- source_branch=$(get_current_branch true)
206
- if [ -z "$source_branch" ]; then
207
- print_message "" -1
208
- print_message "${RED}Failed to get current branch.${NC}" -1
209
- exit 1
210
- fi
225
+ get_current_branch source_branch
211
226
  step_number=$((step_number + 1))
212
227
  fi
213
228
 
package/bin/g-pull CHANGED
@@ -63,7 +63,7 @@ main() {
63
63
  validate_dependencies
64
64
  print_banner
65
65
  if [ -z "$target_branch" ]; then
66
- target_branch=$(get_current_branch)
66
+ get_current_branch target_branch
67
67
  fi
68
68
  if [ "$do_fetch" = "true" ]; then
69
69
  print_message "${BLUE}Fetching changes...${NC}" 1
package/bin/g-push CHANGED
@@ -65,7 +65,9 @@ main() {
65
65
  validate_dependencies
66
66
  print_banner
67
67
  if [ -z "$target_branch" ]; then
68
- push_changes $(get_current_branch) $should_force_push 1
68
+ local current_branch
69
+ get_current_branch current_branch
70
+ push_changes "$current_branch" $should_force_push 1
69
71
  exit 0
70
72
  fi
71
73
  push_changes "$target_branch" $should_force_push 1
package/bin/g-rto CHANGED
@@ -43,7 +43,7 @@ main() {
43
43
  set_flags "$@"
44
44
  validate_dependencies git figlet lolcat
45
45
  print_banner
46
- target_branch=$(get_current_branch)
46
+ get_current_branch target_branch
47
47
  stash_changes true 1
48
48
  print_message ""
49
49
 
package/bin/g-s CHANGED
@@ -44,7 +44,8 @@ main() {
44
44
  print_banner
45
45
  print_message "${BLUE}Fetching git status...${NC}" 1
46
46
 
47
- current_branch=$(get_current_branch)
47
+ local current_branch
48
+ get_current_branch current_branch
48
49
 
49
50
  if ! git -c color.ui=always status 2>&1 | indent; then
50
51
  print_message "${PROMPT}Failed to get git status. Are you in a git repository?${NC}"
package/bin/g-wa CHANGED
@@ -66,8 +66,7 @@ set_flags() {
66
66
  }
67
67
 
68
68
  try_fetching_branch() {
69
- fetch_success=$(fetch_changes "${target_branch}")
70
- if [ "${fetch_success}" = "false" ]; then
69
+ if ! fetch_changes "${target_branch}"; then
71
70
  print_message "" -1
72
71
  print_message "${RED}Failed to fetch changes for branch ${target_branch}. [Fail]${NC}" -1
73
72
  exit 1
@@ -81,7 +80,13 @@ check_branch_in_worktree() {
81
80
  print_message "${BLUE}Checking if ${NC}${branch}${BLUE} branch exists...${NC}" $step_number
82
81
 
83
82
  # Check if branch is already checked out in another worktree
84
- local existing_worktree=$(git worktree list | grep "\[${branch}\]" | awk '{print $1}')
83
+ local existing_worktree
84
+ existing_worktree=$(git worktree list 2>/dev/null | grep -F "[${branch}]" | awk '{print $1}')
85
+ if [ $? -ne 0 ]; then
86
+ print_message "" -1
87
+ print_message "${RED}Failed to check worktree list. [Fail]${NC}" -1
88
+ exit 1
89
+ fi
85
90
  if [ -n "$existing_worktree" ]; then
86
91
  print_message "${RED}Branch ${NC}${branch}${RED} is already checked out in worktree:${NC}"
87
92
  print_message "${GREEN}${existing_worktree}${NC}"
@@ -92,6 +97,7 @@ check_branch_in_worktree() {
92
97
 
93
98
  generate_worktree_path() {
94
99
  local target_branch=$1
100
+ local -n result_var=$2
95
101
 
96
102
  # Determine worktree directory based on current location
97
103
  local worktree_dir
@@ -111,22 +117,50 @@ generate_worktree_path() {
111
117
  fi
112
118
 
113
119
  # Sanitize branch name for worktree directory
114
- local worktree_name=$(sanitize_branch_name "$target_branch")
115
- local path="${worktree_dir}${worktree_name}"
120
+ local worktree_name
121
+ sanitize_branch_name "$target_branch" worktree_name
122
+ if [ $? -ne 0 ]; then
123
+ return 1
124
+ fi
125
+ local result_path="${worktree_dir}${worktree_name}"
116
126
 
117
127
  # Convert to absolute path
118
- path=$(cd "$(dirname "$path")" 2>/dev/null && echo "$(pwd)/$(basename "$path")")
128
+ local abs_path
129
+ abs_path=$(cd "${result_path%/*}" 2>/dev/null && echo "$(pwd)/${result_path##*/}")
130
+ local abs_path_exit=$?
131
+ if [ $abs_path_exit -ne 0 ] || [ -z "$abs_path" ]; then
132
+ print_message "" -1
133
+ print_message "${RED}Failed to resolve absolute path. [Fail]${NC}" -1
134
+ return 1
135
+ fi
136
+ result_path="$abs_path"
119
137
 
120
138
  # If path exists, generate a unique path
121
- if [ -d "$path" ]; then
122
- print_message "${RED}Worktree path already exists. [Fail]${NC}"
123
- no_of_identical_dirs=$(find "$(dirname "$path")" -maxdepth 1 -type d -name "${worktree_name}*" 2>/dev/null | wc -l)
124
- path="$(dirname "$path")/${worktree_name}_$(($no_of_identical_dirs + 1))"
125
- print_message "${BLUE}Using new path: ${NC}${path}${NC}"
139
+ if [ -d "$result_path" ]; then
140
+ print_message "${PROMPT}Worktree path already exists.${NC}"
141
+ local parent_dir="${result_path%/*}"
142
+ local no_of_identical_dirs
143
+ no_of_identical_dirs=$(find "$parent_dir" -maxdepth 1 -type d -name "${worktree_name}*" 2>/dev/null | wc -l)
144
+ local find_exit=$?
145
+ if [ $find_exit -ne 0 ]; then
146
+ print_message "" -1
147
+ print_message "${RED}Failed to count existing directories. [Fail]${NC}" -1
148
+ return 1
149
+ fi
150
+ result_path="${parent_dir}/${worktree_name}_$(($no_of_identical_dirs + 1))"
151
+ print_message "${BLUE}Using new path: ${NC}${result_path}${NC}"
152
+
153
+ # Validate the new path
154
+ if [ -z "$result_path" ]; then
155
+ print_message "" -1
156
+ print_message "${RED}Failed to generate unique worktree path. [Fail]${NC}" -1
157
+ return 1
158
+ fi
126
159
  fi
127
160
 
128
- # Return the generated path
129
- echo "$path"
161
+ # Set the result via nameref
162
+ result_var="$result_path"
163
+ return 0
130
164
  }
131
165
 
132
166
  checkout_or_create_branch() {
@@ -136,7 +170,13 @@ checkout_or_create_branch() {
136
170
  check_branch_in_worktree "${target_branch}" $step_number
137
171
 
138
172
  # Generate worktree path
139
- local path=$(generate_worktree_path "${target_branch}" $step_number)
173
+ local path
174
+ generate_worktree_path "${target_branch}" path
175
+ if [ $? -ne 0 ]; then
176
+ print_message "" -1
177
+ print_message "${RED}Failed to generate worktree path. [Fail]${NC}" -1
178
+ exit 1
179
+ fi
140
180
 
141
181
  # Check if branch exists locally
142
182
  if git show-ref --verify --quiet "refs/heads/${target_branch}"; then
@@ -159,6 +199,22 @@ checkout_or_create_branch() {
159
199
 
160
200
  print_message "${RED}Branch not found on remote.${NC}"
161
201
 
202
+ # Get current branch and repo info for the "Will create" message
203
+ local current_branch repo_name sanitized_target
204
+ get_current_branch current_branch
205
+ get_repo_name repo_name
206
+ sanitize_branch_name "$target_branch" sanitized_target
207
+ if [ $? -ne 0 ]; then
208
+ print_message "" -1
209
+ print_message "${RED}Failed to sanitize branch name. [Fail]${NC}" -1
210
+ exit 1
211
+ fi
212
+
213
+ print_message "" -1
214
+ print_message "${BLUE}Will create ${NC}${target_branch}${BLUE} worktree (cut from ${NC}${current_branch}${BLUE})${NC}" -1
215
+ print_message "Location: ${BLUE}${repo_name}/worktrees/${sanitized_target}${NC}" -1
216
+ print_message "" -1
217
+
162
218
  create_new_branch=$(prompt_user true "Create new branch?" $((step_number + 2)))
163
219
  if [ "${create_new_branch}" = "y" ]; then
164
220
  print_message "${BLUE}Creating ${NC}${target_branch}${BLUE} worktree...${NC}" $((step_number + 3))
@@ -179,49 +235,46 @@ prompt_restructure_confirmation() {
179
235
  local target_branch=$5
180
236
 
181
237
  # Save the current branch before restructuring
182
- local original_branch=$(get_current_branch)
183
- if [ -z "$original_branch" ]; then
184
- print_message "" -1
185
- print_message "${RED}Failed to get current branch. [Fail]${NC}" -1
186
- exit 1
187
- fi
188
- print_message "${step_number}. ${BLUE}Repository Reorganization Required${NC}" -1 >&2
189
- print_message "Your repository will be moved into a 'main' subdirectory to enable worktree support" 0 >&2
190
- print_message "" 0 >&2
191
- print_message "Current location: ${BLUE}${git_root}${NC}" 0 >&2
192
- print_message "New location: ${BLUE}${git_root}/main${NC}" 0 >&2
193
- print_message "Current branch: ${BLUE}${original_branch}${NC}" 0 >&2
194
- print_message "" 0 >&2
195
- print_message "This structure allows you to work on multiple branches simultaneously" 0 >&2
196
- print_message "by creating worktrees in separate directories" 0 >&2
238
+ local original_branch
239
+ get_current_branch original_branch
240
+ print_message "${step_number}. ${BLUE}Repository Reorganization Required${NC}" -1
241
+ print_message "Your repository will be moved into a 'main' subdirectory to enable worktree support" 0
242
+ print_message "" 0
243
+ print_message "Current location: ${BLUE}${git_root}${NC}" 0
244
+ print_message "New location: ${BLUE}${git_root}/main${NC}" 0
245
+ print_message "Current branch: ${BLUE}${original_branch}${NC}" 0
246
+ print_message "" 0
247
+ print_message "This structure allows you to work on multiple branches simultaneously" 0
248
+ print_message "by creating worktrees in separate directories" 0
197
249
 
198
250
  # If not on default branch, mention worktree will be created
199
251
  if [ "$original_branch" != "$default_branch" ]; then
200
- local sanitized_original=$(sanitize_branch_name "$original_branch")
201
- local sanitized_target=$(sanitize_branch_name "$target_branch")
202
- print_message "" 0 >&2
203
- print_message "Note: You're currently on ${BLUE}${original_branch}${NC} (not ${BLUE}${default_branch}${NC})" 0 >&2
252
+ local sanitized_original sanitized_target
253
+ sanitize_branch_name "$original_branch" sanitized_original
254
+ sanitize_branch_name "$target_branch" sanitized_target
255
+ print_message "" 0
256
+ print_message "Note: You're currently on ${BLUE}${original_branch}${NC} (not ${BLUE}${default_branch}${NC})" 0
204
257
 
205
258
  # Check if target branch is the same as original branch
206
259
  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
260
+ print_message "Two directories will be created:" 0
261
+ print_message "" 0
262
+ print_message "• ${BLUE}${repo_name}/main${NC} → ${BLUE}${default_branch}${NC} branch" 0
263
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_original}${NC} → ${BLUE}${original_branch}${NC} branch" 0
211
264
  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
265
+ print_message "Three directories will be created:" 0
266
+ print_message "" 0
267
+ print_message "• ${BLUE}${repo_name}/main${NC} → ${BLUE}${default_branch}${NC} branch" 0
268
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_original}${NC} → ${BLUE}${original_branch}${NC} branch" 0
269
+ print_message "• ${BLUE}${repo_name}/worktrees/${sanitized_target}${NC} → ${BLUE}${target_branch}${NC} branch (cut from ${BLUE}${original_branch}${NC})" 0
217
270
  fi
218
271
  fi
219
272
 
220
273
  # Prompt user for confirmation
221
274
  local proceed=$(prompt_user false "Proceed with restructuring?" "$step_number")
222
275
  if [ "$proceed" != "y" ]; then
223
- print_message "" -1 >&2
224
- print_message "${RED}Restructuring cancelled by user.${NC}" -1 >&2
276
+ print_message "" -1
277
+ print_message "${RED}Restructuring cancelled by user.${NC}" -1
225
278
  exit 1
226
279
  fi
227
280
  }
@@ -230,18 +283,19 @@ create_main_dir() {
230
283
  local git_root=$1
231
284
  local temp_dir=$2
232
285
  local step_number=$3
286
+ local -n result_var=$4
233
287
 
234
288
  # Create temp directory and move everything there
235
289
  if ! mkdir -p "$temp_dir"; then
236
- print_message "" -1 >&2
237
- print_message "${RED}Failed to create temporary directory. [Fail]${NC}" -1 >&2
290
+ print_message "" -1
291
+ print_message "${RED}Failed to create temporary directory. [Fail]${NC}" -1
238
292
  exit 1
239
293
  fi
240
294
 
241
- print_message "${BLUE}Moving repository contents to temporary location...${NC}" $step_number >&2
295
+ print_message "${BLUE}Moving repository contents to temporary location...${NC}" $step_number
242
296
  if ! (shopt -s dotglob && mv "$git_root"/* "$temp_dir/" 2>&1); then
243
- print_message "" -1 >&2
244
- print_message "${RED}Failed to move contents to temporary directory. [Fail]${NC}" -1 >&2
297
+ print_message "" -1
298
+ print_message "${RED}Failed to move contents to temporary directory. [Fail]${NC}" -1
245
299
  rm -rf "$temp_dir"
246
300
  exit 1
247
301
  fi
@@ -249,8 +303,8 @@ create_main_dir() {
249
303
  # Create main directory
250
304
  local main_dir="${git_root}/main"
251
305
  if ! mkdir -p "$main_dir"; then
252
- print_message "" -1 >&2
253
- print_message "${RED}Failed to create main directory. [Fail]${NC}" -1 >&2
306
+ print_message "" -1
307
+ print_message "${RED}Failed to create main directory. [Fail]${NC}" -1
254
308
  # Restore from temp
255
309
  (shopt -s dotglob && mv "$temp_dir"/* "$git_root/")
256
310
  rm -rf "$temp_dir"
@@ -259,15 +313,16 @@ create_main_dir() {
259
313
 
260
314
  # Move everything from temp to main
261
315
  if ! (shopt -s dotglob && mv "$temp_dir"/* "$main_dir/" 2>&1); then
262
- print_message "" -1 >&2
263
- print_message "${RED}Failed to move contents to main directory. [Fail]${NC}" -1 >&2
316
+ print_message "" -1
317
+ print_message "${RED}Failed to move contents to main directory. [Fail]${NC}" -1
264
318
  exit 1
265
319
  fi
266
320
 
267
321
  # Clean up temp directory
268
322
  rm -rf "$temp_dir"
269
323
 
270
- echo "$main_dir"
324
+ result_var="$main_dir"
325
+ return 0
271
326
  }
272
327
 
273
328
  checkout_main_in_main_branch() {
@@ -306,7 +361,13 @@ checkout_current_branch() {
306
361
  return 0
307
362
  fi
308
363
 
309
- local path=$(generate_worktree_path "$original_branch" $step_number)
364
+ local path
365
+ generate_worktree_path "$original_branch" path
366
+ if [ $? -ne 0 ]; then
367
+ print_message "" -1
368
+ print_message "${RED}Failed to generate worktree path. [Fail]${NC}" -1
369
+ exit 1
370
+ fi
310
371
  print_message "${BLUE}Creating ${NC}${original_branch}${BLUE} worktree...${NC}" $step_number
311
372
  create_worktree "$original_branch" "$path"
312
373
 
@@ -339,27 +400,31 @@ create_main_dir_if_necessary() {
339
400
  step_number=0
340
401
  fi
341
402
 
342
- local git_root=$(git rev-parse --show-toplevel)
343
- local current_dir=$(basename "$git_root")
344
- local parent_dir=$(basename "$(dirname "$git_root")")
345
- local repo_name=$(get_repo_name)
403
+ local git_root current_dir parent_dir repo_name
404
+ if ! get_repo_info git_root current_dir parent_dir repo_name; then
405
+ print_message "" -1
406
+ print_message "${RED}Failed to get repository information. [Fail]${NC}" -1
407
+ exit 1
408
+ fi
346
409
 
347
410
  if already_restructured; then
348
411
  return 0
349
412
  fi
350
413
 
351
414
  # Save original branch before restructuring
352
- local original_branch=$(get_current_branch)
415
+ local original_branch
416
+ get_current_branch original_branch
353
417
 
354
418
  # Prompt user for restructuring
355
419
  prompt_restructure_confirmation "$git_root" "$repo_name" "$default_branch" "$step_number" "$target_branch"
356
420
 
357
421
  # Get parent directory path
358
- local parent_path=$(dirname "$git_root")
422
+ local parent_path="${git_root%/*}"
359
423
  local temp_dir="${parent_path}/.temp-${repo_name}-$$"
360
424
 
361
425
  # Create main directory and restructure
362
- local main_dir=$(create_main_dir "$git_root" "$temp_dir" $((step_number + 1)))
426
+ local main_dir
427
+ create_main_dir "$git_root" "$temp_dir" $((step_number + 1)) main_dir
363
428
 
364
429
  # Navigate to main directory using direct path
365
430
  if ! navigate_to_dir "$main_dir"; then
@@ -400,8 +465,9 @@ main() {
400
465
  exit 1
401
466
  fi
402
467
 
403
- local default_branch=$(get_default_branch)
404
- local current_branch=$(get_current_branch)
468
+ local default_branch current_branch
469
+ get_default_branch default_branch
470
+ get_current_branch current_branch
405
471
 
406
472
  local step_number=2
407
473
  if [ "$stash" = true ]; then
@@ -409,32 +475,19 @@ main() {
409
475
  fi
410
476
 
411
477
  # Check if we need to restructure (to calculate proper step numbers)
412
- local git_root=$(git rev-parse --show-toplevel)
413
- local current_dir=$(basename "$git_root")
414
- local parent_dir=$(basename "$(dirname "$git_root")")
415
- local repo_name=$(get_repo_name)
478
+ local git_root current_dir parent_dir repo_name
479
+ if ! get_repo_info git_root current_dir parent_dir repo_name; then
480
+ print_message "" -1
481
+ print_message "${RED}Failed to get repository information. [Fail]${NC}" -1
482
+ exit 1
483
+ fi
416
484
  create_main_dir_if_necessary "$default_branch" $step_number
417
485
  local needs_restructure=$?
418
486
 
419
487
  if [ $needs_restructure -eq 1 ]; then
420
488
  checkout_or_create_branch $((step_number + 4))
421
489
  else
422
- # Notify user that worktree will be created
423
- local sanitized_target=$(sanitize_branch_name "$target_branch")
424
- print_message "" -1
425
- print_message "${BLUE}Will create ${NC}${target_branch}${BLUE} worktree (cut from ${NC}${current_branch}${BLUE})${NC}" -1
426
- print_message "Location: ${BLUE}${repo_name}/worktrees/${sanitized_target}${NC}" -1
427
- print_message "" -1
428
-
429
- # Prompt user for confirmation
430
- local proceed=$(prompt_user false "Proceed with creating worktree?" "$step_number")
431
- if [ "$proceed" != "y" ]; then
432
- print_message "" -1
433
- print_message "${RED}Worktree creation cancelled by user.${NC}" -1
434
- exit 1
435
- fi
436
-
437
- checkout_or_create_branch $((step_number + 1))
490
+ checkout_or_create_branch $step_number
438
491
  fi
439
492
  }
440
493
 
package/bin/g-wr CHANGED
@@ -88,15 +88,21 @@ set_flags() {
88
88
 
89
89
  cd_to_worktrees_dir() {
90
90
  # Get git root and navigate to repo root's worktrees directory
91
- local git_root=$(git rev-parse --show-toplevel)
91
+ local git_root
92
+ git_root=$(git rev-parse --show-toplevel 2>&1)
93
+ if [ $? -ne 0 ] || [ -z "$git_root" ]; then
94
+ print_message "" -1
95
+ print_message "${RED}Failed to get git root directory. [Fail]${NC}" -1
96
+ exit 1
97
+ fi
92
98
  local git_root_parent=$(dirname "$git_root")
93
99
  local git_root_parent_name=$(basename "$git_root_parent")
94
100
 
95
101
  # If we're in a worktree (parent is "worktrees"), use that directory
96
102
  if [ "$git_root_parent_name" = "worktrees" ]; then
97
103
  if ! navigate_to_dir "$git_root_parent"; then
98
- print_message "" -1 >&2
99
- print_message "${RED}Failed to navigate to worktrees directory. [Fail]${NC}" -1 >&2
104
+ print_message "" -1
105
+ print_message "${RED}Failed to navigate to worktrees directory. [Fail]${NC}" -1
100
106
  exit 1
101
107
  fi
102
108
  return 0
@@ -105,14 +111,14 @@ cd_to_worktrees_dir() {
105
111
  # Otherwise, we're in main, so look for ../worktrees
106
112
  local worktrees_dir="$git_root_parent/worktrees"
107
113
  if [ ! -d "$worktrees_dir" ]; then
108
- print_message "" -1 >&2
109
- print_message "${RED}Worktrees directory not found at ${NC}${worktrees_dir}${RED}. [Fail]${NC}" -1 >&2
114
+ print_message "" -1
115
+ print_message "${RED}Worktrees directory not found at ${NC}${worktrees_dir}${RED}. [Fail]${NC}" -1
110
116
  exit 1
111
117
  fi
112
118
 
113
119
  if ! navigate_to_dir "$worktrees_dir"; then
114
- print_message "" -1 >&2
115
- print_message "${RED}Failed to navigate to worktrees directory. [Fail]${NC}" -1 >&2
120
+ print_message "" -1
121
+ print_message "${RED}Failed to navigate to worktrees directory. [Fail]${NC}" -1
116
122
  exit 1
117
123
  fi
118
124
  }
@@ -155,8 +161,10 @@ find_worktree_by_branch() {
155
161
  continue
156
162
  fi
157
163
 
158
- local branch=$(get_current_branch 2>/dev/null)
159
- if [ -z "$branch" ]; then
164
+ local branch
165
+ branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
166
+ local branch_exit=$?
167
+ if [ $branch_exit -ne 0 ] || [ -z "$branch" ]; then
160
168
  continue
161
169
  fi
162
170
 
@@ -247,8 +255,9 @@ main() {
247
255
  print_message "${BLUE}Searching for worktree with branch ${NC}${target_branch}${BLUE}...${NC}" $step_number
248
256
  fi
249
257
 
258
+ local worktree_path
250
259
  worktree_path=$(find_dir_to_remove)
251
- if [ -z "$worktree_path" ]; then
260
+ if [ $? -ne 0 ] || [ -z "$worktree_path" ]; then
252
261
  print_message "" -1
253
262
  if [ -n "$worktree_name" ]; then
254
263
  print_message "${RED}No worktree found with name ${NC}${worktree_name}${RED}. [Fail]${NC}" -1
package/bin/utils CHANGED
@@ -116,56 +116,67 @@ prompt_user() {
116
116
  }
117
117
 
118
118
  get_current_branch() {
119
- current_branch=$(git rev-parse --abbrev-ref HEAD)
120
- if [ -z "$current_branch" ]; then
119
+ local -n result=$1
120
+ result=$(git rev-parse --abbrev-ref HEAD 2>&1)
121
+ local get_branch_exit=$?
122
+ if [ $get_branch_exit -ne 0 ] || [ -z "$result" ]; then
121
123
  print_message "" -1
122
124
  print_message "${RED}Failed to get current branch. [Fail]${NC}" -1
123
125
  exit 1
124
126
  fi
125
- echo "${current_branch}"
127
+ return 0
126
128
  }
127
129
 
128
130
  get_default_branch() {
131
+ local -n result=$1
129
132
  local default_branch=""
130
133
 
131
134
  # Try to get from remote HEAD
132
135
  default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
136
+ local exit_code=$?
133
137
 
134
- if [ -n "$default_branch" ]; then
135
- echo "$default_branch"
138
+ if [ $exit_code -eq 0 ] && [ -n "$default_branch" ]; then
139
+ result="$default_branch"
136
140
  return 0
137
141
  fi
138
142
 
139
143
  # Check if main exists locally
140
144
  if git show-ref --verify --quiet "refs/heads/main"; then
141
- echo "main"
145
+ result="main"
142
146
  return 0
143
147
  fi
144
148
 
145
149
  # Check if master exists locally
146
150
  if git show-ref --verify --quiet "refs/heads/master"; then
147
- echo "master"
151
+ result="master"
148
152
  return 0
149
153
  fi
150
154
 
151
155
  # Fallback to main
152
- echo "main"
156
+ result="main"
153
157
  return 0
154
158
  }
155
159
 
156
160
  sanitize_branch_name() {
157
161
  local branch_name=$1
158
- local max_length=${2:-30} # Default max length is 30
162
+ local -n result=$2
163
+ local max_length=${3:-30} # Default max length is 30
159
164
 
160
165
  # Replace non-alphanumeric characters with underscores and convert to lowercase
161
- local sanitized=$(echo "$branch_name" | sed -E 's/[^[:alnum:]]/_/g' | tr '[:upper:]' '[:lower:]')
166
+ local sanitized
167
+ sanitized=$(echo "$branch_name" | sed -E 's/[^[:alnum:]]/_/g' | tr '[:upper:]' '[:lower:]')
168
+ local sanitize_exit=$?
169
+ if [ $sanitize_exit -ne 0 ]; then
170
+ return 1
171
+ fi
162
172
 
163
173
  # Truncate if longer than max_length
164
174
  if [ ${#branch_name} -gt $max_length ]; then
165
175
  sanitized="${sanitized:0:$max_length}.."
166
176
  fi
167
177
 
168
- echo "$sanitized"
178
+ result="$sanitized"
179
+ return 0
169
180
  }
170
181
 
171
182
  stash_changes() {
@@ -187,7 +198,13 @@ stash_changes() {
187
198
  fi
188
199
 
189
200
  local stash_message
190
- local branch_name=$(git rev-parse --abbrev-ref HEAD)
201
+ local branch_name
202
+ branch_name=$(git rev-parse --abbrev-ref HEAD 2>&1)
203
+ if [ $? -ne 0 ] || [ -z "$branch_name" ]; then
204
+ print_message "" -1
205
+ print_message "${RED}Failed to get current branch name for stash. [Fail]${NC}" -1
206
+ exit 1
207
+ fi
191
208
  local stash_date=$(date '+%Y-%m-%d %H:%M:%S')
192
209
 
193
210
  if [ -n "$tag_message" ]; then
@@ -276,12 +293,8 @@ create_worktree() {
276
293
  exit 1
277
294
  fi
278
295
 
279
- base_branch=$(get_current_branch)
280
- if [ -z "$base_branch" ]; then
281
- print_message "" -1
282
- print_message "${RED}Failed to get current branch. [Fail]${NC}" -1
283
- exit 1
284
- fi
296
+ local base_branch
297
+ get_current_branch base_branch
285
298
 
286
299
  if [ "$new_branch" = "true" ]; then
287
300
  # Make sure the source branch exists remotely!
@@ -436,7 +449,8 @@ already_on_branch() {
436
449
  step_number=1
437
450
  fi
438
451
  print_message "${BLUE}Checking current branch...${NC}" $step_number
439
- current_branch=$(get_current_branch)
452
+ local current_branch
453
+ get_current_branch current_branch
440
454
  if [ "${target_branch}" = "${current_branch}" ]; then
441
455
  print_message "${GREEN}Already on branch ${NC}${target_branch}${GREEN}. [DONE]${NC}"
442
456
  exit 1
@@ -586,14 +600,49 @@ copy_to_clipboard() {
586
600
  }
587
601
 
588
602
  get_repo_name() {
603
+ local -n result=$1
604
+
589
605
  # Try to get repo name from git remote
590
- local remote_url=$(git config --get remote.origin.url 2>/dev/null)
606
+ local remote_url
607
+ remote_url=$(git config --get remote.origin.url 2>/dev/null)
591
608
  if [ -n "$remote_url" ]; then
592
609
  # Extract repo name from URL (handles both https and ssh)
593
- echo "$remote_url" | sed -E 's#.*/([^/]+)(\.git)?$#\1#' | sed 's/\.git$//'
610
+ result=$(echo "$remote_url" | sed -E 's#.*/([^/]+)(\.git)?$#\1#' | sed 's/\.git$//')
611
+ local extract_exit=$?
612
+ if [ $extract_exit -ne 0 ]; then
613
+ return 1
614
+ fi
594
615
  return 0
595
616
  fi
596
617
 
597
618
  # Fallback to directory name
598
- basename "$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
619
+ local toplevel
620
+ toplevel=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
621
+ result="${toplevel##*/}"
622
+ return 0
623
+ }
624
+
625
+ get_repo_info() {
626
+ local -n git_root_ref=$1
627
+ local -n current_dir_ref=$2
628
+ local -n parent_dir_ref=$3
629
+ local -n repo_name_ref=$4
630
+
631
+ # Get git root
632
+ git_root_ref=$(git rev-parse --show-toplevel)
633
+ local git_root_exit=$?
634
+ if [ $git_root_exit -ne 0 ] || [ -z "$git_root_ref" ]; then
635
+ return 1
636
+ fi
637
+
638
+ # Get current directory name (basename of git root)
639
+ current_dir_ref="${git_root_ref##*/}"
640
+
641
+ # Get parent directory (dirname of git root, then basename of that)
642
+ local parent_path="${git_root_ref%/*}"
643
+ parent_dir_ref="${parent_path##*/}"
644
+
645
+ # Get repo name
646
+ get_repo_name repo_name_ref
647
+ return 0
599
648
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@san-siva/gitsy",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "A set of bash utilities for managing Git repositories with ease",
5
5
  "keywords": [
6
6
  "git",