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