Browse Source

subtree: don't have loose code outside of a function

Shove all of the loose code inside of a main() function.

This comes down to personal preference more than anything else.  A
preference that I've developed over years of maintaining large Bash
scripts, but still a mere personal preference.

In this specific case, it's also moving the `set -- -h`, the `git
rev-parse --parseopt`, and the `. git-sh-setup` to be closer to all
the rest of the argument parsing, which is a readability win on its
own, IMO.

"Ignore space change" is probably helpful when viewing this diff.

Signed-off-by: Luke Shumaker <lukeshu@datawire.io>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Luke Shumaker 4 years ago committed by Junio C Hamano
parent
commit
5a3569774f
  1. 77
      contrib/subtree/git-subtree.sh

77
contrib/subtree/git-subtree.sh

@ -4,10 +4,7 @@ @@ -4,10 +4,7 @@
#
# Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
#
if test $# -eq 0
then
set -- -h
fi

OPTS_SPEC="\
git subtree add --prefix=<prefix> <commit>
git subtree add --prefix=<prefix> <repository> <ref>
@ -30,12 +27,8 @@ rejoin merge the new branch back into HEAD @@ -30,12 +27,8 @@ rejoin merge the new branch back into HEAD
options for 'add', 'merge', and 'pull'
squash merge subtree changes as a single commit
"
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"

PATH=$PATH:$(git --exec-path)
. git-sh-setup

require_work_tree

quiet=
branch=
@ -84,8 +77,17 @@ ensure_single_rev () { @@ -84,8 +77,17 @@ ensure_single_rev () {
fi
}

while test $# -gt 0
do
main () {
if test $# -eq 0
then
set -- -h
fi
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
. git-sh-setup
require_work_tree

while test $# -gt 0
do
opt="$1"
shift

@ -150,45 +152,45 @@ do @@ -150,45 +152,45 @@ do
die "Unexpected option: $opt"
;;
esac
done
done

command="$1"
shift
command="$1"
shift

case "$command" in
add|merge|pull)
case "$command" in
add|merge|pull)
default=
;;
split|push)
split|push)
default="--default HEAD"
;;
*)
*)
die "Unknown command '$command'"
;;
esac
esac

if test -z "$prefix"
then
if test -z "$prefix"
then
die "You must provide the --prefix option."
fi
fi

case "$command" in
add)
case "$command" in
add)
test -e "$prefix" &&
die "prefix '$prefix' already exists."
;;
*)
*)
test -e "$prefix" ||
die "'$prefix' does not exist; use 'git subtree add'"
;;
esac
esac

dir="$(dirname "$prefix/.")"
dir="$(dirname "$prefix/.")"

if test "$command" != "pull" &&
if test "$command" != "pull" &&
test "$command" != "add" &&
test "$command" != "push"
then
then
revs=$(git rev-parse $default --revs-only "$@") || exit $?
dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
ensure_single_rev $revs
@ -196,14 +198,17 @@ then @@ -196,14 +198,17 @@ then
then
die "Error: Use --prefix instead of bare filenames."
fi
fi
fi

debug "command: {$command}"
debug "quiet: {$quiet}"
debug "revs: {$revs}"
debug "dir: {$dir}"
debug "opts: {$*}"
debug
debug "command: {$command}"
debug "quiet: {$quiet}"
debug "revs: {$revs}"
debug "dir: {$dir}"
debug "opts: {$*}"
debug

"cmd_$command" "$@"
}

cache_setup () {
cachedir="$GIT_DIR/subtree-cache/$$"
@ -898,4 +903,4 @@ cmd_push () { @@ -898,4 +903,4 @@ cmd_push () {
fi
}

"cmd_$command" "$@"
main "$@"

Loading…
Cancel
Save