git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steffen Prohaska <prohaska@zib.de>
To: git@vger.kernel.org
Cc: Steffen Prohaska <prohaska@zib.de>
Subject: [PATCH 01/10] push: change push to fail if short refname does not exist
Date: Sun, 28 Oct 2007 18:46:12 +0100	[thread overview]
Message-ID: <11935935812741-git-send-email-prohaska@zib.de> (raw)
In-Reply-To: <1193593581312-git-send-email-prohaska@zib.de>

Pushing a short refname used to create a new ref on on the
remote side if it did not yet exist. If you specified the wrong
branch accidentally it was created. A safety valve that pushes
only existing branches may help to avoid errors.

This commit changes push to fail if the remote ref does not yet
exist and the refspec does not start with refs/. Remote refs must
explicitly be created with their full name. If you specify a
branch name that does not yet exist on the remote side, git push
will print a suggestion to push the full refname instead.

The new behaviour is more defensive than the old one. You can
now explicitly distinguish between "push existing branch" and
"create new branch on the remote side". The old implementation
allowed the same command line in both cases.

A follow-up patch will add a flag '--create' that provides an
alternative to using full refnames if creation of new refs is
intended.

Another follow-up patch will support "push origin HEAD". In this
case, the existence check is important. If you're on the wrong
branch and push HEAD you may be surprised if a new branch is
created. This can be avoided by requiring either a full ref or
the '--create' flag.

