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