Add support for merging from upstream by default.
PATCH 1/1 Add support for merging from upstream by default.
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(-)
builtin/merge.c +35 -6
@@ -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);
Signature · 1 line
1.7.4
builtin/merge.c struct strategy { · line 40
Quoted 9 lines · Jared Hance
builtin/merge.c +1
-0@@ -37,6 +37,7 @@ struct strategy { };static const char *const builtin_merge_usage[] = { +"git merge" ,
Doesn't that form take options as well?
builtin/merge.c struct strategy { · line 41
Quoted 1 line
builtin/merge.c +0
-0"git merge [options] <remote>...",
IOW, how about changing that to
"git merge [options] [<remote>...]",
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
PATCH 1/1 Add support for merging from upstream by default.
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(-)
builtin/merge.c +35 -7
@@ -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);
Signature · 1 line
1.7.4
Quoted 4 lines · Jared Hance
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.
Quoted 23 lines
builtin/merge.c +3
-1@@ -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 },
builtin/merge.c static int git_merge_config(const char *k, const char *v, void *cb) · line 528
Quoted 10 lines
builtin/merge.c +5
-2@@ -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 */
}Quoted 3 lines · Jared Hance
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
Quoted 6 lines · Bert Wesarg
Quoted 3 lines · Jared Hance
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
Quoted 5 lines
enough? Or, where is the connection between your new .upstream and the @{upstream} ref?
Bert