The implementation in this patch is less "clever" and hopefully
better readable than an ealier version of the patch. Thanks for
the suggestions to Daniel Barkalow <barkalow@iabervon.org>.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
---
 remote.c              |   25 ++++++++++++++++---------
 t/t5516-fetch-push.sh |   34 ++++++++++++++++++++++++++++++++--
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/remote.c b/remote.c
index 170015a..cf6441a 100644
--- a/remote.c
+++ b/remote.c
@@ -610,7 +610,8 @@ static int match_explicit(struct ref *src, struct ref *dst,
 {
 	struct ref *matched_src, *matched_dst;
 
-	const char *dst_value = rs->dst;
+	const char *lit_dst_value;
+	const char *search_dst_value;
 
 	if (rs->pattern)
 		return errs;
@@ -637,27 +638,33 @@ static int match_explicit(struct ref *src, struct ref *dst,
 	if (!matched_src)
 		errs = 1;
 
-	if (!dst_value) {
+	if (rs->dst) {
+		lit_dst_value = search_dst_value = rs->dst;
+	} else {
 		if (!matched_src)
 			return errs;
-		dst_value = matched_src->name;
+		lit_dst_value = rs->src;
+		search_dst_value = matched_src->name;
 	}
 
-	switch (count_refspec_match(dst_value, dst, &matched_dst)) {
+	switch (count_refspec_match(search_dst_value, dst, &matched_dst)) {
 	case 1:
 		break;
 	case 0:
-		if (!memcmp(dst_value, "refs/", 5))
-			matched_dst = make_linked_ref(dst_value, dst_tail);
-		else
+		if (!memcmp(lit_dst_value , "refs/", 5))
+			matched_dst = make_linked_ref(lit_dst_value, dst_tail);
+		else {
 			error("dst refspec %s does not match any "
 			      "existing ref on the remote and does "
-			      "not start with refs/.", dst_value);
+			      "not start with refs/.", lit_dst_value);
+			if (!rs->dst)
+				error("Did you mean %s?\n", search_dst_value);
+		}
 		break;
 	default:
 		matched_dst = NULL;
 		error("dst refspec %s matches more than one.",
-		      dst_value);
+		      lit_dst_value);
 		break;
 	}
 	if (errs || !matched_dst)
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 4fbd5b1..5ba09e2 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -126,6 +126,36 @@ test_expect_success 'push with wildcard' '
 	)
 '
 
+test_expect_success 'push nonexisting (1)' '
+
+	mk_test &&
+	if git push testrepo master
+	then
+		echo "Oops, should have failed"
+		false
+	fi
+
+'
+
+test_expect_success 'push nonexisting (2)' '
+
+	mk_test &&
+	if git push testrepo heads/master
+	then
+		echo "Oops, should have failed"
+		false
+	fi
+
+'
+
+test_expect_success 'push nonexisting (3)' '
+
+	mk_test &&
+	git push testrepo refs/heads/master &&
+	check_push_result $the_commit heads/master
+
+'
+
 test_expect_success 'push with matching heads' '
 
 	mk_test heads/master &&
@@ -225,7 +255,7 @@ test_expect_success 'push with colon-less refspec (3)' '
 		git tag -d frotz
 	fi &&
 	git branch -f frotz master &&
-	git push testrepo frotz &&
+	git push testrepo refs/heads/frotz &&
 	check_push_result $the_commit heads/frotz &&
 	test 1 = $( cd testrepo && git show-ref | wc -l )
 '
@@ -238,7 +268,7 @@ test_expect_success 'push with colon-less refspec (4)' '
 		git branch -D frotz
 	fi &&
 	git tag -f frotz &&
-	git push testrepo frotz &&
+	git push testrepo refs/tags/frotz &&
 	check_push_result $the_commit tags/frotz &&
 	test 1 = $( cd testrepo && git show-ref | wc -l )
 
-- 
1.5.3.4.439.ge8b49

  reply	other threads:[~2007-10-28 17:51 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-28 17:46 [PATCH 0/10 v3] improve refspec handling in push Steffen Prohaska
2007-10-28 17:46 ` Steffen Prohaska [this message]
2007-10-28 17:46   ` [PATCH 02/10] push: teach push new flag --create Steffen Prohaska
2007-10-28 17:46     ` [PATCH 03/10] push: support pushing HEAD to real branch name Steffen Prohaska
2007-10-28 17:46       ` [PATCH 04/10] push: add "git push HEAD" shorthand for 'push current branch to default repo' Steffen Prohaska
2007-10-28 17:46         ` [PATCH 05/10] rename ref_matches_abbrev() to ref_abbrev_matches_full_with_fetch_rules() Steffen Prohaska
2007-10-28 17:46           ` [PATCH 06/10] add ref_abbrev_matches_full_with_rev_parse_rules() comparing abbrev with full ref name Steffen Prohaska
2007-10-28 17:46             ` [PATCH 07/10] push: use same rules as git-rev-parse to resolve refspecs Steffen Prohaska
2007-10-28 17:46               ` [PATCH 08/10] push: teach push to accept --verbose option Steffen Prohaska
2007-10-28 17:46                 ` [PATCH 09/10] push: teach push to pass --verbose option to transport layer Steffen Prohaska
2007-10-28 17:46                   ` [PATCH 10/10] push: teach push to be quiet if local ref is strict subset of remote ref Steffen Prohaska
2007-10-30  8:29                     ` Junio C Hamano
2007-10-30 10:15                       ` Steffen Prohaska
2007-10-30 10:26                         ` Andreas Ericsson
2007-10-30 10:53                           ` Steffen Prohaska
2007-10-30 19:19                         ` Junio C Hamano
2007-10-31  7:53                           ` Steffen Prohaska
2007-10-31  8:45                             ` Junio C Hamano
     [not found]                               ` <B3C76DB8-076D-4C43-AC28-99119A05325C@z ib.de>
2007-10-31  9:14                               ` Junio C Hamano
2007-10-31 10:50                               ` Steffen Prohaska
2007-10-31 18:51                                 ` Junio C Hamano
2007-10-31 21:09                                   ` Steffen Prohaska
2007-10-31 21:31                                     ` Junio C Hamano
2007-11-01  7:03                                       ` Steffen Prohaska
2007-11-01  9:11                                         ` Andreas Ericsson
2007-11-01 16:43                                           ` Steffen Prohaska
2007-11-01 20:18                                             ` Junio C Hamano
2007-11-02  7:21                                               ` Steffen Prohaska
2007-11-02  7:52                                                 ` Junio C Hamano
2007-11-02 10:03                                                   ` Steffen Prohaska
2007-11-02 10:44                                                     ` Junio C Hamano
2007-11-02 11:40                                                       ` Steffen Prohaska
2007-11-02 10:03                                             ` Andreas Ericsson
2007-11-02 13:24                                               ` Tom Prince
2007-11-02 13:52                                                 ` Andreas Ericsson
2007-11-02 14:49                                                   ` Steffen Prohaska
2007-11-02 19:42                                                   ` Junio C Hamano
2007-11-02 20:19                                                     ` Junio C Hamano
2007-11-01  8:18                                       ` Andreas Ericsson
2007-11-01  8:36                                         ` Steffen Prohaska
2007-11-01  9:29                                           ` Andreas Ericsson
2007-11-02  8:18                                       ` Wincent Colaiuta
2007-11-02 12:14                                         ` Johannes Schindelin
2007-11-02 12:48                                           ` Steffen Prohaska
2007-11-02 13:11                                             ` Wincent Colaiuta
2007-10-30 18:00                       ` Daniel Barkalow
2007-10-30  8:28               ` [PATCH 07/10] push: use same rules as git-rev-parse to resolve refspecs Junio C Hamano
2007-10-30  8:49                 ` Steffen Prohaska
2007-10-30  8:28         ` [PATCH 04/10] push: add "git push HEAD" shorthand for 'push current branch to default repo' Junio C Hamano
2007-10-30  8:28       ` [PATCH 03/10] push: support pushing HEAD to real branch name Junio C Hamano
2007-10-30  8:29   ` [PATCH 01/10] push: change push to fail if short refname does not exist Junio C Hamano
2007-10-30  8:56     ` Steffen Prohaska
2007-10-30  9:22       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11935935812741-git-send-email-prohaska@zib.de \
    --to=prohaska@zib.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).