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 <lukeshu@datawire.io> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Shumaker committed Apr 27, 2021 at 15:17 UTC e4f8baa88a0b294e1e2d91728e728e14574d1d87
1 file changed +24 -38
contrib/subtree/git-subtree.sh
+24 -38
@@ -76,13 +76,6 @@ assert () {
76 fi
77 }
78
79 -ensure_single_rev () {
80 - if test $# -ne 1
81 - then
82 - die "You must provide exactly one revision. Got: '$*'"
83 - fi
84 -}
85 -
79 main () {
80 if test $# -eq 0
81 then
@@ -164,11 +157,8 @@ main () {
157 shift
158
159 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 + :
162 ;;
163 *)
164 die "Unknown command '$arg_command'"
@@ -193,22 +183,8 @@ main () {
183
184 dir="$(dirname "$arg_prefix/.")"
185
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 -
186 debug "command: {$arg_command}"
187 debug "quiet: {$GIT_QUIET}"
211 - debug "revs: {$revs}"
188 debug "dir: {$dir}"
189 debug "opts: {$*}"
190 debug
@@ -714,14 +690,13 @@ cmd_add_repository () {
690 repository=$1
691 refspec=$2
692 git fetch "$@" || exit $?
717 - revs=FETCH_HEAD
718 - set -- $revs
719 - cmd_add_commit "$@"
693 + cmd_add_commit FETCH_HEAD
694 }
695
696 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 $?
700
701 debug "Adding $dir as '$rev'..."
702 git read-tree --prefix="$dir" $rev || exit $?
@@ -752,6 +727,17 @@ cmd_add_commit () {
727 }
728
729 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 +
741 debug "Splitting $dir..."
742 cache_setup || exit $?
743
@@ -768,12 +754,12 @@ cmd_split () {
754 done || exit $?
755 fi
756
771 - unrevs="$(find_existing_splits "$dir" "$revs")" || exit $?
757 + unrevs="$(find_existing_splits "$dir" "$rev")" || exit $?
758
759 # We can't restrict rev-list to only $dir here, because some of our
760 # parents have the $dir contents the root, and those won't match.
761 # (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'
763 revmax=$(eval "$grl" | wc -l)
764 revcount=0
765 createcount=0
@@ -820,8 +806,10 @@ cmd_split () {
806 }
807
808 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"
813 ensure_clean
814
815 if test -n "$arg_addmerge_squash"
@@ -861,9 +849,7 @@ cmd_pull () {
849 ensure_clean
850 ensure_valid_ref_format "$2"
851 git fetch "$@" || exit $?
864 - revs=FETCH_HEAD
865 - set -- $revs
866 - cmd_merge "$@"
852 + cmd_merge FETCH_HEAD
853 }
854
855 cmd_push () {