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

[PATCH 1/2] branch: allow creating a branch with same name and same starting point.

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

Previously, "git branch --track newname old" was rejected if newname existed, even if it already had the same value. This patch allows it, even without --force. This has two advantages:

* Not requiring --force for a safe operation, hence allowing the user to
  benefit from the other safety checks.
* Allow changing the configuration of the checked-out branch.

As a result, "git branch --track" becomes a conveinient and safe way to set up upstream information for an existing branch.

The error message "A branch named '%s' already exists" is no longer precise enough to refuse to act, since part of the reason of the failure is the old branch value, so we include this value in the message.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 branch.c                 |   45 ++++++++++++++++++++++++++++++---------------
 t/t6040-tracking-info.sh |    8 ++++++++
 2 files changed, 38 insertions(+), 15 deletions(-)
diff --git a/branch.c b/branch.c
index 05ef3f5..d187891 100644
--- a/branch.c
+++ b/branch.c
@@ -128,27 +128,39 @@ void create_branch(const char *head,
 		   const char *name, const char *start_name,
 		   int force, int reflog, enum branch_track track)
 {
-	struct ref_lock *lock;
+	struct ref_lock *lock = NULL;
 	struct commit *commit;
-	unsigned char sha1[20];
+	unsigned char old_sha1[20], sha1[20];
 	char *real_ref, msg[PATH_MAX + 20];
 	struct strbuf ref = STRBUF_INIT;
 	int forcing = 0;
+	int dont_change_ref = 0;
 
 	if (strbuf_check_branch_ref(&ref, name))
 		die("'%s' is not a valid branch name.", name);
 
-	if (resolve_ref(ref.buf, sha1, 1, NULL)) {
-		if (!force)
-			die("A branch named '%s' already exists.", name);
-		else if (!is_bare_repository() && !strcmp(head, name))
-			die("Cannot force update the current branch.");
+	if (get_sha1(start_name, sha1))
+		die("Not a valid object name: '%s'.", start_name);
+
+	if (resolve_ref(ref.buf, old_sha1, 1, NULL)) {
+		if (hashcmp(old_sha1, sha1)) {
+			if (!force)
+				die("A branch named '%s' already exists pointing to %.*s.",
+				    name, 8, sha1_to_hex(old_sha1));
+			else if (!is_bare_repository() && !strcmp(head, name))
+				die("Cannot force update the current branch.");
+		} else {
+			/*
+			 * Re-creating a branch with the same value.
+			 * Safe, and usefull to set up the upstream
+			 * branch without touching the ref.
+			 */
+			dont_change_ref = 1;
+		}
 		forcing = 1;
 	}
 
 	real_ref = NULL;
-	if (get_sha1(start_name, sha1))
-		die("Not a valid object name: '%s'.", start_name);
 
 	switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
 	case 0:
@@ -170,9 +182,11 @@ void create_branch(const char *head,
 		die("Not a valid branch point: '%s'.", start_name);
 	hashcpy(sha1, commit->object.sha1);
 
-	lock = lock_any_ref_for_update(ref.buf, NULL, 0);
-	if (!lock)
-		die_errno("Failed to lock ref for update");
+	if (!dont_change_ref) {
+		lock = lock_any_ref_for_update(ref.buf, NULL, 0);
+		if (!lock)
+			die_errno("Failed to lock ref for update");
+	}
 
 	if (reflog)
 		log_all_ref_updates = 1;
@@ -180,15 +194,16 @@ void create_branch(const char *head,
 	if (forcing)
 		snprintf(msg, sizeof msg, "branch: Reset from %s",
 			 start_name);
-	else
+	else if (!dont_change_ref)
 		snprintf(msg, sizeof msg, "branch: Created from %s",
 			 start_name);
 
 	if (real_ref && track)
 		setup_tracking(name, real_ref, track);
 
-	if (write_ref_sha1(lock, sha1, msg) < 0)
-		die_errno("Failed to write ref");
+	if (!dont_change_ref)
+		if (write_ref_sha1(lock, sha1, msg) < 0)
+			die_errno("Failed to write ref");
 
 	strbuf_release(&ref);
 	free(real_ref);
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 664b0f8..3c200b6 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -89,4 +89,12 @@ test_expect_success 'status when tracking annotated tags' '
 	grep "set up to track" actual &&
 	git checkout heavytrack
 '
+
+test_expect_success 'setup tracking with branch --track on existing branch' '
+	git branch from-master master &&
+	test_must_fail git config branch.from-master.merge > actual &&
+	git branch from-master master --track &&
+	git config branch.from-master.merge > actual &&
+	grep -q "^refs/heads/master$" actual
+'
 test_done
-- 
1.6.6.198.gbaea2
Previous: Matthieu MoyNext: Junio C Hamano
Message 2 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.