Skip to content

Commit e4f8baa

Browse files
LukeShugitster
authored andcommitted
subtree: parse revs in individual cmd_ functions
The main argument parser goes ahead and tries to parse revs to make things simpler for the sub-command implementations. But, it includes enough special cases for different sub-commands. And it's difficult having having to think about "is this info coming from an argument, or a global variable?". So the main argument parser's effort to make things "simpler" ends up just making it more confusing and complicated. Begone with the 'revs' global variable; parse 'rev=$(...)' as needed in individual 'cmd_*' functions. Begone with the 'default' global variable. Its would-be value is knowable just from which function we're in. Begone with the 'ensure_single_rev' function. Its functionality can be achieved by passing '--verify' to 'git rev-parse'. Signed-off-by: Luke Shumaker <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bbffb02 commit e4f8baa

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

contrib/subtree/git-subtree.sh

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ assert () {
7676
fi
7777
}
7878

79-
ensure_single_rev () {
80-
if test $# -ne 1
81-
then
82-
die "You must provide exactly one revision. Got: '$*'"
83-
fi
84-
}
85-
8679
main () {
8780
if test $# -eq 0
8881
then
@@ -164,11 +157,8 @@ main () {
164157
shift
165158

166159
case "$arg_command" in
167-
add|merge|pull)
168-
default=
169-
;;
170-
split|push)
171-
default="--default HEAD"
160+
add|merge|pull|split|push)
161+
:
172162
;;
173163
*)
174164
die "Unknown command '$arg_command'"
@@ -193,22 +183,8 @@ main () {
193183

194184
dir="$(dirname "$arg_prefix/.")"
195185

196-
if test "$arg_command" != "pull" &&
197-
test "$arg_command" != "add" &&
198-
test "$arg_command" != "push"
199-
then
200-
revs=$(git rev-parse $default --revs-only "$@") || exit $?
201-
dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
202-
ensure_single_rev $revs
203-
if test -n "$dirs"
204-
then
205-
die "Error: Use --prefix instead of bare filenames."
206-
fi
207-
fi
208-
209186
debug "command: {$arg_command}"
210187
debug "quiet: {$GIT_QUIET}"
211-
debug "revs: {$revs}"
212188
debug "dir: {$dir}"
213189
debug "opts: {$*}"
214190
debug
@@ -714,14 +690,13 @@ cmd_add_repository () {
714690
repository=$1
715691
refspec=$2
716692
git fetch "$@" || exit $?
717-
revs=FETCH_HEAD
718-
set -- $revs
719-
cmd_add_commit "$@"
693+
cmd_add_commit FETCH_HEAD
720694
}
721695

722696
cmd_add_commit () {
723-
rev=$(git rev-parse $default --revs-only "$@") || exit $?
724-
ensure_single_rev $rev
697+
# The rev has already been validated by cmd_add(), we just
698+
# need to normalize it.
699+
rev=$(git rev-parse --verify "$1^{commit}") || exit $?
725700

726701
debug "Adding $dir as '$rev'..."
727702
git read-tree --prefix="$dir" $rev || exit $?
@@ -752,6 +727,17 @@ cmd_add_commit () {
752727
}
753728

754729
cmd_split () {
730+
if test $# -eq 0
731+
then
732+
rev=$(git rev-parse HEAD)
733+
elif test $# -eq 1
734+
then
735+
rev=$(git rev-parse -q --verify "$1^{commit}") ||
736+
die "'$1' does not refer to a commit"
737+
else
738+
die "You must provide exactly one revision. Got: '$*'"
739+
fi
740+
755741
debug "Splitting $dir..."
756742
cache_setup || exit $?
757743

@@ -768,12 +754,12 @@ cmd_split () {
768754
done || exit $?
769755
fi
770756

771-
unrevs="$(find_existing_splits "$dir" "$revs")" || exit $?
757+
unrevs="$(find_existing_splits "$dir" "$rev")" || exit $?
772758

773759
# We can't restrict rev-list to only $dir here, because some of our
774760
# parents have the $dir contents the root, and those won't match.
775761
# (and rev-list --follow doesn't seem to solve this)
776-
grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
762+
grl='git rev-list --topo-order --reverse --parents $rev $unrevs'
777763
revmax=$(eval "$grl" | wc -l)
778764
revcount=0
779765
createcount=0
@@ -820,8 +806,10 @@ cmd_split () {
820806
}
821807

822808
cmd_merge () {
823-
rev=$(git rev-parse $default --revs-only "$@") || exit $?
824-
ensure_single_rev $rev
809+
test $# -eq 1 ||
810+
die "You must provide exactly one revision. Got: '$*'"
811+
rev=$(git rev-parse -q --verify "$1^{commit}") ||
812+
die "'$1' does not refer to a commit"
825813
ensure_clean
826814

827815
if test -n "$arg_addmerge_squash"
@@ -861,9 +849,7 @@ cmd_pull () {
861849
ensure_clean
862850
ensure_valid_ref_format "$2"
863851
git fetch "$@" || exit $?
864-
revs=FETCH_HEAD
865-
set -- $revs
866-
cmd_merge "$@"
852+
cmd_merge FETCH_HEAD
867853
}
868854

869855
cmd_push () {

0 commit comments

Comments
 (0)