5 #include "object-store.h"
6 #include "repository.h"
10 #include "xdiff-interface.h"
14 #include "notes-merge.h"
16 #include "notes-utils.h"
17 #include "commit-reach.h"
19 struct notes_merge_pair
{
20 struct object_id obj
, base
, local
, remote
;
23 void init_notes_merge_options(struct repository
*r
,
24 struct notes_merge_options
*o
)
26 memset(o
, 0, sizeof(struct notes_merge_options
));
27 strbuf_init(&(o
->commit_msg
), 0);
28 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
32 static int path_to_oid(const char *path
, struct object_id
*oid
)
34 char hex_oid
[GIT_MAX_HEXSZ
];
36 while (*path
&& i
< the_hash_algo
->hexsz
) {
41 if (*path
|| i
!= the_hash_algo
->hexsz
)
43 return get_oid_hex(hex_oid
, oid
);
46 static int verify_notes_filepair(struct diff_filepair
*p
, struct object_id
*oid
)
49 case DIFF_STATUS_MODIFIED
:
50 assert(p
->one
->mode
== p
->two
->mode
);
51 assert(!is_null_oid(&p
->one
->oid
));
52 assert(!is_null_oid(&p
->two
->oid
));
54 case DIFF_STATUS_ADDED
:
55 assert(is_null_oid(&p
->one
->oid
));
57 case DIFF_STATUS_DELETED
:
58 assert(is_null_oid(&p
->two
->oid
));
63 assert(!strcmp(p
->one
->path
, p
->two
->path
));
64 return path_to_oid(p
->one
->path
, oid
);
67 static struct notes_merge_pair
*find_notes_merge_pair_pos(
68 struct notes_merge_pair
*list
, int len
, struct object_id
*obj
,
69 int insert_new
, int *occupied
)
72 * Both diff_tree_remote() and diff_tree_local() tend to process
73 * merge_pairs in ascending order. Therefore, cache last returned
74 * index, and search sequentially from there until the appropriate
77 * Since inserts only happen from diff_tree_remote() (which mainly
78 * _appends_), we don't care that inserting into the middle of the
79 * list is expensive (using memmove()).
81 static int last_index
;
82 int i
= last_index
< len
? last_index
: len
- 1;
83 int prev_cmp
= 0, cmp
= -1;
84 while (i
>= 0 && i
< len
) {
85 cmp
= oidcmp(obj
, &list
[i
].obj
);
86 if (!cmp
) /* obj belongs @ i */
88 else if (cmp
< 0 && prev_cmp
<= 0) /* obj belongs < i */
90 else if (cmp
< 0) /* obj belongs between i-1 and i */
92 else if (cmp
> 0 && prev_cmp
>= 0) /* obj belongs > i */
94 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
102 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
108 if (insert_new
&& i
< len
) {
109 MOVE_ARRAY(list
+ i
+ 1, list
+ i
, len
- i
);
110 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
117 static struct object_id uninitialized
= {
119 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
120 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
123 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
124 const struct object_id
*base
,
125 const struct object_id
*remote
,
128 struct diff_options opt
;
129 struct notes_merge_pair
*changes
;
132 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
133 oid_to_hex(base
), oid_to_hex(remote
));
135 repo_diff_setup(o
->repo
, &opt
);
136 opt
.flags
.recursive
= 1;
137 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
138 diff_setup_done(&opt
);
139 diff_tree_oid(base
, remote
, "", &opt
);
142 CALLOC_ARRAY(changes
, diff_queued_diff
.nr
);
144 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
145 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
146 struct notes_merge_pair
*mp
;
148 struct object_id obj
;
150 if (verify_notes_filepair(p
, &obj
)) {
151 trace_printf("\t\tCannot merge entry '%s' (%c): "
152 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
153 p
->status
, oid_to_hex(&p
->one
->oid
),
154 oid_to_hex(&p
->two
->oid
));
157 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 1, &occupied
);
159 /* We've found an addition/deletion pair */
160 assert(oideq(&mp
->obj
, &obj
));
161 if (is_null_oid(&p
->one
->oid
)) { /* addition */
162 assert(is_null_oid(&mp
->remote
));
163 oidcpy(&mp
->remote
, &p
->two
->oid
);
164 } else if (is_null_oid(&p
->two
->oid
)) { /* deletion */
165 assert(is_null_oid(&mp
->base
));
166 oidcpy(&mp
->base
, &p
->one
->oid
);
168 assert(!"Invalid existing change recorded");
170 oidcpy(&mp
->obj
, &obj
);
171 oidcpy(&mp
->base
, &p
->one
->oid
);
172 oidcpy(&mp
->local
, &uninitialized
);
173 oidcpy(&mp
->remote
, &p
->two
->oid
);
176 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
177 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
178 oid_to_hex(&mp
->remote
));
186 static void diff_tree_local(struct notes_merge_options
*o
,
187 struct notes_merge_pair
*changes
, int len
,
188 const struct object_id
*base
,
189 const struct object_id
*local
)
191 struct diff_options opt
;
194 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
195 len
, oid_to_hex(base
), oid_to_hex(local
));
197 repo_diff_setup(o
->repo
, &opt
);
198 opt
.flags
.recursive
= 1;
199 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
200 diff_setup_done(&opt
);
201 diff_tree_oid(base
, local
, "", &opt
);
204 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
205 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
206 struct notes_merge_pair
*mp
;
208 struct object_id obj
;
210 if (verify_notes_filepair(p
, &obj
)) {
211 trace_printf("\t\tCannot merge entry '%s' (%c): "
212 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
213 p
->status
, oid_to_hex(&p
->one
->oid
),
214 oid_to_hex(&p
->two
->oid
));
217 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 0, &match
);
219 trace_printf("\t\tIgnoring local-only change for %s: "
220 "%.7s -> %.7s\n", oid_to_hex(&obj
),
221 oid_to_hex(&p
->one
->oid
),
222 oid_to_hex(&p
->two
->oid
));
226 assert(oideq(&mp
->obj
, &obj
));
227 if (is_null_oid(&p
->two
->oid
)) { /* deletion */
229 * Either this is a true deletion (1), or it is part
230 * of an A/D pair (2), or D/A pair (3):
232 * (1) mp->local is uninitialized; set it to null_sha1
233 * (2) mp->local is not uninitialized; don't touch it
234 * (3) mp->local is uninitialized; set it to null_sha1
235 * (will be overwritten by following addition)
237 if (oideq(&mp
->local
, &uninitialized
))
239 } else if (is_null_oid(&p
->one
->oid
)) { /* addition */
241 * Either this is a true addition (1), or it is part
242 * of an A/D pair (2), or D/A pair (3):
244 * (1) mp->local is uninitialized; set to p->two->sha1
245 * (2) mp->local is uninitialized; set to p->two->sha1
246 * (3) mp->local is null_sha1; set to p->two->sha1
248 assert(is_null_oid(&mp
->local
) ||
249 oideq(&mp
->local
, &uninitialized
));
250 oidcpy(&mp
->local
, &p
->two
->oid
);
251 } else { /* modification */
253 * This is a true modification. p->one->sha1 shall
254 * match mp->base, and mp->local shall be uninitialized.
255 * Set mp->local to p->two->sha1.
257 assert(oideq(&p
->one
->oid
, &mp
->base
));
258 assert(oideq(&mp
->local
, &uninitialized
));
259 oidcpy(&mp
->local
, &p
->two
->oid
);
261 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
262 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
263 oid_to_hex(&mp
->local
));
268 static void check_notes_merge_worktree(struct notes_merge_options
*o
)
270 if (!o
->has_worktree
) {
272 * Must establish NOTES_MERGE_WORKTREE.
273 * Abort if NOTES_MERGE_WORKTREE already exists
275 if (file_exists(git_path(NOTES_MERGE_WORKTREE
)) &&
276 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE
))) {
277 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
278 die(_("You have not concluded your previous "
279 "notes merge (%s exists).\nPlease, use "
280 "'git notes merge --commit' or 'git notes "
281 "merge --abort' to commit/abort the "
282 "previous merge before you start a new "
283 "notes merge."), git_path("NOTES_MERGE_*"));
285 die(_("You have not concluded your notes merge "
286 "(%s exists)."), git_path("NOTES_MERGE_*"));
289 if (safe_create_leading_directories_const(git_path(
290 NOTES_MERGE_WORKTREE
"/.test")))
291 die_errno("unable to create directory %s",
292 git_path(NOTES_MERGE_WORKTREE
));
294 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE
)))
295 /* NOTES_MERGE_WORKTREE should already be established */
296 die("missing '%s'. This should not happen",
297 git_path(NOTES_MERGE_WORKTREE
));
300 static void write_buf_to_worktree(const struct object_id
*obj
,
301 const char *buf
, unsigned long size
)
304 char *path
= git_pathdup(NOTES_MERGE_WORKTREE
"/%s", oid_to_hex(obj
));
305 if (safe_create_leading_directories_const(path
))
306 die_errno("unable to create directory for '%s'", path
);
308 fd
= xopen(path
, O_WRONLY
| O_EXCL
| O_CREAT
, 0666);
311 ssize_t ret
= write_in_full(fd
, buf
, size
);
316 die_errno("notes-merge");
326 static void write_note_to_worktree(const struct object_id
*obj
,
327 const struct object_id
*note
)
329 enum object_type type
;
331 void *buf
= read_object_file(note
, &type
, &size
);
334 die("cannot read note %s for object %s",
335 oid_to_hex(note
), oid_to_hex(obj
));
336 if (type
!= OBJ_BLOB
)
337 die("blob expected in note %s for object %s",
338 oid_to_hex(note
), oid_to_hex(obj
));
339 write_buf_to_worktree(obj
, buf
, size
);
343 static int ll_merge_in_worktree(struct notes_merge_options
*o
,
344 struct notes_merge_pair
*p
)
346 mmbuffer_t result_buf
;
347 mmfile_t base
, local
, remote
;
348 enum ll_merge_result status
;
350 read_mmblob(&base
, &p
->base
);
351 read_mmblob(&local
, &p
->local
);
352 read_mmblob(&remote
, &p
->remote
);
354 status
= ll_merge(&result_buf
, oid_to_hex(&p
->obj
), &base
, NULL
,
355 &local
, o
->local_ref
, &remote
, o
->remote_ref
,
356 o
->repo
->index
, NULL
);
362 if (status
== LL_MERGE_BINARY_CONFLICT
)
363 warning("Cannot merge binary files: %s (%s vs. %s)",
364 oid_to_hex(&p
->obj
), o
->local_ref
, o
->remote_ref
);
365 if ((status
< 0) || !result_buf
.ptr
)
366 die("Failed to execute internal merge");
368 write_buf_to_worktree(&p
->obj
, result_buf
.ptr
, result_buf
.size
);
369 free(result_buf
.ptr
);
374 static int merge_one_change_manual(struct notes_merge_options
*o
,
375 struct notes_merge_pair
*p
,
376 struct notes_tree
*t
)
378 const char *lref
= o
->local_ref
? o
->local_ref
: "local version";
379 const char *rref
= o
->remote_ref
? o
->remote_ref
: "remote version";
381 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
382 "local = %.7s, remote = %.7s)\n",
383 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
384 oid_to_hex(&p
->local
), oid_to_hex(&p
->remote
));
386 /* add "Conflicts:" section to commit message first time through */
387 if (!o
->has_worktree
)
388 strbuf_addstr(&(o
->commit_msg
), "\n\nConflicts:\n");
390 strbuf_addf(&(o
->commit_msg
), "\t%s\n", oid_to_hex(&p
->obj
));
392 if (o
->verbosity
>= 2)
393 printf("Auto-merging notes for %s\n", oid_to_hex(&p
->obj
));
394 check_notes_merge_worktree(o
);
395 if (is_null_oid(&p
->local
)) {
396 /* D/F conflict, checkout p->remote */
397 assert(!is_null_oid(&p
->remote
));
398 if (o
->verbosity
>= 1)
399 printf("CONFLICT (delete/modify): Notes for object %s "
400 "deleted in %s and modified in %s. Version from %s "
402 oid_to_hex(&p
->obj
), lref
, rref
, rref
);
403 write_note_to_worktree(&p
->obj
, &p
->remote
);
404 } else if (is_null_oid(&p
->remote
)) {
405 /* D/F conflict, checkout p->local */
406 assert(!is_null_oid(&p
->local
));
407 if (o
->verbosity
>= 1)
408 printf("CONFLICT (delete/modify): Notes for object %s "
409 "deleted in %s and modified in %s. Version from %s "
411 oid_to_hex(&p
->obj
), rref
, lref
, lref
);
412 write_note_to_worktree(&p
->obj
, &p
->local
);
414 /* "regular" conflict, checkout result of ll_merge() */
415 const char *reason
= "content";
416 if (is_null_oid(&p
->base
))
418 assert(!is_null_oid(&p
->local
));
419 assert(!is_null_oid(&p
->remote
));
420 if (o
->verbosity
>= 1)
421 printf("CONFLICT (%s): Merge conflict in notes for "
422 "object %s\n", reason
,
423 oid_to_hex(&p
->obj
));
424 ll_merge_in_worktree(o
, p
);
427 trace_printf("\t\t\tremoving from partial merge result\n");
428 remove_note(t
, p
->obj
.hash
);
433 static int merge_one_change(struct notes_merge_options
*o
,
434 struct notes_merge_pair
*p
, struct notes_tree
*t
)
437 * Return 0 if change is successfully resolved (stored in notes_tree).
438 * Return 1 is change results in a conflict (NOT stored in notes_tree,
439 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
441 switch (o
->strategy
) {
442 case NOTES_MERGE_RESOLVE_MANUAL
:
443 return merge_one_change_manual(o
, p
, t
);
444 case NOTES_MERGE_RESOLVE_OURS
:
445 if (o
->verbosity
>= 2)
446 printf("Using local notes for %s\n",
447 oid_to_hex(&p
->obj
));
450 case NOTES_MERGE_RESOLVE_THEIRS
:
451 if (o
->verbosity
>= 2)
452 printf("Using remote notes for %s\n",
453 oid_to_hex(&p
->obj
));
454 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_overwrite
))
455 BUG("combine_notes_overwrite failed");
457 case NOTES_MERGE_RESOLVE_UNION
:
458 if (o
->verbosity
>= 2)
459 printf("Concatenating local and remote notes for %s\n",
460 oid_to_hex(&p
->obj
));
461 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_concatenate
))
462 die("failed to concatenate notes "
463 "(combine_notes_concatenate)");
465 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
:
466 if (o
->verbosity
>= 2)
467 printf("Concatenating unique lines in local and remote "
468 "notes for %s\n", oid_to_hex(&p
->obj
));
469 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_cat_sort_uniq
))
470 die("failed to concatenate notes "
471 "(combine_notes_cat_sort_uniq)");
474 die("Unknown strategy (%i).", o
->strategy
);
477 static int merge_changes(struct notes_merge_options
*o
,
478 struct notes_merge_pair
*changes
, int *num_changes
,
479 struct notes_tree
*t
)
481 int i
, conflicts
= 0;
483 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes
);
484 for (i
= 0; i
< *num_changes
; i
++) {
485 struct notes_merge_pair
*p
= changes
+ i
;
486 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
487 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
488 oid_to_hex(&p
->local
),
489 oid_to_hex(&p
->remote
));
491 if (oideq(&p
->base
, &p
->remote
)) {
492 /* no remote change; nothing to do */
493 trace_printf("\t\t\tskipping (no remote change)\n");
494 } else if (oideq(&p
->local
, &p
->remote
)) {
495 /* same change in local and remote; nothing to do */
496 trace_printf("\t\t\tskipping (local == remote)\n");
497 } else if (oideq(&p
->local
, &uninitialized
) ||
498 oideq(&p
->local
, &p
->base
)) {
499 /* no local change; adopt remote change */
500 trace_printf("\t\t\tno local change, adopted remote\n");
501 if (add_note(t
, &p
->obj
, &p
->remote
,
502 combine_notes_overwrite
))
503 BUG("combine_notes_overwrite failed");
505 /* need file-level merge between local and remote */
506 trace_printf("\t\t\tneed content-level merge\n");
507 conflicts
+= merge_one_change(o
, p
, t
);
514 static int merge_from_diffs(struct notes_merge_options
*o
,
515 const struct object_id
*base
,
516 const struct object_id
*local
,
517 const struct object_id
*remote
,
518 struct notes_tree
*t
)
520 struct notes_merge_pair
*changes
;
521 int num_changes
, conflicts
;
523 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
524 "remote = %.7s)\n", oid_to_hex(base
), oid_to_hex(local
),
527 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
528 diff_tree_local(o
, changes
, num_changes
, base
, local
);
530 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
533 if (o
->verbosity
>= 4)
535 "Merge result: %i unmerged notes and a dirty notes tree\n" :
536 "Merge result: %i unmerged notes and a clean notes tree\n",
539 return conflicts
? -1 : 1;
542 int notes_merge(struct notes_merge_options
*o
,
543 struct notes_tree
*local_tree
,
544 struct object_id
*result_oid
)
546 struct object_id local_oid
, remote_oid
;
547 struct commit
*local
, *remote
;
548 struct commit_list
*bases
= NULL
;
549 const struct object_id
*base_oid
, *base_tree_oid
;
552 assert(o
->local_ref
&& o
->remote_ref
);
553 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
556 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
557 o
->local_ref
, o
->remote_ref
);
559 /* Dereference o->local_ref into local_sha1 */
560 if (read_ref_full(o
->local_ref
, 0, &local_oid
, NULL
))
561 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
562 else if (!check_refname_format(o
->local_ref
, 0) &&
563 is_null_oid(&local_oid
))
564 local
= NULL
; /* local_oid == null_oid indicates unborn ref */
565 else if (!(local
= lookup_commit_reference(o
->repo
, &local_oid
)))
566 die("Could not parse local commit %s (%s)",
567 oid_to_hex(&local_oid
), o
->local_ref
);
568 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid
));
570 /* Dereference o->remote_ref into remote_oid */
571 if (get_oid(o
->remote_ref
, &remote_oid
)) {
573 * Failed to get remote_oid. If o->remote_ref looks like an
574 * unborn ref, perform the merge using an empty notes tree.
576 if (!check_refname_format(o
->remote_ref
, 0)) {
580 die("Failed to resolve remote notes ref '%s'",
583 } else if (!(remote
= lookup_commit_reference(o
->repo
, &remote_oid
))) {
584 die("Could not parse remote commit %s (%s)",
585 oid_to_hex(&remote_oid
), o
->remote_ref
);
587 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid
));
589 if (!local
&& !remote
)
590 die("Cannot merge empty notes ref (%s) into empty notes ref "
591 "(%s)", o
->remote_ref
, o
->local_ref
);
593 /* result == remote commit */
594 oidcpy(result_oid
, &remote_oid
);
598 /* result == local commit */
599 oidcpy(result_oid
, &local_oid
);
602 assert(local
&& remote
);
604 /* Find merge bases */
605 bases
= get_merge_bases(local
, remote
);
607 base_oid
= null_oid();
608 base_tree_oid
= the_hash_algo
->empty_tree
;
609 if (o
->verbosity
>= 4)
610 printf("No merge base found; doing history-less merge\n");
611 } else if (!bases
->next
) {
612 base_oid
= &bases
->item
->object
.oid
;
613 base_tree_oid
= get_commit_tree_oid(bases
->item
);
614 if (o
->verbosity
>= 4)
615 printf("One merge base found (%.7s)\n",
616 oid_to_hex(base_oid
));
618 /* TODO: How to handle multiple merge-bases? */
619 base_oid
= &bases
->item
->object
.oid
;
620 base_tree_oid
= get_commit_tree_oid(bases
->item
);
621 if (o
->verbosity
>= 3)
622 printf("Multiple merge bases found. Using the first "
623 "(%.7s)\n", oid_to_hex(base_oid
));
626 if (o
->verbosity
>= 4)
627 printf("Merging remote commit %.7s into local commit %.7s with "
628 "merge-base %.7s\n", oid_to_hex(&remote
->object
.oid
),
629 oid_to_hex(&local
->object
.oid
),
630 oid_to_hex(base_oid
));
632 if (oideq(&remote
->object
.oid
, base_oid
)) {
633 /* Already merged; result == local commit */
634 if (o
->verbosity
>= 2)
635 printf_ln("Already up to date.");
636 oidcpy(result_oid
, &local
->object
.oid
);
639 if (oideq(&local
->object
.oid
, base_oid
)) {
640 /* Fast-forward; result == remote commit */
641 if (o
->verbosity
>= 2)
642 printf("Fast-forward\n");
643 oidcpy(result_oid
, &remote
->object
.oid
);
647 result
= merge_from_diffs(o
, base_tree_oid
,
648 get_commit_tree_oid(local
),
649 get_commit_tree_oid(remote
), local_tree
);
651 if (result
!= 0) { /* non-trivial merge (with or without conflicts) */
652 /* Commit (partial) result */
653 struct commit_list
*parents
= NULL
;
654 commit_list_insert(remote
, &parents
); /* LIFO order */
655 commit_list_insert(local
, &parents
);
656 create_notes_commit(o
->repo
, local_tree
, parents
, o
->commit_msg
.buf
,
657 o
->commit_msg
.len
, result_oid
);
661 free_commit_list(bases
);
662 strbuf_release(&(o
->commit_msg
));
663 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
664 result
, oid_to_hex(result_oid
));
668 int notes_merge_commit(struct notes_merge_options
*o
,
669 struct notes_tree
*partial_tree
,
670 struct commit
*partial_commit
,
671 struct object_id
*result_oid
)
674 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
675 * found notes to 'partial_tree'. Write the updated notes tree to
676 * the DB, and commit the resulting tree object while reusing the
677 * commit message and parents from 'partial_commit'.
678 * Finally store the new commit object OID into 'result_oid'.
682 struct strbuf path
= STRBUF_INIT
;
683 const char *buffer
= get_commit_buffer(partial_commit
, NULL
);
684 const char *msg
= strstr(buffer
, "\n\n");
687 git_path_buf(&path
, NOTES_MERGE_WORKTREE
);
688 if (o
->verbosity
>= 3)
689 printf("Committing notes in notes merge worktree at %s\n",
692 if (!msg
|| msg
[2] == '\0')
693 die("partial notes commit has empty message");
696 dir
= opendir(path
.buf
);
698 die_errno("could not open %s", path
.buf
);
700 strbuf_addch(&path
, '/');
702 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
704 struct object_id obj_oid
, blob_oid
;
706 if (get_oid_hex(e
->d_name
, &obj_oid
)) {
707 if (o
->verbosity
>= 3)
708 printf("Skipping non-SHA1 entry '%s%s'\n",
709 path
.buf
, e
->d_name
);
713 strbuf_addstr(&path
, e
->d_name
);
714 /* write file as blob, and add to partial_tree */
715 if (stat(path
.buf
, &st
))
716 die_errno("Failed to stat '%s'", path
.buf
);
717 if (index_path(o
->repo
->index
, &blob_oid
, path
.buf
, &st
, HASH_WRITE_OBJECT
))
718 die("Failed to write blob object from '%s'", path
.buf
);
719 if (add_note(partial_tree
, &obj_oid
, &blob_oid
, NULL
))
720 die("Failed to add resolved note '%s' to notes tree",
722 if (o
->verbosity
>= 4)
723 printf("Added resolved note for object %s: %s\n",
724 oid_to_hex(&obj_oid
), oid_to_hex(&blob_oid
));
725 strbuf_setlen(&path
, baselen
);
728 create_notes_commit(o
->repo
, partial_tree
, partial_commit
->parents
, msg
,
729 strlen(msg
), result_oid
);
730 unuse_commit_buffer(partial_commit
, buffer
);
731 if (o
->verbosity
>= 4)
732 printf("Finalized notes merge commit: %s\n",
733 oid_to_hex(result_oid
));
734 strbuf_release(&path
);
739 int notes_merge_abort(struct notes_merge_options
*o
)
742 * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
743 * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
744 * the current working directory of the user.
746 struct strbuf buf
= STRBUF_INIT
;
749 git_path_buf(&buf
, NOTES_MERGE_WORKTREE
);
750 if (o
->verbosity
>= 3)
751 printf("Removing notes merge worktree at %s/*\n", buf
.buf
);
752 ret
= remove_dir_recursively(&buf
, REMOVE_DIR_KEEP_TOPLEVEL
);
753 strbuf_release(&buf
);