diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh index 431214a1d3..9e4d9a0619 100755 --- a/contrib/subtree/git-subtree.sh +++ b/contrib/subtree/git-subtree.sh @@ -27,7 +27,7 @@ git subtree add --prefix= git subtree merge --prefix= git subtree split --prefix= [] git subtree pull --prefix= -git subtree push --prefix= +git subtree push --prefix= -- h,help show the help q quiet @@ -952,20 +952,30 @@ cmd_pull () { cmd_merge FETCH_HEAD } -# Usage: cmd_push REPOSITORY REMOTEREF +# Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF cmd_push () { if test $# -ne 2 then - die "You must provide " + die "You must provide " fi - ensure_valid_ref_format "$2" if test -e "$dir" then repository=$1 - refspec=$2 + refspec=${2#+} + remoteref=${refspec#*:} + if test "$remoteref" = "$refspec" + then + localrevname_presplit=HEAD + else + localrevname_presplit=${refspec%%:*} + fi + ensure_valid_ref_format "$remoteref" + localrev_presplit=$(git rev-parse -q --verify "$localrevname_presplit^{commit}") || + die "'$localrevname_presplit' does not refer to a commit" + echo "git push using: " "$repository" "$refspec" - localrev=$(cmd_split) || die - git push "$repository" "$localrev":"refs/heads/$refspec" + localrev=$(cmd_split "$localrev_presplit") || die + git push "$repository" "$localrev":"refs/heads/$remoteref" else die "'$dir' must already exist. Try 'git subtree add'." fi diff --git a/contrib/subtree/git-subtree.txt b/contrib/subtree/git-subtree.txt index fbb52f127b..9cddfa2654 100644 --- a/contrib/subtree/git-subtree.txt +++ b/contrib/subtree/git-subtree.txt @@ -16,7 +16,7 @@ SYNOPSIS [verse] 'git subtree' [] -P pull -'git subtree' [] -P push +'git subtree' [] -P push DESCRIPTION ----------- @@ -115,11 +115,13 @@ pull :: it fetches the given ref from the specified remote repository. -push :: - Does a 'split' using the subtree of HEAD and then - does a 'git push' to push the result to the and - . This can be used to push your subtree to - different branches of the remote repository. +push [+][:]:: + Does a 'split' using the subtree of + and then does a 'git push' to push the result to the + and . This can be used to push your + subtree to different branches of the remote repository. Just + as with 'split', if no is given, then HEAD is + used. The optional leading '+' is ignored. OPTIONS FOR ALL COMMANDS ------------------------ diff --git a/contrib/subtree/t/t7900-subtree.sh b/contrib/subtree/t/t7900-subtree.sh index 5a6541437b..d7ad6ffff0 100755 --- a/contrib/subtree/t/t7900-subtree.sh +++ b/contrib/subtree/t/t7900-subtree.sh @@ -820,6 +820,28 @@ test_expect_success 'push "sub dir"/ with --branch for an incompatible branch' ' ) ' +test_expect_success 'push "sub dir"/ with a local rev' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD + ) && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" "sub dir"/main-sub2 && + ( + cd "$test_count" && + bad_tree=$(git rev-parse --verify HEAD:"sub dir") && + good_tree=$(git rev-parse --verify HEAD^:"sub dir") && + git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline && + split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) && + test "$split_tree" = "$good_tree" + ) +' + # # Validity checking #