subtree: fix argument validation in add/pull/push

When working with a remote repository add/pull/push do not accept a <refspec> as parameter but just a <ref>. They should accept any well-formatted ref name. This patch: - relaxes the check the <ref> argument in "git subtree add <repo>" (previous code would not accept a ref name that does not exist locally too, new code only ensures that the ref is well formatted) - add the same check in "git subtree pull/push" + check the number of parameters - update the doc to use <ref> instead of <refspec> Signed-off-by: Anthony Baire <Anthony.Baire@irisa.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Anthony Baire committed Nov 27, 2013 at 19:34 UTC 1c3e0f007c8c18dd4ceda3c67cbfb8e01ef2c894
2 files changed +23 -13
contrib/subtree/git-subtree.sh
+16 -6
@@ -9,10 +9,10 @@ if [ $# -eq 0 ]; then
9 fi
10 OPTS_SPEC="\
11 git subtree add --prefix=<prefix> <commit>
12 -git subtree add --prefix=<prefix> <repository> <commit>
12 +git subtree add --prefix=<prefix> <repository> <ref>
13 git subtree merge --prefix=<prefix> <commit>
14 -git subtree pull --prefix=<prefix> <repository> <refspec...>
15 -git subtree push --prefix=<prefix> <repository> <refspec...>
14 +git subtree pull --prefix=<prefix> <repository> <ref>
15 +git subtree push --prefix=<prefix> <repository> <ref>
16 git subtree split --prefix=<prefix> <commit...>
17 --
18 h,help show the help
@@ -489,6 +489,12 @@ ensure_clean()
489 fi
490 }
491
492 +ensure_valid_ref_format()
493 +{
494 + git check-ref-format "refs/heads/$1" ||
495 + die "'$1' does not look like a ref"
496 +}
497 +
498 cmd_add()
499 {
500 if [ -e "$dir" ]; then
@@ -508,8 +514,7 @@ cmd_add()
514 # specified directory. Allowing a refspec might be
515 # misleading because we won't do anything with any other
516 # branches fetched via the refspec.
511 - git rev-parse -q --verify "$2^{commit}" >/dev/null ||
512 - die "'$2' does not refer to a commit"
517 + ensure_valid_ref_format "$2"
518
519 "cmd_add_repository" "$@"
520 else
@@ -699,7 +704,11 @@ cmd_merge()
704
705 cmd_pull()
706 {
707 + if [ $# -ne 2 ]; then
708 + die "You must provide <repository> <ref>"
709 + fi
710 ensure_clean
711 + ensure_valid_ref_format "$2"
712 git fetch "$@" || exit $?
713 revs=FETCH_HEAD
714 set -- $revs
@@ -709,8 +718,9 @@ cmd_pull()
718 cmd_push()
719 {
720 if [ $# -ne 2 ]; then
712 - die "You must provide <repository> <refspec>"
721 + die "You must provide <repository> <ref>"
722 fi
723 + ensure_valid_ref_format "$2"
724 if [ -e "$dir" ]; then
725 repository=$1
726 refspec=$2
contrib/subtree/git-subtree.txt
+7 -7
@@ -9,10 +9,10 @@ git-subtree - Merge subtrees together and split repository into subtrees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git subtree' add -P <prefix> <refspec>
13 -'git subtree' add -P <prefix> <repository> <refspec>
14 -'git subtree' pull -P <prefix> <repository> <refspec...>
15 -'git subtree' push -P <prefix> <repository> <refspec...>
12 +'git subtree' add -P <prefix> <commit>
13 +'git subtree' add -P <prefix> <repository> <ref>
14 +'git subtree' pull -P <prefix> <repository> <ref>
15 +'git subtree' push -P <prefix> <repository> <ref>
16 'git subtree' merge -P <prefix> <commit>
17 'git subtree' split -P <prefix> [OPTIONS] [<commit>]
18
@@ -68,7 +68,7 @@ COMMANDS
68 --------
69 add::
70 Create the <prefix> subtree by importing its contents
71 - from the given <refspec> or <repository> and remote <refspec>.
71 + from the given <commit> or <repository> and remote <ref>.
72 A new commit is created automatically, joining the imported
73 project's history with your own. With '--squash', imports
74 only a single commit from the subproject, rather than its
@@ -90,13 +90,13 @@ merge::
90
91 pull::
92 Exactly like 'merge', but parallels 'git pull' in that
93 - it fetches the given commit from the specified remote
93 + it fetches the given ref from the specified remote
94 repository.
95
96 push::
97 Does a 'split' (see below) using the <prefix> supplied
98 and then does a 'git push' to push the result to the
99 - repository and refspec. This can be used to push your
99 + repository and ref. This can be used to push your
100 subtree to different branches of the remote repository.
101
102 split::