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