[PATCH] Add push --set-upstream

Subsystems: documentation, the rest

DORMANTno replies

5 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH] Add push --set-upstream

From: Ilari Liusvaara <hidden>
Date: 2016-06-15 22:48:02

Frequent complaint is lack of easy way to set up upstream (tracking)
references for git pull to work as part of push command. So add switch
--set-upstream (-u) to do just that.

Signed-off-by: Ilari Liusvaara <redacted>
---
This is built on top of master.

 Documentation/git-push.txt |    8 +++++++-
 builtin-push.c             |    1 +
 transport.c                |   35 +++++++++++++++++++++++++++++++++++
 transport.h                |    1 +
 4 files changed, 44 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt
index e3eb1e8..6c68978 100644
--- a/Documentation/git-push.txt
+++ b/Documentation/git-push.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git push' [--all | --mirror | --tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
-	   [--repo=<repository>] [-f | --force] [-v | --verbose]
+	   [--repo=<repository>] [-f | --force] [-v | --verbose] [-u | --set-upstream]
 	   [<repository> <refspec>...]
 
 DESCRIPTION
@@ -122,6 +122,12 @@ nor in any Push line of the corresponding remotes file---see below).
 	the name "origin" is used. For this latter case, this option
 	can be used to override the name "origin". In other words,
 	the difference between these two commands
+
+-u::
+--set-upstream::
+	For every branch that is up to date or successfully pushed, add
+	upstream (tracking) reference for argument-less git pull.
+
 +
 --------------------------
 git push public         #1
diff --git a/builtin-push.c b/builtin-push.c
index 28a26e7..75ddaf4 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -218,6 +218,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
 		OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
 		OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
+		OPT_BIT('u', "set-upstream", &flags, "Set upstream for git pull", TRANSPORT_PUSH_SET_UPSTREAM),
 		OPT_END()
 	};
 
diff --git a/transport.c b/transport.c
index b5332c0..956d2ed 100644
--- a/transport.c
+++ b/transport.c
@@ -8,6 +8,7 @@
 #include "bundle.h"
 #include "dir.h"
 #include "refs.h"
+#include "branch.h"
 
 /* rsync support */
 
@@ -135,6 +136,33 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
 	}
 }
 
