Skip to content

Commit 75b17fe

Browse files
committed
Merge branch 'jf/merge-ignore-ws'
* jf/merge-ignore-ws: merge-recursive: options to ignore whitespace changes merge-recursive --patience ll-merge: replace flag argument with options struct merge-recursive: expose merge options for builtin merge
2 parents 5a3a484 + 4e5dd04 commit 75b17fe

File tree

12 files changed

+351
-97
lines changed

12 files changed

+351
-97
lines changed

Documentation/merge-strategies.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ the other tree did, declaring 'our' history contains all that happened in it.
4040
theirs;;
4141
This is opposite of 'ours'.
4242

43+
patience;;
44+
With this option, 'merge-recursive' spends a little extra time
45+
to avoid mismerges that sometimes occur due to unimportant
46+
matching lines (e.g., braces from distinct functions). Use
47+
this when the branches to be merged have diverged wildly.
48+
See also linkgit:git-diff[1] `--patience`.
49+
50+
ignore-space-change;;
51+
ignore-all-space;;
52+
ignore-space-at-eol;;
53+
Treats lines with the indicated type of whitespace change as
54+
unchanged for the sake of a three-way merge. Whitespace
55+
changes mixed with other changes to a line are not ignored.
56+
See also linkgit:git-diff[1] `-b`, `-w`, and
57+
`--ignore-space-at-eol`.
58+
+
59+
* If 'their' version only introduces whitespace changes to a line,
60+
'our' version is used;
61+
* If 'our' version introduces whitespace changes but 'their'
62+
version includes a substantial change, 'their' version is used;
63+
* Otherwise, the merge proceeds in the usual way.
64+
4365
renormalize;;
4466
This runs a virtual check-out and check-in of all three stages
4567
of a file when resolving a three-way merge. This option is

Documentation/technical/api-merge.txt

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@ responsible for a few things.
1717
path-specific merge drivers (specified in `.gitattributes`)
1818
into account.
1919

20+
Data structures
21+
---------------
22+
23+
* `mmbuffer_t`, `mmfile_t`
24+
25+
These store data usable for use by the xdiff backend, for writing and
26+
for reading, respectively. See `xdiff/xdiff.h` for the definitions
27+
and `diff.c` for examples.
28+
29+
* `struct ll_merge_options`
30+
31+
This describes the set of options the calling program wants to affect
32+
the operation of a low-level (single file) merge. Some options:
33+
34+
`virtual_ancestor`::
35+
Behave as though this were part of a merge between common
36+
ancestors in a recursive merge.
37+
If a helper program is specified by the
38+
`[merge "<driver>"] recursive` configuration, it will
39+
be used (see linkgit:gitattributes[5]).
40+
41+
`variant`::
42+
Resolve local conflicts automatically in favor
43+
of one side or the other (as in 'git merge-file'
44+
`--ours`/`--theirs`/`--union`). Can be `0`,
45+
`XDL_MERGE_FAVOR_OURS`, `XDL_MERGE_FAVOR_THEIRS`, or
46+
`XDL_MERGE_FAVOR_UNION`.
47+
48+
`renormalize`::
49+
Resmudge and clean the "base", "theirs" and "ours" files
50+
before merging. Use this when the merge is likely to have
51+
overlapped with a change in smudge/clean or end-of-line
52+
normalization rules.
53+
2054
Low-level (single file) merge
2155
-----------------------------
2256

@@ -28,15 +62,24 @@ Low-level (single file) merge
2862
`.git/info/attributes` into account. Returns 0 for a
2963
clean merge.
3064

31-
The caller:
65+
Calling sequence:
3266

