1 #include "git-compat-util.h"
6 #include "object-file.h"
7 #include "object-name.h"
8 #include "object-store-ll.h"
10 #include "repository.h"
14 #include "xdiff-interface.h"
18 #include "notes-merge.h"
21 #include "notes-utils.h"
22 #include "commit-reach.h"
25 struct notes_merge_pair
{
26 struct object_id obj
, base
, local
, remote
;
29 void init_notes_merge_options(struct repository
*r
,
30 struct notes_merge_options
*o
)
32 memset(o
, 0, sizeof(struct notes_merge_options
));
33 strbuf_init(&(o
->commit_msg
), 0);
34 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
38 static int path_to_oid(const char *path
, struct object_id
*oid
)
40 char hex_oid
[GIT_MAX_HEXSZ
];
42 while (*path
&& i
< the_hash_algo
->hexsz
) {
47 if (*path
|| i
!= the_hash_algo
->hexsz
)
49 return get_oid_hex(hex_oid
, oid
);
52 static int verify_notes_filepair(struct diff_filepair
*p
, struct object_id
*oid
)
55 case DIFF_STATUS_MODIFIED
:
56 assert(p
->one
->mode
== p
->two
->mode
);
57 assert(!is_null_oid(&p
->one
->oid
));
58 assert(!is_null_oid(&p
->two
->oid
));
60 case DIFF_STATUS_ADDED
:
61 assert(is_null_oid(&p
->one
->oid
));
63 case DIFF_STATUS_DELETED
:
64 assert(is_null_oid(&p
->two
->oid
));
69 assert(!strcmp(p
->one
->path
, p
->two
->path
));
70 return path_to_oid(p
->one
->path
, oid
);
73 static struct notes_merge_pair
*find_notes_merge_pair_pos(
74 struct notes_merge_pair
*list
, int len
, struct object_id
*obj
,
75 int insert_new
, int *occupied
)
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
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()).
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
) {
91 cmp
= oidcmp(obj
, &list
[i
].obj
);
92 if (!cmp
) /* obj belongs @ i */
94 else if (cmp
< 0 && prev_cmp
<= 0) /* obj belongs < i */
96 else if (cmp
< 0) /* obj belongs between i-1 and i */
98 else if (cmp
> 0 && prev_cmp
>= 0) /* obj belongs > i */
100 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
108 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
114 if (insert_new
&& i
< len
) {
115 MOVE_ARRAY(list
+ i
+ 1, list
+ i
, len
- i
);
116 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
123 static struct object_id uninitialized
= {
125 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
126 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
129 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
130 const struct object_id
*base
,
131 const struct object_id
*remote
,
134 struct diff_options opt
;
135 struct notes_merge_pair
*changes
;
138 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
139 oid_to_hex(base
), oid_to_hex(remote
));
141 repo_diff_setup(o
->repo
, &opt
);
142 opt
.flags
.recursive
= 1;
143 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
144 diff_setup_done(&opt
);
145 diff_tree_oid(base
, remote
, "", &opt
);
148 CALLOC_ARRAY(changes
, diff_queued_diff
.nr
);
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
;
154 struct object_id obj
;
156 if (verify_notes_filepair(p
, &obj
)) {
157 trace_printf("\t\tCannot merge entry '%s' (%c): "
158 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
159 p
->status
, oid_to_hex(&p
->one
->oid
),
160 oid_to_hex(&p
->two
->oid
));
163 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 1, &occupied
);
165 /* We've found an addition/deletion pair */
166 assert(oideq(&mp
->obj
, &obj
));
167 if (is_null_oid(&p
->one
->oid
)) { /* addition */
168 assert(is_null_oid(&mp
->remote
));
169 oidcpy(&mp
->remote
, &p
->two
->oid
);
170 } else if (is_null_oid(&p
->two
->oid
)) { /* deletion */
171 assert(is_null_oid(&mp
->base
));
172 oidcpy(&mp
->base
, &p
->one
->oid
);
174 assert(!"Invalid existing change recorded");
176 oidcpy(&mp
->obj
, &obj
);
177 oidcpy(&mp
->base
, &p
->one
->oid
);
178 oidcpy(&mp
->local
, &uninitialized
);
179 oidcpy(&mp
->remote
, &p
->two
->oid
);
182 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
183 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
184 oid_to_hex(&mp
->remote
));
192 static void diff_tree_local(struct notes_merge_options
*o
,
193 struct notes_merge_pair
*changes
, int len
,
194 const struct object_id
*base
,
195 const struct object_id
*local
)
197 struct diff_options opt
;
200 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
201 len
, oid_to_hex(base
), oid_to_hex(local
));
203 repo_diff_setup(o
->repo
, &opt
);
204 opt
.flags
.recursive
= 1;
205 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
206 diff_setup_done(&opt
);
207 diff_tree_oid(base
, local
, "", &opt
);
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
;
214 struct object_id obj
;
216 if (verify_notes_filepair(p
, &obj
)) {
217 trace_printf("\t\tCannot merge entry '%s' (%c): "
218 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
219 p
->status
, oid_to_hex(&p
->one
->oid
),
220 oid_to_hex(&p
->two
->oid
));
223 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 0, &match
);
225 trace_printf("\t\tIgnoring local-only change for %s: "
226 "%.7s -> %.7s\n", oid_to_hex(&obj
),
227 oid_to_hex(&p
->one
->oid
),
228 oid_to_hex(&p
->two
->oid
));
232 assert(oideq(&mp
->obj
, &obj
));
233 if (is_null_oid(&p
->two
->oid
)) { /* deletion */
235 * Either this is a true deletion (1), or it is part
236 * of an A/D pair (2), or D/A pair (3):
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)
243 if (oideq(&mp
->local
, &uninitialized
))
245 } else if (is_null_oid(&p
->one
->oid
)) { /* addition */
247 * Either this is a true addition (1), or it is part
248 * of an A/D pair (2), or D/A pair (3):
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
254 assert(is_null_oid(&mp
->local
) ||
255 oideq(&mp
->local
, &uninitialized
));
256 oidcpy(&mp
->local
, &p
->two
->oid
);
257 } else { /* modification */
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.
263 assert(oideq(&p
->one
->oid
, &mp
->base
));
264 assert(oideq(&mp
->local
, &uninitialized
));
265 oidcpy(&mp
->local
, &p
->two
->oid
);
267 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
268 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
269 oid_to_hex(&mp
->local
));
274 static void check_notes_merge_worktree(struct notes_merge_options
*o
)
276 if (!o
->has_worktree
) {
278 * Must establish NOTES_MERGE_WORKTREE.
279 * Abort if NOTES_MERGE_WORKTREE already exists
281 if (file_exists(git_path(NOTES_MERGE_WORKTREE
)) &&
282 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE
))) {
283 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
284 die(_("You have not concluded your previous "
285 "notes merge (%s exists).\nPlease, use "
286 "'git notes merge --commit' or 'git notes "
287 "merge --abort' to commit/abort the "
288 "previous merge before you start a new "
289 "notes merge."), git_path("NOTES_MERGE_*"));
291 die(_("You have not concluded your notes merge "
292 "(%s exists)."), git_path("NOTES_MERGE_*"));
295 if (safe_create_leading_directories_const(git_path(
296 NOTES_MERGE_WORKTREE
"/.test")))
297 die_errno("unable to create directory %s",
298 git_path(NOTES_MERGE_WORKTREE
));
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
));
306 static void write_buf_to_worktree(const struct object_id
*obj
,
307 const char *buf
, unsigned long size
)
310 char *path
= git_pathdup(NOTES_MERGE_WORKTREE
"/%s", oid_to_hex(obj
));
311 if (safe_create_leading_directories_const(path
))
312 die_errno("unable to create directory for '%s'", path
);
314 fd
= xopen(path
, O_WRONLY
| O_EXCL
| O_CREAT
, 0666);
317 ssize_t ret
= write_in_full(fd
, buf
, size
);
322 die_errno("notes-merge");
332 static void write_note_to_worktree(const struct object_id
*obj
,
333 const struct object_id
*note
)
335 enum object_type type
;
337 void *buf
= repo_read_object_file(the_repository
, note
, &type
, &size
);
340 die("cannot read note %s for object %s",
341 oid_to_hex(note
), oid_to_hex(obj
));
342 if (type
!= OBJ_BLOB
)
343 die("blob expected in note %s for object %s",
344 oid_to_hex(note
), oid_to_hex(obj
));
345 write_buf_to_worktree(obj
, buf
, size
);
349 static int ll_merge_in_worktree(struct notes_merge_options
*o
,
350 struct notes_merge_pair
*p
)
352 mmbuffer_t result_buf
;
353 mmfile_t base
, local
, remote
;
354 enum ll_merge_result status
;
356 read_mmblob(&base
, &p
->base
);
357 read_mmblob(&local
, &p
->local
);
358 read_mmblob(&remote
, &p
->remote
);
360 status
= ll_merge(&result_buf
, oid_to_hex(&p
->obj
), &base
, NULL
,
361 &local
, o
->local_ref
, &remote
, o
->remote_ref
,
362 o
->repo
->index
, NULL
);
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
);
371 if ((status
< 0) || !result_buf
.ptr
)
372 die("Failed to execute internal merge");
374 write_buf_to_worktree(&p
->obj
, result_buf
.ptr
, result_buf
.size
);
375 free(result_buf
.ptr
);
380 static int merge_one_change_manual(struct notes_merge_options
*o
,
381 struct notes_merge_pair
*p
,
382 struct notes_tree
*t
)
384 const char *lref
= o
->local_ref
? o
->local_ref
: "local version";
385 const char *rref
= o
->remote_ref
? o
->remote_ref
: "remote version";
387 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
388 "local = %.7s, remote = %.7s)\n",
389 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
390 oid_to_hex(&p
->local
), oid_to_hex(&p
->remote
));
392 /* add "Conflicts:" section to commit message first time through */
393 if (!o
->has_worktree
)
394 strbuf_addstr(&(o
->commit_msg
), "\n\nConflicts:\n");
396 strbuf_addf(&(o
->commit_msg
), "\t%s\n", oid_to_hex(&p
->obj
));
398 if (o
->verbosity
>= 2)
399 printf("Auto-merging notes for %s\n", oid_to_hex(&p
->obj
));
400 check_notes_merge_worktree(o
);
401 if (is_null_oid(&p
->local
)) {
402 /* D/F conflict, checkout p->remote */
403 assert(!is_null_oid(&p
->remote
));
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 "
408 oid_to_hex(&p
->obj
), lref
, rref
, rref
);
409 write_note_to_worktree(&p
->obj
, &p
->remote
);
410 } else if (is_null_oid(&p
->remote
)) {
411 /* D/F conflict, checkout p->local */
412 assert(!is_null_oid(&p
->local
));
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 "
417 oid_to_hex(&p
->obj
), rref
, lref
, lref
);
418 write_note_to_worktree(&p
->obj
, &p
->local
);
420 /* "regular" conflict, checkout result of ll_merge() */
421 const char *reason
= "content";
422 if (is_null_oid(&p
->base
))
424 assert(!is_null_oid(&p
->local
));
425 assert(!is_null_oid(&p
->remote
));
426 if (o
->verbosity
>= 1)
427 printf("CONFLICT (%s): Merge conflict in notes for "
428 "object %s\n", reason
,
429 oid_to_hex(&p
->obj
));
430 ll_merge_in_worktree(o
, p
);
433 trace_printf("\t\t\tremoving from partial merge result\n");
434 remove_note(t
, p
->obj
.hash
);
439 static int merge_one_change(struct notes_merge_options
*o
,
440 struct notes_merge_pair
*p
, struct notes_tree
*t
)
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).
447 switch (o
->strategy
) {
448 case NOTES_MERGE_RESOLVE_MANUAL
:
449 return merge_one_change_manual(o
, p
, t
);
450 case NOTES_MERGE_RESOLVE_OURS
:
451 if (o
->verbosity
>= 2)
452 printf("Using local notes for %s\n",
453 oid_to_hex(&p
->obj
));
456 case NOTES_MERGE_RESOLVE_THEIRS
:
457 if (o
->verbosity
>= 2)
458 printf("Using remote notes for %s\n",
459 oid_to_hex(&p
->obj
));
460 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_overwrite
))
461 BUG("combine_notes_overwrite failed");
463 case NOTES_MERGE_RESOLVE_UNION
:
464 if (o
->verbosity
>= 2)
465 printf("Concatenating local and remote notes for %s\n",
466 oid_to_hex(&p
->obj
));
467 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_concatenate
))
468 die("failed to concatenate notes "
469 "(combine_notes_concatenate)");
471 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
:
472 if (o
->verbosity
>= 2)
473 printf("Concatenating unique lines in local and remote "
474 "notes for %s\n", oid_to_hex(&p
->obj
));
475 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_cat_sort_uniq
))
476 die("failed to concatenate notes "
477 "(combine_notes_cat_sort_uniq)");
480 die("Unknown strategy (%i).", o
->strategy
);
483 static int merge_changes(struct notes_merge_options
*o
,
484 struct notes_merge_pair
*changes
, int *num_changes
,
485 struct notes_tree
*t
)
487 int i
, conflicts
= 0;
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",
493 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
494 oid_to_hex(&p
->local
),
495 oid_to_hex(&p
->remote
));
497 if (oideq(&p
->base
, &p
->remote
)) {
498 /* no remote change; nothing to do */
499 trace_printf("\t\t\tskipping (no remote change)\n");
500 } else if (oideq(&p
->local
, &p
->remote
)) {
501 /* same change in local and remote; nothing to do */
502 trace_printf("\t\t\tskipping (local == remote)\n");
503 } else if (oideq(&p
->local
, &uninitialized
) ||
504 oideq(&p
->local
, &p
->base
)) {
505 /* no local change; adopt remote change */
506 trace_printf("\t\t\tno local change, adopted remote\n");
507 if (add_note(t
, &p
->obj
, &p
->remote
,
508 combine_notes_overwrite
))
509 BUG("combine_notes_overwrite failed");
511 /* need file-level merge between local and remote */
512 trace_printf("\t\t\tneed content-level merge\n");
513 conflicts
+= merge_one_change(o
, p
, t
);
520 static int merge_from_diffs(struct notes_merge_options
*o
,
521 const struct object_id
*base
,
522 const struct object_id
*local
,
523 const struct object_id
*remote
,
524 struct notes_tree
*t
)
526 struct notes_merge_pair
*changes
;
527 int num_changes
, conflicts
;
529 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
530 "remote = %.7s)\n", oid_to_hex(base
), oid_to_hex(local
),
533 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
534 diff_tree_local(o
, changes
, num_changes
, base
, local
);
536 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
539 if (o
->verbosity
>= 4)
541 "Merge result: %i unmerged notes and a dirty notes tree\n" :
542 "Merge result: %i unmerged notes and a clean notes tree\n",
545 return conflicts
? -1 : 1;
548 int notes_merge(struct notes_merge_options
*o
,
549 struct notes_tree
*local_tree
,
550 struct object_id
*result_oid
)
552 struct object_id local_oid
, remote_oid
;
553 struct commit
*local
, *remote
;
554 struct commit_list
*bases
= NULL
;
555 const struct object_id
*base_oid
, *base_tree_oid
;
558 assert(o
->local_ref
&& o
->remote_ref
);
559 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
562 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
563 o
->local_ref
, o
->remote_ref
);
565 /* Dereference o->local_ref into local_sha1 */
566 if (read_ref_full(o
->local_ref
, 0, &local_oid
, NULL
))
567 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
568 else if (!check_refname_format(o
->local_ref
, 0) &&
569 is_null_oid(&local_oid
))
570 local
= NULL
; /* local_oid == null_oid indicates unborn ref */
571 else if (!(local
= lookup_commit_reference(o
->repo
, &local_oid
)))
572 die("Could not parse local commit %s (%s)",
573 oid_to_hex(&local_oid
), o
->local_ref
);
574 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid
));
576 /* Dereference o->remote_ref into remote_oid */
577 if (repo_get_oid(the_repository
, o
->remote_ref
, &remote_oid
)) {
579 * Failed to get remote_oid. If o->remote_ref looks like an
580 * unborn ref, perform the merge using an empty notes tree.
582 if (!check_refname_format(o
->remote_ref
, 0)) {
586 die("Failed to resolve remote notes ref '%s'",
589 } else if (!(remote
= lookup_commit_reference(o
->repo
, &remote_oid
))) {
590 die("Could not parse remote commit %s (%s)",
591 oid_to_hex(&remote_oid
), o
->remote_ref
);
593 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid
));
595 if (!local
&& !remote
)
596 die("Cannot merge empty notes ref (%s) into empty notes ref "
597 "(%s)", o
->remote_ref
, o
->local_ref
);
599 /* result == remote commit */
600 oidcpy(result_oid
, &remote_oid
);
604 /* result == local commit */
605 oidcpy(result_oid
, &local_oid
);
608 assert(local
&& remote
);
610 /* Find merge bases */
611 bases
= repo_get_merge_bases(the_repository
, local
, remote
);
613 base_oid
= null_oid();
614 base_tree_oid
= the_hash_algo
->empty_tree
;
615 if (o
->verbosity
>= 4)
616 printf("No merge base found; doing history-less merge\n");
617 } else if (!bases
->next
) {
618 base_oid
= &bases
->item
->object
.oid
;
619 base_tree_oid
= get_commit_tree_oid(bases
->item
);
620 if (o
->verbosity
>= 4)
621 printf("One merge base found (%.7s)\n",
622 oid_to_hex(base_oid
));
624 /* TODO: How to handle multiple merge-bases? */
625 base_oid
= &bases
->item
->object
.oid
;
626 base_tree_oid
= get_commit_tree_oid(bases
->item
);
627 if (o
->verbosity
>= 3)
628 printf("Multiple merge bases found. Using the first "
629 "(%.7s)\n", oid_to_hex(base_oid
));
632 if (o
->verbosity
>= 4)
633 printf("Merging remote commit %.7s into local commit %.7s with "
634 "merge-base %.7s\n", oid_to_hex(&remote
->object
.oid
),
635 oid_to_hex(&local
->object
.oid
),
636 oid_to_hex(base_oid
));
638 if (oideq(&remote
->object
.oid
, base_oid
)) {
639 /* Already merged; result == local commit */
640 if (o
->verbosity
>= 2)
641 printf_ln("Already up to date.");
642 oidcpy(result_oid
, &local
->object
.oid
);
645 if (oideq(&local
->object
.oid
, base_oid
)) {
646 /* Fast-forward; result == remote commit */
647 if (o
->verbosity
>= 2)
648 printf("Fast-forward\n");
649 oidcpy(result_oid
, &remote
->object
.oid
);
653 result
= merge_from_diffs(o
, base_tree_oid
,
654 get_commit_tree_oid(local
),
655 get_commit_tree_oid(remote
), local_tree
);
657 if (result
!= 0) { /* non-trivial merge (with or without conflicts) */
658 /* Commit (partial) result */
659 struct commit_list
*parents
= NULL
;
660 commit_list_insert(remote
, &parents
); /* LIFO order */
661 commit_list_insert(local
, &parents
);
662 create_notes_commit(o
->repo
, local_tree
, parents
, o
->commit_msg
.buf
,
663 o
->commit_msg
.len
, result_oid
);
667 free_commit_list(bases
);
668 strbuf_release(&(o
->commit_msg
));
669 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
670 result
, oid_to_hex(result_oid
));
674 int notes_merge_commit(struct notes_merge_options
*o
,
675 struct notes_tree
*partial_tree
,
676 struct commit
*partial_commit
,
677 struct object_id
*result_oid
)
680 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
681 * found notes to 'partial_tree'. Write the updated notes tree to
682 * the DB, and commit the resulting tree object while reusing the
683 * commit message and parents from 'partial_commit'.
684 * Finally store the new commit object OID into 'result_oid'.
688 struct strbuf path
= STRBUF_INIT
;
689 const char *buffer
= repo_get_commit_buffer(the_repository
,
690 partial_commit
, NULL
);
691 const char *msg
= strstr(buffer
, "\n\n");
694 git_path_buf(&path
, NOTES_MERGE_WORKTREE
);
695 if (o
->verbosity
>= 3)
696 printf("Committing notes in notes merge worktree at %s\n",
699 if (!msg
|| msg
[2] == '\0')
700 die("partial notes commit has empty message");
703 dir
= opendir(path
.buf
);
705 die_errno("could not open %s", path
.buf
);
707 strbuf_addch(&path
, '/');
709 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
711 struct object_id obj_oid
, blob_oid
;
713 if (get_oid_hex(e
->d_name
, &obj_oid
)) {
714 if (o
->verbosity
>= 3)
715 printf("Skipping non-SHA1 entry '%s%s'\n",
716 path
.buf
, e
->d_name
);
720 strbuf_addstr(&path
, e
->d_name
);
721 /* write file as blob, and add to partial_tree */
722 if (stat(path
.buf
, &st
))
723 die_errno("Failed to stat '%s'", path
.buf
);
724 if (index_path(o
->repo
->index
, &blob_oid
, path
.buf
, &st
, HASH_WRITE_OBJECT
))
725 die("Failed to write blob object from '%s'", path
.buf
);
726 if (add_note(partial_tree
, &obj_oid
, &blob_oid
, NULL
))
727 die("Failed to add resolved note '%s' to notes tree",
729 if (o
->verbosity
>= 4)
730 printf("Added resolved note for object %s: %s\n",
731 oid_to_hex(&obj_oid
), oid_to_hex(&blob_oid
));
732 strbuf_setlen(&path
, baselen
);
735 create_notes_commit(o
->repo
, partial_tree
, partial_commit
->parents
, msg
,
736 strlen(msg
), result_oid
);
737 repo_unuse_commit_buffer(the_repository
, partial_commit
, buffer
);
738 if (o
->verbosity
>= 4)
739 printf("Finalized notes merge commit: %s\n",
740 oid_to_hex(result_oid
));
741 strbuf_release(&path
);
746 int notes_merge_abort(struct notes_merge_options
*o
)
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.
753 struct strbuf buf
= STRBUF_INIT
;
756 git_path_buf(&buf
, NOTES_MERGE_WORKTREE
);
757 if (o
->verbosity
>= 3)
758 printf("Removing notes merge worktree at %s/*\n", buf
.buf
);
759 ret
= remove_dir_recursively(&buf
, REMOVE_DIR_KEEP_TOPLEVEL
);
760 strbuf_release(&buf
);