Skip to content

Commit 73cf7f7

Browse files
jrngitster
authored andcommitted
ll-merge: make flag easier to populate
ll_merge() takes its options in a flag word, which has a few advantages: - options flags can be cheaply passed around in registers, while an option struct passed by pointer cannot; - callers can easily pass 0 without trouble for no options, while an option struct passed by value would not allow that. The downside is that code to populate and access the flag word can be somewhat opaque. Mitigate that with a few macros. Cc: Avery Pennarun <[email protected]> Cc: Bert Wesarg <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24d113e commit 73cf7f7

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Documentation/technical/api-merge.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ supports this.
4949

5050
The `flag` parameter is a bitfield:
5151

52-
- The least significant bit indicates whether this is an internal
53-
merge to consolidate ancestors for a recursive merge.
52+
- The `LL_OPT_VIRTUAL_ANCESTOR` bit indicates whether this is an
53+
internal merge to consolidate ancestors for a recursive merge.
5454

55-
- The next two bits allow local conflicts to be automatically
55+
- The `LL_OPT_FAVOR_MASK` bits allow local conflicts to be automatically
5656
resolved in favor of one side or the other (as in 'git merge-file'
57-
`--ours`/`--theirs`/`--union` for 01, 10, and 11, respectively).
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`.
5861

5962
Everything else
6063
---------------

ll-merge.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
4646
* or common ancestor for an internal merge. Still return
4747
* "conflicted merge" status.
4848
*/
49-
mmfile_t *stolen = (flag & 01) ? orig : src1;
49+
mmfile_t *stolen = (flag & LL_OPT_VIRTUAL_ANCESTOR) ? orig : src1;
5050

5151
result->ptr = stolen->ptr;
5252
result->size = stolen->size;
@@ -79,7 +79,7 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
7979

8080
memset(&xmp, 0, sizeof(xmp));
8181
xmp.level = XDL_MERGE_ZEALOUS;
82-
xmp.favor= (flag >> 1) & 03;
82+
xmp.favor = ll_opt_favor(flag);
8383
if (git_xmerge_style >= 0)
8484
xmp.style = git_xmerge_style;
8585
if (marker_size > 0)
@@ -99,7 +99,8 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
9999
int flag, int marker_size)
100100
{
101101
/* Use union favor */
102-
flag = (flag & 1) | (XDL_MERGE_FAVOR_UNION << 1);
102+
flag = (flag & LL_OPT_VIRTUAL_ANCESTOR) |
103+
create_ll_flag(XDL_MERGE_FAVOR_UNION);
103104
return ll_xdl_merge(drv_unused, result, path_unused,
104105
orig, NULL, src1, NULL, src2, NULL,
105106
flag, marker_size);
@@ -342,7 +343,7 @@ int ll_merge(mmbuffer_t *result_buf,
342343
const char *ll_driver_name = NULL;
343344
int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
344345
const struct ll_merge_driver *driver;
345-
int virtual_ancestor = flag & 01;
346+
int virtual_ancestor = flag & LL_OPT_VIRTUAL_ANCESTOR;
346347

347348
if (merge_renormalize) {
348349
normalize_file(ancestor, path);

ll-merge.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
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+
12+
static inline int ll_opt_favor(int flag)
13+
{
14+
return (flag & LL_OPT_FAVOR_MASK) >> LL_OPT_FAVOR_SHIFT;
15+
}
16+
17+
static inline int create_ll_flag(int favor)
18+
{
19+
return ((favor << LL_OPT_FAVOR_SHIFT) & LL_OPT_FAVOR_MASK);
20+
}
21+
822
int ll_merge(mmbuffer_t *result_buf,
923
const char *path,
1024
mmfile_t *ancestor, const char *ancestor_label,

merge-recursive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ static int merge_3way(struct merge_options *o,
647647

648648
merge_status = ll_merge(result_buf, a->path, &orig, base_name,
649649
&src1, name1, &src2, name2,
650-
(!!o->call_depth) | (favor << 1));
650+
((o->call_depth ? LL_OPT_VIRTUAL_ANCESTOR : 0) |
651+
create_ll_flag(favor)));
651652

652653
free(name1);
653654
free(name2);

0 commit comments

Comments
 (0)