33-
1. allocates an mmbuffer_t variable for the result;
34-
2. allocates and fills variables with the file's original content
35-
and two modified versions (using `read_mmfile`, for example);
36-
3. calls ll_merge();
37-
4. reads the output from result_buf.ptr and result_buf.size;
38-
5. releases buffers when finished (free(ancestor.ptr); free(ours.ptr);
39-
free(theirs.ptr); free(result_buf.ptr);).
67+
* Prepare a `struct ll_merge_options` to record options.
68+
If you have no special requests, skip this and pass `NULL`
69+
as the `opts` parameter to use the default options.
70+
71+
* Allocate an mmbuffer_t variable for the result.
72+
73+
* Allocate and fill variables with the file's original content
74+
and two modified versions (using `read_mmfile`, for example).
75+
76+
* Call `ll_merge()`.
77+
78+
* Read the merged content from `result_buf.ptr` and `result_buf.size`.
79+
80+
* Release buffers when finished. A simple
81+
`free(ancestor.ptr); free(ours.ptr); free(theirs.ptr);
82+
free(result_buf.ptr);` will do.
4083

4184
If the modifications do not merge cleanly, `ll_merge` will return a
4285
nonzero value and `result_buf` will generally include a description of
@@ -47,18 +90,6 @@ The `ancestor_label`, `our_label`, and `their_label` parameters are
4790
used to label the different sides of a conflict if the merge driver
4891
supports this.
4992

50-
The `flag` parameter is a bitfield:
51-
52-
- The `LL_OPT_VIRTUAL_ANCESTOR` bit indicates whether this is an
53-
internal merge to consolidate ancestors for a recursive merge.
54-
55-
- The `LL_OPT_FAVOR_MASK` bits allow local conflicts to be automatically
56-
resolved in favor of one side or the other (as in 'git merge-file'
57-
`--ours`/`--theirs`/`--union`).
58-
They can be populated by `create_ll_flag`, whose argument can be
59-
`XDL_MERGE_FAVOR_OURS`, `XDL_MERGE_FAVOR_THEIRS`, or
60-
`XDL_MERGE_FAVOR_UNION`.
61-
6293
Everything else
6394
---------------
6495

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static int checkout_merged(int pos, struct checkout *state)
161161
* merge.renormalize set, too
162162
*/
163163
status = ll_merge(&result_buf, path, &ancestor, "base",
164-
&ours, "ours", &theirs, "theirs", 0);
164+
&ours, "ours", &theirs, "theirs", NULL);
165165
free(ancestor.ptr);
166166
free(ours.ptr);
167167
free(theirs.ptr);

builtin/merge-recursive.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "commit.h"
33
#include "tag.h"
44
#include "merge-recursive.h"
5+
#include "xdiff-interface.h"
56

67
static const char builtin_merge_recursive_usage[] =
78
"git %s <base>... -- <head> <remote> ...";
@@ -40,19 +41,7 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
4041
if (!prefixcmp(arg, "--")) {
4142
if (!arg[2])
4243
break;
43-
if (!strcmp(arg+2, "ours"))
44-
o.recursive_variant = MERGE_RECURSIVE_OURS;
45-
else if (!strcmp(arg+2, "theirs"))
46-
o.recursive_variant = MERGE_RECURSIVE_THEIRS;
47-
else if (!strcmp(arg+2, "subtree"))
48-
o.subtree_shift = "";
49-
else if (!prefixcmp(arg+2, "subtree="))
50-
o.subtree_shift = arg + 10;
51-
else if (!strcmp(arg+2, "renormalize"))
52-
o.renormalize = 1;
53-
else if (!strcmp(arg+2, "no-renormalize"))
54-
o.renormalize = 0;
55-
else
44+
if (parse_merge_opt(&o, arg + 2))
5645
die("Unknown option %s", arg);
5746
continue;
5847
}

builtin/merge.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -639,25 +639,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
639639

640640
o.renormalize = option_renormalize;
641641

