@matheusbsbs / matheus-bagliano / commits / 73cf7f713d
Fork 0

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 <apenwarr@gmail.com> Cc: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Aug 5, 2010 at 06:17 UTC 73cf7f713da4fc797e2393a9e490ad4ec9466c53
4 files changed +28 -9
Documentation/technical/api-merge.txt
+7 -4
@@ -49,12 +49,15 @@ supports this.
49
50 The `flag` parameter is a bitfield:
51
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.
54
55 - - The next two bits allow local conflicts to be automatically
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` 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`.
61
62 Everything else
63 ---------------
ll-merge.c
+5 -4
@@ -46,7 +46,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
46 * or common ancestor for an internal merge. Still return
47 * "conflicted merge" status.
48 */
49 - mmfile_t *stolen = (flag & 01) ? orig : src1;
49 + mmfile_t *stolen = (flag & LL_OPT_VIRTUAL_ANCESTOR) ? orig : src1;
50
51 result->ptr = stolen->ptr;
52 result->size = stolen->size;
@@ -79,7 +79,7 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
79
80 memset(&xmp, 0, sizeof(xmp));
81 xmp.level = XDL_MERGE_ZEALOUS;
82 - xmp.favor= (flag >> 1) & 03;
82 + xmp.favor = ll_opt_favor(flag);
83 if (git_xmerge_style >= 0)
84 xmp.style = git_xmerge_style;
85 if (marker_size > 0)
@@ -99,7 +99,8 @@ static int ll_union_merge(const struct ll_merge_driver *drv_unused,
99 int flag, int marker_size)
100 {
101 /* 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);
104 return ll_xdl_merge(drv_unused, result, path_unused,
105 orig, NULL, src1, NULL, src2, NULL,
106 flag, marker_size);
@@ -342,7 +343,7 @@ int ll_merge(mmbuffer_t *result_buf,
343 const char *ll_driver_name = NULL;
344 int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
345 const struct ll_merge_driver *driver;
345 - int virtual_ancestor = flag & 01;
346 + int virtual_ancestor = flag & LL_OPT_VIRTUAL_ANCESTOR;
347
348 if (merge_renormalize) {
349 normalize_file(ancestor, path);
ll-merge.h
+14
@@ -5,6 +5,20 @@
5 #ifndef LL_MERGE_H
6 #define LL_MERGE_H
7
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 +
22 int ll_merge(mmbuffer_t *result_buf,
23 const char *path,
24 mmfile_t *ancestor, const char *ancestor_label,
merge-recursive.c
+2 -1
@@ -647,7 +647,8 @@ static int merge_3way(struct merge_options *o,
647
648 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
649 &src1, name1, &src2, name2,
650 - (!!o->call_depth) | (favor << 1));
650 + ((o->call_depth ? LL_OPT_VIRTUAL_ANCESTOR : 0) |
651 + create_ll_flag(favor)));
652
653 free(name1);
654 free(name2);