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

[PATCH 2/2] branch: warn and refuse to set a branch as a tracking branch of itself.

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Jan 17, 2010, 14:06 UTC
Message-ID
<1263737212-8101-3-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1263737212-8101-1-git-send-email-Matthieu.Moy@imag.fr>

Previous patch allows commands like "git branch foo foo --track", which doesn't make much sense. Warn the user and don't change the configuration in this case. Don't die to let the caller finish its job in such case.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 branch.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/branch.c b/branch.c
index d187891..d769c8a 100644
--- a/branch.c
+++ b/branch.c
@@ -49,9 +49,19 @@ static int should_setup_rebase(const char *origin)
 
 void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
 {
+	const char *shortname = remote + 11;
+	int remote_is_branch = !prefixcmp(remote, "refs/heads/");
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
 
+	if (remote_is_branch
+	    && !strcmp(local, shortname)
+	    && !origin) {
+		warning("Not setting branch %s as its own upstream.",
+			local);
+		return;
+	}
+
 	strbuf_addf(&key, "branch.%s.remote", local);
 	git_config_set(key.buf, origin ? origin : ".");
 
@@ -71,8 +81,8 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 		strbuf_addstr(&key, origin ? "remote" : "local");
 
 		/* Are we tracking a proper "branch"? */
-		if (!prefixcmp(remote, "refs/heads/")) {
-			strbuf_addf(&key, " branch %s", remote + 11);
+		if (remote_is_branch) {
+			strbuf_addf(&key, " branch %s", shortname);
 			if (origin)
 				strbuf_addf(&key, " from %s", origin);
 		}
-- 
1.6.6.198.gbaea2
Previous: Junio C HamanoNext: Ilari Liusvaara
Message 4 of 7 in “Make it easy to use branch --track on existing branch”
  1. 0/2 Make it easy to use branch --track on existing branchMatthieu Moy, Jan 17, 2010
  2. 1/2 branch: allow creating a branch with same name and same starting point.Matthieu Moy, Jan 17, 2010
  3. Junio C HamanoJan 17, 2010
  4. 2/2 branch: warn and refuse to set a branch as a tracking branch of itself.Matthieu Moy, Jan 17, 2010
  5. Ilari LiusvaaraJan 17, 2010
  6. Matthieu MoyJan 17, 2010
  7. Ilari LiusvaaraJan 17, 2010

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.