# [PATCH] Add support for merging from upstream by default.

6 messages from 2011-02-05 to 2011-02-07. Participants: Jared Hance, Andreas Schwab, Junio C Hamano, Bert Wesarg.
Thread: https://gitlist.dev/t/26413

## Jared Hance, 2011-02-05 00:45

Subject: [PATCH] Add support for merging from upstream by default.
Message-ID: <1296866721-27818-1-git-send-email-jaredhance@gmail.com>
URL: https://gitlist.dev/e/1296866721-27818-1-git-send-email-jaredhance%40gmail.com

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

So it turns out that the old code _did_ work with options; I had thought it
hadn't because I relied on !argc == 0, but since argc is decreased after parsing
options anyway, it isn't a problem. This update fixes the usage as Junio
suggested, since it does support options.

 builtin/merge.c |   41 +++++++++++++++++++++++++++++++++++------
 1 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 42fff38..a69b69f 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -37,6 +37,7 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
+        "git merge",
 	"git merge [options] <remote>...",
 	"git merge [options] <msg> HEAD <remote>",
 	NULL
@@ -58,6 +59,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 },
@@ -519,8 +522,15 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 			      builtin_merge_usage, 0);
 		free(buf);
 	}
-
-	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
+        else if(branch && !prefixcmp(k, "branch.") &&
+                !prefixcmp(k + 7, branch) &&
+                !strcmp(k + 7 + strlen(branch), ".upstream")) {
+                return git_config_string(&upstream_branch, k, v);
+        }
+
+        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);
@@ -983,9 +993,28 @@ 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) {
+		        struct object *o;
+                        struct commit *commit;
+
+                        o = peel_to_type(upstream_branch, 0, NULL, OBJ_COMMIT);
+                        if (!o)
+                            die("%s - not something we can merge", argv[i]);
+                        commit = lookup_commit(o->sha1);
+                        commit->util = (void *)upstream_branch;
+                        remotes = &commit_list_insert(commit, remotes)->next;
+
+                        strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
+                        setenv(buf.buf, upstream_branch, 1);
+                        strbuf_reset(&buf);
+                }
+                else {
+		        usage_with_options(builtin_merge_usage,
+			        builtin_merge_options);
+
+                }
+        }
 
 	/*
 	 * This could be traditional "merge <msg> HEAD <commit>..."  and
@@ -1048,7 +1077,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (head_invalid || !argc)
+	if (head_invalid || (!argc && !(default_upstream && upstream_branch)))
 		usage_with_options(builtin_merge_usage,
 			builtin_merge_options);
 
-- 
1.7.4

```

## Andreas Schwab, 2011-02-05 10:03

Subject: Re: [PATCH] Add support for merging from upstream by default.
Message-ID: <m2k4he23z6.fsf@whitebox.home>
URL: https://gitlist.dev/e/m2k4he23z6.fsf%40whitebox.home
In-Reply-To: <1296866721-27818-1-git-send-email-jaredhance@gmail.com>

```
Jared Hance <jaredhance@gmail.com> writes:

> diff --git a/builtin/merge.c b/builtin/merge.c
> index 42fff38..a69b69f 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -37,6 +37,7 @@ struct strategy {
>  };
>  
>  static const char * const builtin_merge_usage[] = {
> +        "git merge",

Doesn't that form take options as well?

>  	"git merge [options] <remote>...",

IOW, how about changing that to

  	"git merge [options] [<remote>...]",

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Jared Hance, 2011-02-05 13:31

Subject: [PATCH] Add support for merging from upstream by default.
Message-ID: <1296912681-4161-1-git-send-email-jaredhance@gmail.com>
URL: https://gitlist.dev/e/1296912681-4161-1-git-send-email-jaredhance%40gmail.com
In-Reply-To: <m2k4he23z6.fsf@whitebox.home>

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

I checked it this time, it really is the right usage.

 builtin/merge.c |   42 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 42fff38..596febe 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -37,7 +37,7 @@ struct strategy {
 };
 
 static const char * const builtin_merge_usage[] = {
-	"git merge [options] <remote>...",
+	"git merge [options] [<remote>...]",
 	"git merge [options] <msg> HEAD <remote>",
 	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 },
@@ -519,8 +521,15 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 			      builtin_merge_usage, 0);
 		free(buf);
 	}
-
-	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
+        else if(branch && !prefixcmp(k, "branch.") &&
+                !prefixcmp(k + 7, branch) &&
+                !strcmp(k + 7 + strlen(branch), ".upstream")) {
+                return git_config_string(&upstream_branch, k, v);
+        }
+
+        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);
@@ -983,9 +992,28 @@ 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) {
+		        struct object *o;
+                        struct commit *commit;
+
+                        o = peel_to_type(upstream_branch, 0, NULL, OBJ_COMMIT);
+                        if (!o)
+                            die("%s - not something we can merge", argv[i]);
+                        commit = lookup_commit(o->sha1);
+                        commit->util = (void *)upstream_branch;
+                        remotes = &commit_list_insert(commit, remotes)->next;
+
+                        strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
+                        setenv(buf.buf, upstream_branch, 1);
+                        strbuf_reset(&buf);
+                }
+                else {
+		        usage_with_options(builtin_merge_usage,
+			        builtin_merge_options);
+
+                }
+        }
 
 	/*
 	 * This could be traditional "merge <msg> HEAD <commit>..."  and
@@ -1048,7 +1076,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (head_invalid || !argc)
+	if (head_invalid || (!argc && !(default_upstream && upstream_branch)))
 		usage_with_options(builtin_merge_usage,
 			builtin_merge_options);
 
-- 
1.7.4

```

## Junio C Hamano, 2011-02-07 01:59

Subject: Re: [PATCH] Add support for merging from upstream by default.
Message-ID: <7vaai8wqny.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vaai8wqny.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1296912681-4161-1-git-send-email-jaredhance@gmail.com>

```
Jared Hance <jaredhance@gmail.com> writes:

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

Sign off?

The patch (rather your code before getting handed to your MUA) seems
severely whitespace damaged.

> diff --git a/builtin/merge.c b/builtin/merge.c
> index 42fff38..596febe 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -37,7 +37,7 @@ struct strategy {
>  };
>  
>  static const char * const builtin_merge_usage[] = {
> -	"git merge [options] <remote>...",
> +	"git merge [options] [<remote>...]",
>  	"git merge [options] <msg> HEAD <remote>",
>  	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 },
> @@ -519,8 +521,15 @@ static int git_merge_config(const char *k, const char *v, void *cb)
>  			      builtin_merge_usage, 0);
>  		free(buf);
>  	}
> -
> -	if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
> +        else if(branch && !prefixcmp(k, "branch.") &&
> +                !prefixcmp(k + 7, branch) &&
> +                !strcmp(k + 7 + strlen(branch), ".upstream")) {
> +                return git_config_string(&upstream_branch, k, v);
> +        }

Shouldn't this be inside a single large if block that is guarded by

	if (branch && !prefixcmp(k, "branch.") && !prefixcmp(k + 7, branch))

with a small refactoring of the existing code in git_merge_config(), the
first if statement?  It probably is a good idea to hand that off to a
small helper function as well, i.e. the first if statement in
git_merge_config() becomes something like:

	status = per_branch_config(k, v, cb);
        if (status <= 0)
		return status;

and then a new helper function is defined right in front of it, perhaps

	static int per_branch_config(const char *k, const char *v, void *cb)
	{
        	const char *variable;
        	if (!branch || prefixcmp(k, "branch.")
                    || prefixcmp(k + 7, branch))
			return 1; /* ignore me */
		variable = k + 7 + strlen(branch);
                if (strcmp(variable, ".mergeoptions")) {
                	...
                        return 0; /* done */
		}
                if (strcmp(variable, ".upstream")) {
                	...
                        return 0; /* or -1 if you see an error */
		}
                return 1; /* not what I handle */
	}

