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

[PATCH 3/3] push: also use "upstream" mapping when pushing a single ref

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 4, 2013, 00:39 UTC
Message-ID
<1386117594-22062-4-git-send-email-gitster@pobox.com>
In-Reply-To
<1386117594-22062-1-git-send-email-gitster@pobox.com>
When the user is using the 'upstream' mode, these commands:
    $ git push
    $ git push origin

would find the 'upstream' branch for the current branch, and then push the current branch to update it. However, pushing a single branch explicitly, i.e.

    $ git push origin $(git symbolic-ref --short HEAD)

would not go through the same ref mapping process, and ends up updating the branch at 'origin' of the same name, which may not necessarily be the upstream of the branch being pushed.

In the spirit similar to the previous one, map a colon-less refspec using the upstream mapping logic.

Note that this is deliberately done after the logic to map refspec via the remote.$name.push configuration did not find any. This is because most of those that employ a simple 'upstream' mode do not have the push refspec, and checking with remote.$name.push refspec first will allow it to be used as a mechanism to override the usual 'upstream' push mapping (which is primarily useful for centralized workflow) by those who want to use a triangular workflow.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/push.c        | 11 +++++++++++
 t/t5516-fetch-push.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
diff --git a/builtin/push.c b/builtin/push.c
index bdc1fc1..71c081e 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -58,6 +58,17 @@ static const char *map_refspec(const char *ref,
 		}
 	}
 
+	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	    !prefixcmp(matched->name, "refs/heads/")) {
+		struct branch *branch = branch_get(matched->name + 11);
+		if (branch->merge_nr == 1 && branch->merge[0]->src) {
+			struct strbuf buf = STRBUF_INIT;
+			strbuf_addf(&buf, "%s:%s",
+				    ref, branch->merge[0]->src);
+			return strbuf_detach(&buf, NULL);
+		}
+	}
+
 	return ref;
 }
 
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 6d7f102..926e7f6 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -1160,6 +1160,7 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
 		git pull ../testrepo master &&
 		git branch next &&
 		git config remote.dst.url ../dst &&
+		git config push.default matching &&
 		git push dst master &&
 		git show-ref refs/heads/master >../dst/expect
 	) &&
@@ -1171,6 +1172,35 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
 	test_cmp dst/expect dst/actual
 '
 
+test_expect_success 'with no remote.$name.push, upstream mapping is used' '
+	mk_test testrepo heads/master &&
+	rm -fr src dst &&
+	git init src &&
+	git init --bare dst &&
+	(
+		cd src &&
+		git pull ../testrepo master &&
+		git branch next &&
+		git config remote.dst.url ../dst &&
+		git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
+		git config push.default upstream &&
+
+		git config branch.master.merge refs/heads/trunk &&
+		git config branch.master.remote dst &&
+
+		git push dst master &&
+		git show-ref refs/heads/master |
+		sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
+	) &&
+	(
+		cd dst &&
+		test_must_fail git show-ref refs/heads/master &&
+		test_must_fail git show-ref refs/heads/next &&
+		git show-ref refs/heads/trunk >actual
+	) &&
+	test_cmp dst/expect dst/actual
+'
+
 test_expect_success 'push does not follow tags by default' '
 	mk_test testrepo heads/master &&
 	rm -fr src dst &&
-- 
1.8.5.1-400-gbc1da41
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 8 in “Teaching "git push" to map pushed refs”
  1. 0/3 Teaching "git push" to map pushed refsJunio C Hamano, Dec 4, 2013
  2. 1/3 builtin/push.c: use strbuf instead of manual allocationJunio C Hamano, Dec 4, 2013
  3. 2/3 push: use remote.$name.push as a refmapJunio C Hamano, Dec 4, 2013
  4. 3/3 push: also use "upstream" mapping when pushing a single refJunio C Hamano, Dec 4, 2013
  5. 0/3 Teaching "git push" to map pushed refsJunio C Hamano, Dec 5, 2013
  6. 1/3 builtin/push.c: use strbuf instead of manual allocationJunio C Hamano, Dec 5, 2013
  7. 2/3 push: use remote.$name.push as a refmapJunio C Hamano, Dec 5, 2013
  8. 3/3 push: also use "upstream" mapping when pushing a single refJunio C Hamano, Dec 5, 2013

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.