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