```

## Bert Wesarg, 2011-02-07 09:36

Subject: Re: [PATCH] Add support for merging from upstream by default.
Message-ID: <AANLkTikii-fx5nYeBDc70eh_wj05sEZ6fH6=bF9GqHBK@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTikii-fx5nYeBDc70eh_wj05sEZ6fH6%3DbF9GqHBK%40mail.gmail.com
In-Reply-To: <1296912681-4161-1-git-send-email-jaredhance@gmail.com>

```
On Sat, Feb 5, 2011 at 14:31, Jared Hance <jaredhance@gmail.com> wrote:
> Adds the option merge.defaultupstream to add support for merging from the
> upstream branch by default. The upstream branch is found using
> branch.[name].upstream.

Why do we need a per branch config for this, isn't .origin/.merge
enough? Or, where is the connection between your new .upstream and the
@{upstream} ref?

Bert

```

## Bert Wesarg, 2011-02-07 09:43

Subject: Re: [PATCH] Add support for merging from upstream by default.
Message-ID: <AANLkTim7hRrGtT_9dsHWp-405H_RF1s=UUQ6BPFCOY+n@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTim7hRrGtT_9dsHWp-405H_RF1s%3DUUQ6BPFCOY%2Bn%40mail.gmail.com
In-Reply-To: <AANLkTikii-fx5nYeBDc70eh_wj05sEZ6fH6=bF9GqHBK@mail.gmail.com>

```
On Mon, Feb 7, 2011 at 10:36, Bert Wesarg <bert.wesarg@googlemail.com> wrote:
> On Sat, Feb 5, 2011 at 14:31, Jared Hance <jaredhance@gmail.com> wrote:
>> Adds the option merge.defaultupstream to add support for merging from the
>> upstream branch by default. The upstream branch is found using
>> branch.[name].upstream.
>
> Why do we need a per branch config for this, isn't .origin/.merge

Why do we need a *new* per branch config for this, isn't .origin/.merge

> enough? Or, where is the connection between your new .upstream and the
> @{upstream} ref?
>
> Bert
>

```
