Skip to contentSkip to conversation

Updated patch series for default upstream merge

PATCH Superseded Jared Hance 2 participants 12 messages Jun 15, 2016 Documentation

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Updated patch series for default upstream merge

This patch series allows for git merge to default to the upstream of the current branch.

This update adds fixes based on all of the style-feedback from the previous patch series; functionally it is equivalent (except for the first patch).

Should I have put the patch submitted by Jonathan Nieder in this series? I didn't place it in the tree but it should apply on top if it.

Jared Hance (4):
merge: update the usage information to be more modern
merge: introduce setup_merge_commit helper function
merge: introduce per-branch-configuration helper function
merge: add support for merging from upstream by default

 Documentation/config.txt |    6 +++
 builtin/merge.c          |   90 ++++++++++++++++++++++++++++++++--------------
 2 files changed, 69 insertions(+), 27 deletions(-)
Signature · 1 line
1.7.4
#1 lore raw

PATCH v4 1/5 t5526: Fix wrong argument order in "git config"

From: Jens Lehmann <[email protected]>

This fixes a typo where the "git config" arguments "-f" and "--unset" were swapped leading to the creation of a "--unset" file.

---
 t/t5526-fetch-submodules.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
t/t5526-fetch-submodules.sh +1 -1
index 884a5e5..a5f4585 100755
@@ -124,7 +124,7 @@ test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti
 	(
 		cd downstream &&
 		git fetch --recurse-submodules >../actual.out 2>../actual.err &&
-		git config -f --unset .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+		git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
 		git config --unset submodule.submodule.fetchRecurseSubmodules
 	) &&
 	test_cmp expect.out actual.out &&
Signature · 1 line
1.7.4
#2 lore raw

PATCH v4 1/4 merge: update the usage information to be more modern

In reply to Jared Hance

Apparantly, merge's usage information was outdated and used old terminology. Fix it.

---
 builtin/merge.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
builtin/merge.c +2 -2
index 42fff38..439d2c7 100644
@@ -37,8 +37,8 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
-	"git merge [options] <remote>...",
-	"git merge [options] <msg> HEAD <remote>",
+	"git merge [options] <branch>...",
+	"git merge [options] <msg> HEAD <branch>",
 	NULL
 };
 
Signature · 1 line
1.7.4
#3 lore raw

PATCH v4 2/5 merge: update the usage information to be more modern

In reply to Jared Hance

Apparantly, merge's usage information was outdated and used old terminology. Fix it.

---
 builtin/merge.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
builtin/merge.c +2 -2
index 42fff38..439d2c7 100644
@@ -37,8 +37,8 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
-	"git merge [options] <remote>...",
-	"git merge [options] <msg> HEAD <remote>",
+	"git merge [options] <branch>...",
+	"git merge [options] <msg> HEAD <branch>",
 	NULL
 };
 
Signature · 1 line
1.7.4
#4 lore raw

PATCH v4 2/4 merge: introduce setup_merge_commit helper function

In reply to Jared Hance

Add a new function to set up a merge commit given a branch or commit, which is currently used when parsing argv.

---
 builtin/merge.c |   44 +++++++++++++++++++++++++++-----------------
 1 files changed, 27 insertions(+), 17 deletions(-)