+static void set_upstreams(struct transport *trans, struct ref *refs)
+{
+	struct ref *i;
+	for (i = refs; i; i = i->next) {
+		/*
+		 * Check suitability for tracking. Must be successful /
+		 * alreay up-to-date ref create/modify (not delete) and
+		 * both sides must be local branches.
+		 */
+		if (i->status != REF_STATUS_OK &&
+			i->status != REF_STATUS_UPTODATE)
+			continue;
+		if (!i->peer_ref)
+			continue;
+		if (!i->new_sha1 || is_null_sha1(i->new_sha1))
+			continue;
+		if (prefixcmp(i->peer_ref->name, "refs/heads/"))
+			continue;
+		if (prefixcmp(i->name, "refs/heads/"))
+			continue;
+
+		install_branch_config(BRANCH_CONFIG_VERBOSE,
+			i->peer_ref->name + 11, trans->remote->name,
+			i->name);
+	}
+}
+
 static const char *rsync_url(const char *url)
 {
 	return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
@@ -974,6 +1002,10 @@ int transport_push(struct transport *transport,
 	verify_remote_names(refspec_nr, refspec);
 
 	if (transport->push) {
+		/* Maybe FIXME. But no important transport uses this case. */
+		if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
+			die("This transport does not support using --set-upstream");
+
 		return transport->push(transport, refspec_nr, refspec, flags);
 	} else if (transport->push_refs) {
 		struct ref *remote_refs =
@@ -1002,6 +1034,9 @@ int transport_push(struct transport *transport,
 					verbose | porcelain, porcelain,
 					nonfastforward);
 
+		if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
+			set_upstreams(transport, remote_refs);
+
 		if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
 			struct ref *ref;
 			for (ref = remote_refs; ref; ref = ref->next)
diff --git a/transport.h b/transport.h
index 97ba251..c4314dd 100644
--- a/transport.h
+++ b/transport.h
@@ -91,6 +91,7 @@ struct transport {
 #define TRANSPORT_PUSH_VERBOSE 16
 #define TRANSPORT_PUSH_PORCELAIN 32
 #define TRANSPORT_PUSH_QUIET 64
+#define TRANSPORT_PUSH_SET_UPSTREAM 128
 
 /* Returns a transport suitable for the url */
 struct transport *transport_get(struct remote *, const char *);
-- 
1.6.6.102.gd6f8f.dirty

Re: [PATCH] Add push --set-upstream

From: Jeff King <hidden>
Date: 2016-06-15 22:48:02

On Fri, Jan 15, 2010 at 06:36:47PM +0200, Ilari Liusvaara wrote:
Frequent complaint is lack of easy way to set up upstream (tracking)
references for git pull to work as part of push command. So add switch
--set-upstream (-u) to do just that.
Huzzah, finally this feature is done right. I even like the name.
 Documentation/git-push.txt |    8 +++++++-
 builtin-push.c             |    1 +
 transport.c                |   35 +++++++++++++++++++++++++++++++++++
 transport.h                |    1 +
 4 files changed, 44 insertions(+), 1 deletions(-)
No tests. But since in writing this you have crossed an item off of my
long-term todo, I feel obliged to help out by providing some. :)

The patch below is squash-able, but note that the final test, "git push
-u HEAD" is marked as broken. We should probably support that. I suspect
is is an issue of dereferencing symrefs before doing the
prefixcmp("refs/heads/", ...) but I haven't checked yet.
diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh
new file mode 100755
index 0000000..e977553
--- /dev/null
+++ b/t/t5523-push-upstream.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='push with --set-upstream'
+. ./test-lib.sh
+
+test_expect_success 'setup bare parent' '
+	git init --bare parent &&
+	git remote add upstream parent
+'
+
+test_expect_success 'setup local commit' '
+	echo content >file &&
+	git add file &&
+	git commit -m one
+'
+
+check_config() {
+	(echo $2; echo $3) >expect.$1
+	(git config branch.$1.remote
+	 git config branch.$1.merge) >actual.$1
+	test_cmp expect.$1 actual.$1
+}
+
+test_expect_success 'push -u master:master' '
+	git push -u upstream master:master &&
+	check_config master upstream refs/heads/master
+'
+
+test_expect_success 'push -u master:other' '
+	git push -u upstream master:other &&
+	check_config master upstream refs/heads/other
+'
+
+test_expect_success 'push -u --all' '
+	git branch all1 &&
+	git branch all2 &&
+	git push -u --all &&
+	check_config all1 upstream refs/heads/all1 &&
+	check_config all2 upstream refs/heads/all2
+'
+
+test_expect_failure 'push -u HEAD' '
+	git checkout -b headbranch &&
+	git push -u upstream HEAD &&
+	check_config headbranch upstream refs/heads/headbranch
+'
+
+test_done

Re: [PATCH] Add push --set-upstream

From: Jeff King <hidden>
Date: 2016-06-15 22:48:02

On Fri, Jan 15, 2010 at 12:17:45PM -0500, Jeff King wrote:
The patch below is squash-able, but note that the final test, "git push
-u HEAD" is marked as broken. We should probably support that. I suspect
is is an issue of dereferencing symrefs before doing the
prefixcmp("refs/heads/", ...) but I haven't checked yet.
The patch below fixes it, but I am not 100% happy with it. Calling
resolve_ref means we actually bother to look up the ref again, which is
wasted effort. The ref struct has a "symref" field which should contain
this information, but for some reason it is not recorded. So we can
probably do better by simply recording the information properly when we
resolve the ref in the first place.

Unfortunately, I don't have time to look at it anymore right now, so it
will have to wait.
diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh
index e977553..d43473f 100755
--- a/t/t5523-push-upstream.sh
+++ b/t/t5523-push-upstream.sh
@@ -39,7 +39,7 @@ test_expect_success 'push -u --all' '
 	check_config all2 upstream refs/heads/all2
 '
 
-test_expect_failure 'push -u HEAD' '
+test_expect_success 'push -u HEAD' '
 	git checkout -b headbranch &&
 	git push -u upstream HEAD &&
 	check_config headbranch upstream refs/heads/headbranch
diff --git a/transport.c b/transport.c
index 956d2ed..01ff364 100644
--- a/transport.c
+++ b/transport.c
@@ -140,6 +140,7 @@ static void set_upstreams(struct transport *trans, struct ref *refs)
 {
 	struct ref *i;
 	for (i = refs; i; i = i->next) {
+		const char *branch;
 		/*
 		 * Check suitability for tracking. Must be successful /
 		 * alreay up-to-date ref create/modify (not delete) and
@@ -152,14 +153,20 @@ static void set_upstreams(struct transport *trans, struct ref *refs)
 			continue;
 		if (!i->new_sha1 || is_null_sha1(i->new_sha1))
 			continue;
-		if (prefixcmp(i->peer_ref->name, "refs/heads/"))
-			continue;
 		if (prefixcmp(i->name, "refs/heads/"))
 			continue;
 
+		if (!prefixcmp(i->peer_ref->name, "refs/heads/"))
+			branch = i->peer_ref->name;
+		else {
+			unsigned char sha1[20];
+			branch = resolve_ref(i->peer_ref->name, sha1, 1, NULL);
+			if (!branch || prefixcmp(branch, "refs/heads/"))
+				continue;
+		}
+
 		install_branch_config(BRANCH_CONFIG_VERBOSE,
-			i->peer_ref->name + 11, trans->remote->name,
-			i->name);
+			branch + 11, trans->remote->name, i->name);
 	}
 }
 

Re: [PATCH] Add push --set-upstream

From: Ilari Liusvaara <hidden>
Date: 2016-06-15 22:48:02

On Fri, Jan 15, 2010 at 12:17:45PM -0500, Jeff King wrote:
On Fri, Jan 15, 2010 at 06:36:47PM +0200, Ilari Liusvaara wrote:

No tests. But since in writing this you have crossed an item off of my
long-term todo, I feel obliged to help out by providing some. :)
 
Signoff for those tests? Or do they need it anyway?

<Snip testscript> 

Re: [PATCH] Add push --set-upstream

From: Jeff King <hidden>
Date: 2016-06-15 22:48:02

On Sat, Jan 16, 2010 at 12:06:58AM +0200, Ilari Liusvaara wrote:
On Fri, Jan 15, 2010 at 12:17:45PM -0500, Jeff King wrote:
quoted
On Fri, Jan 15, 2010 at 06:36:47PM +0200, Ilari Liusvaara wrote:

No tests. But since in writing this you have crossed an item off of my
long-term todo, I feel obliged to help out by providing some. :)
 
Signoff for those tests? Or do they need it anyway?
Sorry, yes:

Signed-off-by: Jeff King <redacted>

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help