]> git.ipfire.org Git - thirdparty/git.git/blame - notes-merge.c
environment.h: move declarations for environment.c functions from cache.h
[thirdparty/git.git] / notes-merge.c
CommitLineData
75ef3f4a
JH
1#include "cache.h"
2#include "commit.h"
f394e093 3#include "gettext.h"
75ef3f4a 4#include "refs.h"
cbd53a21 5#include "object-store.h"
2122f675 6#include "repository.h"
2085b16a
JH
7#include "diff.h"
8#include "diffcore.h"
41771fa4 9#include "hex.h"
809f38c8
JH
10#include "xdiff-interface.h"
11#include "ll-merge.h"
12#include "dir.h"
56881843 13#include "notes.h"
75ef3f4a 14#include "notes-merge.h"
443259cf 15#include "strbuf.h"
bf9a05ba 16#include "notes-utils.h"
64043556 17#include "commit-reach.h"
d5ebb50d 18#include "wrapper.h"
75ef3f4a 19
2085b16a 20struct notes_merge_pair {
e910bb1e 21 struct object_id obj, base, local, remote;
2085b16a
JH
22};
23
5684200f
NTND
24void init_notes_merge_options(struct repository *r,
25 struct notes_merge_options *o)
75ef3f4a
JH
26{
27 memset(o, 0, sizeof(struct notes_merge_options));
443259cf 28 strbuf_init(&(o->commit_msg), 0);
75ef3f4a 29 o->verbosity = NOTES_MERGE_VERBOSITY_DEFAULT;
5684200f 30 o->repo = r;
75ef3f4a
JH
31}
32
4d77896e 33static int path_to_oid(const char *path, struct object_id *oid)
2085b16a 34{
22350307 35 char hex_oid[GIT_MAX_HEXSZ];
2085b16a 36 int i = 0;
22350307 37 while (*path && i < the_hash_algo->hexsz) {
2085b16a 38 if (*path != '/')
4d77896e 39 hex_oid[i++] = *path;
2085b16a
JH
40 path++;
41 }
22350307 42 if (*path || i != the_hash_algo->hexsz)
2085b16a 43 return -1;
4d77896e 44 return get_oid_hex(hex_oid, oid);
2085b16a
JH
45}
46
4d77896e 47static int verify_notes_filepair(struct diff_filepair *p, struct object_id *oid)
2085b16a
JH
48{
49 switch (p->status) {
50 case DIFF_STATUS_MODIFIED:
51 assert(p->one->mode == p->two->mode);
a0d12c44 52 assert(!is_null_oid(&p->one->oid));
53 assert(!is_null_oid(&p->two->oid));
2085b16a
JH
54 break;
55 case DIFF_STATUS_ADDED:
a0d12c44 56 assert(is_null_oid(&p->one->oid));
2085b16a
JH
57 break;
58 case DIFF_STATUS_DELETED:
a0d12c44 59 assert(is_null_oid(&p->two->oid));
2085b16a
JH
60 break;
61 default:
62 return -1;
63 }
64 assert(!strcmp(p->one->path, p->two->path));
4d77896e 65 return path_to_oid(p->one->path, oid);
2085b16a
JH
66}
67
68static struct notes_merge_pair *find_notes_merge_pair_pos(
d7a7c708 69 struct notes_merge_pair *list, int len, struct object_id *obj,
2085b16a
JH
70 int insert_new, int *occupied)
71{
72 /*
73 * Both diff_tree_remote() and diff_tree_local() tend to process
74 * merge_pairs in ascending order. Therefore, cache last returned
75 * index, and search sequentially from there until the appropriate
76 * position is found.
77 *
78 * Since inserts only happen from diff_tree_remote() (which mainly
79 * _appends_), we don't care that inserting into the middle of the
80 * list is expensive (using memmove()).
81 */
82 static int last_index;
83 int i = last_index < len ? last_index : len - 1;
84 int prev_cmp = 0, cmp = -1;
85 while (i >= 0 && i < len) {
d7a7c708 86 cmp = oidcmp(obj, &list[i].obj);
2085b16a
JH
87 if (!cmp) /* obj belongs @ i */
88 break;
89 else if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */
90 i--;
91 else if (cmp < 0) /* obj belongs between i-1 and 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 and i+1 */
96 i++;
97 break;
98 }
99 prev_cmp = cmp;
100 }
101 if (i < 0)
102 i = 0;
103 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
104
105 if (!cmp)
106 *occupied = 1;
107 else {
108 *occupied = 0;
109 if (insert_new && i < len) {
f331ab9d 110 MOVE_ARRAY(list + i + 1, list + i, len - i);
2085b16a
JH
111 memset(list + i, 0, sizeof(struct notes_merge_pair));
112 }
113 }
114 last_index = i;
115 return list + i;
116}
117
e910bb1e 118static struct object_id uninitialized = {
50103649 119 .hash =
2085b16a 120 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
e910bb1e 121 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
122};
2085b16a
JH
123
124static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
9d6babb2
BW
125 const struct object_id *base,
126 const struct object_id *remote,
2085b16a
JH
127 int *num_changes)
128{
129 struct diff_options opt;
130 struct notes_merge_pair *changes;
131 int i, len = 0;
132
133 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
9d6babb2 134 oid_to_hex(base), oid_to_hex(remote));
2085b16a 135
5684200f 136 repo_diff_setup(o->repo, &opt);
0d1e0e78 137 opt.flags.recursive = 1;
2085b16a 138 opt.output_format = DIFF_FORMAT_NO_OUTPUT;
28452655 139 diff_setup_done(&opt);
66f414f8 140 diff_tree_oid(base, remote, "", &opt);
2085b16a
JH
141 diffcore_std(&opt);
142
ca56dadb 143 CALLOC_ARRAY(changes, diff_queued_diff.nr);
2085b16a
JH
144
145 for (i = 0; i < diff_queued_diff.nr; i++) {
146 struct diff_filepair *p = diff_queued_diff.queue[i];
147 struct notes_merge_pair *mp;
148 int occupied;
d7a7c708 149 struct object_id obj;
2085b16a 150
4d77896e 151 if (verify_notes_filepair(p, &obj)) {
2085b16a
JH
152 trace_printf("\t\tCannot merge entry '%s' (%c): "
153 "%.7s -> %.7s. Skipping!\n", p->one->path,
a0d12c44 154 p->status, oid_to_hex(&p->one->oid),
155 oid_to_hex(&p->two->oid));
2085b16a
JH
156 continue;
157 }
d7a7c708 158 mp = find_notes_merge_pair_pos(changes, len, &obj, 1, &occupied);
2085b16a
JH
159 if (occupied) {
160 /* We've found an addition/deletion pair */
4a7e27e9 161 assert(oideq(&mp->obj, &obj));
a0d12c44 162 if (is_null_oid(&p->one->oid)) { /* addition */
e910bb1e 163 assert(is_null_oid(&mp->remote));
164 oidcpy(&mp->remote, &p->two->oid);
a0d12c44 165 } else if (is_null_oid(&p->two->oid)) { /* deletion */
e910bb1e 166 assert(is_null_oid(&mp->base));
167 oidcpy(&mp->base, &p->one->oid);
2085b16a
JH
168 } else
169 assert(!"Invalid existing change recorded");
170 } else {
d7a7c708 171 oidcpy(&mp->obj, &obj);
e910bb1e 172 oidcpy(&mp->base, &p->one->oid);
173 oidcpy(&mp->local, &uninitialized);
174 oidcpy(&mp->remote, &p->two->oid);
2085b16a
JH
175 len++;
176 }
177 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
e910bb1e 178 oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
179 oid_to_hex(&mp->remote));
2085b16a
JH
180 }
181 diff_flush(&opt);
2085b16a
JH
182
183 *num_changes = len;
184 return changes;
185}
186
187static void diff_tree_local(struct notes_merge_options *o,
188 struct notes_merge_pair *changes, int len,
9d6babb2
BW
189 const struct object_id *base,
190 const struct object_id *local)
2085b16a
JH
191{
192 struct diff_options opt;
193 int i;
194
195 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
9d6babb2 196 len, oid_to_hex(base), oid_to_hex(local));
2085b16a 197
5684200f 198 repo_diff_setup(o->repo, &opt);
0d1e0e78 199 opt.flags.recursive = 1;
2085b16a 200 opt.output_format = DIFF_FORMAT_NO_OUTPUT;
28452655 201 diff_setup_done(&opt);
66f414f8 202 diff_tree_oid(base, local, "", &opt);
2085b16a
JH
203 diffcore_std(&opt);
204
205 for (i = 0; i < diff_queued_diff.nr; i++) {
206 struct diff_filepair *p = diff_queued_diff.queue[i];
207 struct notes_merge_pair *mp;
208 int match;
d7a7c708 209 struct object_id obj;
2085b16a 210
4d77896e 211 if (verify_notes_filepair(p, &obj)) {
2085b16a
JH
212 trace_printf("\t\tCannot merge entry '%s' (%c): "
213 "%.7s -> %.7s. Skipping!\n", p->one->path,
a0d12c44 214 p->status, oid_to_hex(&p->one->oid),
215 oid_to_hex(&p->two->oid));
2085b16a
JH
216 continue;
217 }
d7a7c708 218 mp = find_notes_merge_pair_pos(changes, len, &obj, 0, &match);
2085b16a
JH
219 if (!match) {
220 trace_printf("\t\tIgnoring local-only change for %s: "
d7a7c708 221 "%.7s -> %.7s\n", oid_to_hex(&obj),
a0d12c44 222 oid_to_hex(&p->one->oid),
223 oid_to_hex(&p->two->oid));
2085b16a
JH
224 continue;
225 }
226
4a7e27e9 227 assert(oideq(&mp->obj, &obj));
a0d12c44 228 if (is_null_oid(&p->two->oid)) { /* deletion */
2085b16a
JH
229 /*
230 * Either this is a true deletion (1), or it is part
231 * of an A/D pair (2), or D/A pair (3):
232 *
233 * (1) mp->local is uninitialized; set it to null_sha1
234 * (2) mp->local is not uninitialized; don't touch it
235 * (3) mp->local is uninitialized; set it to null_sha1
236 * (will be overwritten by following addition)
237 */
4a7e27e9 238 if (oideq(&mp->local, &uninitialized))
e910bb1e 239 oidclr(&mp->local);
a0d12c44 240 } else if (is_null_oid(&p->one->oid)) { /* addition */
2085b16a
JH
241 /*
242 * Either this is a true addition (1), or it is part
243 * of an A/D pair (2), or D/A pair (3):
244 *
245 * (1) mp->local is uninitialized; set to p->two->sha1
246 * (2) mp->local is uninitialized; set to p->two->sha1
247 * (3) mp->local is null_sha1; set to p->two->sha1
248 */
e910bb1e 249 assert(is_null_oid(&mp->local) ||
4a7e27e9 250 oideq(&mp->local, &uninitialized));
e910bb1e 251 oidcpy(&mp->local, &p->two->oid);
2085b16a
JH
252 } else { /* modification */
253 /*
254 * This is a true modification. p->one->sha1 shall
255 * match mp->base, and mp->local shall be uninitialized.
256 * Set mp->local to p->two->sha1.
257 */
4a7e27e9
JK
258 assert(oideq(&p->one->oid, &mp->base));
259 assert(oideq(&mp->local, &uninitialized));
e910bb1e 260 oidcpy(&mp->local, &p->two->oid);
2085b16a
JH
261 }
262 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
e910bb1e 263 oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
264 oid_to_hex(&mp->local));
2085b16a
JH
265 }
266 diff_flush(&opt);
2085b16a
JH
267}
268
809f38c8
JH
269static void check_notes_merge_worktree(struct notes_merge_options *o)
270{
271 if (!o->has_worktree) {
272 /*
273 * Must establish NOTES_MERGE_WORKTREE.
274 * Abort if NOTES_MERGE_WORKTREE already exists
275 */
dabba590
JH
276 if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
277 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
ed9bff08 278 if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
c041c6d0 279 die(_("You have not concluded your previous "
809f38c8
JH
280 "notes merge (%s exists).\nPlease, use "
281 "'git notes merge --commit' or 'git notes "
6abb3655 282 "merge --abort' to commit/abort the "
809f38c8 283 "previous merge before you start a new "
c041c6d0 284 "notes merge."), git_path("NOTES_MERGE_*"));
809f38c8 285 else
c041c6d0
VA
286 die(_("You have not concluded your notes merge "
287 "(%s exists)."), git_path("NOTES_MERGE_*"));
809f38c8
JH
288 }
289
dcf69262 290 if (safe_create_leading_directories_const(git_path(
809f38c8
JH
291 NOTES_MERGE_WORKTREE "/.test")))
292 die_errno("unable to create directory %s",
293 git_path(NOTES_MERGE_WORKTREE));
294 o->has_worktree = 1;
295 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))
296 /* NOTES_MERGE_WORKTREE should already be established */
297 die("missing '%s'. This should not happen",
298 git_path(NOTES_MERGE_WORKTREE));
299}
300
9e5e0c28 301static void write_buf_to_worktree(const struct object_id *obj,
809f38c8
JH
302 const char *buf, unsigned long size)
303{
304 int fd;
9e5e0c28 305 char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", oid_to_hex(obj));
dcf69262 306 if (safe_create_leading_directories_const(path))
809f38c8 307 die_errno("unable to create directory for '%s'", path);
809f38c8 308
deb9c157 309 fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
809f38c8
JH
310
311 while (size > 0) {
634eb82b 312 ssize_t ret = write_in_full(fd, buf, size);
809f38c8
JH
313 if (ret < 0) {
314 /* Ignore epipe */
315 if (errno == EPIPE)
316 break;
317 die_errno("notes-merge");
809f38c8
JH
318 }
319 size -= ret;
320 buf += ret;
321 }
322
323 close(fd);
fcd12db6 324 free(path);
809f38c8
JH
325}
326
9e5e0c28
BW
327static void write_note_to_worktree(const struct object_id *obj,
328 const struct object_id *note)
809f38c8
JH
329{
330 enum object_type type;
331 unsigned long size;
b4f5aca4 332 void *buf = read_object_file(note, &type, &size);
809f38c8
JH
333
334 if (!buf)
335 die("cannot read note %s for object %s",
9e5e0c28 336 oid_to_hex(note), oid_to_hex(obj));
809f38c8
JH
337 if (type != OBJ_BLOB)
338 die("blob expected in note %s for object %s",
9e5e0c28 339 oid_to_hex(note), oid_to_hex(obj));
809f38c8
JH
340 write_buf_to_worktree(obj, buf, size);
341 free(buf);
342}
343
344static int ll_merge_in_worktree(struct notes_merge_options *o,
345 struct notes_merge_pair *p)
346{
347 mmbuffer_t result_buf;
348 mmfile_t base, local, remote;
35f69671 349 enum ll_merge_result status;
809f38c8 350
d449347d 351 read_mmblob(&base, &p->base);
352 read_mmblob(&local, &p->local);
353 read_mmblob(&remote, &p->remote);
809f38c8 354
e910bb1e 355 status = ll_merge(&result_buf, oid_to_hex(&p->obj), &base, NULL,
32eaa468 356 &local, o->local_ref, &remote, o->remote_ref,
5684200f 357 o->repo->index, NULL);
809f38c8
JH
358
359 free(base.ptr);
360 free(local.ptr);
361 free(remote.ptr);
362
35f69671
EN
363 if (status == LL_MERGE_BINARY_CONFLICT)
364 warning("Cannot merge binary files: %s (%s vs. %s)",
365 oid_to_hex(&p->obj), o->local_ref, o->remote_ref);
809f38c8
JH
366 if ((status < 0) || !result_buf.ptr)
367 die("Failed to execute internal merge");
368
9e5e0c28 369 write_buf_to_worktree(&p->obj, result_buf.ptr, result_buf.size);
809f38c8
JH
370 free(result_buf.ptr);
371
372 return status;
373}
374
375static int merge_one_change_manual(struct notes_merge_options *o,
376 struct notes_merge_pair *p,
377 struct notes_tree *t)
378{
379 const char *lref = o->local_ref ? o->local_ref : "local version";
380 const char *rref = o->remote_ref ? o->remote_ref : "remote version";
381
382 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
383 "local = %.7s, remote = %.7s)\n",
e910bb1e 384 oid_to_hex(&p->obj), oid_to_hex(&p->base),
385 oid_to_hex(&p->local), oid_to_hex(&p->remote));
809f38c8 386
443259cf
JH
387 /* add "Conflicts:" section to commit message first time through */
388 if (!o->has_worktree)
389 strbuf_addstr(&(o->commit_msg), "\n\nConflicts:\n");
390
e910bb1e 391 strbuf_addf(&(o->commit_msg), "\t%s\n", oid_to_hex(&p->obj));
443259cf 392
5f9f8d15 393 if (o->verbosity >= 2)
e910bb1e 394 printf("Auto-merging notes for %s\n", oid_to_hex(&p->obj));
809f38c8 395 check_notes_merge_worktree(o);
e910bb1e 396 if (is_null_oid(&p->local)) {
809f38c8 397 /* D/F conflict, checkout p->remote */
e910bb1e 398 assert(!is_null_oid(&p->remote));
5f9f8d15
JN
399 if (o->verbosity >= 1)
400 printf("CONFLICT (delete/modify): Notes for object %s "
401 "deleted in %s and modified in %s. Version from %s "
402 "left in tree.\n",
e910bb1e 403 oid_to_hex(&p->obj), lref, rref, rref);
9e5e0c28 404 write_note_to_worktree(&p->obj, &p->remote);
e910bb1e 405 } else if (is_null_oid(&p->remote)) {
809f38c8 406 /* D/F conflict, checkout p->local */
e910bb1e 407 assert(!is_null_oid(&p->local));
5f9f8d15
JN
408 if (o->verbosity >= 1)
409 printf("CONFLICT (delete/modify): Notes for object %s "
410 "deleted in %s and modified in %s. Version from %s "
411 "left in tree.\n",
e910bb1e 412 oid_to_hex(&p->obj), rref, lref, lref);
9e5e0c28 413 write_note_to_worktree(&p->obj, &p->local);
809f38c8
JH
414 } else {
415 /* "regular" conflict, checkout result of ll_merge() */
416 const char *reason = "content";
e910bb1e 417 if (is_null_oid(&p->base))
809f38c8 418 reason = "add/add";
e910bb1e 419 assert(!is_null_oid(&p->local));
420 assert(!is_null_oid(&p->remote));
5f9f8d15
JN
421 if (o->verbosity >= 1)
422 printf("CONFLICT (%s): Merge conflict in notes for "
e910bb1e 423 "object %s\n", reason,
424 oid_to_hex(&p->obj));
809f38c8
JH
425 ll_merge_in_worktree(o, p);
426 }
427
428 trace_printf("\t\t\tremoving from partial merge result\n");
e910bb1e 429 remove_note(t, p->obj.hash);
809f38c8
JH
430
431 return 1;
432}
433
3228e671
JH
434static int merge_one_change(struct notes_merge_options *o,
435 struct notes_merge_pair *p, struct notes_tree *t)
436{
437 /*
809f38c8
JH
438 * Return 0 if change is successfully resolved (stored in notes_tree).
439 * Return 1 is change results in a conflict (NOT stored in notes_tree,
440 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
3228e671
JH
441 */
442 switch (o->strategy) {
443 case NOTES_MERGE_RESOLVE_MANUAL:
809f38c8 444 return merge_one_change_manual(o, p, t);
3228e671 445 case NOTES_MERGE_RESOLVE_OURS:
5f9f8d15
JN
446 if (o->verbosity >= 2)
447 printf("Using local notes for %s\n",
e910bb1e 448 oid_to_hex(&p->obj));
3228e671
JH
449 /* nothing to do */
450 return 0;
451 case NOTES_MERGE_RESOLVE_THEIRS:
5f9f8d15
JN
452 if (o->verbosity >= 2)
453 printf("Using remote notes for %s\n",
e910bb1e 454 oid_to_hex(&p->obj));
5ee8a954 455 if (add_note(t, &p->obj, &p->remote, combine_notes_overwrite))
033abf97 456 BUG("combine_notes_overwrite failed");
3228e671
JH
457 return 0;
458 case NOTES_MERGE_RESOLVE_UNION:
5f9f8d15
JN
459 if (o->verbosity >= 2)
460 printf("Concatenating local and remote notes for %s\n",
e910bb1e 461 oid_to_hex(&p->obj));
5ee8a954 462 if (add_note(t, &p->obj, &p->remote, combine_notes_concatenate))
3228e671
JH
463 die("failed to concatenate notes "
464 "(combine_notes_concatenate)");
465 return 0;
a6a09095 466 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ:
5f9f8d15
JN
467 if (o->verbosity >= 2)
468 printf("Concatenating unique lines in local and remote "
e910bb1e 469 "notes for %s\n", oid_to_hex(&p->obj));
5ee8a954 470 if (add_note(t, &p->obj, &p->remote, combine_notes_cat_sort_uniq))
a6a09095
JH
471 die("failed to concatenate notes "
472 "(combine_notes_cat_sort_uniq)");
473 return 0;
3228e671
JH
474 }
475 die("Unknown strategy (%i).", o->strategy);
476}
477
2085b16a
JH
478static int merge_changes(struct notes_merge_options *o,
479 struct notes_merge_pair *changes, int *num_changes,
480 struct notes_tree *t)
481{
482 int i, conflicts = 0;
483
484 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes);
485 for (i = 0; i < *num_changes; i++) {
486 struct notes_merge_pair *p = changes + i;
487 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
e910bb1e 488 oid_to_hex(&p->obj), oid_to_hex(&p->base),
489 oid_to_hex(&p->local),
490 oid_to_hex(&p->remote));
2085b16a 491
4a7e27e9 492 if (oideq(&p->base, &p->remote)) {
2085b16a
JH
493 /* no remote change; nothing to do */
494 trace_printf("\t\t\tskipping (no remote change)\n");
4a7e27e9 495 } else if (oideq(&p->local, &p->remote)) {
2085b16a
JH
496 /* same change in local and remote; nothing to do */
497 trace_printf("\t\t\tskipping (local == remote)\n");
4a7e27e9
JK
498 } else if (oideq(&p->local, &uninitialized) ||
499 oideq(&p->local, &p->base)) {
2085b16a
JH
500 /* no local change; adopt remote change */
501 trace_printf("\t\t\tno local change, adopted remote\n");
5ee8a954 502 if (add_note(t, &p->obj, &p->remote,
2085b16a 503 combine_notes_overwrite))
033abf97 504 BUG("combine_notes_overwrite failed");
2085b16a
JH
505 } else {
506 /* need file-level merge between local and remote */
507 trace_printf("\t\t\tneed content-level merge\n");
3228e671 508 conflicts += merge_one_change(o, p, t);
2085b16a
JH
509 }
510 }
511
512 return conflicts;
513}
514
515static int merge_from_diffs(struct notes_merge_options *o,
9d6babb2
BW
516 const struct object_id *base,
517 const struct object_id *local,
518 const struct object_id *remote,
519 struct notes_tree *t)
2085b16a
JH
520{
521 struct notes_merge_pair *changes;
522 int num_changes, conflicts;
523
524 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
9d6babb2
BW
525 "remote = %.7s)\n", oid_to_hex(base), oid_to_hex(local),
526 oid_to_hex(remote));
2085b16a
JH
527
528 changes = diff_tree_remote(o, base, remote, &num_changes);
529 diff_tree_local(o, changes, num_changes, base, local);
530
531 conflicts = merge_changes(o, changes, &num_changes, t);
532 free(changes);
533
5f9f8d15 534 if (o->verbosity >= 4)
2ca0c53b
NTND
535 printf(t->dirty ?
536 "Merge result: %i unmerged notes and a dirty notes tree\n" :
537 "Merge result: %i unmerged notes and a clean notes tree\n",
538 conflicts);
2085b16a
JH
539
540 return conflicts ? -1 : 1;
541}
542
75ef3f4a 543int notes_merge(struct notes_merge_options *o,
2085b16a 544 struct notes_tree *local_tree,
5237e0eb 545 struct object_id *result_oid)
75ef3f4a 546{
1e43ed98 547 struct object_id local_oid, remote_oid;
75ef3f4a
JH
548 struct commit *local, *remote;
549 struct commit_list *bases = NULL;
5237e0eb 550 const struct object_id *base_oid, *base_tree_oid;
75ef3f4a
JH
551 int result = 0;
552
553 assert(o->local_ref && o->remote_ref);
2085b16a 554 assert(!strcmp(o->local_ref, local_tree->ref));
5237e0eb 555 oidclr(result_oid);
75ef3f4a
JH
556
557 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
558 o->local_ref, o->remote_ref);
559
560 /* Dereference o->local_ref into local_sha1 */
34c290a6 561 if (read_ref_full(o->local_ref, 0, &local_oid, NULL))
75ef3f4a 562 die("Failed to resolve local notes ref '%s'", o->local_ref);
8d9c5010 563 else if (!check_refname_format(o->local_ref, 0) &&
1e43ed98 564 is_null_oid(&local_oid))
5237e0eb 565 local = NULL; /* local_oid == null_oid indicates unborn ref */
878d8329 566 else if (!(local = lookup_commit_reference(o->repo, &local_oid)))
75ef3f4a 567 die("Could not parse local commit %s (%s)",
1e43ed98 568 oid_to_hex(&local_oid), o->local_ref);
569 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid));
75ef3f4a 570
5237e0eb 571 /* Dereference o->remote_ref into remote_oid */
1e43ed98 572 if (get_oid(o->remote_ref, &remote_oid)) {
75ef3f4a 573 /*
5237e0eb 574 * Failed to get remote_oid. If o->remote_ref looks like an
75ef3f4a
JH
575 * unborn ref, perform the merge using an empty notes tree.
576 */
8d9c5010 577 if (!check_refname_format(o->remote_ref, 0)) {
1e43ed98 578 oidclr(&remote_oid);
75ef3f4a
JH
579 remote = NULL;
580 } else {
581 die("Failed to resolve remote notes ref '%s'",
582 o->remote_ref);
583 }
878d8329 584 } else if (!(remote = lookup_commit_reference(o->repo, &remote_oid))) {
75ef3f4a 585 die("Could not parse remote commit %s (%s)",
1e43ed98 586 oid_to_hex(&remote_oid), o->remote_ref);
75ef3f4a 587 }
1e43ed98 588 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid));
75ef3f4a
JH
589
590 if (!local && !remote)
591 die("Cannot merge empty notes ref (%s) into empty notes ref "
592 "(%s)", o->remote_ref, o->local_ref);
593 if (!local) {
594 /* result == remote commit */
5237e0eb 595 oidcpy(result_oid, &remote_oid);
75ef3f4a
JH
596 goto found_result;
597 }
598 if (!remote) {
599 /* result == local commit */
5237e0eb 600 oidcpy(result_oid, &local_oid);
75ef3f4a
JH
601 goto found_result;
602 }
603 assert(local && remote);
604
605 /* Find merge bases */
2ce406cc 606 bases = get_merge_bases(local, remote);
75ef3f4a 607 if (!bases) {
14228447 608 base_oid = null_oid();
eb0ccfd7 609 base_tree_oid = the_hash_algo->empty_tree;
5f9f8d15
JN
610 if (o->verbosity >= 4)
611 printf("No merge base found; doing history-less merge\n");
75ef3f4a 612 } else if (!bases->next) {
5237e0eb 613 base_oid = &bases->item->object.oid;
2e27bd77 614 base_tree_oid = get_commit_tree_oid(bases->item);
5f9f8d15
JN
615 if (o->verbosity >= 4)
616 printf("One merge base found (%.7s)\n",
5237e0eb 617 oid_to_hex(base_oid));
75ef3f4a
JH
618 } else {
619 /* TODO: How to handle multiple merge-bases? */
5237e0eb 620 base_oid = &bases->item->object.oid;
2e27bd77 621 base_tree_oid = get_commit_tree_oid(bases->item);
5f9f8d15
JN
622 if (o->verbosity >= 3)
623 printf("Multiple merge bases found. Using the first "
5237e0eb 624 "(%.7s)\n", oid_to_hex(base_oid));
75ef3f4a
JH
625 }
626
5f9f8d15
JN
627 if (o->verbosity >= 4)
628 printf("Merging remote commit %.7s into local commit %.7s with "
f2fd0760 629 "merge-base %.7s\n", oid_to_hex(&remote->object.oid),
630 oid_to_hex(&local->object.oid),
5237e0eb 631 oid_to_hex(base_oid));
75ef3f4a 632
4a7e27e9 633 if (oideq(&remote->object.oid, base_oid)) {
75ef3f4a 634 /* Already merged; result == local commit */
5f9f8d15 635 if (o->verbosity >= 2)
80cde95e 636 printf_ln("Already up to date.");
5237e0eb 637 oidcpy(result_oid, &local->object.oid);
75ef3f4a
JH
638 goto found_result;
639 }
4a7e27e9 640 if (oideq(&local->object.oid, base_oid)) {
75ef3f4a 641 /* Fast-forward; result == remote commit */
5f9f8d15
JN
642 if (o->verbosity >= 2)
643 printf("Fast-forward\n");
5237e0eb 644 oidcpy(result_oid, &remote->object.oid);
75ef3f4a
JH
645 goto found_result;
646 }
647
2e27bd77
DS
648 result = merge_from_diffs(o, base_tree_oid,
649 get_commit_tree_oid(local),
650 get_commit_tree_oid(remote), local_tree);
2085b16a 651
809f38c8
JH
652 if (result != 0) { /* non-trivial merge (with or without conflicts) */
653 /* Commit (partial) result */
2085b16a
JH
654 struct commit_list *parents = NULL;
655 commit_list_insert(remote, &parents); /* LIFO order */
656 commit_list_insert(local, &parents);
1d18d758 657 create_notes_commit(o->repo, local_tree, parents, o->commit_msg.buf,
5078f344 658 o->commit_msg.len, result_oid);
2085b16a 659 }
75ef3f4a
JH
660
661found_result:
662 free_commit_list(bases);
443259cf 663 strbuf_release(&(o->commit_msg));
5237e0eb
BW
664 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
665 result, oid_to_hex(result_oid));
75ef3f4a
JH
666 return result;
667}
6abb3655
JH
668
669int notes_merge_commit(struct notes_merge_options *o,
670 struct notes_tree *partial_tree,
671 struct commit *partial_commit,
5237e0eb 672 struct object_id *result_oid)
6abb3655
JH
673{
674 /*
675 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
a0be62c1 676 * found notes to 'partial_tree'. Write the updated notes tree to
6abb3655
JH
677 * the DB, and commit the resulting tree object while reusing the
678 * commit message and parents from 'partial_commit'.
5237e0eb 679 * Finally store the new commit object OID into 'result_oid'.
6abb3655 680 */
a0be62c1
JH
681 DIR *dir;
682 struct dirent *e;
683 struct strbuf path = STRBUF_INIT;
8597ea3a 684 const char *buffer = get_commit_buffer(partial_commit, NULL);
bc6b8fc1 685 const char *msg = strstr(buffer, "\n\n");
a0be62c1 686 int baselen;
6abb3655 687
8c2ca3a6 688 git_path_buf(&path, NOTES_MERGE_WORKTREE);
5f9f8d15 689 if (o->verbosity >= 3)
a0be62c1
JH
690 printf("Committing notes in notes merge worktree at %s\n",
691 path.buf);
6abb3655
JH
692
693 if (!msg || msg[2] == '\0')
694 die("partial notes commit has empty message");
695 msg += 2;
696
a0be62c1
JH
697 dir = opendir(path.buf);
698 if (!dir)
699 die_errno("could not open %s", path.buf);
700
701 strbuf_addch(&path, '/');
702 baselen = path.len;
b548f0f1 703 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
6abb3655 704 struct stat st;
5ee8a954 705 struct object_id obj_oid, blob_oid;
6abb3655 706
5ee8a954 707 if (get_oid_hex(e->d_name, &obj_oid)) {
5f9f8d15 708 if (o->verbosity >= 3)
a0be62c1
JH
709 printf("Skipping non-SHA1 entry '%s%s'\n",
710 path.buf, e->d_name);
6abb3655
JH
711 continue;
712 }
713
a0be62c1 714 strbuf_addstr(&path, e->d_name);
6abb3655 715 /* write file as blob, and add to partial_tree */
a0be62c1
JH
716 if (stat(path.buf, &st))
717 die_errno("Failed to stat '%s'", path.buf);
5684200f 718 if (index_path(o->repo->index, &blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
a0be62c1 719 die("Failed to write blob object from '%s'", path.buf);
5ee8a954 720 if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
6abb3655 721 die("Failed to add resolved note '%s' to notes tree",
a0be62c1 722 path.buf);
5f9f8d15
JN
723 if (o->verbosity >= 4)
724 printf("Added resolved note for object %s: %s\n",
5ee8a954 725 oid_to_hex(&obj_oid), oid_to_hex(&blob_oid));
a0be62c1 726 strbuf_setlen(&path, baselen);
6abb3655
JH
727 }
728
1d18d758 729 create_notes_commit(o->repo, partial_tree, partial_commit->parents, msg,
5078f344 730 strlen(msg), result_oid);
bc6b8fc1 731 unuse_commit_buffer(partial_commit, buffer);
5f9f8d15
JN
732 if (o->verbosity >= 4)
733 printf("Finalized notes merge commit: %s\n",
5237e0eb 734 oid_to_hex(result_oid));
a0be62c1
JH
735 strbuf_release(&path);
736 closedir(dir);
6abb3655
JH
737 return 0;
738}
739
740int notes_merge_abort(struct notes_merge_options *o)
741{
dabba590
JH
742 /*
743 * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
744 * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
745 * the current working directory of the user.
746 */
6abb3655
JH
747 struct strbuf buf = STRBUF_INIT;
748 int ret;
749
8c2ca3a6 750 git_path_buf(&buf, NOTES_MERGE_WORKTREE);
5f9f8d15 751 if (o->verbosity >= 3)
dabba590
JH
752 printf("Removing notes merge worktree at %s/*\n", buf.buf);
753 ret = remove_dir_recursively(&buf, REMOVE_DIR_KEEP_TOPLEVEL);
6abb3655
JH
754 strbuf_release(&buf);
755 return ret;
756}