642-
/*
643-
* NEEDSWORK: merge with table in builtin/merge-recursive
644-
*/
645-
for (x = 0; x < xopts_nr; x++) {
646-
if (!strcmp(xopts[x], "ours"))
647-
o.recursive_variant = MERGE_RECURSIVE_OURS;
648-
else if (!strcmp(xopts[x], "theirs"))
649-
o.recursive_variant = MERGE_RECURSIVE_THEIRS;
650-
else if (!strcmp(xopts[x], "subtree"))
651-
o.subtree_shift = "";
652-
else if (!prefixcmp(xopts[x], "subtree="))
653-
o.subtree_shift = xopts[x]+8;
654-
else if (!strcmp(xopts[x], "renormalize"))
655-
o.renormalize = 1;
656-
else if (!strcmp(xopts[x], "no-renormalize"))
657-
o.renormalize = 0;
658-
else
642+
for (x = 0; x < xopts_nr; x++)
643+
if (parse_merge_opt(&o, xopts[x]))
659644
die("Unknown option for merge-recursive: -X%s", xopts[x]);
660-
}
661645

662646
o.branch1 = head_arg;
663647
o.branch2 = remoteheads->item->util;

ll-merge.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
1818
mmfile_t *orig, const char *orig_name,
1919
mmfile_t *src1, const char *name1,
2020
mmfile_t *src2, const char *name2,
21-
int flag,
21+
const struct ll_merge_options *opts,
2222
int marker_size);
2323

2424
struct ll_merge_driver {
@@ -39,14 +39,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
3939
mmfile_t *orig, const char *orig_name,
4040
mmfile_t *src1, const char *name1,
4141
mmfile_t *src2, const char *name2,
42-
int flag, int marker_size)
42+
const struct ll_merge_options *opts,
43+
int marker_size)
4344
{
45+
mmfile_t *stolen;
46+
assert(opts);
47+
4448
/*
4549
* The tentative merge result is "ours" for the final round,
4650
* or common ancestor for an internal merge. Still return
4751
* "conflicted merge" status.
4852
*/
49-
mmfile_t *stolen = (flag & LL_OPT_VIRTUAL_ANCESTOR) ? orig : src1;
53+
stolen = opts->virtual_ancestor ? orig : src1;
5054

5155
result->ptr = stolen->ptr;
5256
result->size = stolen->size;
@@ -60,9 +64,11 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
6064
mmfile_t *orig, const char *orig_name,
6165
mmfile_t *src1, const char *name1,
6266
mmfile_t *src2, const char *name2,
63-
int flag, int marker_size)
67+
const struct ll_merge_options *opts,
68+
int marker_size)
6469
{
6570
xmparam_t xmp;
71+
assert(opts);
6672

6773
if (buffer_is_binary(orig->ptr, orig->size) ||
6874
buffer_is_binary(src1->ptr, src1->size) ||
@@ -74,12 +80,13 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
7480
orig, orig_name,
7581
src1, name1,
7682
src2, name2,
77-
flag, marker_size);
83+
opts, marker_size);
7884
}
7985

8086
memset(&xmp, 0, sizeof(xmp));
8187
xmp.level = XDL_MERGE_ZEALOUS;
82-
xmp.favor = ll_opt_favor(flag);
88+
xmp.favor = opts->variant;
89+
xmp.xpp.flags = opts->xdl_opts;
8390
if (git_xmerge_style >= 0)
8491
xmp.style = git_xmerge_style;
8592
if (marker_size > 0)
@@ -96,15 +103,17 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
96103
mmfile_t *orig, const char *orig_name,
97104
mmfile_t *src1, const char *name1,
98105
mmfile_t *src2, const char *name2,
99-
int flag, int marker_size)
106+
const struct ll_merge_options *opts,
107+
int marker_size)
100108
{
101109
/* Use union favor */
102-
flag &= ~LL_OPT_FAVOR_MASK;
103-
flag |= create_ll_flag(XDL_MERGE_FAVOR_UNION);
110+
struct ll_merge_options o;
111+
assert(opts);
112+
o = *opts;
113+
o.variant = XDL_MERGE_FAVOR_UNION;
104114
return ll_xdl_merge(drv_unused, result, path_unused,
105115
orig, NULL, src1, NULL, src2, NULL,
106-
flag, marker_size);
107-
return 0;
116+
&o, marker_size);
108117
}
109118

