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