Skip to content

Commit 9e90d0c

Browse files
committed
git-helper: fix remote making repos shallow
bug introduced in 6c9e339 which shouldn't have been an issue, but turns out providing a depth discards `--dry-run` once unshallowed, this undoes the need for the prior rebase workaround from 1deb5b7 which sought to resolve this error on shallow repos, and introduced a bug of `/github/home/.local/share/dorothy/commands/git-helper: line 1211: rebase: unbound variable` here is what the diverged/shallow bug looked like: ``` ┌ Update Dorothy ┐ ┌ Ensure required dependencies ┐ └ Ensure required dependencies ┘ ┌ Syncing the Dorothy installation at /Users/balupton/.local/share/dorothy ┐ Fetched /Users/balupton/.local/share/dorothy remote = origin Pulling /Users/balupton/.local/share/dorothy remote = origin branch = master < git pull origin master > From github.com:bevry/dorothy * branch master -> FETCH_HEAD hint: Diverging branches can't be fast-forwarded, you need to either: hint: hint: git merge --no-ff hint: hint: or: hint: hint: git rebase hint: hint: Disable this message with "git config set advice.diverging false" fatal: Not possible to fast-forward, aborting. </ git pull origin master >[128] Failed to pull /Users/balupton/.local/share/dorothy remote = origin branch = master ERROR: Dorothy was unable to be automatically updated. Update Dorothy manually then try again. ```
1 parent 97e6431 commit 9e90d0c

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

commands/dorothy

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ function dorothy_() (
166166
--remote=<remote>
167167
If Dorothy needs to be installed/updated, use this remote name.
168168
--user=<user> | --no-user
169-
If Dorothy needs to be installed,updated, use this Repository URI for the Dorothy User Configuration.
169+
If Dorothy needs to be installed/updated, use this Repository URI for the Dorothy User Configuration.
170+
--depth=<depth> | --unshallow
171+
If dorothy needs to be installed/updated, only fetch <depth> commits from tip.
172+
If \`0\` or <unshallow>, then unshallow the history.
170173
171174
--[no-]deps
172175
If disabled, skip dependency checks. Saves time if they are already known to be configured correctly.
@@ -243,7 +246,7 @@ function dorothy_() (
243246
}
244247

245248
# process action arguments
246-
local item action='' option_xdg='' option_deps='' option_branch='' option_remote='' option_user='' option_args=() # option_slug option_reference defined earlier
249+
local item action='' option_xdg='' option_deps='' option_branch='' option_remote='' option_depth='' option_user='' option_args=() # option_slug option_reference defined earlier
247250
function __affirm_action_is_undefined {
248251
if [[ -n $action ]]; then
249252
help --help='Only one <action> may be provided.' || return $?
@@ -269,6 +272,13 @@ function dorothy_() (
269272
;;
270273
'--commit='* | '--ref='* | '--reference='*) option_reference="${item#*=}" ;;
271274
'--remote='*) option_remote="${item#*=}" ;;
275+
--depth=*) option_depth="${item#*=}" ;;
276+
--no-unshallow* | --unshallow*)
277+
__flag --source+target={item} --affirmative --coerce || return $?
278+
if [[ $item == 'yes' ]]; then
279+
option_depth=0
280+
fi
281+
;;
272282
'--no-user') option_user='none' ;;
273283
'--user='*) option_user="${item#*=}" ;;
274284
'repl')
@@ -755,9 +765,9 @@ function dorothy_() (
755765
# verify, so we don't have to do it via `--verify` later
756766
git-helper verify --path="$DOROTHY" || return $?
757767
# sync with its configured remote first (their upstream)
758-
git-helper sync --rebase --path="$DOROTHY" || return $?
768+
git-helper sync --rebase --depth="$option_depth" --path="$DOROTHY" || return $?
759769
# sync with our intended remote second (the upstream)
760-
git-helper sync --rebase --path="$DOROTHY" --remote="$the_upstream_remote" --url="$the_upstream_git_https" || return $?
770+
git-helper sync --rebase --depth="$option_depth" --path="$DOROTHY" --remote="$the_upstream_remote" --url="$the_upstream_git_https" || return $?
761771
__print_style --g2="Syncing the Dorothy installation at $DOROTHY" || return $?
762772
else
763773
return 1
@@ -824,7 +834,7 @@ function dorothy_() (
824834

825835
# create/update
826836
__print_style --h2="Syncing the Dorothy User Configuration at $DOROTHY_USER" || return $?
827-
git-helper sync --rebase --path="$DOROTHY_USER" --label='Dorothy User Configuration' --new --update=optional --project='dotfiles' --description="$dorothy_user_description" --homepage="$dorothy_homepage" --url="$option_user"
837+
git-helper sync --rebase --depth="$option_depth" --path="$DOROTHY_USER" --label='Dorothy User Configuration' --new --update=optional --project='dotfiles' --description="$dorothy_user_description" --homepage="$dorothy_homepage" --url="$option_user"
828838
__print_style --g2="Syncing the Dorothy User Configuration at $DOROTHY_USER" || return $?
829839

830840
# move if necessary, and fix permissions
@@ -1471,7 +1481,7 @@ function dorothy_() (
14711481
source "$DOROTHY/sources/ripgrep.bash"
14721482

14731483
# fixtures
1474-
git-helper sync --path="$DOROTHY/fixtures" --label='Dorothy Fixtures' --update=optional --url="$dorothy_fixtures_uri" || return $?
1484+
git-helper sync --rebase --depth="$option_depth" --path="$DOROTHY/fixtures" --label='Dorothy Fixtures' --update=optional --url="$dorothy_fixtures_uri" || return $?
14751485

14761486
# now process arguments, as arguments depends on `debug-bash`, which depends on Dorothy existing
14771487
local item only=() skips=('dorothy' 'bash.bash') debug='' trace='' user_bash_binaries=()

commands/git-helper

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ function git_helper() (
130130
--[no-]test
131131
If enabled, test the connection to <remote>.
132132
Will default to enabled if <remote> and <url> is provided with <mode:apply>.
133+
--depth=<depth> | --unshallow
134+
Prune the remote's fetched history to <depth> commits from tip.
135+
If \`0\` or <unshallow>, then unshallow the history.
136+
Only applicable with <test> enabled.
133137
& url [...arguments]
134138
Alias for \`remote --result=url [...arguments]\`
135139
@@ -149,8 +153,9 @@ function git_helper() (
149153
If provided, then configure and sync the remote to this <url>.
150154
--branch=<branch>
151155
If provided, sync with this branch, instead of the current branch, or if indeterminate, the default branch.
152-
--depth=<depth>
156+
--depth=<depth> | --unshallow
153157
If cloning or pulling, only fetch <depth> commits from tip.
158+
If \`0\` or <unshallow>, then unshallow the history.
154159
--[no-]rebase
155160
If pulling, rebase our changes atop of the remote changes, useful to avoid diverging branch blocks.
156161
--[no-]new [...arguments]
@@ -937,7 +942,7 @@ function git_helper() (
937942

938943
function __remote {
939944
# process
940-
local item mode='get' protocol='' remote='' url='' result='' verify='' test=''
945+
local item mode='get' protocol='' remote='' url='' result='' verify='' test='' depth=''
941946
while [[ $# -ne 0 ]]; do
942947
item="$1"
943948
shift
@@ -951,6 +956,13 @@ function git_helper() (
951956
--no-result* | --result*) __flag --source={item} --target={result} --affirmative --no-coerce || return $? ;;
952957
--no-verify* | --verify*) __flag --source={item} --target={verify} --affirmative --coerce || return $? ;;
953958
--no-test* | --test*) __flag --source={item} --target={test} --affirmative --coerce || return $? ;;
959+
--depth=*) depth="${item#*=}" ;;
960+
--no-unshallow* | --unshallow*)
961+
__flag --source+target={item} --affirmative --coerce || return $?
962+
if [[ $item == 'yes' ]]; then
963+
depth=0
964+
fi
965+
;;
954966
--*) __unrecognised_flag "$item" || return $? ;;
955967
*) __unrecognised_argument "$item" || return $? ;;
956968
esac
@@ -1099,9 +1111,25 @@ function git_helper() (
10991111

11001112
# test application
11011113
function __test_with_fallback {
1102-
if eval-helper --quiet --wrap -- git fetch --dry-run --depth=1 "$remote"; then
1114+
# before this was `--dry-run --depth=1`, however specifying `--depth=<depth>` disregards `--dry-run` which then requires `git fetch --unshallow` to repair the `grafted` and diverged (diverged only in perception) commit history
1115+
# there is `---negotiate-only` which seems like it would work, but it isn't
1116+
# however, without any other args, it seems it will just do what was previously configured, however, this is called in `sync` before our initial fetch/pull, so respect the depth if one was provided, and file a bug with git that depth discards dry-run
1117+
local depth_args=()
1118+
# if our depth is 0, and if it is shallow
1119+
if [[ -n $depth ]]; then
1120+
if [[ $depth -ge 1 ]]; then
1121+
# make shallow
1122+
depth_args+=("--depth=$depth")
1123+
elif [[ -e .git/shallow ]]; then
1124+
# make unshallow
1125+
depth_args+=('--unshallow')
1126+
fi # else already unshallow
1127+
fi
1128+
# perform the fetch
1129+
if eval-helper --quiet --wrap -- git fetch --dry-run "${depth_args[@]}" "$remote"; then
11031130
return 0
11041131
fi
1132+
# it failed, handle fallback
11051133
local tested_url
11061134
tested_url="$(git remote get-url "$remote")" || return $?
11071135
# @todo once `choose` supports `--inline` then change to `choose` instead of this `confirm` seesaw
@@ -1178,7 +1206,7 @@ function git_helper() (
11781206

11791207
function __sync {
11801208
# process
1181-
local item remote='origin' url='' branch='' depth='' protocol='' project='' description='' homepage='' new='' init='' update=''
1209+
local item remote='origin' url='' branch='' depth='' protocol='' project='' description='' homepage='' rebase='' new='' init='' update=''
11821210
while [[ $# -ne 0 ]]; do
11831211
item="$1"
11841212
shift
@@ -1187,6 +1215,12 @@ function git_helper() (
11871215
--url=*) url="${item#*=}" ;;
11881216
--branch=*) branch="${item#*=}" ;;
11891217
--depth=*) depth="${item#*=}" ;;
1218+
--no-unshallow* | --unshallow*)
1219+
__flag --source+target={item} --affirmative --coerce || return $?
1220+
if [[ $item == 'yes' ]]; then
1221+
depth=0
1222+
fi
1223+
;;
11901224
--no-protocol* | --protocol*) __flag --source={item} --target={protocol} --affirmative --no-coerce || return $? ;;
11911225
--project=*) project="${item#*=}" ;;
11921226
--description=*) description="${item#*=}" ;;
@@ -1200,12 +1234,6 @@ function git_helper() (
12001234
esac
12011235
done
12021236

1203-
# depth
1204-
local depth_args=()
1205-
if [[ -n $depth ]]; then
1206-
depth_args+=("--depth=$depth")
1207-
fi
1208-
12091237
# rebase
12101238
local rebase_args=()
12111239
if [[ $rebase == 'yes' ]]; then
@@ -1401,6 +1429,22 @@ function git_helper() (
14011429
return 0
14021430
fi
14031431

1432+
# prepare depth
1433+
local depth_args=()
1434+
function __prepare_depth {
1435+
depth_args=()
1436+
# if our depth is 0, and if it is shallow
1437+
if [[ -n $depth ]]; then
1438+
if [[ $depth -ge 1 ]]; then
1439+
# make shallow
1440+
depth_args+=("--depth=$depth")
1441+
elif [[ -e .git/shallow ]]; then
1442+
# make unshallow
1443+
depth_args+=('--unshallow')
1444+
fi # else already unshallow
1445+
fi
1446+
}
1447+
14041448
# wrap the update to handle optional
14051449
function __do_update {
14061450
# avoid merge conflicts
@@ -1409,10 +1453,11 @@ function git_helper() (
14091453
git config --local pull.ff only || return $?
14101454

14111455
# ensure and fetch the remote, testing it
1412-
remote="$(__remote --apply --remote="$remote" --url="$url" --protocol="$protocol" --verify --test)" || return $?
1413-
1456+
remote="$(__remote --apply --remote="$remote" --url="$url" --protocol="$protocol" --verify --test --depth="$depth")" || return $?
1457+
14141458
# fetch all origins, branches, and prune remote deleted branches
14151459
local pending success failure
1460+
__prepare_depth
14161461
pending="$(__print_style --bold='Fetching' ' ' --path="$option_path" ' ' --variable={remote})" || return $?
14171462
success="$(__print_style --success='Fetched' ' ' --path="$option_path" ' ' --variable={remote})" || return $?
14181463
failure="$(__print_style --error='Failed to fetch' ' ' --path="$option_path" ' ' --variable={remote})" || return $?
@@ -1506,6 +1551,7 @@ function git_helper() (
15061551
eval-helper --quiet --wrap --pending="$pending" --success="$success" --failure="$failure" -- \
15071552
git checkout --track "$remote/$branch" || return $?
15081553
else
1554+
__prepare_depth
15091555
pending="$(__print_style --bold='Pulling' ' ' --path="$option_path" ' ' --variable={remote} ' ' --variable={branch})" || return $?
15101556
success="$(__print_style --success='Pulled' ' ' --path="$option_path" ' ' --variable={remote} ' ' --variable={branch})" || return $?
15111557
failure="$(__print_style --error='Failed to pull' ' ' --path="$option_path" ' ' --variable={remote} ' ' --variable={branch})" || return $?

0 commit comments

Comments
 (0)