Skip to content

Commit ffc5e3c

Browse files
committed
Merge branch 'jc/merge-sans-branch'
* jc/merge-sans-branch: merge: merge with the default upstream branch without argument merge: match the help text with the documentation Conflicts: builtin/merge.c
2 parents b966427 + 93e535a commit ffc5e3c

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Documentation/git-merge.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git merge' [-n] [--stat] [--no-commit] [--squash]
1313
[-s <strategy>] [-X <strategy-option>]
14-
[--[no-]rerere-autoupdate] [-m <msg>] <commit>...
14+
[--[no-]rerere-autoupdate] [-m <msg>] [<commit>...]
1515
'git merge' <msg> HEAD <commit>...
1616
'git merge' --abort
1717

@@ -95,8 +95,13 @@ commit or stash your changes before running 'git merge'.
9595

9696
<commit>...::
9797
Commits, usually other branch heads, to merge into our branch.
98-
You need at least one <commit>. Specifying more than one
99-
<commit> obviously means you are trying an Octopus.
98+
Specifying more than one commit will create a merge with
99+
more than two parents (affectionately called an Octopus merge).
100+
+
101+
If no commit is given from the command line, and if `merge.defaultToUpstream`
102+
configuration variable is set, merge the remote tracking branches
103+
that the current branch is configured to use as its upstream.
104+
See also the configuration section of this manual page.
100105

101106

102107
PRE-MERGE CHECKS

Documentation/merge-config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ merge.conflictstyle::
66
a `>>>>>>>` marker. An alternate style, "diff3", adds a `|||||||`
77
marker and the original text before the `=======` marker.
88

9+
merge.defaultToUpstream::
10+
If merge is called without any commit argument, merge the upstream
11+
branches configured for the current branch by using their last
12+
observed values stored in their remote tracking branches.
13+
The values of the `branch.<current branch>.merge` that name the
14+
branches at the remote named by `branch.<current branch>.remote`
15+
are consulted, and then they are mapped via `remote.<remote>.fetch`
16+
to their corresponding remote tracking branches, and the tips of
17+
these tracking branches are merged.
18+
919
merge.log::
1020
In addition to branch names, populate the log message with at
1121
most the specified number of one-line descriptions from the

builtin/merge.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "help.h"
2626
#include "merge-recursive.h"
2727
#include "resolve-undo.h"
28+
#include "remote.h"
2829

2930
#define DEFAULT_TWOHEAD (1<<0)
3031
#define DEFAULT_OCTOPUS (1<<1)
@@ -37,8 +38,9 @@ struct strategy {
3738
};
3839

3940
static const char * const builtin_merge_usage[] = {
40-
"git merge [options] <remote>...",
41-
"git merge [options] <msg> HEAD <remote>",
41+
"git merge [options] [<commit>...]",
42+
"git merge [options] <msg> HEAD <commit>",
43+
"git merge --abort",
4244
NULL
4345
};
4446

@@ -59,6 +61,7 @@ static int verbosity;
5961
static int allow_rerere_auto;
6062
static int abort_current_merge;
6163
static int show_progress = -1;
64+
static int default_to_upstream;
6265

6366
static struct strategy all_strategy[] = {
6467
{ "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -538,6 +541,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
538541
if (is_bool && shortlog_len)
539542
shortlog_len = DEFAULT_MERGE_LOG_LEN;
540543
return 0;
544+
} else if (!strcmp(k, "merge.defaulttoupstream")) {
545+
default_to_upstream = git_config_bool(k, v);
546+
return 0;
541547
}
542548
return git_diff_ui_config(k, v, cb);
543549
}
@@ -945,6 +951,35 @@ static int evaluate_result(void)
945951
return cnt;
946952
}
947953

954+
/*
955+
* Pretend as if the user told us to merge with the tracking
956+
* branch we have for the upstream of the current branch
957+
*/
958+
static int setup_with_upstream(const char ***argv)
959+
{
960+
struct branch *branch = branch_get(NULL);
961+
int i;
962+
const char **args;
963+
964+
if (!branch)
965+
die("No current branch.");
966+
if (!branch->remote)
967+
die("No remote for the current branch.");
968+
if (!branch->merge_nr)
969+
die("No default upstream defined for the current branch.");
970+
971+
args = xcalloc(branch->merge_nr + 1, sizeof(char *));
972+
for (i = 0; i < branch->merge_nr; i++) {
973+
if (!branch->merge[i]->dst)
974+
die("No remote tracking branch for %s from %s",
975+
branch->merge[i]->src, branch->remote_name);
976+
args[i] = branch->merge[i]->dst;
977+
}
978+
args[i] = NULL;
979+
*argv = args;
980+
return i;
981+
}
982+
948983
int cmd_merge(int argc, const char **argv, const char *prefix)
949984
{
950985
unsigned char result_tree[20];
@@ -1027,6 +1062,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10271062
if (!allow_fast_forward && fast_forward_only)
10281063
die(_("You cannot combine --no-ff with --ff-only."));
10291064

1065+
if (!argc && !abort_current_merge && default_to_upstream)
1066+
argc = setup_with_upstream(&argv);
1067+
10301068
if (!argc)
10311069
usage_with_options(builtin_merge_usage,
10321070
builtin_merge_options);

0 commit comments

Comments
 (0)