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

Re: defaults for where to merge from

From
Paolo Bonzini <paolo.bonzini@lu.unisi.ch>
Date
Feb 28, 2007, 17:11 UTC
Message-ID
<45E5B7B4.9080605@lu.unisi.ch>
In-Reply-To
<Pine.LNX.4.63.0702281643200.22628@wbgn013.biozentrum.uni-wuerzburg.de>
> I don't think that you should be forced to do it explicitely. If you want 
> to merge in another branch, you can do that _explicitely_. So, defaulting 
> to what most people want anyway is A Good Thing.

Here is a prototype patch to implement this functionality. One problem is that config.c does not remove cleaned sections, so after "git-branch -d mybranch" one is left with a useless "[branch "mybranch"]" section in .git/config. Other than this, it seems to work well in my experiments.

Paolo
diff --git a/builtin-branch.c b/builtin-branch.c
index d0179b0..4c42825 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -147,8 +147,21 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 			error("Error deleting %sbranch '%s'", remote,
 			       argv[i]);
 			ret = 1;
-		} else
+		} else {
+			if (kinds == REF_LOCAL_BRANCH) {
+				/* Remove git-config keys.  */
+				char *config_key = xmalloc(strlen(argv[i]) + 15);
+				sprintf(config_key, "branch.%s.remote", argv[i]);
+				git_config_set(config_key, NULL);
+
+				sprintf(config_key, "branch.%s.merge", argv[i]);
+				git_config_set(config_key, NULL);
+				sprintf(config_key, "branch.%s", argv[i]);
+				git_config_set(config_key, NULL);
+			}
+
 			printf("Deleted %sbranch %s.\n", remote, argv[i]);
+		}
 
 	}
 
@@ -316,7 +330,7 @@ static void create_branch(const char *name, const char *start_name,
 	struct commit *commit;
 	unsigned char sha1[20];
 	char ref[PATH_MAX], msg[PATH_MAX + 20];
-	int forcing = 0;
+	int forcing = 0, remote = 0;
 
 	snprintf(ref, sizeof ref, "refs/heads/%s", name);
 	if (check_ref_format(ref))
@@ -335,6 +349,13 @@ static void create_branch(const char *name, const char *start_name,
 		hashcpy(sha1, start_sha1);
 	else if (get_sha1(start_name, sha1))
 		die("Not a valid object name: '%s'.", start_name);
+	else {
+		unsigned char remote_sha1[20];
+		remote = strchr(start_name, '/')
+			 && read_ref(mkpath("refs/remotes/%s", start_name),
+				     remote_sha1) != -1
+			 && !memcmp(sha1, remote_sha1, 20);
+	}
 
 	if ((commit = lookup_commit_reference(sha1)) == NULL)
 		die("Not a valid branch point: '%s'.", start_name);
@@ -354,6 +375,23 @@ static void create_branch(const char *name, const char *start_name,
 		snprintf(msg, sizeof msg, "branch: Created from %s",
 			 start_name);
 
+	if (remote) {
+		/* Branching off a remote branch.  Set up so that git-pull
+		   automatically merges from there.  */
+		char *config_key = xmalloc(strlen(name) + 15);
+		char *merge_value = xmalloc(strlen(start_name) + 10);
+		char *slash = strchr(start_name, '/');
+
+		char *remote_value = xstrdup(start_name);
+		remote_value[slash - start_name] = 0;
+		sprintf(config_key, "branch.%s.remote", name);
+		git_config_set(config_key, remote_value);
+
+		sprintf(merge_value, "refs/heads/%s", slash + 1);
+		sprintf(config_key, "branch.%s.merge", name);
+		git_config_set(config_key, merge_value);
+	}
+
 	if (write_ref_sha1(lock, sha1, msg) < 0)
 		die("Failed to write ref: %s.", strerror(errno));
 }
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 8 of 48 in “defaults for where to merge from”
  1. Paolo BonziniFeb 28, 2007
  2. Johannes SchindelinFeb 28, 2007
  3. Andy ParkinsFeb 28, 2007
  4. Paolo BonziniFeb 28, 2007
  5. Andy ParkinsFeb 28, 2007
  6. Julian PhillipsFeb 28, 2007
  7. Johannes SchindelinFeb 28, 2007
  8. Paolo BonziniFeb 28, 2007
  9. Johannes SchindelinFeb 28, 2007
  10. defaults for where to merge from (take 2)Paolo Bonzini, Mar 1, 2007
  11. Alex RiesenFeb 28, 2007
  12. Paolo BonziniFeb 28, 2007
  13. Alex RiesenMar 1, 2007
  14. Johannes SchindelinMar 1, 2007
  15. Alex RiesenMar 1, 2007
  16. Paolo BonziniMar 1, 2007
  17. Alex RiesenMar 1, 2007
  18. Junio C HamanoMar 1, 2007
  19. J. Bruce FieldsMar 2, 2007
  20. Paolo BonziniMar 1, 2007
  21. Alex RiesenMar 1, 2007
  22. Paolo BonziniMar 1, 2007
  23. Alex RiesenMar 1, 2007
  24. defaults for where to merge from (take 3)Paolo Bonzini, Mar 1, 2007
  25. Alex RiesenMar 1, 2007
  26. Paolo BonziniMar 1, 2007
  27. Junio C HamanoMar 1, 2007
  28. Alex RiesenMar 1, 2007
  29. Junio C HamanoMar 2, 2007
  30. Alex RiesenMar 2, 2007
  31. Junio C HamanoMar 2, 2007
  32. Alex RiesenMar 1, 2007
  33. Paolo BonziniMar 1, 2007
  34. Johannes SchindelinMar 1, 2007
  35. Paolo BonziniMar 2, 2007
  36. git-branch: register where to merge from, when branching off a remote branchPaolo Bonzini, Mar 2, 2007
  37. Junio C HamanoMar 2, 2007
  38. Junio C HamanoMar 2, 2007
  39. Jeff KingMar 2, 2007
  40. Paolo BonziniMar 2, 2007
  41. Jeff KingMar 2, 2007
  42. Paolo BonziniMar 2, 2007
  43. Johannes SchindelinMar 2, 2007
  44. Paolo BonziniMar 2, 2007
  45. Johannes SchindelinMar 2, 2007
  46. Johannes SchindelinMar 2, 2007
  47. Jakub NarebskiMar 2, 2007
  48. Peter BaumannFeb 28, 2007

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.