Skip to content

Commit 647868e

Browse files
committed
Allow runtime override of dependencies while also simplifying coding with alias
1 parent fdcb296 commit 647868e

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

vcsh.in

+41-37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ VCSH_SELF='@TRANSFORMED_PACKAGE_NAME@'; export VCSH_SELF
2121
# Ensure all files created are accessible only to the current user.
2222
umask 0077
2323

24+
# Allow override of shell dependencies (including outside of $PATH) either by
25+
# setting ENV vars at build time or run time.
26+
alias git="${GIT:-@GIT@}"
27+
2428
fatal() {
2529
echo "$VCSH_SELF: fatal: $1" >&2
2630
[ -z "$2" ] && exit 1
@@ -169,7 +173,7 @@ info() {
169173
clone() {
170174
hook pre-clone
171175
# Check if remote is reachable. Abort early if there's a typo, TLS certificate problem, etc
172-
@GIT@ ls-remote "$GIT_REMOTE" 2> /dev/null || fatal "Can not reach '$GIT_REMOTE'"
176+
git ls-remote "$GIT_REMOTE" 2> /dev/null || fatal "Can not reach '$GIT_REMOTE'"
173177
init
174178
# Test which, if any, given or detected branches can be pulled from.
175179
# In a future version, if we need the logic, we could do the following:
@@ -179,7 +183,7 @@ clone() {
179183
# set VCSH_BRANCH if only one match
180184
# offer a list of all matching refs for the user to choose
181185
for VCSH_BRANCH_TEST in "$VCSH_BRANCH" master trunk development; do
182-
if [ $(@GIT@ ls-remote "$GIT_REMOTE" "$VCSH_BRANCH_TEST" 2> /dev/null | @WC@ -l ) -lt 1 ]; then
186+
if [ $(git ls-remote "$GIT_REMOTE" "$VCSH_BRANCH_TEST" 2> /dev/null | @WC@ -l ) -lt 1 ]; then
183187
info "remote branch '$VCSH_BRANCH_TEST' empty"
184188
else
185189
info "remote branch '$VCSH_BRANCH_TEST' found"
@@ -194,21 +198,21 @@ clone() {
194198
VCSH_BRANCH=$VCSH_BRANCH_REMOTE
195199

196200
# Set up remote
197-
@GIT@ remote add origin "$GIT_REMOTE"
198-
@GIT@ checkout -b "$VCSH_BRANCH" || return $?
199-
@GIT@ config branch."$VCSH_BRANCH".remote origin
200-
@GIT@ config branch."$VCSH_BRANCH".merge refs/heads/"$VCSH_BRANCH"
201-
GIT_VERSION_MAJOR=$(@GIT@ --version | @SED@ -E -n 's/.* ([0-9]+)\..*/\1/p' )
201+
git remote add origin "$GIT_REMOTE"
202+
git checkout -b "$VCSH_BRANCH" || return $?
203+
git config branch."$VCSH_BRANCH".remote origin
204+
git config branch."$VCSH_BRANCH".merge refs/heads/"$VCSH_BRANCH"
205+
GIT_VERSION_MAJOR=$(git --version | @SED@ -E -n 's/.* ([0-9]+)\..*/\1/p' )
202206
if [ 1 -lt "$GIT_VERSION_MAJOR" ];then
203-
@GIT@ fetch origin "$VCSH_BRANCH"
207+
git fetch origin "$VCSH_BRANCH"
204208
else
205-
@GIT@ fetch origin
209+
git fetch origin
206210
fi
207211
hook pre-merge
208-
@GIT@ read-tree -n -mu origin/"$VCSH_BRANCH" \
212+
git read-tree -n -mu origin/"$VCSH_BRANCH" \
209213
|| fatal "will stop after fetching and not try to merge!
210214
Once this situation has been resolved, run 'vcsh $VCSH_REPO_NAME pull' to finish cloning." 17 # editorconfig-checker-disable-line
211-
@GIT@ -c merge.ff=true merge origin/"$VCSH_BRANCH"
215+
git -c merge.ff=true merge origin/"$VCSH_BRANCH"
212216
hook post-merge
213217
hook post-clone
214218
retire
@@ -224,7 +228,7 @@ commit() {
224228
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
225229
use
226230
hook_repo pre-commit
227-
@GIT@ commit --untracked-files=no --quiet "$@"
231+
git commit --untracked-files=no --quiet "$@"
228232
hook_repo post-commit
229233
VCSH_COMMAND_RETURN_CODE=$?
230234
echo
@@ -236,7 +240,7 @@ delete() {
236240
cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
237241
use
238242
info "This operation WILL DESTROY DATA!"
239-
files=$(@GIT@ ls-files)
243+
files=$(git ls-files)
240244
echo "These files will be deleted:
241245
242246
$files
@@ -263,7 +267,7 @@ foreach() {
263267

264268
# We default to prefixing `git` to all commands passed to foreach, but
265269
# allow running in general context with -g
266-
command_prefix=@GIT@
270+
command_prefix=git
267271
# shellcheck disable=SC2220
268272
while getopts gp flag; do
269273
case "$flag" in
@@ -319,7 +323,7 @@ init() {
319323
[ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10
320324
mkdir -p "$VCSH_BASE" || fatal "could not create '$VCSH_BASE'" 50
321325
cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
322-
@GIT@ init --shared=false
326+
git init --shared=false
323327
upgrade
324328
hook post-init
325329
}
@@ -335,13 +339,13 @@ list_has_remote() {
335339
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
336340
# This command returns the tracking branch of the currently-checked-out local
337341
# branch, if any. See https://stackoverflow.com/a/9753364
338-
[ -n "$(@GIT@ for-each-ref "$(@GIT@ symbolic-ref -q HEAD)")" ] && echo "$VCSH_REPO_NAME"
342+
[ -n "$(git for-each-ref "$(git symbolic-ref -q HEAD)")" ] && echo "$VCSH_REPO_NAME"
339343
done
340344
}
341345

342346
get_files() {
343347
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
344-
@GIT@ ls-files --full-name
348+
git ls-files --full-name
345349
}
346350

347351
list_tracked() {
@@ -397,7 +401,7 @@ list_untracked() {
397401

398402
list_untracked_helper() {
399403
export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
400-
@GIT@ ls-files --others $exclude_standard_opt $directory_opt | (
404+
git ls-files --others $exclude_standard_opt $directory_opt | (
401405
while read -r line; do
402406
echo "$line"
403407
directory_component=${line%%/*}
@@ -420,7 +424,7 @@ pull() {
420424
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
421425
use
422426
hook_repo pre-pull
423-
@GIT@ pull
427+
git pull
424428
hook_repo post-pull
425429
VCSH_COMMAND_RETURN_CODE=$?
426430
echo
@@ -436,7 +440,7 @@ push() {
436440
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
437441
use
438442
hook_repo pre-push
439-
@GIT@ push
443+
git push
440444
hook_repo post-push
441445
VCSH_COMMAND_RETURN_CODE=$?
442446
echo
@@ -494,13 +498,13 @@ status_helper() {
494498
use
495499
# Shellcheck isn't understanding a complex block.
496500
# shellcheck disable=SC1083
497-
remote_tracking_branch=$(@GIT@ rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) && {
498-
commits_behind=$(@GIT@ log ..${remote_tracking_branch} --oneline | @WC@ -l)
499-
commits_ahead=$(@GIT@ log ${remote_tracking_branch}.. --oneline | @WC@ -l)
501+
remote_tracking_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) && {
502+
commits_behind=$(git log ..${remote_tracking_branch} --oneline | @WC@ -l)
503+
commits_ahead=$(git log ${remote_tracking_branch}.. --oneline | @WC@ -l)
500504
[ ${commits_behind} -ne 0 ] && echo "Behind $remote_tracking_branch by $commits_behind commits"
501505
[ ${commits_ahead} -ne 0 ] && echo "Ahead of $remote_tracking_branch by $commits_ahead commits"
502506
}
503-
@GIT@ ${VCSH_GIT_OPTIONS} status --short --untracked-files='no' | @SED@ -E 's@([^ ] +)@\1~/@'
507+
git ${VCSH_GIT_OPTIONS} status --short --untracked-files='no' | @SED@ -E 's@([^ ] +)@\1~/@'
504508
VCSH_COMMAND_RETURN_CODE=$?
505509
}
506510

@@ -509,20 +513,20 @@ upgrade() {
509513
# fake-bare repositories are not bare, actually. Set this to false
510514
# because otherwise Git complains "fatal: core.bare and core.worktree
511515
# do not make sense"
512-
@GIT@ config core.bare false
516+
git config core.bare false
513517
# core.worktree may be absolute or relative to $GIT_DIR, depending on
514518
# user preference
515519
if [ ! "x$VCSH_WORKTREE" = 'xabsolute' ]; then
516-
@GIT@ config core.worktree "$(cd "$GIT_DIR" && GIT_WORK_TREE=$VCSH_BASE @GIT@ rev-parse --show-cdup)"
520+
git config core.worktree "$(cd "$GIT_DIR" && GIT_WORK_TREE=$VCSH_BASE git rev-parse --show-cdup)"
517521
elif [ ! "x$VCSH_WORKTREE" = 'xrelative' ]; then
518-
@GIT@ config core.worktree "$VCSH_BASE"
522+
git config core.worktree "$VCSH_BASE"
519523
fi
520-
[ ! "x$VCSH_GITIGNORE" = 'xnone' ] && @GIT@ config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME"
521-
[ ! "x$VCSH_GITATTRIBUTES" = 'xnone' ] && @GIT@ config core.attributesfile ".gitattributes.d/$VCSH_REPO_NAME"
522-
@GIT@ config vcsh.vcsh 'true'
524+
[ ! "x$VCSH_GITIGNORE" = 'xnone' ] && git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME"
525+
[ ! "x$VCSH_GITATTRIBUTES" = 'xnone' ] && git config core.attributesfile ".gitattributes.d/$VCSH_REPO_NAME"
526+
git config vcsh.vcsh 'true'
523527
use
524-
[ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ] && @GIT@ add -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME"
525-
[ -e "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME" ] && @GIT@ add -f "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME"
528+
[ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME"
529+
[ -e "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME"
526530
hook post-upgrade
527531
}
528532

@@ -558,12 +562,12 @@ write_gitignore() {
558562
# Works in all shells we care about.
559563
# shellcheck disable=SC2039,SC3043
560564
local GIT_VERSION GIT_VERSION_MAJOR GIT_VERSION_MINOR
561-
GIT_VERSION="$(@GIT@ --version)"
565+
GIT_VERSION="$(git --version)"
562566
GIT_VERSION_MAJOR="$(echo "$GIT_VERSION" | @SED@ -E -n 's/.* ([0-9]+)\..*/\1/p')"
563567
GIT_VERSION_MINOR="$(echo "$GIT_VERSION" | @SED@ -E -n 's/.* ([0-9]+)\.([0-9]+)\..*/\2/p')"
564568
OLDIFS=$IFS
565569
IFS=$(printf '\n\t')
566-
gitignores=$(for file in $(@GIT@ ls-files); do
570+
gitignores=$(for file in $(git ls-files); do
567571
while true; do
568572
echo "$file"; new=${file%/*}
569573
[ x"$file" = x"$new" ] && break
@@ -604,7 +608,7 @@ write_gitignore() {
604608
fatal "could not move '$tempfile' to '$GIT_IGNORE_PATH'" 53
605609
}
606610

607-
debug "$(@GIT@ version)"
611+
debug "$(git version)"
608612

609613
if [ ! "x$VCSH_GITIGNORE" = 'xexact' ] && [ ! "x$VCSH_GITIGNORE" = 'xnone' ] && [ ! "x$VCSH_GITIGNORE" = 'xrecursive' ]; then
610614
fatal "'\$VCSH_GITIGNORE' must equal 'exact', 'none', or 'recursive'" 1
@@ -658,7 +662,7 @@ elif [ "$VCSH_COMMAND" = 'help' ]; then
658662
help && exit
659663
elif [ "$VCSH_COMMAND" = 'version' ]; then
660664
echo "$VCSH_SELF $VCSH_VERSION"
661-
@GIT@ version
665+
git version
662666
exit
663667
elif [ x"$VCSH_COMMAND" = x'which' ]; then
664668
[ -z "$2" ] && fatal "$VCSH_COMMAND: please specify a filename" 1
@@ -705,7 +709,7 @@ elif [ -n "$2" ]; then
705709
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
706710
[ -d "$GIT_DIR" ] || { help; exit 1; }
707711
shift 1
708-
set -- "@GIT@" "$@"
712+
set -- "git" "$@"
709713
elif [ -n "$VCSH_COMMAND" ]; then
710714
VCSH_COMMAND='enter'; export VCSH_COMMAND
711715
VCSH_REPO_NAME=$1; export VCSH_REPO_NAME

0 commit comments

Comments
 (0)