Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 1c3e0f0

Browse files
a-bagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2446df commit 1c3e0f0

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

contrib/subtree/git-subtree.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ if [ $# -eq 0 ]; then
99
fi
1010
OPTS_SPEC="\
1111
git subtree add --prefix=<prefix> <commit>
12-
git subtree add --prefix=<prefix> <repository> <commit>
12+
git subtree add --prefix=<prefix> <repository> <ref>
1313
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>
1616
git subtree split --prefix=<prefix> <commit...>
1717
--
1818
h,help show the help
@@ -489,6 +489,12 @@ ensure_clean()
489489
fi
490490
}
491491

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+
492498
cmd_add()
493499
{
494500
if [ -e "$dir" ]; then
@@ -508,8 +514,7 @@ cmd_add()
508514
# specified directory. Allowing a refspec might be
509515
# misleading because we won't do anything with any other
510516
# 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"
513518

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

700705
cmd_pull()
701706
{
707+
if [ $# -ne 2 ]; then
708+
die "You must provide <repository> <ref>"
709+
fi
702710
ensure_clean
711+
ensure_valid_ref_format "$2"
703712
git fetch "$@" || exit $?
704713
revs=FETCH_HEAD
705714
set -- $revs
@@ -709,8 +718,9 @@ cmd_pull()
709718
cmd_push()
710719
{
711720
if [ $# -ne 2 ]; then
712-
die "You must provide <repository> <refspec>"
721+
die "You must provide <repository> <ref>"
713722
fi
723+
ensure_valid_ref_format "$2"
714724
if [ -e "$dir" ]; then
715725
repository=$1
716726
refspec=$2

contrib/subtree/git-subtree.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ git-subtree - Merge subtrees together and split repository into subtrees
99
SYNOPSIS
1010
--------
1111
[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>
1616
'git subtree' merge -P <prefix> <commit>
1717
'git subtree' split -P <prefix> [OPTIONS] [<commit>]
1818

@@ -68,7 +68,7 @@ COMMANDS
6868
--------
6969
add::
7070
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>.
7272
A new commit is created automatically, joining the imported
7373
project's history with your own. With '--squash', imports
7474
only a single commit from the subproject, rather than its
@@ -90,13 +90,13 @@ merge::
9090

9191
pull::
9292
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
9494
repository.
9595

9696
push::
9797
Does a 'split' (see below) using the <prefix> supplied
9898
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
100100
subtree to different branches of the remote repository.
101101

102102
split::

0 commit comments

Comments
 (0)