git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] fix "git push $there +HEAD"

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 20, 2008, 10:40 UTC
Message-ID
<7vprurc3n8.fsf@gitster.siamese.dyndns.org>

An earlier commit 47d996a (push: support pushing HEAD to real branch name) added support for "git push $there HEAD" by introducing a rewrite rule for the refspecs obtained from the command line. However, unlike the usual refspecs, it did not allow prefixing with '+' to mean forcing the branch.

This refactors the rewriting rule into a separate function, and teaches it to pay attention to a possible '+' prefix.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * If we were to do the "remote.*.push = HEAD" I mentioned
   earlier for defeating the default "matching" behaviour while
   pushing into shared repositories, we would need to apply the
   same rewriting rule for the refspecs obtained from remote at
   the beginning of do_push(), and that is what triggered this
   refactoring.
 builtin-push.c        |   40 +++++++++++++++++++++++++++++++---------
 t/t5516-fetch-push.sh |   21 +++++++++++++++++++++
 2 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/builtin-push.c b/builtin-push.c
index 9f727c0..0336ef8 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -28,6 +28,36 @@ static void add_refspec(const char *ref)
 	refspec_nr = nr;
 }
 
+static const char *replace_HEAD(const char *ref)
+{
+	static int checked_HEAD;
+	static char *plus_HEAD;
+	int force = 0;
+	const char *s = ref;
+
+	if (s[0] == '+') {
+		force = 1;
+		s++;
+	}
+	if (strcmp("HEAD", s))
+		return ref;
+	if (!checked_HEAD) {
+		unsigned char sha1[20];
+		size_t len;
+
+		checked_HEAD = 1;
+		s = resolve_ref(s, sha1, 1, NULL);
+		if (!s || prefixcmp(s, "refs/heads/"))
+			die("HEAD cannot be resolved to branch.");
+		len = strlen(s);
+		plus_HEAD = xmalloc(len + 2);
+		plus_HEAD[0] = '+';
+		strcpy(plus_HEAD + 1, s);
+	}
+
+	return plus_HEAD + !force;
+}
+
 static void set_refspecs(const char **refs, int nr)
 {
 	int i;
@@ -44,15 +74,7 @@ static void set_refspecs(const char **refs, int nr)
 			strcat(tag, refs[i]);
 			ref = tag;
 		}
-		if (!strcmp("HEAD", ref)) {
-			unsigned char sha1_dummy[20];
-			ref = resolve_ref(ref, sha1_dummy, 1, NULL);
-			if (!ref)
-				die("HEAD cannot be resolved.");
-			if (prefixcmp(ref, "refs/heads/"))
-				die("HEAD cannot be resolved to branch.");
-			ref = xstrdup(ref + 11);
-		}
+		ref = replace_HEAD(ref);
 		add_refspec(ref);
 	}
 }
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 9d2dc33..3370d53 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -271,6 +271,27 @@ test_expect_success 'push with HEAD nonexisting at remote' '
 	check_push_result $the_commit heads/local
 '
 
+test_expect_success 'push with +HEAD' '
+
+	mk_test heads/master &&
+	git checkout master &&
+	git branch -D local &&
+	git checkout -b local &&
+	git push testrepo master local &&
+	check_push_result $the_commit heads/master &&
+	check_push_result $the_commit heads/local &&
+
+	# Without force rewinding should fail
+	git reset --hard HEAD^ &&
+	! git push testrepo HEAD &&
+	check_push_result $the_commit heads/local &&
+
+	# With force rewinding should succeed
+	git push testrepo +HEAD &&
+	check_push_result $the_first_commit heads/local
+
+'
+
 test_expect_success 'push with dry-run' '
 
 	mk_test heads/master &&
Next: Johannes Schindelin
Message 1 of 4 in “fix "git push $there +HEAD"”
  1. fix "git push $there +HEAD"Junio C Hamano, Feb 20, 2008
  2. Johannes SchindelinFeb 20, 2008
  3. Daniel BarkalowFeb 20, 2008
  4. Junio C HamanoFeb 20, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.