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

[PATCH] branch: make sure the upstream remote is configured

From
Carlos Martín Nieto <cmn@elego.de>
Date
Jul 26, 2013, 17:39 UTC
Message-ID
<1374860377-17652-1-git-send-email-cmn@elego.de>
A command of e.g.
    git push --set-upstream /tmp/t master

will call install_branch_config() with a remote name of "/tmp/t". This function will set the 'branch.master.remote' key to, which is nonsensical as there is no remote by that name.

Instead, make sure that the remote given does exist when writing the configuration and warn if it does not. In order to distinguish named remotes, introduce REMOTE_NONE as the default origin for remotes, which the functions reading from the different sources will overwrite. Thus, an origin of REMOTE_NONE means it has been created at run-time in order to push to it.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

It's somewhat surprising that there didn't seem to be a way to distinguish named remotes from those created from a command-line path, but I guess nobody needed to.

 branch.c                 | 11 +++++++++++
 remote.h                 |  1 +
 t/t5523-push-upstream.sh |  5 +++++
 3 files changed, 17 insertions(+)
diff --git a/branch.c b/branch.c
index c5c6984..cefb8f6 100644
--- a/branch.c
+++ b/branch.c
@@ -53,6 +53,7 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	int remote_is_branch = !prefixcmp(remote, "refs/heads/");
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
+	struct remote *r = remote_get(origin);
 
 	if (remote_is_branch
 	    && !strcmp(local, shortname)
@@ -62,6 +63,16 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 		return;
 	}
 
+	/*
+	 * Make sure that the remote passed is a configured remote, or
+	 * we end up setting 'branch.foo.remote = /tmp/t' which is
+	 * nonsensical.
+	 */
+	if (origin && strcmp(origin, ".") && r->origin == REMOTE_NONE) {
+		warning(_("there is no remote named '%s', no upstream configuration will be set."), origin);
+		return;
+	}
+
 	strbuf_addf(&key, "branch.%s.remote", local);
 	git_config_set(key.buf, origin ? origin : ".");
 
diff --git a/remote.h b/remote.h
index cf56724..92f6e33 100644
--- a/remote.h
+++ b/remote.h
@@ -2,6 +2,7 @@
 #define REMOTE_H
 
 enum {
+	REMOTE_NONE,
 	REMOTE_CONFIG,
 	REMOTE_REMOTES,
 	REMOTE_BRANCHES
diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh
index 3683df1..e84c2f8 100755
--- a/t/t5523-push-upstream.sh
+++ b/t/t5523-push-upstream.sh
@@ -71,6 +71,11 @@ test_expect_success 'push -u HEAD' '
 	check_config headbranch upstream refs/heads/headbranch
 '
 
+test_expect_success 'push -u <url>' '
+        git push -u parent HEAD 2>err &&
+        grep "no upstream configuration will be set" err
+'
+
 test_expect_success TTY 'progress messages go to tty' '
 	ensure_fresh_upstream &&
 
-- 
1.8.3
Next: Jeff King
Message 1 of 6 in “branch: make sure the upstream remote is configured”
  1. branch: make sure the upstream remote is configuredCarlos Martín Nieto, Jul 26, 2013
  2. Jeff KingJul 26, 2013
  3. Jeff KingJul 26, 2013
  4. Carlos Martín NietoJul 26, 2013
  5. Jeff KingJul 26, 2013
  6. Jeff KingJul 26, 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.