Add support for merging from upstream by default.
PATCH v2 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].merge.
Also add helper functions per_branch_config and setup_merge_commit to reduce reduncancy and impove clarity.
--- Documentation/config.txt | 5 +++ builtin/merge.c | 81 +++++++++++++++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 23 deletions(-)
Documentation/config.txt +5 -0
@@ -1389,6 +1389,11 @@ 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. + 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 +58 -23
132 lines not on this page · raw message
Signature · 1 line
1.7.4
v3 cover Updated patch series for default upstream merge
This patch series allows for git merge to default to the upstream branch of the current branch.
Notes/Complications:
- I'm not sure whether the option should be merge.defaultUpstream or merge.defaultToUpstream
- Should [remotes] be changed to [branches]? I felt like it was a completely different change and didn't belong in the patch series.
- I left one of the ifs with unnecessary braces for clarity because of a nested if-else: is this the preferred style?
Jared Hance (3):
Add setup_merge_commit function to merge/builtin.c. Add function per_branch_config.
Add support for merging from upstream by default.
Documentation/config.txt | 6 +++ builtin/merge.c | 87 ++++++++++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 25 deletions(-)
Signature · 1 line
1.7.4
PATCH v3 1/3 Add setup_merge_commit function to merge/builtin.c.
Adds a new function that will be used in a later patch as well as the current code to reduce redundancy.
--- builtin/merge.c | 32 +++++++++++++++++++------------- 1 files changed, 19 insertions(+), 13 deletions(-)
builtin/merge.c +19 -13
@@ -911,6 +911,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 +1077,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
Re: [PATCH v3 1/3] Add setup_merge_commit function to merge/builtin.c.
Quoted 1 line · Jared Hance
Subject: Re: [PATCH v3 1/3] Add
setup_merge_commitfunction tomerge/builtin.c.
Please spell the name of the file you are touching correctly ;-).
Subject: builtin/merge.c: Add setup_merge_commit function
Quoted 2 lines
Adds a new function that will be used in a later patch as well as the current code to reduce redundancy.
Redandunt. If you say "add", we know the function is "new" ;-)
State what that function does instead of a vague "reduce redundancy", perhaps like...
Refactor the loop to parse list of commits given from the command line into a separate helper function.
The patch itself looks good.
Thanks.
PATCH v3 2/3 Add function per_branch_config.
In reply to Jared HanceAdds a configuration function to be filled up more in the next patch.
--- builtin/merge.c | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-)
builtin/merge.c +20 -4
@@ -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;
@@ -518,8 +522,20 @@ static int git_merge_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
Re: [PATCH v3 2/3] Add function per_branch_config.
Quoted 3 lines · Jared Hance
[PATCH v3 2/3] Add function
per_branch_config.Adds a configuration function to be filled up more in the next patch.
Micronit: subject should be of the form
subsystem: do something really great
with no full stop. So the series might be, roughly:
- merge: introduce setup_merge_commit helper function
- merge: introduce per-branch configuration helper function
- merge: add support for merging from upstream by default
Commit message bodies tend to be in the imperative mood, just like the patches themselves.
Anyway, I like patch 1. One tiny nit on this one:
Quoted 5 lines
+++
b/builtin/merge.cbuiltin/merge.c +0
-0@@ -498,11 +498,15 @@ cleanup:strbuf_release (&bname); }
builtin/merge.c cleanup: · line 506
Quoted 10 lines
builtin/merge.c +5
-4-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 */
Alignment: isn't the "||" meant to be one space over?
Based on a quick grep[1], it seems the prevailing style is rather
if (!branch || prefixcmp(k, "branch.") || prefixcmp(k + 7, branch)) return 1;
with the || at the end of the line, though I'm not sure I care much. :)
With or without an extra space before the ||,
Thanks for a nice cleanup.
Jonathan
[1]
addup () {
sum=0
while read term
do
: $((sum = sum + term))
done
echo $sum
}$ git grep --cached -c -e '||$' | cut -d: -f2 | addup 730 $ git grep --cached -c -e ' ||' | cut -d: -f2 | addup 98 $ git grep --cached -c -e ' ||' | cut -d: -f2 | addup 79
Re: [PATCH v3 2/3] Add function per_branch_config.
In reply to Jared HanceQuoted 1 line · Jared Hance
Adds a configuration function to be filled up more in the next patch.
Again, "to be filled up more" is not the primary purpose of the new function that was refactored from the existing one. State what it does, and what it is for instead, perhaps like...
Split a part of the configuration callback into a separate function to handle per-branch configuration.
PATCH v3 3/3 Add support for merging from upstream by default.
In reply to Jared HanceAdds 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 | 31 +++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-)
Documentation/config.txt +6 -0
@@ -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 -8
@@ -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 },
@@ -525,6 +527,8 @@ static int per_branch_config(const char *k, const char *v, void *cb)return 0 ; } +else if (!strcmp (variable,".merge" )) +return git_config_string (&upstream_branch, k, v);return 1 ;/* not what I handle */ }
@@ -536,7 +540,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 +1023,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); +else +setup_merge_commit (&buf, &remotes, upstream_branch); + }/* * This could be traditional "merge <msg> HEAD <commit>..." and
@@ -1082,9 +1092,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
Documentation/config.txt man.<tool>.path:: · line 1392
Quoted 7 lines · Jared Hance
Documentation/config.txt +1
-0@@ -1389,6 +1389,12 @@ man.<tool>.path:: include::merge-config.txt[] +merge.defaultUpstream::
[...]
Might make sense to put this in merge-config.txt, to get documentation in git-merge(1) for free.
builtin/merge.c static int per_branch_config(const char *k, const char *v, void *cb) · line 531
Quoted 8 lines
builtin/merge.c +2
-0@@ -525,6 +527,8 @@ static int per_branch_config(const char *k, const char *v, void *cb)return 0 ; } +else if (!strcmp (variable,".merge" )) +return git_config_string (&upstream_branch, k, v);
Style:
} else if (...) {
return git_config_string(...);
}builtin/merge.c int cmd_merge(int argc, const char **argv, const char *prefix) · line 1031
Quoted 13 lines
builtin/merge.c +6
-3@@ -1017,9 +1023,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); +else +setup_merge_commit (&buf, &remotes, upstream_branch);
Why the "else"? usage_msg_opt doesn't return to the caller.
Hopefully someone will chime in with some tests to complement t5520-pull (hint, hint). :)
Jonathan
Documentation/git-merge.txt +5 -2
@@ -11,7 +11,7 @@ SYNOPSIS [verse] 'git merge' [-n] [--stat] [--no-commit] [--squash] [-s <strategy>] [-X <strategy-option>] - [--[no-]rerere-autoupdate] [-m <msg>] <commit>... + [--[no-]rerere-autoupdate] [-m <msg>] [<commit>...] 'git merge' <msg> HEAD <commit>... 'git merge' --abort
@@ -97,7 +97,10 @@ commit or stash your changes before running 'git merge'. Commits, usually other branch heads, to merge into our branch. You need at least one <commit>. Specifying more than one <commit> obviously means you are trying an Octopus. - ++ +By default, if no commit is specified then git will error out. +However, the `merge.defaultToUpstream` configuration item (see below) +changes that. PRE-MERGE CHECKS ----------------
Re: [PATCH v3 0/3] Updated patch series for default upstream merge
In reply to Jared HanceQuoted 3 lines · Jared Hance
Notes/Complications:
- I'm not sure whether the option should be merge.defaultUpstream or merge.defaultToUpstream
Probably the latter.
Quoted 2 lines
- Should [remotes] be changed to [branches]? I felt like it was a completely different change and didn't belong in the patch series.
Perhaps at the beginning (just like your 1/3 refactoring) or at the end as a separate patch?
Quoted 2 lines
- I left one of the ifs with unnecessary braces for clarity because of a nested if-else: is this the preferred style?
If you are talking about the one at @@ -1017,9 +1023,13 @@, it looks fine to me. The new "else" you added to @@ -525,6 +527,8 @@ may probably want to start on the same line as closing "}" of the "if", though. IOW, like this:
if (cond) {
...
} else {
...
}not like this:
if (cond) {
...
}
else {
...
}But other than that looks reasonable.
Thanks.
Quoted 2 lines · Jared Hance
Adds the option merge.defaultupstream to add support for merging from the upstream branch by default.
Could you give an example of breakage this configurability is designed to prevent?
Not that it is a bad idea to be careful anyway, mind you --- just looking for a clear answer for when people ask "and why would I ever want to set merge.defaultToUpstream to false?"
Quoted 1 line
reduce reduncancy and impove clarity.
redundancy, improve :)
Documentation/config.txt man.<tool>.path:: · line 1395
Quoted 10 lines
Documentation/config.txt +4
-0@@ -1389,6 +1389,11 @@ 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.
I'd prefer to say "merge from the branch configured with --track or --set-upstream" (but that's just me).
Documentation/config.txt struct strategy { · line 42
Quoted 9 lines
Documentation/config.txt +2
-1+++ 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
Side note: these should probably say "<commit>" or "<branch>" rather than "<remote>". I'm guessing the usage string comes from the days before the separate-remotes ref layout...
Quoted 1 line
Documentation/config.txt +0
-0@@ -58,6 +58,8 @@ static int option_renormalize;
[...]
Documentation/config.txt
Quoted 10 lines
Documentation/config.txt +5
-4-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; /* ignore me */
Style: missing space after "if" keyword.
Clarity: we are not supposed to _ignore_ the non-branch.* configuration, just leave it for other functions to handle, no? Maybe the comment should say "let others handle it" or something?
Documentation/config.txt
Quoted 2 lines
Documentation/config.txt +2
-0+ + variable = k + 7 + strlen(branch);
'7' can be written in a more self-explanatory way as 'strlen("branch.")' and the optimizer will take care of translating it to 7. Don't worry about it if that makes the diff or code harder to read, though; I'm just mentioning the trick for future reference.
Documentation/config.txt
Quoted 1 line
Documentation/config.txt +1
-0+ if(!strcmp(variable, ".mergeoptions")) {
Style: missing space after "if" keyword.
Documentation/config.txt static int git_merge_config(const char *k, const char *v, void *cb) · line 528
Quoted 6 lines
Documentation/config.txt +2
-0@@ -518,9 +524,26 @@ static int git_merge_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;
I'd group the cleanup with the return.
parse_options(...); free(buf); return 0;
Documentation/config.txt static int git_merge_config(const char *k, const char *v, void *cb) · line 530
Quoted 2 lines
Documentation/config.txt +1
-0} + else if(strcmp(variable, ".merge")) {
Style: missing space after "if". Uncuddled brace.
[...]
Quoted 11 lines
Documentation/config.txt +7
-0@@ -911,6 +934,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);
Documentation/config.txt static int evaluate_result(void) · line 953
Quoted 10 lines
Documentation/config.txt +10
-0+ 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); +}
Would be easier to review if this code movement were in a separate patch (separating cleanup from semantic changes).
Documentation/config.txt int cmd_merge(int argc, const char **argv, const char *prefix) · line 1028
Quoted 9 lines
Documentation/config.txt +2
-3@@ -983,9 +1024,15 @@ 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) {
Style: missing space after "if". Unnecessary braces. To avoid keeping the reader in suspense, it's usually best to handle the (simple) error case first, like so:
if (!default_upstream || !upstream_branch) usage_with_options(...); setup_merge_commit(...);
[...]
Documentation/config.txt int cmd_merge(int argc, const char **argv, const char *prefix) · line 1098
Quoted 6 lines
Documentation/config.txt +1
-1@@ -1048,7 +1095,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) } } - if (head_invalid || !argc) + if (head_invalid || (!argc && !(default_upstream && upstream_branch)))
Might be clearer to split the line?
if (head_invalid || (!argc && (!default_upstream || !upstream_branch))) usage_with_options(...);
Even better would be to use descriptive messages, like so:
if (head_invalid)
usage_msg_opt("cannot use old-style invocation from an unborn branch",
...);
if (!argc && ...)
usage_msg_opt("no commit to merge specified", ...);Thanks for making this happen. :)
Jonathan
Quoted 3 lines · Jonathan Nieder
Quoted 1 line · Jared Hance
Quoted 1 line
Subject: Re: [PATCH v2] Add support for merging from upstream by default.
Drop full-stop '.' at the end.
Quoted 5 lines
Quoted 2 lines
Adds the option merge.defaultupstream to add support for merging from the upstream branch by default.
Could you give an example of breakage this configurability is designed to prevent?
I think there is no "prevent" or "breakage"; the patch is to give people a way to turn the feature on; without the configuration, "git merge" will keep the traditional behaviour, no?
Quoted 4 lines
Quoted 4 lines
+++
b/builtin/merge.c(quoted diff) +0
-0@@ -37,7 +37,7 @@ struct strategy { };
Quoted 9 lines
builtin/merge.c struct strategy { · line 42
Quoted 5 lines
builtin/merge.c +1
-1static const char *const builtin_merge_usage[] = { -"git merge [options] <remote>..." , +"git merge [options] [<remote>...]" ,"git merge [options] <msg> HEAD <remote>" ,NULL Side note: these should probably say "<commit>" or "<branch>" rather than "<remote>". I'm guessing the usage string comes from the days before the separate-remotes ref layout...
Yes, your guess is correct.
Quoted 16 lines
Quoted 16 lines
builtin/merge.c +12
-0@@ -911,6 +934,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;
Quoted 8 lines
builtin/merge.c static int evaluate_result(void) · line 953
Quoted 5 lines
builtin/merge.c +5
-0+ +strbuf_addf (buf,"GITHEAD_%s" ,sha1_to_hex (o->sha1)); +setenv (buf->buf, s,1 ); +strbuf_reset (buf); +}Would be easier to review if this code movement were in a separate patch (separating cleanup from semantic changes).
Probably. It is a very good idea to move this code out to its own helper function, nevertheless; I like this part of the patch.
Quoted 7 lines
Even better would be to use descriptive messages, like so:
if (
head_invalid)usage_msg_opt("cannot use old-style invocation from an unborn branch", ...); if (!argc && ...) usage_msg_opt("no commit to merge specified", ...);
Much better.
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Jonathan Nieder
Could you give an example of breakage this configurability is designed to prevent?
I think there is no "prevent" or "breakage"; the patch is to give people a way to turn the feature on; without the configuration, "git merge" will keep the traditional behaviour, no?
Yes. One answer to my question is that a person might try
git fetch git://some/random/repository branch git merge
and at least for now it seems sensible to make that error out by default rather than doing something unexpected.
In other words, another possible rule is "default to the last fetched branch" (FETCH_HEAD), so the intent behind running "git merge" without a branchname for the first time is not as obvious as I would hope.
Quoted 2 lines · Jared Hance
Adds the option merge.defaultupstream to add support for merging from the
Nit: I agree with Jeff[1] that this should be called merge.defaultToUpstream
[1] http://article.gmane.org/gmane.comp.version-control.git/165745
j.