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