builtin/merge.c +27 -17
index 439d2c7..cd23880 100644
@@ -498,11 +498,15 @@ cleanup:
 	strbuf_release(&bname);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int per_branch_config(const char *k, const char *v, void *cb)
 {
-	if (branch && !prefixcmp(k, "branch.") &&
-		!prefixcmp(k + 7, branch) &&
-		!strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
+	const char *variable;
+	if (!branch || prefixcmp(k, "branch.") ||
+	   prefixcmp(k + 7, branch))
+		return 1; /* not what I handle */
+
+	variable = k + 7 + strlen(branch);
+	if (!strcmp(variable, ".mergeoptions")) {
 		const char **argv;
 		int argc;
 		char *buf;
@@ -911,6 +915,24 @@ static int evaluate_result(void)
 	return cnt;
 }
 
+static void setup_merge_commit(struct strbuf *buf,
+	struct commit_list ***remotes, const char *s)
+{
+	struct object *o;
+	struct commit *commit;
+
+	o = peel_to_type(s, 0, NULL, OBJ_COMMIT);
+	if (!o)
+		die("%s - not something we can merge", s);
+	commit = lookup_commit(o->sha1);
+	commit->util = (void *)s;
+	*remotes = &commit_list_insert(commit, *remotes)->next;
+
+	strbuf_addf(buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
+	setenv(buf->buf, s, 1);
+	strbuf_reset(buf);
+}
+
 int cmd_merge(int argc, const char **argv, const char *prefix)
 {
 	unsigned char result_tree[20];
@@ -1059,19 +1081,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	strbuf_reset(&buf);
 
 	for (i = 0; i < argc; i++) {
-		struct object *o;
-		struct commit *commit;
-
-		o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
-		if (!o)
-			die("%s - not something we can merge", argv[i]);
-		commit = lookup_commit(o->sha1);
-		commit->util = (void *)argv[i];
-		remotes = &commit_list_insert(commit, remotes)->next;
-
-		strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
-		setenv(buf.buf, argv[i], 1);
-		strbuf_reset(&buf);
+		setup_merge_commit(&buf, &remotes, argv[i]);
 	}
 
 	if (!use_strategies) {
Signature · 1 line
1.7.4
#5 lore raw

PATCH v4 3/5 merge: introduce setup_merge_commit helper function

In reply to Jared Hance

Add a new function to set up a merge commit given a branch or commit, which is currently used when parsing argv.

---
 builtin/merge.c |   44 +++++++++++++++++++++++++++-----------------
 1 files changed, 27 insertions(+), 17 deletions(-)
builtin/merge.c +27 -17
index 439d2c7..cd23880 100644
@@ -498,11 +498,15 @@ cleanup:
 	strbuf_release(&bname);
 }
 
-static int git_merge_config(const char *k, const char *v, void *cb)
+static int per_branch_config(const char *k, const char *v, void *cb)
 {
-	if (branch && !prefixcmp(k, "branch.") &&
-		!prefixcmp(k + 7, branch) &&
-		!strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
+	const char *variable;
+	if (!branch || prefixcmp(k, "branch.") ||
+	   prefixcmp(k + 7, branch))
+		return 1; /* not what I handle */
+
+	variable = k + 7 + strlen(branch);
+	if (!strcmp(variable, ".mergeoptions")) {
 		const char **argv;
 		int argc;
 		char *buf;
@@ -911,6 +915,24 @@ static int evaluate_result(void)
 	return cnt;
 }
 
+static void setup_merge_commit(struct strbuf *buf,
+	struct commit_list ***remotes, const char *s)
+{
+	struct object *o;
+	struct commit *commit;
+
+	o = peel_to_type(s, 0, NULL, OBJ_COMMIT);
+	if (!o)
+		die("%s - not something we can merge", s);
+	commit = lookup_commit(o->sha1);
+	commit->util = (void *)s;
+	*remotes = &commit_list_insert(commit, *remotes)->next;
+
+	strbuf_addf(buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
+	setenv(buf->buf, s, 1);
+	strbuf_reset(buf);
+}
+
 int cmd_merge(int argc, const char **argv, const char *prefix)
 {
 	unsigned char result_tree[20];
@@ -1059,19 +1081,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	strbuf_reset(&buf);
 
 	for (i = 0; i < argc; i++) {
-		struct object *o;
-		struct commit *commit;
-
-		o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
-		if (!o)
-			die("%s - not something we can merge", argv[i]);
-		commit = lookup_commit(o->sha1);
-		commit->util = (void *)argv[i];
-		remotes = &commit_list_insert(commit, remotes)->next;
-
-		strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
-		setenv(buf.buf, argv[i], 1);
-		strbuf_reset(&buf);
+		setup_merge_commit(&buf, &remotes, argv[i]);
 	}
 
 	if (!use_strategies) {
Signature · 1 line
1.7.4
#6 lore raw

PATCH v4 3/4 merge: introduce per-branch-configuration helper function

In reply to Jared Hance

Create a new function that will hold configuration code for configuration values that are specified per branch, as suggested by Junio.

---
 builtin/merge.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
builtin/merge.c +12 -0
index cd23880..71b392d 100644
@@ -522,8 +522,20 @@ static int per_branch_config(const char *k, const char *v, void *cb)
 		parse_options(argc, argv, NULL, builtin_merge_options,
 			      builtin_merge_usage, 0);
 		free(buf);
+
+		return 0;
 	}
 
+	return 1; /* not what I handle */
+}
+
+static int git_merge_config(const char *k, const char *v, void *cb)
+{
+	int status = per_branch_config(k, v, cb);
+
+	if (status <= 0)
+		return status;
+
 	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
 		show_diffstat = git_config_bool(k, v);
 	else if (!strcmp(k, "pull.twohead"))
Signature · 1 line
1.7.4
#7 lore raw

PATCH v4 4/5 merge: introduce per-branch-configuration helper function

In reply to Jared Hance

Create a new function that will hold configuration code for configuration values that are specified per branch, as suggested by Junio.

---
 builtin/merge.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
builtin/merge.c +12 -0
index cd23880..71b392d 100644
@@ -522,8 +522,20 @@ static int per_branch_config(const char *k, const char *v, void *cb)
 		parse_options(argc, argv, NULL, builtin_merge_options,
 			      builtin_merge_usage, 0);
 		free(buf);
+
+		return 0;
 	}
 
+	return 1; /* not what I handle */
+}
+
+static int git_merge_config(const char *k, const char *v, void *cb)
+{
+	int status = per_branch_config(k, v, cb);
+
+	if (status <= 0)
+		return status;
+
 	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
 		show_diffstat = git_config_bool(k, v);
 	else if (!strcmp(k, "pull.twohead"))
Signature · 1 line
1.7.4
#8 lore raw

PATCH v4 4/4 merge: add support for merging from upstream by default

In reply to Jared Hance

Add the option merge.defaultupstream to add support for merging from the upstream branch by default. The upstream branch is found using branch.[name].merge.

---
 Documentation/config.txt |    6 ++++++
 builtin/merge.c          |   32 +++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)
Documentation/config.txt +6 -0
index c5e1835..4415691 100644
@@ -1389,6 +1389,12 @@ man.<tool>.path::
 
 include::merge-config.txt[]
 
+merge.defaultUpstream::
+	If merge is called without any ref arguments, merge from the branch
+	specified in branch.<current branch>.merge, which is considered to be
+	the upstream branch for the current branch, possibly set by --track or
+	--set-upstream.
+
 mergetool.<tool>.path::
 	Override the path for the given tool.  This is useful in case
 	your tool is not in the PATH.
builtin/merge.c +23 -9
index 71b392d..a3ca6a0 100644
@@ -37,7 +37,7 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
-	"git merge [options] <branch>...",
+	"git merge [options] [<branch>...]",
 	"git merge [options] <msg> HEAD <branch>",
 	NULL
 };
@@ -58,6 +58,8 @@ static int option_renormalize;
 static int verbosity;
 static int allow_rerere_auto;
 static int abort_current_merge;
+static int default_upstream;
+static const char *upstream_branch;
 
 static struct strategy all_strategy[] = {
 	{ "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -524,7 +526,8 @@ static int per_branch_config(const char *k, const char *v, void *cb)
 		free(buf);
 
 		return 0;
-	}
+	} else if (!strcmp(variable, ".merge"))
+		return git_config_string(&upstream_branch, k, v);
 
 	return 1; /* not what I handle */
 }
@@ -536,7 +539,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 	if (status <= 0)
 		return status;
 
-	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
+	if (!strcmp(k, "merge.defaultupstream"))
+		default_upstream = git_config_bool(k, v);
+	else if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
 		show_diffstat = git_config_bool(k, v);
 	else if (!strcmp(k, "pull.twohead"))
 		return git_config_string(&pull_twohead, k, v);
@@ -1017,9 +1022,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	if (!allow_fast_forward && fast_forward_only)
 		die("You cannot combine --no-ff with --ff-only.");
 
-	if (!argc)
-		usage_with_options(builtin_merge_usage,
-			builtin_merge_options);
+	if (!argc) {
+		if (!default_upstream || !upstream_branch)
+			usage_with_options(builtin_merge_usage,
+					builtin_merge_options);
+
+		setup_merge_commit(&buf, &remotes, upstream_branch);
+	}
 
 	/*
 	 * This could be traditional "merge <msg> HEAD <commit>..."  and
@@ -1082,9 +1091,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (head_invalid || !argc)
-		usage_with_options(builtin_merge_usage,
-			builtin_merge_options);
+	if (head_invalid)
+		usage_msg_opt("cannot use old-style invocation from an unborn"
+				"branch", 
+				builtin_merge_usage, builtin_merge_options);
+
+	if (!argc && !(default_upstream && upstream_branch))
+		usage_msg_opt("no commit to merge specified",
+				builtin_merge_usage, builtin_merge_options);
 
 	strbuf_addstr(&buf, "merge");
 	for (i = 0; i < argc; i++)
Signature · 1 line
1.7.4
#9 lore raw

Re: [PATCH v4 4/4] merge: add support for merging from upstream by default

Quoted 50 lines · Jared Hance

Add the option merge.defaultupstream to add support for merging from the upstream branch by default. The upstream branch is found using branch.[name].merge.

---
 Documentation/config.txt |    6 ++++++
 builtin/merge.c          |   32 +++++++++++++++++++++++---------

 2 files changed, 29 insertions(+), 9 deletions(-)
Documentation/config.txt +0 -0
index c5e1835..4415691 100644
@@ -1389,6 +1389,12 @@ man.<tool>.path::
 

 include::merge-config.txt[]

Documentation/config.txt +6 -0
+merge.defaultUpstream::
+       If merge is called without any ref arguments, merge from the branch
+       specified in branch.<current branch>.merge, which is considered to be
+       the upstream branch for the current branch, possibly set by --track or
+       --set-upstream.
+

 mergetool.<tool>.path::
       Override the path for the given tool.  This is useful in case your tool is not in the PATH.

builtin/merge.c +1 -1
index 71b392d..a3ca6a0 100644
@@ -37,7 +37,7 @@ struct strategy {
  };
 
  static const char * const builtin_merge_usage[] = {
-       "git merge [options] <branch>...",
+       "git merge [options] [<branch>...]",
        "git merge [options] <msg> HEAD <branch>",

       NULL
 };

builtin/merge.c +2 -0
@@ -58,6 +58,8 @@ static int option_renormalize;
  static int verbosity;
  static int allow_rerere_auto;
  static int abort_current_merge;
+static int default_upstream;
+static const char *upstream_branch;
 
  static struct strategy all_strategy[] = {
        { "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },

builtin/merge.c static int per_branch_config(const char *k, const char *v, void *cb) · line 530

Quoted 6 lines
builtin/merge.c +2 -1
@@ -524,7 +526,8 @@ static int per_branch_config(const char *k, const char *v, void *cb)
                free(buf);
 
                return 0;
-       }
+       } else if (!strcmp(variable, ".merge"))
+               return git_config_string(&upstream_branch, k, v);

I don't think, that this is the correct way to get the "upstream" from a branch. Please have a look into sha1_name.c:interpret_branch_name() and builtin/for-each-ref.c:populate_value() how they resolve "upstream".

Bert

#10 lore raw

PATCH v4 5/5 merge: add support for merging from upstream by default

In reply to Jared Hance

Add the option merge.defaultupstream to add support for merging from the upstream branch by default. The upstream branch is found using branch.[name].merge.

---
 Documentation/config.txt |    6 ++++++
 builtin/merge.c          |   32 +++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)
Documentation/config.txt +6 -0
index c5e1835..4415691 100644
@@ -1389,6 +1389,12 @@ man.<tool>.path::
 
 include::merge-config.txt[]
 
+merge.defaultUpstream::
+	If merge is called without any ref arguments, merge from the branch
+	specified in branch.<current branch>.merge, which is considered to be
+	the upstream branch for the current branch, possibly set by --track or
+	--set-upstream.
+
 mergetool.<tool>.path::
 	Override the path for the given tool.  This is useful in case
 	your tool is not in the PATH.
builtin/merge.c +23 -9
index 71b392d..a3ca6a0 100644
@@ -37,7 +37,7 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
-	"git merge [options] <branch>...",
+	"git merge [options] [<branch>...]",
 	"git merge [options] <msg> HEAD <branch>",
 	NULL
 };
@@ -58,6 +58,8 @@ static int option_renormalize;
 static int verbosity;
 static int allow_rerere_auto;
 static int abort_current_merge;
+static int default_upstream;
+static const char *upstream_branch;
 
 static struct strategy all_strategy[] = {
 	{ "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -524,7 +526,8 @@ static int per_branch_config(const char *k, const char *v, void *cb)
 		free(buf);
 
 		return 0;
-	}
+	} else if (!strcmp(variable, ".merge"))
+		return git_config_string(&upstream_branch, k, v);
 
 	return 1; /* not what I handle */
 }
@@ -536,7 +539,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 	if (status <= 0)
 		return status;
 
-	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
+	if (!strcmp(k, "merge.defaultupstream"))
+		default_upstream = git_config_bool(k, v);
+	else if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
 		show_diffstat = git_config_bool(k, v);
 	else if (!strcmp(k, "pull.twohead"))
 		return git_config_string(&pull_twohead, k, v);
@@ -1017,9 +1022,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	if (!allow_fast_forward && fast_forward_only)
 		die("You cannot combine --no-ff with --ff-only.");
 
-	if (!argc)
-		usage_with_options(builtin_merge_usage,
-			builtin_merge_options);
+	if (!argc) {
+		if (!default_upstream || !upstream_branch)
+			usage_with_options(builtin_merge_usage,
+					builtin_merge_options);
+
+		setup_merge_commit(&buf, &remotes, upstream_branch);
+	}
 
 	/*
 	 * This could be traditional "merge <msg> HEAD <commit>..."  and
@@ -1082,9 +1091,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (head_invalid || !argc)
-		usage_with_options(builtin_merge_usage,
-			builtin_merge_options);
+	if (head_invalid)
+		usage_msg_opt("cannot use old-style invocation from an unborn"
+				"branch", 
+				builtin_merge_usage, builtin_merge_options);
+
+	if (!argc && !(default_upstream && upstream_branch))
+		usage_msg_opt("no commit to merge specified",
+				builtin_merge_usage, builtin_merge_options);
 
 	strbuf_addstr(&buf, "merge");
 	for (i = 0; i < argc; i++)
Signature · 1 line
1.7.4
#11 lore raw
In reply to Jared Hance

Sorry, this came out completely wrong due to some leftover patches in my tree. I'll resend a correct one in a few minutes (I just got some feedback that somehow I missed anyway, so some changes need to be made). Ignore this.

My apologies.