Skip to content

Commit 72bd207

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

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

vcsh.in

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ 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 comm="${COMM:-@COMM@}"
27+
alias git="${GIT:-@GIT@}"
28+
alias grep="${GREP:-@GREP@}"
29+
alias sed="${SED:-@SED@}"
30+
alias wc="${WC:-@WC@}"
31+
2432
fatal() {
2533
echo "$VCSH_SELF: fatal: $1" >&2
2634
[ -z "$2" ] && exit 1
@@ -169,7 +177,7 @@ info() {
169177
clone() {
170178
hook pre-clone
171179
# 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'"
180+
git ls-remote "$GIT_REMOTE" 2> /dev/null || fatal "Can not reach '$GIT_REMOTE'"
173181
init
174182
# Test which, if any, given or detected branches can be pulled from.
175183
# In a future version, if we need the logic, we could do the following:
@@ -179,7 +187,7 @@ clone() {
179187
# set VCSH_BRANCH if only one match
180188
# offer a list of all matching refs for the user to choose
181189
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
190+
if [ $(git ls-remote "$GIT_REMOTE" "$VCSH_BRANCH_TEST" 2> /dev/null | wc -l ) -lt 1 ]; then
183191
info "remote branch '$VCSH_BRANCH_TEST' empty"
184192
else
185193
info "remote branch '$VCSH_BRANCH_TEST' found"
@@ -194,21 +202,21 @@ clone() {
194202
VCSH_BRANCH=$VCSH_BRANCH_REMOTE
195203

196204
# 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' )
205+
git remote add origin "$GIT_REMOTE"
206+
git checkout -b "$VCSH_BRANCH" || return $?
207+
git config branch."$VCSH_BRANCH".remote origin
208+
git config branch."$VCSH_BRANCH".merge refs/heads/"$VCSH_BRANCH"
209+
GIT_VERSION_MAJOR=$(git --version | sed -E -n 's/.* ([0-9]+)\..*/\1/p' )
202210
if [ 1 -lt "$GIT_VERSION_MAJOR" ];then
203-
@GIT@ fetch origin "$VCSH_BRANCH"
211+
git fetch origin "$VCSH_BRANCH"
204212
else
205-
@GIT@ fetch origin
213+
git fetch origin
206214
fi
207215
hook pre-merge
208-
@GIT@ read-tree -n -mu origin/"$VCSH_BRANCH" \
216+
git read-tree -n -mu origin/"$VCSH_BRANCH" \
209217
|| fatal "will stop after fetching and not try to merge!
210218
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"
219+
git -c merge.ff=true merge origin/"$VCSH_BRANCH"
212220
hook post-merge
213221
hook post-clone
214222
retire
@@ -224,7 +232,7 @@ commit() {
224232
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
225233
use
226234
hook_repo pre-commit
227-
@GIT@ commit --untracked-files=no --quiet "$@"
235+
git commit --untracked-files=no --quiet "$@"
228236
hook_repo post-commit
229237
VCSH_COMMAND_RETURN_CODE=$?
230238
echo
@@ -236,7 +244,7 @@ delete() {
236244
cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
237245
use
238246
info "This operation WILL DESTROY DATA!"
239-
files=$(@GIT@ ls-files)
247+
files=$(git ls-files)
240248
echo "These files will be deleted:
241249
242250
$files
@@ -263,7 +271,7 @@ foreach() {
263271

264272
# We default to prefixing `git` to all commands passed to foreach, but
265273
# allow running in general context with -g
266-
command_prefix=@GIT@
274+
command_prefix=git
267275
# shellcheck disable=SC2220
268276
while getopts gp flag; do
269277
case "$flag" in
@@ -279,7 +287,7 @@ foreach() {
279287
use
280288
hook_repo pre-foreach
281289
if [ -n "${VCSH_PRINT_REPO_PREFIX+x}" ]; then
282-
$command_prefix "$@" | @SED@ "s/^/$VCSH_REPO_NAME: /"
290+
$command_prefix "$@" | sed "s/^/$VCSH_REPO_NAME: /"
283291
else
284292
echo "$VCSH_REPO_NAME:"
285293
$command_prefix "$@"
@@ -319,7 +327,7 @@ init() {
319327
[ ! -e "$GIT_DIR" ] || fatal "'$GIT_DIR' exists" 10
320328
mkdir -p "$VCSH_BASE" || fatal "could not create '$VCSH_BASE'" 50
321329
cd "$VCSH_BASE" || fatal "could not enter '$VCSH_BASE'" 11
322-
@GIT@ init --shared=false
330+
git init --shared=false
323331
upgrade
324332
hook post-init
325333
}
@@ -335,13 +343,13 @@ list_has_remote() {
335343
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
336344
# This command returns the tracking branch of the currently-checked-out local
337345
# branch, if any. See https://stackoverflow.com/a/9753364
338-
[ -n "$(@GIT@ for-each-ref "$(@GIT@ symbolic-ref -q HEAD)")" ] && echo "$VCSH_REPO_NAME"
346+
[ -n "$(git for-each-ref "$(git symbolic-ref -q HEAD)")" ] && echo "$VCSH_REPO_NAME"
339347
done
340348
}
341349

342350
get_files() {
343351
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
344-
@GIT@ ls-files --full-name
352+
git ls-files --full-name
345353
}
346354

347355
list_tracked() {
@@ -356,7 +364,7 @@ list_tracked() {
356364
}
357365

358366
list_tracked_helper() {
359-
@SED@ "s,^,$(printf '%s\n' "$VCSH_BASE/" | @SED@ 's/[,\&]/\\&/g')," | sort -u
367+
sed "s,^,$(printf '%s\n' "$VCSH_BASE/" | sed 's/[,\&]/\\&/g')," | sort -u
360368
}
361369

362370
list_tracked_by() {
@@ -397,7 +405,7 @@ list_untracked() {
397405

398406
list_untracked_helper() {
399407
export GIT_DIR="$VCSH_REPO_D/$VCSH_REPO_NAME.git"
400-
@GIT@ ls-files --others $exclude_standard_opt $directory_opt | (
408+
git ls-files --others $exclude_standard_opt $directory_opt | (
401409
while read -r line; do
402410
echo "$line"
403411
directory_component=${line%%/*}
@@ -409,7 +417,7 @@ list_untracked_helper() {
409417
cp "$temp_file_others" "$temp_file_untracked" || fatal 'Could not copy temp file'
410418
fi
411419
cp "$temp_file_untracked" "$temp_file_untracked_copy" || fatal 'Could not copy temp file'
412-
@COMM@ -12 "$temp_file_others" "$temp_file_untracked_copy" > "$temp_file_untracked"
420+
comm -12 "$temp_file_others" "$temp_file_untracked_copy" > "$temp_file_untracked"
413421
}
414422

415423
pull() {
@@ -420,7 +428,7 @@ pull() {
420428
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
421429
use
422430
hook_repo pre-pull
423-
@GIT@ pull
431+
git pull
424432
hook_repo post-pull
425433
VCSH_COMMAND_RETURN_CODE=$?
426434
echo
@@ -436,7 +444,7 @@ push() {
436444
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
437445
use
438446
hook_repo pre-push
439-
@GIT@ push
447+
git push
440448
hook_repo post-push
441449
VCSH_COMMAND_RETURN_CODE=$?
442450
echo
@@ -494,13 +502,13 @@ status_helper() {
494502
use
495503
# Shellcheck isn't understanding a complex block.
496504
# 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)
505+
remote_tracking_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) && {
506+
commits_behind=$(git log ..${remote_tracking_branch} --oneline | wc -l)
507+
commits_ahead=$(git log ${remote_tracking_branch}.. --oneline | wc -l)
500508
[ ${commits_behind} -ne 0 ] && echo "Behind $remote_tracking_branch by $commits_behind commits"
501509
[ ${commits_ahead} -ne 0 ] && echo "Ahead of $remote_tracking_branch by $commits_ahead commits"
502510
}
503-
@GIT@ ${VCSH_GIT_OPTIONS} status --short --untracked-files='no' | @SED@ -E 's@([^ ] +)@\1~/@'
511+
git ${VCSH_GIT_OPTIONS} status --short --untracked-files='no' | sed -E 's@([^ ] +)@\1~/@'
504512
VCSH_COMMAND_RETURN_CODE=$?
505513
}
506514

@@ -509,20 +517,20 @@ upgrade() {
509517
# fake-bare repositories are not bare, actually. Set this to false
510518
# because otherwise Git complains "fatal: core.bare and core.worktree
511519
# do not make sense"
512-
@GIT@ config core.bare false
520+
git config core.bare false
513521
# core.worktree may be absolute or relative to $GIT_DIR, depending on
514522
# user preference
515523
if [ ! "x$VCSH_WORKTREE" = 'xabsolute' ]; then
516-
@GIT@ config core.worktree "$(cd "$GIT_DIR" && GIT_WORK_TREE=$VCSH_BASE @GIT@ rev-parse --show-cdup)"
524+
git config core.worktree "$(cd "$GIT_DIR" && GIT_WORK_TREE=$VCSH_BASE git rev-parse --show-cdup)"
517525
elif [ ! "x$VCSH_WORKTREE" = 'xrelative' ]; then
518-
@GIT@ config core.worktree "$VCSH_BASE"
526+
git config core.worktree "$VCSH_BASE"
519527
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'
528+
[ ! "x$VCSH_GITIGNORE" = 'xnone' ] && git config core.excludesfile ".gitignore.d/$VCSH_REPO_NAME"
529+
[ ! "x$VCSH_GITATTRIBUTES" = 'xnone' ] && git config core.attributesfile ".gitattributes.d/$VCSH_REPO_NAME"
530+
git config vcsh.vcsh 'true'
523531
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"
532+
[ -e "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitignore.d/$VCSH_REPO_NAME"
533+
[ -e "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME" ] && git add -f "$VCSH_BASE/.gitattributes.d/$VCSH_REPO_NAME"
526534
hook post-upgrade
527535
}
528536

@@ -535,7 +543,7 @@ which() {
535543
# It's ok to modify VCSH_REPO_NAME in a subshell.
536544
# shellcheck disable=SC2030
537545
output=$(for VCSH_REPO_NAME in $(list); do
538-
get_files | @GREP@ -- "$VCSH_COMMAND_PARAMETER" | @SED@ "s/^/$VCSH_REPO_NAME: /"
546+
get_files | grep -- "$VCSH_COMMAND_PARAMETER" | sed "s/^/$VCSH_REPO_NAME: /"
539547
done | sort -u)
540548
if [ -z "$output" ]; then
541549
fatal "'$VCSH_COMMAND_PARAMETER' does not exist" 1
@@ -558,12 +566,12 @@ write_gitignore() {
558566
# Works in all shells we care about.
559567
# shellcheck disable=SC2039,SC3043
560568
local GIT_VERSION GIT_VERSION_MAJOR GIT_VERSION_MINOR
561-
GIT_VERSION="$(@GIT@ --version)"
562-
GIT_VERSION_MAJOR="$(echo "$GIT_VERSION" | @SED@ -E -n 's/.* ([0-9]+)\..*/\1/p')"
563-
GIT_VERSION_MINOR="$(echo "$GIT_VERSION" | @SED@ -E -n 's/.* ([0-9]+)\.([0-9]+)\..*/\2/p')"
569+
GIT_VERSION="$(git --version)"
570+
GIT_VERSION_MAJOR="$(echo "$GIT_VERSION" | sed -E -n 's/.* ([0-9]+)\..*/\1/p')"
571+
GIT_VERSION_MINOR="$(echo "$GIT_VERSION" | sed -E -n 's/.* ([0-9]+)\.([0-9]+)\..*/\2/p')"
564572
OLDIFS=$IFS
565573
IFS=$(printf '\n\t')
566-
gitignores=$(for file in $(@GIT@ ls-files); do
574+
gitignores=$(for file in $(git ls-files); do
567575
while true; do
568576
echo "$file"; new=${file%/*}
569577
[ x"$file" = x"$new" ] && break
@@ -580,10 +588,10 @@ write_gitignore() {
580588

581589
echo '*' > "$tempfile" || fatal "could not write to '$tempfile'" 57
582590
for gitignore in $gitignores; do
583-
echo "$gitignore" | @SED@ 's@^@!/@' >> "$tempfile" ||
591+
echo "$gitignore" | sed 's@^@!/@' >> "$tempfile" ||
584592
fatal "could not write to '$tempfile'" 57
585593
if [ "x$VCSH_GITIGNORE" = 'xrecursive' ] && [ -d "$gitignore" ]; then
586-
{ echo "$gitignore/*" | @SED@ 's@^@!/@' >> "$tempfile" ||
594+
{ echo "$gitignore/*" | sed 's@^@!/@' >> "$tempfile" ||
587595
fatal "could not write to '$tempfile'" 57; }
588596
fi
589597
done
@@ -604,7 +612,7 @@ write_gitignore() {
604612
fatal "could not move '$tempfile' to '$GIT_IGNORE_PATH'" 53
605613
}
606614

607-
debug "$(@GIT@ version)"
615+
debug "$(git version)"
608616

609617
if [ ! "x$VCSH_GITIGNORE" = 'xexact' ] && [ ! "x$VCSH_GITIGNORE" = 'xnone' ] && [ ! "x$VCSH_GITIGNORE" = 'xrecursive' ]; then
610618
fatal "'\$VCSH_GITIGNORE' must equal 'exact', 'none', or 'recursive'" 1
@@ -658,7 +666,7 @@ elif [ "$VCSH_COMMAND" = 'help' ]; then
658666
help && exit
659667
elif [ "$VCSH_COMMAND" = 'version' ]; then
660668
echo "$VCSH_SELF $VCSH_VERSION"
661-
@GIT@ version
669+
git version
662670
exit
663671
elif [ x"$VCSH_COMMAND" = x'which' ]; then
664672
[ -z "$2" ] && fatal "$VCSH_COMMAND: please specify a filename" 1
@@ -705,7 +713,7 @@ elif [ -n "$2" ]; then
705713
GIT_DIR=$VCSH_REPO_D/$VCSH_REPO_NAME.git; export GIT_DIR
706714
[ -d "$GIT_DIR" ] || { help; exit 1; }
707715
shift 1
708-
set -- "@GIT@" "$@"
716+
set -- "git" "$@"
709717
elif [ -n "$VCSH_COMMAND" ]; then
710718
VCSH_COMMAND='enter'; export VCSH_COMMAND
711719
VCSH_REPO_NAME=$1; export VCSH_REPO_NAME
@@ -718,7 +726,7 @@ fi
718726

719727
# Did we receive a directory instead of a name?
720728
# Mangle the input to fit normal operation.
721-
if echo "$VCSH_REPO_NAME" | @GREP@ -q '/'; then
729+
if echo "$VCSH_REPO_NAME" | grep -q '/'; then
722730
GIT_DIR=$VCSH_REPO_NAME; export GIT_DIR
723731
VCSH_REPO_NAME=$(basename "$VCSH_REPO_NAME" .git); export VCSH_REPO_NAME
724732
fi
@@ -740,7 +748,7 @@ check_dir "$VCSH_REPO_D"
740748
[ ! "x$VCSH_GITATTRIBUTES" = 'xnone' ] && check_dir "$VCSH_BASE/.gitattributes.d"
741749

742750
verbose "$VCSH_COMMAND begin"
743-
VCSH_COMMAND=$(echo "$VCSH_COMMAND" | @SED@ 's/-/_/g'); export VCSH_COMMAND
751+
VCSH_COMMAND=$(echo "$VCSH_COMMAND" | sed 's/-/_/g'); export VCSH_COMMAND
744752

745753
# Source repo-specific configuration file
746754
# shellcheck source=/dev/null

0 commit comments

Comments
 (0)