7 #include "notes-merge.h"
9 struct notes_merge_pair
{
10 unsigned char obj
[20], base
[20], local
[20], remote
[20];
13 void init_notes_merge_options(struct notes_merge_options
*o
)
15 memset(o
, 0, sizeof(struct notes_merge_options
));
16 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
19 #define OUTPUT(o, v, ...) \
21 if ((o)->verbosity >= (v)) { \
22 printf(__VA_ARGS__); \
27 static int path_to_sha1(const char *path
, unsigned char *sha1
)
31 while (*path
&& i
< 40) {
33 hex_sha1
[i
++] = *path
;
38 return get_sha1_hex(hex_sha1
, sha1
);
41 static int verify_notes_filepair(struct diff_filepair
*p
, unsigned char *sha1
)
44 case DIFF_STATUS_MODIFIED
:
45 assert(p
->one
->mode
== p
->two
->mode
);
46 assert(!is_null_sha1(p
->one
->sha1
));
47 assert(!is_null_sha1(p
->two
->sha1
));
49 case DIFF_STATUS_ADDED
:
50 assert(is_null_sha1(p
->one
->sha1
));
52 case DIFF_STATUS_DELETED
:
53 assert(is_null_sha1(p
->two
->sha1
));
58 assert(!strcmp(p
->one
->path
, p
->two
->path
));
59 return path_to_sha1(p
->one
->path
, sha1
);
62 static struct notes_merge_pair
*find_notes_merge_pair_pos(
63 struct notes_merge_pair
*list
, int len
, unsigned char *obj
,
64 int insert_new
, int *occupied
)
67 * Both diff_tree_remote() and diff_tree_local() tend to process
68 * merge_pairs in ascending order. Therefore, cache last returned
69 * index, and search sequentially from there until the appropriate
72 * Since inserts only happen from diff_tree_remote() (which mainly
73 * _appends_), we don't care that inserting into the middle of the
74 * list is expensive (using memmove()).
76 static int last_index
;
77 int i
= last_index
< len
? last_index
: len
- 1;
78 int prev_cmp
= 0, cmp
= -1;
79 while (i
>= 0 && i
< len
) {
80 cmp
= hashcmp(obj
, list
[i
].obj
);
81 if (!cmp
) /* obj belongs @ i */
83 else if (cmp
< 0 && prev_cmp
<= 0) /* obj belongs < i */
85 else if (cmp
< 0) /* obj belongs between i-1 and i */
87 else if (cmp
> 0 && prev_cmp
>= 0) /* obj belongs > i */
89 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
97 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
103 if (insert_new
&& i
< len
) {
104 memmove(list
+ i
+ 1, list
+ i
,
105 (len
- i
) * sizeof(struct notes_merge_pair
));
106 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
113 static unsigned char uninitialized
[20] =
114 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
115 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
117 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
118 const unsigned char *base
,
119 const unsigned char *remote
,
122 struct diff_options opt
;
123 struct notes_merge_pair
*changes
;
126 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
127 sha1_to_hex(base
), sha1_to_hex(remote
));
130 DIFF_OPT_SET(&opt
, RECURSIVE
);
131 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
132 if (diff_setup_done(&opt
) < 0)
133 die("diff_setup_done failed");
134 diff_tree_sha1(base
, remote
, "", &opt
);
137 changes
= xcalloc(diff_queued_diff
.nr
, sizeof(struct notes_merge_pair
));
139 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
140 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
141 struct notes_merge_pair
*mp
;
143 unsigned char obj
[20];
145 if (verify_notes_filepair(p
, obj
)) {
146 trace_printf("\t\tCannot merge entry '%s' (%c): "
147 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
148 p
->status
, sha1_to_hex(p
->one
->sha1
),
149 sha1_to_hex(p
->two
->sha1
));
152 mp
= find_notes_merge_pair_pos(changes
, len
, obj
, 1, &occupied
);
154 /* We've found an addition/deletion pair */
155 assert(!hashcmp(mp
->obj
, obj
));
156 if (is_null_sha1(p
->one
->sha1
)) { /* addition */
157 assert(is_null_sha1(mp
->remote
));
158 hashcpy(mp
->remote
, p
->two
->sha1
);
159 } else if (is_null_sha1(p
->two
->sha1
)) { /* deletion */
160 assert(is_null_sha1(mp
->base
));
161 hashcpy(mp
->base
, p
->one
->sha1
);
163 assert(!"Invalid existing change recorded");
165 hashcpy(mp
->obj
, obj
);
166 hashcpy(mp
->base
, p
->one
->sha1
);
167 hashcpy(mp
->local
, uninitialized
);
168 hashcpy(mp
->remote
, p
->two
->sha1
);
171 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
172 sha1_to_hex(mp
->obj
), sha1_to_hex(mp
->base
),
173 sha1_to_hex(mp
->remote
));
176 diff_tree_release_paths(&opt
);
182 static void diff_tree_local(struct notes_merge_options
*o
,
183 struct notes_merge_pair
*changes
, int len
,
184 const unsigned char *base
,
185 const unsigned char *local
)
187 struct diff_options opt
;
190 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
191 len
, sha1_to_hex(base
), sha1_to_hex(local
));
194 DIFF_OPT_SET(&opt
, RECURSIVE
);
195 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
196 if (diff_setup_done(&opt
) < 0)
197 die("diff_setup_done failed");
198 diff_tree_sha1(base
, local
, "", &opt
);
201 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
202 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
203 struct notes_merge_pair
*mp
;
205 unsigned char obj
[20];
207 if (verify_notes_filepair(p
, obj
)) {
208 trace_printf("\t\tCannot merge entry '%s' (%c): "
209 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
210 p
->status
, sha1_to_hex(p
->one
->sha1
),
211 sha1_to_hex(p
->two
->sha1
));
214 mp
= find_notes_merge_pair_pos(changes
, len
, obj
, 0, &match
);
216 trace_printf("\t\tIgnoring local-only change for %s: "
217 "%.7s -> %.7s\n", sha1_to_hex(obj
),
218 sha1_to_hex(p
->one
->sha1
),
219 sha1_to_hex(p
->two
->sha1
));
223 assert(!hashcmp(mp
->obj
, obj
));
224 if (is_null_sha1(p
->two
->sha1
)) { /* deletion */
226 * Either this is a true deletion (1), or it is part
227 * of an A/D pair (2), or D/A pair (3):
229 * (1) mp->local is uninitialized; set it to null_sha1
230 * (2) mp->local is not uninitialized; don't touch it
231 * (3) mp->local is uninitialized; set it to null_sha1
232 * (will be overwritten by following addition)
234 if (!hashcmp(mp
->local
, uninitialized
))
236 } else if (is_null_sha1(p
->one
->sha1
)) { /* addition */
238 * Either this is a true addition (1), or it is part
239 * of an A/D pair (2), or D/A pair (3):
241 * (1) mp->local is uninitialized; set to p->two->sha1
242 * (2) mp->local is uninitialized; set to p->two->sha1
243 * (3) mp->local is null_sha1; set to p->two->sha1
245 assert(is_null_sha1(mp
->local
) ||
246 !hashcmp(mp
->local
, uninitialized
));
247 hashcpy(mp
->local
, p
->two
->sha1
);
248 } else { /* modification */
250 * This is a true modification. p->one->sha1 shall
251 * match mp->base, and mp->local shall be uninitialized.
252 * Set mp->local to p->two->sha1.
254 assert(!hashcmp(p
->one
->sha1
, mp
->base
));
255 assert(!hashcmp(mp
->local
, uninitialized
));
256 hashcpy(mp
->local
, p
->two
->sha1
);
258 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
259 sha1_to_hex(mp
->obj
), sha1_to_hex(mp
->base
),
260 sha1_to_hex(mp
->local
));
263 diff_tree_release_paths(&opt
);
266 static int merge_one_change(struct notes_merge_options
*o
,
267 struct notes_merge_pair
*p
, struct notes_tree
*t
)
270 * Return 0 if change was resolved (and added to notes_tree),
273 switch (o
->strategy
) {
274 case NOTES_MERGE_RESOLVE_MANUAL
:
276 case NOTES_MERGE_RESOLVE_OURS
:
277 OUTPUT(o
, 2, "Using local notes for %s", sha1_to_hex(p
->obj
));
280 case NOTES_MERGE_RESOLVE_THEIRS
:
281 OUTPUT(o
, 2, "Using remote notes for %s", sha1_to_hex(p
->obj
));
282 if (add_note(t
, p
->obj
, p
->remote
, combine_notes_overwrite
))
283 die("BUG: combine_notes_overwrite failed");
285 case NOTES_MERGE_RESOLVE_UNION
:
286 OUTPUT(o
, 2, "Concatenating local and remote notes for %s",
287 sha1_to_hex(p
->obj
));
288 if (add_note(t
, p
->obj
, p
->remote
, combine_notes_concatenate
))
289 die("failed to concatenate notes "
290 "(combine_notes_concatenate)");
293 die("Unknown strategy (%i).", o
->strategy
);
296 static int merge_changes(struct notes_merge_options
*o
,
297 struct notes_merge_pair
*changes
, int *num_changes
,
298 struct notes_tree
*t
)
300 int i
, conflicts
= 0;
302 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes
);
303 for (i
= 0; i
< *num_changes
; i
++) {
304 struct notes_merge_pair
*p
= changes
+ i
;
305 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
306 sha1_to_hex(p
->obj
), sha1_to_hex(p
->base
),
307 sha1_to_hex(p
->local
), sha1_to_hex(p
->remote
));
309 if (!hashcmp(p
->base
, p
->remote
)) {
310 /* no remote change; nothing to do */
311 trace_printf("\t\t\tskipping (no remote change)\n");
312 } else if (!hashcmp(p
->local
, p
->remote
)) {
313 /* same change in local and remote; nothing to do */
314 trace_printf("\t\t\tskipping (local == remote)\n");
315 } else if (!hashcmp(p
->local
, uninitialized
) ||
316 !hashcmp(p
->local
, p
->base
)) {
317 /* no local change; adopt remote change */
318 trace_printf("\t\t\tno local change, adopted remote\n");
319 if (add_note(t
, p
->obj
, p
->remote
,
320 combine_notes_overwrite
))
321 die("BUG: combine_notes_overwrite failed");
323 /* need file-level merge between local and remote */
324 trace_printf("\t\t\tneed content-level merge\n");
325 conflicts
+= merge_one_change(o
, p
, t
);
332 static int merge_from_diffs(struct notes_merge_options
*o
,
333 const unsigned char *base
,
334 const unsigned char *local
,
335 const unsigned char *remote
, struct notes_tree
*t
)
337 struct notes_merge_pair
*changes
;
338 int num_changes
, conflicts
;
340 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
341 "remote = %.7s)\n", sha1_to_hex(base
), sha1_to_hex(local
),
342 sha1_to_hex(remote
));
344 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
345 diff_tree_local(o
, changes
, num_changes
, base
, local
);
347 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
350 OUTPUT(o
, 4, "Merge result: %i unmerged notes and a %s notes tree",
351 conflicts
, t
->dirty
? "dirty" : "clean");
353 return conflicts
? -1 : 1;
356 void create_notes_commit(struct notes_tree
*t
, struct commit_list
*parents
,
357 const char *msg
, unsigned char *result_sha1
)
359 unsigned char tree_sha1
[20];
361 assert(t
->initialized
);
363 if (write_notes_tree(t
, tree_sha1
))
364 die("Failed to write notes tree to database");
367 /* Deduce parent commit from t->ref */
368 unsigned char parent_sha1
[20];
369 if (!read_ref(t
->ref
, parent_sha1
)) {
370 struct commit
*parent
= lookup_commit(parent_sha1
);
371 if (!parent
|| parse_commit(parent
))
372 die("Failed to find/parse commit %s", t
->ref
);
373 commit_list_insert(parent
, &parents
);
375 /* else: t->ref points to nothing, assume root/orphan commit */
378 if (commit_tree(msg
, tree_sha1
, parents
, result_sha1
, NULL
))
379 die("Failed to commit notes tree to database");
382 int notes_merge(struct notes_merge_options
*o
,
383 struct notes_tree
*local_tree
,
384 unsigned char *result_sha1
)
386 unsigned char local_sha1
[20], remote_sha1
[20];
387 struct commit
*local
, *remote
;
388 struct commit_list
*bases
= NULL
;
389 const unsigned char *base_sha1
, *base_tree_sha1
;
392 assert(o
->local_ref
&& o
->remote_ref
);
393 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
394 hashclr(result_sha1
);
396 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
397 o
->local_ref
, o
->remote_ref
);
399 /* Dereference o->local_ref into local_sha1 */
400 if (!resolve_ref(o
->local_ref
, local_sha1
, 0, NULL
))
401 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
402 else if (!check_ref_format(o
->local_ref
) && is_null_sha1(local_sha1
))
403 local
= NULL
; /* local_sha1 == null_sha1 indicates unborn ref */
404 else if (!(local
= lookup_commit_reference(local_sha1
)))
405 die("Could not parse local commit %s (%s)",
406 sha1_to_hex(local_sha1
), o
->local_ref
);
407 trace_printf("\tlocal commit: %.7s\n", sha1_to_hex(local_sha1
));
409 /* Dereference o->remote_ref into remote_sha1 */
410 if (get_sha1(o
->remote_ref
, remote_sha1
)) {
412 * Failed to get remote_sha1. If o->remote_ref looks like an
413 * unborn ref, perform the merge using an empty notes tree.
415 if (!check_ref_format(o
->remote_ref
)) {
416 hashclr(remote_sha1
);
419 die("Failed to resolve remote notes ref '%s'",
422 } else if (!(remote
= lookup_commit_reference(remote_sha1
))) {
423 die("Could not parse remote commit %s (%s)",
424 sha1_to_hex(remote_sha1
), o
->remote_ref
);
426 trace_printf("\tremote commit: %.7s\n", sha1_to_hex(remote_sha1
));
428 if (!local
&& !remote
)
429 die("Cannot merge empty notes ref (%s) into empty notes ref "
430 "(%s)", o
->remote_ref
, o
->local_ref
);
432 /* result == remote commit */
433 hashcpy(result_sha1
, remote_sha1
);
437 /* result == local commit */
438 hashcpy(result_sha1
, local_sha1
);
441 assert(local
&& remote
);
443 /* Find merge bases */
444 bases
= get_merge_bases(local
, remote
, 1);
446 base_sha1
= null_sha1
;
447 base_tree_sha1
= (unsigned char *)EMPTY_TREE_SHA1_BIN
;
448 OUTPUT(o
, 4, "No merge base found; doing history-less merge");
449 } else if (!bases
->next
) {
450 base_sha1
= bases
->item
->object
.sha1
;
451 base_tree_sha1
= bases
->item
->tree
->object
.sha1
;
452 OUTPUT(o
, 4, "One merge base found (%.7s)",
453 sha1_to_hex(base_sha1
));
455 /* TODO: How to handle multiple merge-bases? */
456 base_sha1
= bases
->item
->object
.sha1
;
457 base_tree_sha1
= bases
->item
->tree
->object
.sha1
;
458 OUTPUT(o
, 3, "Multiple merge bases found. Using the first "
459 "(%.7s)", sha1_to_hex(base_sha1
));
462 OUTPUT(o
, 4, "Merging remote commit %.7s into local commit %.7s with "
463 "merge-base %.7s", sha1_to_hex(remote
->object
.sha1
),
464 sha1_to_hex(local
->object
.sha1
), sha1_to_hex(base_sha1
));
466 if (!hashcmp(remote
->object
.sha1
, base_sha1
)) {
467 /* Already merged; result == local commit */
468 OUTPUT(o
, 2, "Already up-to-date!");
469 hashcpy(result_sha1
, local
->object
.sha1
);
472 if (!hashcmp(local
->object
.sha1
, base_sha1
)) {
473 /* Fast-forward; result == remote commit */
474 OUTPUT(o
, 2, "Fast-forward");
475 hashcpy(result_sha1
, remote
->object
.sha1
);
479 result
= merge_from_diffs(o
, base_tree_sha1
, local
->tree
->object
.sha1
,
480 remote
->tree
->object
.sha1
, local_tree
);
482 if (result
> 0) { /* successful non-trivial merge */
484 struct commit_list
*parents
= NULL
;
485 commit_list_insert(remote
, &parents
); /* LIFO order */
486 commit_list_insert(local
, &parents
);
487 create_notes_commit(local_tree
, parents
, o
->commit_msg
,
492 free_commit_list(bases
);
493 trace_printf("notes_merge(): result = %i, result_sha1 = %.7s\n",
494 result
, sha1_to_hex(result_sha1
));