-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_git.sh
More file actions
739 lines (650 loc) · 27.2 KB
/
Copy pathlib_git.sh
File metadata and controls
739 lines (650 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# shellcheck shell=bash
#
# lib_git.sh: Git operations
#
[[ -n "${BASE_BASH_LIBS_GIT_LOADED:-}" ]] && return 0
if [[ "${BASE_BASH_LIBS_STDLIB_LOADED:-}" != "1" ]]; then
printf '%s\n' "Error: lib_git.sh requires lib_std.sh to be sourced first." >&2
return 1 2>/dev/null || exit 1
fi
readonly BASE_BASH_LIBS_GIT_LOADED=1
base_git_detect_default_branch() {
if (($# != 2)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_detect_default_branch <repo_dir> <result_variable_name>"
return 1
fi
__base_bash_libs_std_assert_public_variable_names__ base_git_detect_default_branch "${2-}" || return 1
local __base_bash_libs_git_detect_repo_dir="$1"
local __base_bash_libs_git_detect_result_name="$2"
local __base_bash_libs_git_detect_branch
if [[ -z "$__base_bash_libs_git_detect_repo_dir" || -z "$__base_bash_libs_git_detect_result_name" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_detect_default_branch <repo_dir> <result_variable_name>"
return 1
fi
base_std_assert_variable_name "$__base_bash_libs_git_detect_result_name" || return 1
__base_bash_libs_std_assert_writable_output__ base_git_detect_default_branch "$__base_bash_libs_git_detect_result_name" || return 1
if __base_bash_libs_git_detect_branch="$(git -C "$__base_bash_libs_git_detect_repo_dir" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"; then
__base_bash_libs_git_detect_branch="${__base_bash_libs_git_detect_branch#origin/}"
if [[ -n "$__base_bash_libs_git_detect_branch" ]]; then
printf -v "$__base_bash_libs_git_detect_result_name" '%s' "$__base_bash_libs_git_detect_branch"
return 0
fi
fi
if git -C "$__base_bash_libs_git_detect_repo_dir" show-ref --verify --quiet refs/remotes/origin/main ||
git -C "$__base_bash_libs_git_detect_repo_dir" show-ref --verify --quiet refs/heads/main; then
printf -v "$__base_bash_libs_git_detect_result_name" '%s' main
return 0
fi
if git -C "$__base_bash_libs_git_detect_repo_dir" show-ref --verify --quiet refs/remotes/origin/master ||
git -C "$__base_bash_libs_git_detect_repo_dir" show-ref --verify --quiet refs/heads/master; then
printf -v "$__base_bash_libs_git_detect_result_name" '%s' master
return 0
fi
return 1
}
base_git_worktree_path_for_branch() {
if (($# < 1 || $# > 2)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_worktree_path_for_branch <branch> [repo_dir]"
return 1
fi
local branch="$1"
local repo_dir="${2:-}"
local target_ref="refs/heads/$branch"
local line path="" ref output
local -a git_cmd=(git)
[[ -n "$branch" ]] || {
base_std_log_error -l base_bash_libs.git "Usage: base_git_worktree_path_for_branch <branch> [repo_dir]"
return 1
}
[[ -z "$repo_dir" ]] || git_cmd=(git -C "$repo_dir")
if ! output="$("${git_cmd[@]+"${git_cmd[@]}"}" worktree list --porcelain 2>&1)"; then
base_std_log_error -l base_bash_libs.git "Unable to list Git worktrees."
return 1
fi
while IFS= read -r line; do
case "$line" in
"worktree "*)
path="${line#worktree }"
;;
"branch "*)
ref="${line#branch }"
if [[ "$ref" == "$target_ref" ]]; then
printf '%s\n' "$path"
return 0
fi
;;
esac
done <<<"$output"
return 1
}
base_git_list_worktree_branches() {
if (($# > 1)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_list_worktree_branches [repo_dir]"
return 1
fi
local repo_dir="${1:-}"
local line path="" branch="" output
local -a git_cmd=(git)
[[ -z "$repo_dir" ]] || git_cmd=(git -C "$repo_dir")
if ! output="$("${git_cmd[@]+"${git_cmd[@]}"}" worktree list --porcelain 2>&1)"; then
base_std_log_error -l base_bash_libs.git "Unable to list Git worktrees."
return 1
fi
output+=$'\n'
while IFS= read -r line; do
case "$line" in
"")
if [[ -n "$path" && -n "$branch" ]]; then
branch="${branch#refs/heads/}"
printf '%s\t%s\n' "$path" "$branch"
fi
path=""
branch=""
;;
"worktree "*)
path="${line#worktree }"
;;
"branch "*)
branch="${line#branch }"
;;
esac
done <<<"$output"
}
base_git_branch_upstream() {
if (($# != 2)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_branch_upstream <repo_dir> <branch>"
return 1
fi
local repo_dir="$1"
local branch="$2"
if [[ -z "$repo_dir" || -z "$branch" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_branch_upstream <repo_dir> <branch>"
return 1
fi
git -C "$repo_dir" for-each-ref --format='%(upstream:short)' "refs/heads/$branch"
}
base_git_branch_merged_to_ref() {
if (($# != 3)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_branch_merged_to_ref <repo_dir> <branch> <ref>"
return 1
fi
local repo_dir="$1"
local branch="$2"
local ref="$3"
if [[ -z "$repo_dir" || -z "$branch" || -z "$ref" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_branch_merged_to_ref <repo_dir> <branch> <ref>"
return 1
fi
git -C "$repo_dir" merge-base --is-ancestor "refs/heads/$branch" "$ref" >/dev/null 2>&1
}
base_git_list_remote_branches() {
if (($# > 1)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_list_remote_branches [repo_dir]"
return 1
fi
local repo_dir="${1:-.}"
local output ref _sha
if ! output="$(git -C "$repo_dir" ls-remote --heads origin)"; then
base_std_log_error -l base_bash_libs.git "Unable to list remote branches from origin."
return 1
fi
while IFS=$' \t' read -r _sha ref; do
[[ "$ref" == refs/heads/* ]] || continue
printf '%s\n' "${ref#refs/heads/}"
done <<<"$output"
}
#
# Returns success when tracked changes are limited to one repo-relative path.
#
# @param $1 allowed_path Path in repository root that may be dirty (for example "shared").
#
__base_bash_libs_git_path_matches_allowed_path__() {
(($# == 2)) || return 1
local path="$1" allowed_path="$2"
[[ "$path" == "$allowed_path" || "$path" == "$allowed_path/"* ]]
}
__base_bash_libs_git_only_path_dirty__() {
(($# == 1)) || return 1
local allowed_path="$1"
local status_file status_record status_code path related_path
base_std_make_temp_file status_file base-git-status || return 1
if ! git status --porcelain=v1 --untracked-files=no --ignore-submodules=none -z > "$status_file"; then
base_std_unregister_cleanup_path "$status_file"
rm -f -- "$status_file"
return 1
fi
if [[ ! -s "$status_file" ]]; then
base_std_unregister_cleanup_path "$status_file"
rm -f -- "$status_file"
return 1
fi
while IFS= read -r -d '' status_record; do
[[ -n "$status_record" ]] || continue
status_code="${status_record:0:2}"
path="${status_record:3}"
if ! __base_bash_libs_git_path_matches_allowed_path__ "$path" "$allowed_path"; then
base_std_unregister_cleanup_path "$status_file"
rm -f -- "$status_file"
return 1
fi
if [[ "$status_code" == *R* || "$status_code" == *C* ]]; then
if ! IFS= read -r -d '' related_path ||
! __base_bash_libs_git_path_matches_allowed_path__ "$related_path" "$allowed_path"; then
base_std_unregister_cleanup_path "$status_file"
rm -f -- "$status_file"
return 1
fi
fi
done < "$status_file"
base_std_unregister_cleanup_path "$status_file"
rm -f -- "$status_file"
return 0
}
__base_bash_libs_git_expected_update_branch__() {
(($# <= 1)) || return 1
local configured_branch="${1:-}"
local default_branch
if [[ -n "$configured_branch" ]]; then
printf '%s\n' "$configured_branch"
return 0
fi
if default_branch="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"; then
default_branch="${default_branch#origin/}"
if [[ -n "$default_branch" ]]; then
printf '%s\n' "$default_branch"
return 0
fi
fi
if git show-ref --verify --quiet refs/remotes/origin/main; then
printf '%s\n' main
return 0
fi
if git show-ref --verify --quiet refs/remotes/origin/master; then
printf '%s\n' master
return 0
fi
if git show-ref --verify --quiet refs/heads/main; then
printf '%s\n' main
return 0
fi
if git show-ref --verify --quiet refs/heads/master; then
printf '%s\n' master
return 0
fi
printf '%s\n' main
}
__base_bash_libs_git_update_repo_finish__() {
local git_log="${1:-}"
local should_popd="${2:-false}"
local status="${3:-0}"
local submodule_log="${4:-}"
if [[ "$should_popd" == true ]]; then
popd >/dev/null || status=1
fi
if [[ -n "$git_log" ]]; then
rm -f -- "$git_log"
base_std_unregister_cleanup_path "$git_log"
fi
if [[ -n "$submodule_log" ]]; then
rm -f -- "$submodule_log"
base_std_unregister_cleanup_path "$submodule_log"
fi
return "$status"
}
__base_bash_libs_git_pull_with_retry__() {
(($# == 1)) || return 1
local git_log="$1"
local max_attempts="${BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS:-2}"
local attempt=1
if [[ ! "$max_attempts" =~ ^[0-9]+$ ]] || ((max_attempts < 1)); then
base_std_log_warn -l base_bash_libs.git "BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS must be a positive integer; using 2."
max_attempts=2
fi
while ((attempt <= max_attempts)); do
if git pull --ff-only >"$git_log" 2>&1; then
if ((attempt > 1)); then
base_std_log_debug -l base_bash_libs.git "git pull succeeded on attempt $attempt."
fi
return 0
fi
if ((attempt == max_attempts)); then
return 1
fi
if ((max_attempts == 2)); then
base_std_log_warn -l base_bash_libs.git "git pull failed on attempt $attempt; retrying once."
else
base_std_log_warn -l base_bash_libs.git "git pull failed on attempt $attempt; retrying (attempt $((attempt + 1)) of $max_attempts)."
fi
[[ -s "$git_log" ]] && base_std_log_debug_file -l base_bash_libs.git "$git_log"
attempt=$((attempt + 1))
done
}
#
# Safely updates a Git repository and its submodules after checking that the
# current branch is the repo default branch or an explicit expected branch.
#
# @param $1 git_repo The path to the local git repository.
# @param $2 allowed_dirty_path Optional repo-relative path that may be dirty.
# @param $3 expected_branch Optional branch name to require instead of auto-detecting.
#
# Environment:
# BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS Positive integer retry count for `git pull --ff-only`; defaults to 2.
#
base_git_update_repo() {
if (($# < 1 || $# > 3)); then
base_std_log_info -l base_bash_libs.git "Usage: base_git_update_repo /path/to/repo [allowed_dirty_path] [expected_branch]"
return 1
fi
local git_repo="$1"
local allowed_dirty_path="${2:-}"
local expected_branch="${3:-}"
local git_log submodule_log=""
if [[ -z "$git_repo" ]]; then
base_std_log_error -l base_bash_libs.git "No git repository path provided."
base_std_log_info -l base_bash_libs.git "Usage: base_git_update_repo /path/to/repo [allowed_dirty_path] [expected_branch]"
return 1
fi
if [[ ! -d "$git_repo" ]]; then
base_std_log_error -l base_bash_libs.git "Git repo not found at '$git_repo'"
return 1
fi
base_std_make_temp_file git_log git_log || {
base_std_log_error -l base_bash_libs.git "Unable to create temporary git log file."
return 1
}
# base_git_update_repo intentionally works inside the target repository because
# the submodule update sequence below needs the repository as its cwd.
if ! pushd "$git_repo" > /dev/null; then
# If cd fails, we can't proceed.
__base_bash_libs_git_update_repo_finish__ "$git_log" false 1
return $?
fi
# Check if it's a valid git repo
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
base_std_log_error -l base_bash_libs.git "'$git_repo' is not a Git repository."
__base_bash_libs_git_update_repo_finish__ "$git_log" true 1
return $?
fi
# Make sure the current branch is the expected update branch.
local current_branch
expected_branch="$(__base_bash_libs_git_expected_update_branch__ "$expected_branch")"
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" != "$expected_branch" ]]; then
base_std_log_debug -l base_bash_libs.git "Current branch of '$git_repo' is '${current_branch}', not '$expected_branch'. Skipping update."
__base_bash_libs_git_update_repo_finish__ "$git_log" true 0
return $?
fi
local dirty=false
if ! git diff --quiet; then
dirty=true
fi
if ! git diff --cached --quiet; then
dirty=true
fi
if [[ "$dirty" == true ]]; then
if [[ -n "$allowed_dirty_path" ]] && __base_bash_libs_git_only_path_dirty__ "$allowed_dirty_path"; then
base_std_log_debug -l base_bash_libs.git "Repo '$git_repo' only has tracked changes in '$allowed_dirty_path'; attempting git pull."
else
base_std_log_debug -l base_bash_libs.git "Repo '$git_repo' has local changes; skipping auto-update. Commit or stash to enable git pull."
__base_bash_libs_git_update_repo_finish__ "$git_log" true 0
return $?
fi
fi
if ! __base_bash_libs_git_pull_with_retry__ "$git_log"; then
base_std_log_error -l base_bash_libs.git "git pull failed on repo '$git_repo'"
[[ -s "$git_log" ]] && base_std_log_info_file -l base_bash_libs.git "$git_log"
__base_bash_libs_git_update_repo_finish__ "$git_log" true 1
return $?
fi
# it is safe to run submodule commands even if the repo has no submodules
if ! base_std_make_temp_file submodule_log git-submodule-log; then
base_std_log_error -l base_bash_libs.git "Unable to create temporary submodule log file."
__base_bash_libs_git_update_repo_finish__ "$git_log" true 1
return $?
fi
if ! { git submodule init && git submodule sync && git submodule update; } >"$submodule_log" 2>&1; then
base_std_log_error -l base_bash_libs.git "git submodule update failed on repo '$git_repo'"
[[ -s "$submodule_log" ]] && base_std_log_info_file -l base_bash_libs.git "$submodule_log"
__base_bash_libs_git_update_repo_finish__ "$git_log" true 1 "$submodule_log"
return $?
fi
base_std_log_debug -l base_bash_libs.git "Git repo '$git_repo' updated to latest '$expected_branch'"
__base_bash_libs_git_update_repo_finish__ "$git_log" true 0 "$submodule_log"
return $?
}
#
# Gets the currently checked-out branch of a Git repository without changing directories.
#
# This function safely checks a directory, determines if it's a Git repository,
# and returns the current branch name via a name reference (nameref).
#
# @param $1 target_dir The path to the directory to check.
# @param $2 result_var_name The name of the variable in the calling scope
# that will receive the output.
#
# Returns:
# - The branch name (e.g., "main", "feature/login") is stored in the result variable.
# - "detached head" if the repository is in a detached HEAD state.
# - An empty string "" if the directory doesn't exist or is not a Git repo.
# - The function itself returns an exit code of 0 on success, 1 on invalid usage.
#
base_git_get_current_branch() {
if (($# != 2)); then
base_std_log_error -l base_bash_libs.git "Usage: base_git_get_current_branch <directory> <result_variable_name>"
return 1
fi
__base_bash_libs_std_assert_public_variable_names__ base_git_get_current_branch "${2-}" || return 1
local __base_bash_libs_git_branch_target_dir="$1"
local __base_bash_libs_git_branch_result_name="$2"
# --- Argument Validation ---
if [[ -z "$__base_bash_libs_git_branch_target_dir" || -z "$__base_bash_libs_git_branch_result_name" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_get_current_branch <directory> <result_variable_name>"
return 1
fi
if ! __base_bash_libs_std_is_valid_variable_name__ "$__base_bash_libs_git_branch_result_name"; then
base_std_log_error -l base_bash_libs.git "base_git_get_current_branch: result variable name must be a valid Bash variable name."
return 1
fi
__base_bash_libs_std_assert_writable_output__ base_git_get_current_branch "$__base_bash_libs_git_branch_result_name" || return 1
printf -v "$__base_bash_libs_git_branch_result_name" '%s' ""
if [[ ! -d "$__base_bash_libs_git_branch_target_dir" ]]; then
return 0
fi
# Check if we are inside a Git repository.
if ! git -C "$__base_bash_libs_git_branch_target_dir" rev-parse --is-inside-work-tree > /dev/null 2>&1; then
# Not a Git repo, result is already an empty string.
return 0
fi
# Use 'git symbolic-ref' to get the branch name.
# It's the most reliable way to distinguish a branch from a detached HEAD.
# -q (--quiet) suppresses errors and returns a non-zero exit code on failure.
local __base_bash_libs_git_branch_name
if __base_bash_libs_git_branch_name=$(git -C "$__base_bash_libs_git_branch_target_dir" symbolic-ref --short -q HEAD); then
# Success: We are on a named branch.
printf -v "$__base_bash_libs_git_branch_result_name" '%s' "$__base_bash_libs_git_branch_name"
else
# Failure: We are in a detached HEAD state.
printf -v "$__base_bash_libs_git_branch_result_name" '%s' "detached head"
fi
return 0
}
#
# Checks whether a script appears up to date with its git upstream and logs status.
#
# @param $1 Optional --fetch to refresh remote-tracking refs before comparing.
# @param $2 script_path The path to a script file tracked in a git repo.
#
# Returns:
# - 0 if the repository is current or ahead, or the check is skipped for a
# documented non-error state (missing git, not a repo, untracked script,
# detached HEAD, or missing upstream).
# - 1 on invalid usage.
# - 2 if the repository is behind its upstream (the script may be stale).
# - 3 if the script has local modifications and the comparison completed or
# was skipped for a documented non-error state.
# - 4 if the repository has diverged from its upstream.
# - 5 if Git metadata, diff, fetch, or comparison commands fail.
#
# Status precedence:
# - A dirty script takes precedence over a successful comparison, including
# behind, ahead, diverged, and known skip states.
# - An operational failure takes precedence over the dirty status because the
# result cannot be trusted.
# - A failed comparison never reports the repository as up to date.
#
# Expected skip states are not failures, but they are always logged explicitly.
# They return 3 when the tracked script is dirty so callers cannot lose that
# fact merely because an upstream comparison is unavailable.
#
base_git_check_script_up_to_date() {
local fetch_before_check=false script_path
if (($# == 2)); then
if [[ "$1" != "--fetch" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_check_script_up_to_date [--fetch] <script_path>"
return 1
fi
fetch_before_check=true
shift
elif (($# != 1)) || [[ "$1" == "--fetch" ]]; then
base_std_log_error -l base_bash_libs.git "Usage: base_git_check_script_up_to_date [--fetch] <script_path>"
return 1
fi
script_path="$1"
if [[ ! -e "$script_path" ]]; then
base_std_log_warn -l base_bash_libs.git "Script '$script_path' not found; skipping latest-version check."
return 0
fi
if ! command -v git &> /dev/null; then
base_std_log_info -l base_bash_libs.git "git not available; skipping latest-version check."
return 0
fi
local script_dir repo_root prefix rel_path
script_dir=$(dirname -- "$script_path") || {
base_std_log_error -l base_bash_libs.git "Unable to resolve the script directory for '$script_path'."
return 5
}
local repo_probe repo_probe_status
if repo_probe=$(git -C "$script_dir" rev-parse --is-inside-work-tree 2>&1); then
if [[ "$repo_probe" != true ]]; then
base_std_log_info -l base_bash_libs.git "Script '$script_path' is not in a Git worktree; skipping latest-version check."
return 0
fi
else
repo_probe_status=$?
if [[ "$repo_probe" == *"not a git repository"* ||
"$repo_probe" == *"not a git repo"* ]]; then
base_std_log_info -l base_bash_libs.git "Not in a Git repo; skipping latest-version check."
return 0
fi
base_std_log_error -l base_bash_libs.git "Unable to discover the Git repository for '$script_path' (git status $repo_probe_status)."
return 5
fi
local repo_root_status
if repo_root=$(git -C "$script_dir" rev-parse --show-toplevel 2>/dev/null); then
:
else
repo_root_status=$?
base_std_log_error -l base_bash_libs.git "Unable to resolve the Git repository root for '$script_path' (git status $repo_root_status)."
return 5
fi
local prefix_status
if prefix=$(git -C "$script_dir" rev-parse --show-prefix 2>/dev/null); then
:
else
prefix_status=$?
base_std_log_error -l base_bash_libs.git "Unable to resolve the repo-relative path for '$script_path' (git status $prefix_status)."
return 5
fi
rel_path="${prefix}$(basename -- "$script_path")"
local tracked_status
if git -C "$repo_root" ls-files --error-unmatch -- "$rel_path" >/dev/null 2>&1; then
tracked_status=0
else
tracked_status=$?
fi
case "$tracked_status" in
0)
;;
1)
base_std_log_info -l base_bash_libs.git "Script '$rel_path' is not tracked in git; skipping latest-version check."
return 0
;;
*)
base_std_log_error -l base_bash_libs.git "Unable to determine whether '$rel_path' is tracked (git status $tracked_status)."
return 5
;;
esac
local dirty=false diff_status
if git -C "$repo_root" diff --quiet -- "$rel_path"; then
diff_status=0
else
diff_status=$?
fi
case "$diff_status" in
0)
;;
1)
dirty=true
;;
*)
base_std_log_error -l base_bash_libs.git "Unable to inspect working-tree changes for '$rel_path' (git status $diff_status)."
return 5
;;
esac
if git -C "$repo_root" diff --cached --quiet -- "$rel_path"; then
diff_status=0
else
diff_status=$?
fi
case "$diff_status" in
0)
;;
1)
dirty=true
;;
*)
base_std_log_error -l base_bash_libs.git "Unable to inspect staged changes for '$rel_path' (git status $diff_status)."
return 5
;;
esac
if [[ "$dirty" == true ]]; then
base_std_log_warn -l base_bash_libs.git "Script '$rel_path' has local modifications; version may not match repo."
fi
local branch_name branch_status
if branch_name=$(git -C "$repo_root" symbolic-ref --quiet --short HEAD 2>/dev/null); then
:
else
branch_status=$?
if ((branch_status == 1)); then
base_std_log_info -l base_bash_libs.git "Repository is in a detached HEAD state; skipping latest-version check."
[[ "$dirty" == true ]] && return 3
return 0
fi
base_std_log_error -l base_bash_libs.git "Unable to determine the current Git branch (git status $branch_status)."
return 5
fi
local upstream upstream_result upstream_status
if upstream_result=$(git -C "$repo_root" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>&1); then
upstream="$upstream_result"
else
upstream_status=$?
if [[ "$upstream_result" == *"no upstream"* ||
"$upstream_result" == *"does not have any upstream"* ]]; then
base_std_log_info -l base_bash_libs.git "No upstream branch configured for '$branch_name'; skipping latest-version check."
[[ "$dirty" == true ]] && return 3
return 0
fi
base_std_log_error -l base_bash_libs.git "Unable to resolve the upstream for '$branch_name' (git status $upstream_status)."
return 5
fi
if [[ -z "$upstream" || "$upstream" == *$'\n'* ]]; then
base_std_log_error -l base_bash_libs.git "Git returned an invalid upstream for '$branch_name'."
return 5
fi
if [[ "$fetch_before_check" == true ]]; then
if git -C "$repo_root" fetch --quiet; then
base_std_log_info -l base_bash_libs.git "Fetched upstream state before latest-version check."
else
base_std_log_error -l base_bash_libs.git "Unable to fetch upstream state for '$upstream'; freshness is unknown."
return 5
fi
else
base_std_log_debug -l base_bash_libs.git "Using local remote-tracking refs; pass --fetch for a live remote check."
fi
local behind ahead rev_list_status
if behind=$(git -C "$repo_root" rev-list --count "HEAD..$upstream" 2>/dev/null); then
:
else
rev_list_status=$?
base_std_log_error -l base_bash_libs.git "Unable to compare HEAD with '$upstream' (git status $rev_list_status); freshness is unknown."
return 5
fi
if ahead=$(git -C "$repo_root" rev-list --count "$upstream..HEAD" 2>/dev/null); then
:
else
rev_list_status=$?
base_std_log_error -l base_bash_libs.git "Unable to compare '$upstream' with HEAD (git status $rev_list_status); freshness is unknown."
return 5
fi
if [[ ! "$behind" =~ ^[0-9]+$ || ! "$ahead" =~ ^[0-9]+$ ]]; then
base_std_log_error -l base_bash_libs.git "Git returned invalid freshness counts for '$upstream'; freshness is unknown."
return 5
fi
if ((behind > 0 && ahead > 0)); then
base_std_log_warn -l base_bash_libs.git "Repository has diverged from $upstream ($behind commit(s) behind, $ahead commit(s) ahead)."
[[ "$dirty" == true ]] && return 3
return 4
fi
if ((behind > 0)); then
base_std_log_warn -l base_bash_libs.git "Repository is $behind commit(s) behind $upstream. Script may be out of date."
[[ "$dirty" == true ]] && return 3
return 2
fi
if ((ahead > 0)); then
base_std_log_info -l base_bash_libs.git "Repository is $ahead commit(s) ahead of $upstream."
else
base_std_log_info -l base_bash_libs.git "Repository is up to date with $upstream."
fi
[[ "$dirty" == true ]] && return 3
return 0
}