110119
#define LL_BINARY_MERGE 0
@@ -136,14 +145,16 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
136145
mmfile_t *orig, const char *orig_name,
137146
mmfile_t *src1, const char *name1,
138147
mmfile_t *src2, const char *name2,
139-
int flag, int marker_size)
148+
const struct ll_merge_options *opts,
149+
int marker_size)
140150
{
141151
char temp[4][50];
142152
struct strbuf cmd = STRBUF_INIT;
143153
struct strbuf_expand_dict_entry dict[5];
144154
const char *args[] = { NULL, NULL };
145155
int status, fd, i;
146156
struct stat st;
157+
assert(opts);
147158

148159
dict[0].placeholder = "O"; dict[0].value = temp[0];
149160
dict[1].placeholder = "A"; dict[1].value = temp[1];
@@ -337,15 +348,21 @@ int ll_merge(mmbuffer_t *result_buf,
337348
mmfile_t *ancestor, const char *ancestor_label,
338349
mmfile_t *ours, const char *our_label,
339350
mmfile_t *theirs, const char *their_label,
340-
int flag)
351+
const struct ll_merge_options *opts)
341352
{
342353
static struct git_attr_check check[2];
343354
const char *ll_driver_name = NULL;
344355
int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
345356
const struct ll_merge_driver *driver;
346-
int virtual_ancestor = flag & LL_OPT_VIRTUAL_ANCESTOR;
347357

348-
if (flag & LL_OPT_RENORMALIZE) {
358+
if (!opts) {
359+
struct ll_merge_options default_opts = {0};
360+
return ll_merge(result_buf, path, ancestor, ancestor_label,
361+
ours, our_label, theirs, their_label,
362+
&default_opts);
363+
}
364+
365+
if (opts->renormalize) {
349366
normalize_file(ancestor, path);
350367
normalize_file(ours, path);
351368
normalize_file(theirs, path);
@@ -359,11 +376,11 @@ int ll_merge(mmbuffer_t *result_buf,
359376
}
360377
}
361378
driver = find_ll_merge_driver(ll_driver_name);
362-
if (virtual_ancestor && driver->recursive)
379+
if (opts->virtual_ancestor && driver->recursive)
363380
driver = find_ll_merge_driver(driver->recursive);
364381
return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
365382
ours, our_label, theirs, their_label,
366-
flag, marker_size);
383+
opts, marker_size);
367384
}
368385

369386
int ll_merge_marker_size(const char *path)

ll-merge.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@
55
#ifndef LL_MERGE_H
66
#define LL_MERGE_H
77

8-
#define LL_OPT_VIRTUAL_ANCESTOR (1 << 0)
9-
#define LL_OPT_FAVOR_MASK ((1 << 1) | (1 << 2))
10-
#define LL_OPT_FAVOR_SHIFT 1
11-
#define LL_OPT_RENORMALIZE (1 << 3)
12-
13-
static inline int ll_opt_favor(int flag)
14-
{
15-
return (flag & LL_OPT_FAVOR_MASK) >> LL_OPT_FAVOR_SHIFT;
16-
}
17-
18-
static inline int create_ll_flag(int favor)
19-
{
20-
return ((favor << LL_OPT_FAVOR_SHIFT) & LL_OPT_FAVOR_MASK);
21-
}
8+
struct ll_merge_options {
9+
unsigned virtual_ancestor : 1;
10+
unsigned variant : 2; /* favor ours, favor theirs, or union merge */
11+
unsigned renormalize : 1;
12+
long xdl_opts;
13+
};
2214

2315
int ll_merge(mmbuffer_t *result_buf,
2416
const char *path,
2517
mmfile_t *ancestor, const char *ancestor_label,
2618
mmfile_t *ours, const char *our_label,
2719
mmfile_t *theirs, const char *their_label,
28-
int flag);
20+
const struct ll_merge_options *opts);
2921

3022
int ll_merge_marker_size(const char *path);
3123

merge-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our
3737
* common ancestor.
3838
*/
3939
merge_status = ll_merge(&res, path, base, NULL,
40-
our, ".our", their, ".their", 0);
40+
our, ".our", their, ".their", NULL);
4141
if (merge_status < 0)
4242
return NULL;
4343

0 commit comments

Comments
 (0)