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

[PATCH 4/6] push, send-pack: support pushing HEAD to real ref name

From
Steffen Prohaska <prohaska@zib.de>
Date
Oct 14, 2007, 08:54 UTC
Message-ID
<11923520851656-git-send-email-prohaska@zib.de>
In-Reply-To
<11923520853189-git-send-email-prohaska@zib.de>

This teaches "push <remote> HEAD" to resolve HEAD on the local side to its real ref name, e.g. refs/heads/master, and then use the real ref name on the remote side to search a matching remote ref.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
---
 remote.c              |   18 +++++++++++++-----
 t/t5516-fetch-push.sh |   22 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/remote.c b/remote.c
index 36071b2..58bc019 100644
--- a/remote.c
+++ b/remote.c
@@ -439,6 +439,8 @@ static struct ref *try_explicit_object_name(const char *name)
 	unsigned char sha1[20];
 	struct ref *ref;
 	int len;
+	char *real_name = 0;
+	const char *best_name;
 
 	if (!*name) {
 		ref = alloc_ref(20);
@@ -446,12 +448,17 @@ static struct ref *try_explicit_object_name(const char *name)
 		hashclr(ref->new_sha1);
 		return ref;
 	}
-	if (get_sha1(name, sha1))
+	if (get_sha1_with_real_ref(name, sha1, &real_name))
 		return NULL;
-	len = strlen(name) + 1;
+	best_name = real_name ? real_name : name;
+	len = strlen(best_name) + 1;
 	ref = alloc_ref(len);
-	memcpy(ref->name, name, len);
+	memcpy(ref->name, best_name, len);
 	hashcpy(ref->new_sha1, sha1);
+
+	if (real_name) {
+		free(real_name);
+	}
 	return ref;
 }
 
@@ -475,6 +482,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
 	struct ref *matched_src, *matched_dst;
 
 	const char *dst_value = rs->dst;
+	const char * const orig_dst_value = rs->dst ? rs->dst : rs->src;
 
 	if (rs->pattern)
 		return errs;
@@ -511,12 +519,12 @@ static int match_explicit(struct ref *src, struct ref *dst,
 	case 1:
 		break;
 	case 0:
-		if (!memcmp(rs->dst ? rs->dst : rs->src , "refs/", 5))
+		if (!memcmp(orig_dst_value , "refs/", 5))
 			matched_dst = make_linked_ref(dst_value, dst_tail);
 		else
 			error("dst refspec %s does not match any "
 			      "existing ref on the remote and does "
-			      "not start with refs/.", rs->dst ? rs->dst : rs->src);
+			      "not start with refs/.", orig_dst_value);
 		break;
 	default:
 		matched_dst = NULL;
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 8629cf2..97a032e 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -274,4 +274,26 @@ test_expect_success 'push with colon-less refspec (4)' '
 
 '
 
+test_expect_success 'push with HEAD' '
+
+	mk_test heads/master &&
+	git push testrepo HEAD &&
+	check_push_result $the_commit heads/master
+
+'
+
+test_expect_success 'push with HEAD not existing at remote' '
+
+	mk_test heads/master &&
+	git checkout -b local master &&
+	if git push testrepo HEAD
+	then
+		echo "Oops, should have failed"
+		false
+	else
+		check_push_result $the_first_commit heads/master
+	fi
+
+'
+
 test_done
-- 
1.5.3.4.224.gc6b84
Previous: Steffen ProhaskaNext: Steffen Prohaska
Message 5 of 12 in “change push's refspec behavior to match rev-parse”
  1. Steffen ProhaskaOct 14, 2007
  2. 1/6 push, send-pack: fix test if remote branch exists for colon-less refspecSteffen Prohaska, Oct 14, 2007
  3. 2/6 add get_sha1_with_real_ref() returning full name of ref on demandSteffen Prohaska, Oct 14, 2007
  4. 3/6 rev-parse: teach "git rev-parse --symbolic" to print the full ref nameSteffen Prohaska, Oct 14, 2007
  5. 4/6 push, send-pack: support pushing HEAD to real ref nameSteffen Prohaska, Oct 14, 2007
  6. 5/6 add ref_cmp_full_short() comparing full ref name with a short nameSteffen Prohaska, Oct 14, 2007
  7. 6/6 push, send-pack: use same rules as git-rev-parse to resolve refspecsSteffen Prohaska, Oct 14, 2007
  8. 4/6 push, send-pack: support pushing HEAD to real ref nameSteffen Prohaska, Oct 14, 2007
  9. Johannes SchindelinOct 14, 2007
  10. Steffen ProhaskaOct 14, 2007
  11. 1/6 push, send-pack: fix test if remote branch exists for colon-less refspecSteffen Prohaska, Oct 14, 2007
  12. Steffen ProhaskaOct 14, 2007

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.