Browse Source

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>
maint
Anthony Baire 11 years ago committed by Junio C Hamano
parent
commit
1c3e0f007c
  1. 22
      contrib/subtree/git-subtree.sh
  2. 14
      contrib/subtree/git-subtree.txt

22
contrib/subtree/git-subtree.sh

@ -9,10 +9,10 @@ if [ $# -eq 0 ]; then
fi fi
OPTS_SPEC="\ OPTS_SPEC="\
git subtree add --prefix=<prefix> <commit> git subtree add --prefix=<prefix> <commit>
git subtree add --prefix=<prefix> <repository> <commit> git subtree add --prefix=<prefix> <repository> <ref>
git subtree merge --prefix=<prefix> <commit> git subtree merge --prefix=<prefix> <commit>
git subtree pull --prefix=<prefix> <repository> <refspec...> git subtree pull --prefix=<prefix> <repository> <ref>
git subtree push --prefix=<prefix> <repository> <refspec...> git subtree push --prefix=<prefix> <repository> <ref>
git subtree split --prefix=<prefix> <commit...> git subtree split --prefix=<prefix> <commit...>
-- --
h,help show the help h,help show the help
@ -489,6 +489,12 @@ ensure_clean()
fi fi
} }


ensure_valid_ref_format()
{
git check-ref-format "refs/heads/$1" ||
die "'$1' does not look like a ref"
}

cmd_add() cmd_add()
{ {
if [ -e "$dir" ]; then if [ -e "$dir" ]; then
@ -508,8 +514,7 @@ cmd_add()
# specified directory. Allowing a refspec might be # specified directory. Allowing a refspec might be
# misleading because we won't do anything with any other # misleading because we won't do anything with any other
# branches fetched via the refspec. # branches fetched via the refspec.
git rev-parse -q --verify "$2^{commit}" >/dev/null || ensure_valid_ref_format "$2"
die "'$2' does not refer to a commit"


"cmd_add_repository" "$@" "cmd_add_repository" "$@"
else else
@ -699,7 +704,11 @@ cmd_merge()


cmd_pull() cmd_pull()
{ {
if [ $# -ne 2 ]; then
die "You must provide <repository> <ref>"
fi
ensure_clean ensure_clean
ensure_valid_ref_format "$2"
git fetch "$@" || exit $? git fetch "$@" || exit $?
revs=FETCH_HEAD revs=FETCH_HEAD
set -- $revs set -- $revs
@ -709,8 +718,9 @@ cmd_pull()
cmd_push() cmd_push()
{ {
if [ $# -ne 2 ]; then if [ $# -ne 2 ]; then
die "You must provide <repository> <refspec>" die "You must provide <repository> <ref>"
fi fi
ensure_valid_ref_format "$2"
if [ -e "$dir" ]; then if [ -e "$dir" ]; then
repository=$1 repository=$1
refspec=$2 refspec=$2

14
contrib/subtree/git-subtree.txt

@ -9,10 +9,10 @@ git-subtree - Merge subtrees together and split repository into subtrees
SYNOPSIS SYNOPSIS
-------- --------
[verse] [verse]
'git subtree' add -P <prefix> <refspec> 'git subtree' add -P <prefix> <commit>
'git subtree' add -P <prefix> <repository> <refspec> 'git subtree' add -P <prefix> <repository> <ref>
'git subtree' pull -P <prefix> <repository> <refspec...> 'git subtree' pull -P <prefix> <repository> <ref>
'git subtree' push -P <prefix> <repository> <refspec...> 'git subtree' push -P <prefix> <repository> <ref>
'git subtree' merge -P <prefix> <commit> 'git subtree' merge -P <prefix> <commit>
'git subtree' split -P <prefix> [OPTIONS] [<commit>] 'git subtree' split -P <prefix> [OPTIONS] [<commit>]


@ -68,7 +68,7 @@ COMMANDS
-------- --------
add:: add::
Create the <prefix> subtree by importing its contents Create the <prefix> subtree by importing its contents
from the given <refspec> or <repository> and remote <refspec>. from the given <commit> or <repository> and remote <ref>.
A new commit is created automatically, joining the imported A new commit is created automatically, joining the imported
project's history with your own. With '--squash', imports project's history with your own. With '--squash', imports
only a single commit from the subproject, rather than its only a single commit from the subproject, rather than its
@ -90,13 +90,13 @@ merge::
pull:: pull::
Exactly like 'merge', but parallels 'git pull' in that Exactly like 'merge', but parallels 'git pull' in that
it fetches the given commit from the specified remote it fetches the given ref from the specified remote
repository. repository.
push:: push::
Does a 'split' (see below) using the <prefix> supplied Does a 'split' (see below) using the <prefix> supplied
and then does a 'git push' to push the result to the and then does a 'git push' to push the result to the
repository and refspec. This can be used to push your repository and ref. This can be used to push your
subtree to different branches of the remote repository. subtree to different branches of the remote repository.


split:: split::

Loading…
Cancel
Save