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