]> git.ipfire.org Git - thirdparty/git.git/blob - notes-merge.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / notes-merge.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "gettext.h"
4 #include "refs.h"
5 #include "object-store.h"
6 #include "repository.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "hex.h"
10 #include "xdiff-interface.h"
11 #include "ll-merge.h"
12 #include "dir.h"
13 #include "notes.h"
14 #include "notes-merge.h"
15 #include "strbuf.h"
16 #include "notes-utils.h"
17 #include "commit-reach.h"
18
19 struct notes_merge_pair {
20 struct object_id obj, base, local, remote;
21 };
22
23 void init_notes_merge_options(struct repository *r,
24 struct notes_merge_options *o)
25 {
26 memset(o, 0, sizeof(struct notes_merge_options));
27 strbuf_init(&(o->commit_msg), 0);
28 o->verbosity = NOTES_MERGE_VERBOSITY_DEFAULT;
29 o->repo = r;
30 }
31
32 static int path_to_oid(const char *path, struct object_id *oid)
33 {
34 char hex_oid[GIT_MAX_HEXSZ];
35 int i = 0;
36 while (*path && i < the_hash_algo->hexsz) {
37 if (*path != '/')
38 hex_oid[i++] = *path;
39 path++;
40 }
41 if (*path || i != the_hash_algo->hexsz)
42 return -1;
43 return get_oid_hex(hex_oid, oid);
44 }
45
46 static int verify_notes_filepair(struct diff_filepair *p, struct object_id *oid)
47 {
48 switch (p->status) {
49 case DIFF_STATUS_MODIFIED:
50 assert(p->one->mode == p->two->mode);
51 assert(!is_null_oid(&p->one->oid));
52 assert(!is_null_oid(&p->two->oid));
53 break;
54 case DIFF_STATUS_ADDED:
55 assert(is_null_oid(&p->one->oid));
56 break;
57 case DIFF_STATUS_DELETED:
58 assert(is_null_oid(&p->two->oid));
59 break;
60 default:
61 return -1;
62 }
63 assert(!strcmp(p->one->path, p->two->path));
64 return path_to_oid(p->one->path, oid);
65 }
66
67 static struct notes_merge_pair *find_notes_merge_pair_pos(
68 struct notes_merge_pair *list, int len, struct object_id *obj,
69 int insert_new, int *occupied)
70 {
71 /*
72 * Both diff_tree_remote() and diff_tree_local() tend to process
73 * merge_pairs in ascending order. Therefore, cache last returned
74 * index, and search sequentially from there until the appropriate
75 * position is found.
76 *
77 * Since inserts only happen from diff_tree_remote() (which mainly
78 * _appends_), we don't care that inserting into the middle of the
79 * list is expensive (using memmove()).
80 */
81 static int last_index;
82 int i = last_index < len ? last_index : len - 1;
83 int prev_cmp = 0, cmp = -1;
84 while (i >= 0 && i < len) {
85 cmp = oidcmp(obj, &list[i].obj);
86 if (!cmp) /* obj belongs @ i */
87 break;
88 else if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */
89 i--;
90 else if (cmp < 0) /* obj belongs between i-1 and i */
91 break;
92 else if (cmp > 0 && prev_cmp >= 0) /* obj belongs > i */
93 i++;
94 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
95 i++;
96 break;
97 }
98 prev_cmp = cmp;
99 }
100 if (i < 0)
101 i = 0;
102 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
103
104 if (!cmp)
105 *occupied = 1;
106 else {
107 *occupied = 0;
108 if (insert_new && i < len) {
109 MOVE_ARRAY(list + i + 1, list + i, len - i);
110 memset(list + i, 0, sizeof(struct notes_merge_pair));
111 }
112 }
113 last_index = i;
114 return list + i;
115 }
116
117 static struct object_id uninitialized = {
118 .hash =
119 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
120 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
121 };
122
123 static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
124 const struct object_id *base,
125 const struct object_id *remote,
126 int *num_changes)
127 {
128 struct diff_options opt;
129 struct notes_merge_pair *changes;
130 int i, len = 0;
131
132 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
133 oid_to_hex(base), oid_to_hex(remote));
134
135 repo_diff_setup(o->repo, &opt);
136 opt.flags.recursive = 1;
137 opt.output_format = DIFF_FORMAT_NO_OUTPUT;
138 diff_setup_done(&opt);
139 diff_tree_oid(base, remote, "", &opt);
140 diffcore_std(&opt);
141
142 CALLOC_ARRAY(changes, diff_queued_diff.nr);
143
144 for (i = 0; i < diff_queued_diff.nr; i++) {
145 struct diff_filepair *p = diff_queued_diff.queue[i];
146 struct notes_merge_pair *mp;
147 int occupied;
148 struct object_id obj;
149
150 if (verify_notes_filepair(p, &obj)) {
151 trace_printf("\t\tCannot merge entry '%s' (%c): "
152 "%.7s -> %.7s. Skipping!\n", p->one->path,
153 p->status, oid_to_hex(&p->one->oid),
154 oid_to_hex(&p->two->oid));
155 continue;
156 }
157 mp = find_notes_merge_pair_pos(changes, len, &obj, 1, &occupied);
158 if (occupied) {
159 /* We've found an addition/deletion pair */
160 assert(oideq(&mp->obj, &obj));
161 if (is_null_oid(&p->one->oid)) { /* addition */
162 assert(is_null_oid(&mp->remote));
163 oidcpy(&mp->remote, &p->two->oid);
164 } else if (is_null_oid(&p->two->oid)) { /* deletion */
165 assert(is_null_oid(&mp->base));
166 oidcpy(&mp->base, &p->one->oid);
167 } else
168 assert(!"Invalid existing change recorded");
169 } else {
170 oidcpy(&mp->obj, &obj);
171 oidcpy(&mp->base, &p->one->oid);
172 oidcpy(&mp->local, &uninitialized);
173 oidcpy(&mp->remote, &p->two->oid);
174 len++;
175 }
176 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
177 oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
178 oid_to_hex(&mp->remote));
179 }
180 diff_flush(&opt);
181
182 *num_changes = len;
183 return changes;
184 }
185
186 static void diff_tree_local(struct notes_merge_options *o,
187 struct notes_merge_pair *changes, int len,
188 const struct object_id *base,
189 const struct object_id *local)
190 {
191 struct diff_options opt;
192 int i;
193
194 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
195 len, oid_to_hex(base), oid_to_hex(local));
196
197 repo_diff_setup(o->repo, &opt);
198 opt.flags.recursive = 1;
199 opt.output_format = DIFF_FORMAT_NO_OUTPUT;
200 diff_setup_done(&opt);
201 diff_tree_oid(base, local, "", &opt);
202 diffcore_std(&opt);
203
204 for (i = 0; i < diff_queued_diff.nr; i++) {
205 struct diff_filepair *p = diff_queued_diff.queue[i];
206 struct notes_merge_pair *mp;
207 int match;
208 struct object_id obj;
209
210 if (verify_notes_filepair(p, &obj)) {
211 trace_printf("\t\tCannot merge entry '%s' (%c): "
212 "%.7s -> %.7s. Skipping!\n", p->one->path,
213 p->status, oid_to_hex(&p->one->oid),
214 oid_to_hex(&p->two->oid));
215 continue;
216 }
217 mp = find_notes_merge_pair_pos(changes, len, &obj, 0, &match);
218 if (!match) {
219 trace_printf("\t\tIgnoring local-only change for %s: "
220 "%.7s -> %.7s\n", oid_to_hex(&obj),
221 oid_to_hex(&p->one->oid),
222 oid_to_hex(&p->two->oid));
223 continue;
224 }
225
226 assert(oideq(&mp->obj, &obj));
227 if (is_null_oid(&p->two->oid)) { /* deletion */
228 /*
229 * Either this is a true deletion (1), or it is part
230 * of an A/D pair (2), or D/A pair (3):
231 *
232 * (1) mp->local is uninitialized; set it to null_sha1
233 * (2) mp->local is not uninitialized; don't touch it
234 * (3) mp->local is uninitialized; set it to null_sha1
235 * (will be overwritten by following addition)
236 */
237 if (oideq(&mp->local, &uninitialized))
238 oidclr(&mp->local);
239 } else if (is_null_oid(&p->one->oid)) { /* addition */
240 /*
241 * Either this is a true addition (1), or it is part
242 * of an A/D pair (2), or D/A pair (3):
243 *
244 * (1) mp->local is uninitialized; set to p->two->sha1
245 * (2) mp->local is uninitialized; set to p->two->sha1
246 * (3) mp->local is null_sha1; set to p->two->sha1
247 */
248 assert(is_null_oid(&mp->local) ||
249 oideq(&mp->local, &uninitialized));
250 oidcpy(&mp->local, &p->two->oid);
251 } else { /* modification */
252 /*
253 * This is a true modification. p->one->sha1 shall
254 * match mp->base, and mp->local shall be uninitialized.
255 * Set mp->local to p->two->sha1.
256 */
257 assert(oideq(&p->one->oid, &mp->base));
258 assert(oideq(&mp->local, &uninitialized));
259 oidcpy(&mp->local, &p->two->oid);
260 }
261 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
262 oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
263 oid_to_hex(&mp->local));
264 }
265 diff_flush(&opt);
266 }
267
268 static void check_notes_merge_worktree(struct notes_merge_options *o)
269 {
270 if (!o->has_worktree) {
271 /*
272 * Must establish NOTES_MERGE_WORKTREE.
273 * Abort if NOTES_MERGE_WORKTREE already exists
274 */
275 if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
276 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
277 if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
278 die(_("You have not concluded your previous "
279 "notes merge (%s exists).\nPlease, use "
280 "'git notes merge --commit' or 'git notes "
281 "merge --abort' to commit/abort the "
282 "previous merge before you start a new "
283 "notes merge."), git_path("NOTES_MERGE_*"));
284 else
285 die(_("You have not concluded your notes merge "
286 "(%s exists)."), git_path("NOTES_MERGE_*"));
287 }
288
289 if (safe_create_leading_directories_const(git_path(
290 NOTES_MERGE_WORKTREE "/.test")))
291 die_errno("unable to create directory %s",
292 git_path(NOTES_MERGE_WORKTREE));
293 o->has_worktree = 1;
294 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))
295 /* NOTES_MERGE_WORKTREE should already be established */
296 die("missing '%s'. This should not happen",
297 git_path(NOTES_MERGE_WORKTREE));
298 }
299
300 static void write_buf_to_worktree(const struct object_id *obj,
301 const char *buf, unsigned long size)
302 {
303 int fd;
304 char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", oid_to_hex(obj));
305 if (safe_create_leading_directories_const(path))
306 die_errno("unable to create directory for '%s'", path);
307
308 fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
309
310 while (size > 0) {
311 ssize_t ret = write_in_full(fd, buf, size);
312 if (ret < 0) {
313 /* Ignore epipe */
314 if (errno == EPIPE)
315 break;
316 die_errno("notes-merge");
317 }
318 size -= ret;
319 buf += ret;
320 }
321
322 close(fd);
323 free(path);
324 }
325
326 static void write_note_to_worktree(const struct object_id *obj,
327 const struct object_id *note)
328 {
329 enum object_type type;
330 unsigned long size;
331 void *buf = read_object_file(note, &type, &size);
332
333 if (!buf)
334 die("cannot read note %s for object %s",
335 oid_to_hex(note), oid_to_hex(obj));
336 if (type != OBJ_BLOB)
337 die("blob expected in note %s for object %s",
338 oid_to_hex(note), oid_to_hex(obj));
339 write_buf_to_worktree(obj, buf, size);
340 free(buf);
341 }
342
343 static int ll_merge_in_worktree(struct notes_merge_options *o,
344 struct notes_merge_pair *p)
345 {
346 mmbuffer_t result_buf;
347 mmfile_t base, local, remote;
348 enum ll_merge_result status;
349
350 read_mmblob(&base, &p->base);
351 read_mmblob(&local, &p->local);
352 read_mmblob(&remote, &p->remote);
353
354 status = ll_merge(&result_buf, oid_to_hex(&p->obj), &base, NULL,
355 &local, o->local_ref, &remote, o->remote_ref,
356 o->repo->index, NULL);
357
358 free(base.ptr);
359 free(local.ptr);
360 free(remote.ptr);
361
362 if (status == LL_MERGE_BINARY_CONFLICT)
363 warning("Cannot merge binary files: %s (%s vs. %s)",
364 oid_to_hex(&p->obj), o->local_ref, o->remote_ref);
365 if ((status < 0) || !result_buf.ptr)
366 die("Failed to execute internal merge");
367
368 write_buf_to_worktree(&p->obj, result_buf.ptr, result_buf.size);
369 free(result_buf.ptr);
370
371 return status;
372 }
373
374 static int merge_one_change_manual(struct notes_merge_options *o,
375 struct notes_merge_pair *p,
376 struct notes_tree *t)
377 {
378 const char *lref = o->local_ref ? o->local_ref : "local version";
379 const char *rref = o->remote_ref ? o->remote_ref : "remote version";
380
381 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
382 "local = %.7s, remote = %.7s)\n",
383 oid_to_hex(&p->obj), oid_to_hex(&p->base),
384 oid_to_hex(&p->local), oid_to_hex(&p->remote));
385
386 /* add "Conflicts:" section to commit message first time through */
387 if (!o->has_worktree)
388 strbuf_addstr(&(o->commit_msg), "\n\nConflicts:\n");
389
390 strbuf_addf(&(o->commit_msg), "\t%s\n", oid_to_hex(&p->obj));
391
392 if (o->verbosity >= 2)
393 printf("Auto-merging notes for %s\n", oid_to_hex(&p->obj));
394 check_notes_merge_worktree(o);
395 if (is_null_oid(&p->local)) {
396 /* D/F conflict, checkout p->remote */
397 assert(!is_null_oid(&p->remote));
398 if (o->verbosity >= 1)
399 printf("CONFLICT (delete/modify): Notes for object %s "
400 "deleted in %s and modified in %s. Version from %s "
401 "left in tree.\n",
402 oid_to_hex(&p->obj), lref, rref, rref);
403 write_note_to_worktree(&p->obj, &p->remote);
404 } else if (is_null_oid(&p->remote)) {
405 /* D/F conflict, checkout p->local */
406 assert(!is_null_oid(&p->local));
407 if (o->verbosity >= 1)
408 printf("CONFLICT (delete/modify): Notes for object %s "
409 "deleted in %s and modified in %s. Version from %s "
410 "left in tree.\n",
411 oid_to_hex(&p->obj), rref, lref, lref);
412 write_note_to_worktree(&p->obj, &p->local);
413 } else {
414 /* "regular" conflict, checkout result of ll_merge() */
415 const char *reason = "content";
416 if (is_null_oid(&p->base))
417 reason = "add/add";
418 assert(!is_null_oid(&p->local));
419 assert(!is_null_oid(&p->remote));
420 if (o->verbosity >= 1)
421 printf("CONFLICT (%s): Merge conflict in notes for "
422 "object %s\n", reason,
423 oid_to_hex(&p->obj));
424 ll_merge_in_worktree(o, p);
425 }
426
427 trace_printf("\t\t\tremoving from partial merge result\n");
428 remove_note(t, p->obj.hash);
429
430 return 1;
431 }
432
433 static int merge_one_change(struct notes_merge_options *o,
434 struct notes_merge_pair *p, struct notes_tree *t)
435 {
436 /*
437 * Return 0 if change is successfully resolved (stored in notes_tree).
438 * Return 1 is change results in a conflict (NOT stored in notes_tree,
439 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
440 */
441 switch (o->strategy) {
442 case NOTES_MERGE_RESOLVE_MANUAL:
443 return merge_one_change_manual(o, p, t);
444 case NOTES_MERGE_RESOLVE_OURS:
445 if (o->verbosity >= 2)
446 printf("Using local notes for %s\n",
447 oid_to_hex(&p->obj));
448 /* nothing to do */
449 return 0;
450 case NOTES_MERGE_RESOLVE_THEIRS:
451 if (o->verbosity >= 2)
452 printf("Using remote notes for %s\n",
453 oid_to_hex(&p->obj));
454 if (add_note(t, &p->obj, &p->remote, combine_notes_overwrite))
455 BUG("combine_notes_overwrite failed");
456 return 0;
457 case NOTES_MERGE_RESOLVE_UNION:
458 if (o->verbosity >= 2)
459 printf("Concatenating local and remote notes for %s\n",
460 oid_to_hex(&p->obj));
461 if (add_note(t, &p->obj, &p->remote, combine_notes_concatenate))
462 die("failed to concatenate notes "
463 "(combine_notes_concatenate)");
464 return 0;
465 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ:
466 if (o->verbosity >= 2)
467 printf("Concatenating unique lines in local and remote "
468 "notes for %s\n", oid_to_hex(&p->obj));
469 if (add_note(t, &p->obj, &p->remote, combine_notes_cat_sort_uniq))
470 die("failed to concatenate notes "
471 "(combine_notes_cat_sort_uniq)");
472 return 0;
473 }
474 die("Unknown strategy (%i).", o->strategy);
475 }
476
477 static int merge_changes(struct notes_merge_options *o,
478 struct notes_merge_pair *changes, int *num_changes,
479 struct notes_tree *t)
480 {
481 int i, conflicts = 0;
482
483 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes);
484 for (i = 0; i < *num_changes; i++) {
485 struct notes_merge_pair *p = changes + i;
486 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
487 oid_to_hex(&p->obj), oid_to_hex(&p->base),
488 oid_to_hex(&p->local),
489 oid_to_hex(&p->remote));
490
491 if (oideq(&p->base, &p->remote)) {
492 /* no remote change; nothing to do */
493 trace_printf("\t\t\tskipping (no remote change)\n");
494 } else if (oideq(&p->local, &p->remote)) {
495 /* same change in local and remote; nothing to do */
496 trace_printf("\t\t\tskipping (local == remote)\n");
497 } else if (oideq(&p->local, &uninitialized) ||
498 oideq(&p->local, &p->base)) {
499 /* no local change; adopt remote change */
500 trace_printf("\t\t\tno local change, adopted remote\n");
501 if (add_note(t, &p->obj, &p->remote,
502 combine_notes_overwrite))
503 BUG("combine_notes_overwrite failed");
504 } else {
505 /* need file-level merge between local and remote */
506 trace_printf("\t\t\tneed content-level merge\n");
507 conflicts += merge_one_change(o, p, t);
508 }
509 }
510
511 return conflicts;
512 }
513
514 static int merge_from_diffs(struct notes_merge_options *o,
515 const struct object_id *base,
516 const struct object_id *local,
517 const struct object_id *remote,
518 struct notes_tree *t)
519 {
520 struct notes_merge_pair *changes;
521 int num_changes, conflicts;
522
523 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
524 "remote = %.7s)\n", oid_to_hex(base), oid_to_hex(local),
525 oid_to_hex(remote));
526
527 changes = diff_tree_remote(o, base, remote, &num_changes);
528 diff_tree_local(o, changes, num_changes, base, local);
529
530 conflicts = merge_changes(o, changes, &num_changes, t);
531 free(changes);
532
533 if (o->verbosity >= 4)
534 printf(t->dirty ?
535 "Merge result: %i unmerged notes and a dirty notes tree\n" :
536 "Merge result: %i unmerged notes and a clean notes tree\n",
537 conflicts);
538
539 return conflicts ? -1 : 1;
540 }
541
542 int notes_merge(struct notes_merge_options *o,
543 struct notes_tree *local_tree,
544 struct object_id *result_oid)
545 {
546 struct object_id local_oid, remote_oid;
547 struct commit *local, *remote;
548 struct commit_list *bases = NULL;
549 const struct object_id *base_oid, *base_tree_oid;
550 int result = 0;
551
552 assert(o->local_ref && o->remote_ref);
553 assert(!strcmp(o->local_ref, local_tree->ref));
554 oidclr(result_oid);
555
556 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
557 o->local_ref, o->remote_ref);
558
559 /* Dereference o->local_ref into local_sha1 */
560 if (read_ref_full(o->local_ref, 0, &local_oid, NULL))
561 die("Failed to resolve local notes ref '%s'", o->local_ref);
562 else if (!check_refname_format(o->local_ref, 0) &&
563 is_null_oid(&local_oid))
564 local = NULL; /* local_oid == null_oid indicates unborn ref */
565 else if (!(local = lookup_commit_reference(o->repo, &local_oid)))
566 die("Could not parse local commit %s (%s)",
567 oid_to_hex(&local_oid), o->local_ref);
568 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid));
569
570 /* Dereference o->remote_ref into remote_oid */
571 if (get_oid(o->remote_ref, &remote_oid)) {
572 /*
573 * Failed to get remote_oid. If o->remote_ref looks like an
574 * unborn ref, perform the merge using an empty notes tree.
575 */
576 if (!check_refname_format(o->remote_ref, 0)) {
577 oidclr(&remote_oid);
578 remote = NULL;
579 } else {
580 die("Failed to resolve remote notes ref '%s'",
581 o->remote_ref);
582 }
583 } else if (!(remote = lookup_commit_reference(o->repo, &remote_oid))) {
584 die("Could not parse remote commit %s (%s)",
585 oid_to_hex(&remote_oid), o->remote_ref);
586 }
587 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid));
588
589 if (!local && !remote)
590 die("Cannot merge empty notes ref (%s) into empty notes ref "
591 "(%s)", o->remote_ref, o->local_ref);
592 if (!local) {
593 /* result == remote commit */
594 oidcpy(result_oid, &remote_oid);
595 goto found_result;
596 }
597 if (!remote) {
598 /* result == local commit */
599 oidcpy(result_oid, &local_oid);
600 goto found_result;
601 }
602 assert(local && remote);
603
604 /* Find merge bases */
605 bases = get_merge_bases(local, remote);
606 if (!bases) {
607 base_oid = null_oid();
608 base_tree_oid = the_hash_algo->empty_tree;
609 if (o->verbosity >= 4)
610 printf("No merge base found; doing history-less merge\n");
611 } else if (!bases->next) {
612 base_oid = &bases->item->object.oid;
613 base_tree_oid = get_commit_tree_oid(bases->item);
614 if (o->verbosity >= 4)
615 printf("One merge base found (%.7s)\n",
616 oid_to_hex(base_oid));
617 } else {
618 /* TODO: How to handle multiple merge-bases? */
619 base_oid = &bases->item->object.oid;
620 base_tree_oid = get_commit_tree_oid(bases->item);
621 if (o->verbosity >= 3)
622 printf("Multiple merge bases found. Using the first "
623 "(%.7s)\n", oid_to_hex(base_oid));
624 }
625
626 if (o->verbosity >= 4)
627 printf("Merging remote commit %.7s into local commit %.7s with "
628 "merge-base %.7s\n", oid_to_hex(&remote->object.oid),
629 oid_to_hex(&local->object.oid),
630 oid_to_hex(base_oid));
631
632 if (oideq(&remote->object.oid, base_oid)) {
633 /* Already merged; result == local commit */
634 if (o->verbosity >= 2)
635 printf_ln("Already up to date.");
636 oidcpy(result_oid, &local->object.oid);
637 goto found_result;
638 }
639 if (oideq(&local->object.oid, base_oid)) {
640 /* Fast-forward; result == remote commit */
641 if (o->verbosity >= 2)
642 printf("Fast-forward\n");
643 oidcpy(result_oid, &remote->object.oid);
644 goto found_result;
645 }
646
647 result = merge_from_diffs(o, base_tree_oid,
648 get_commit_tree_oid(local),
649 get_commit_tree_oid(remote), local_tree);
650
651 if (result != 0) { /* non-trivial merge (with or without conflicts) */
652 /* Commit (partial) result */
653 struct commit_list *parents = NULL;
654 commit_list_insert(remote, &parents); /* LIFO order */
655 commit_list_insert(local, &parents);
656 create_notes_commit(o->repo, local_tree, parents, o->commit_msg.buf,
657 o->commit_msg.len, result_oid);
658 }
659
660 found_result:
661 free_commit_list(bases);
662 strbuf_release(&(o->commit_msg));
663 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
664 result, oid_to_hex(result_oid));
665 return result;
666 }
667
668 int notes_merge_commit(struct notes_merge_options *o,
669 struct notes_tree *partial_tree,
670 struct commit *partial_commit,
671 struct object_id *result_oid)
672 {
673 /*
674 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
675 * found notes to 'partial_tree'. Write the updated notes tree to
676 * the DB, and commit the resulting tree object while reusing the
677 * commit message and parents from 'partial_commit'.
678 * Finally store the new commit object OID into 'result_oid'.
679 */
680 DIR *dir;
681 struct dirent *e;
682 struct strbuf path = STRBUF_INIT;
683 const char *buffer = get_commit_buffer(partial_commit, NULL);
684 const char *msg = strstr(buffer, "\n\n");
685 int baselen;
686
687 git_path_buf(&path, NOTES_MERGE_WORKTREE);
688 if (o->verbosity >= 3)
689 printf("Committing notes in notes merge worktree at %s\n",
690 path.buf);
691
692 if (!msg || msg[2] == '\0')
693 die("partial notes commit has empty message");
694 msg += 2;
695
696 dir = opendir(path.buf);
697 if (!dir)
698 die_errno("could not open %s", path.buf);
699
700 strbuf_addch(&path, '/');
701 baselen = path.len;
702 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
703 struct stat st;
704 struct object_id obj_oid, blob_oid;
705
706 if (get_oid_hex(e->d_name, &obj_oid)) {
707 if (o->verbosity >= 3)
708 printf("Skipping non-SHA1 entry '%s%s'\n",
709 path.buf, e->d_name);
710 continue;
711 }
712
713 strbuf_addstr(&path, e->d_name);
714 /* write file as blob, and add to partial_tree */
715 if (stat(path.buf, &st))
716 die_errno("Failed to stat '%s'", path.buf);
717 if (index_path(o->repo->index, &blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
718 die("Failed to write blob object from '%s'", path.buf);
719 if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
720 die("Failed to add resolved note '%s' to notes tree",
721 path.buf);
722 if (o->verbosity >= 4)
723 printf("Added resolved note for object %s: %s\n",
724 oid_to_hex(&obj_oid), oid_to_hex(&blob_oid));
725 strbuf_setlen(&path, baselen);
726 }
727
728 create_notes_commit(o->repo, partial_tree, partial_commit->parents, msg,
729 strlen(msg), result_oid);
730 unuse_commit_buffer(partial_commit, buffer);
731 if (o->verbosity >= 4)
732 printf("Finalized notes merge commit: %s\n",
733 oid_to_hex(result_oid));
734 strbuf_release(&path);
735 closedir(dir);
736 return 0;
737 }
738
739 int notes_merge_abort(struct notes_merge_options *o)
740 {
741 /*
742 * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
743 * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
744 * the current working directory of the user.
745 */
746 struct strbuf buf = STRBUF_INIT;
747 int ret;
748
749 git_path_buf(&buf, NOTES_MERGE_WORKTREE);
750 if (o->verbosity >= 3)
751 printf("Removing notes merge worktree at %s/*\n", buf.buf);
752 ret = remove_dir_recursively(&buf, REMOVE_DIR_KEEP_TOPLEVEL);
753 strbuf_release(&buf);
754 return ret;
755 }