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