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"
20 struct notes_merge_pair
{
21 struct object_id obj
, base
, local
, remote
;
24 void init_notes_merge_options(struct repository
*r
,
25 struct notes_merge_options
*o
)
27 memset(o
, 0, sizeof(struct notes_merge_options
));
28 strbuf_init(&(o
->commit_msg
), 0);
29 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
33 static int path_to_oid(const char *path
, struct object_id
*oid
)
35 char hex_oid
[GIT_MAX_HEXSZ
];
37 while (*path
&& i
< the_hash_algo
->hexsz
) {
42 if (*path
|| i
!= the_hash_algo
->hexsz
)
44 return get_oid_hex(hex_oid
, oid
);
47 static int verify_notes_filepair(struct diff_filepair
*p
, struct object_id
*oid
)
50 case DIFF_STATUS_MODIFIED
:
51 assert(p
->one
->mode
== p
->two
->mode
);
52 assert(!is_null_oid(&p
->one
->oid
));
53 assert(!is_null_oid(&p
->two
->oid
));
55 case DIFF_STATUS_ADDED
:
56 assert(is_null_oid(&p
->one
->oid
));
58 case DIFF_STATUS_DELETED
:
59 assert(is_null_oid(&p
->two
->oid
));
64 assert(!strcmp(p
->one
->path
, p
->two
->path
));
65 return path_to_oid(p
->one
->path
, oid
);
68 static struct notes_merge_pair
*find_notes_merge_pair_pos(
69 struct notes_merge_pair
*list
, int len
, struct object_id
*obj
,
70 int insert_new
, int *occupied
)
73 * Both diff_tree_remote() and diff_tree_local() tend to process
74 * merge_pairs in ascending order. Therefore, cache last returned
75 * index, and search sequentially from there until the appropriate
78 * Since inserts only happen from diff_tree_remote() (which mainly
79 * _appends_), we don't care that inserting into the middle of the
80 * list is expensive (using memmove()).
82 static int last_index
;
83 int i
= last_index
< len
? last_index
: len
- 1;
84 int prev_cmp
= 0, cmp
= -1;
85 while (i
>= 0 && i
< len
) {
86 cmp
= oidcmp(obj
, &list
[i
].obj
);
87 if (!cmp
) /* obj belongs @ i */
89 else if (cmp
< 0 && prev_cmp
<= 0) /* obj belongs < i */
91 else if (cmp
< 0) /* obj belongs between i-1 and i */
93 else if (cmp
> 0 && prev_cmp
>= 0) /* obj belongs > i */
95 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
103 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
109 if (insert_new
&& i
< len
) {
110 MOVE_ARRAY(list
+ i
+ 1, list
+ i
, len
- i
);
111 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
118 static struct object_id uninitialized
= {
120 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
121 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
124 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
125 const struct object_id
*base
,
126 const struct object_id
*remote
,
129 struct diff_options opt
;
130 struct notes_merge_pair
*changes
;
133 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
134 oid_to_hex(base
), oid_to_hex(remote
));
136 repo_diff_setup(o
->repo
, &opt
);
137 opt
.flags
.recursive
= 1;
138 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
139 diff_setup_done(&opt
);
140 diff_tree_oid(base
, remote
, "", &opt
);
143 CALLOC_ARRAY(changes
, diff_queued_diff
.nr
);
145 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
146 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
147 struct notes_merge_pair
*mp
;
149 struct object_id obj
;
151 if (verify_notes_filepair(p
, &obj
)) {
152 trace_printf("\t\tCannot merge entry '%s' (%c): "
153 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
154 p
->status
, oid_to_hex(&p
->one
->oid
),
155 oid_to_hex(&p
->two
->oid
));
158 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 1, &occupied
);
160 /* We've found an addition/deletion pair */
161 assert(oideq(&mp
->obj
, &obj
));
162 if (is_null_oid(&p
->one
->oid
)) { /* addition */
163 assert(is_null_oid(&mp
->remote
));
164 oidcpy(&mp
->remote
, &p
->two
->oid
);
165 } else if (is_null_oid(&p
->two
->oid
)) { /* deletion */
166 assert(is_null_oid(&mp
->base
));
167 oidcpy(&mp
->base
, &p
->one
->oid
);
169 assert(!"Invalid existing change recorded");
171 oidcpy(&mp
->obj
, &obj
);
172 oidcpy(&mp
->base
, &p
->one
->oid
);
173 oidcpy(&mp
->local
, &uninitialized
);
174 oidcpy(&mp
->remote
, &p
->two
->oid
);
177 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
178 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
179 oid_to_hex(&mp
->remote
));
187 static void diff_tree_local(struct notes_merge_options
*o
,
188 struct notes_merge_pair
*changes
, int len
,
189 const struct object_id
*base
,
190 const struct object_id
*local
)
192 struct diff_options opt
;
195 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
196 len
, oid_to_hex(base
), oid_to_hex(local
));
198 repo_diff_setup(o
->repo
, &opt
);
199 opt
.flags
.recursive
= 1;
200 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
201 diff_setup_done(&opt
);
202 diff_tree_oid(base
, local
, "", &opt
);
205 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
206 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
207 struct notes_merge_pair
*mp
;
209 struct object_id obj
;
211 if (verify_notes_filepair(p
, &obj
)) {
212 trace_printf("\t\tCannot merge entry '%s' (%c): "
213 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
214 p
->status
, oid_to_hex(&p
->one
->oid
),
215 oid_to_hex(&p
->two
->oid
));
218 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 0, &match
);
220 trace_printf("\t\tIgnoring local-only change for %s: "
221 "%.7s -> %.7s\n", oid_to_hex(&obj
),
222 oid_to_hex(&p
->one
->oid
),
223 oid_to_hex(&p
->two
->oid
));
227 assert(oideq(&mp
->obj
, &obj
));
228 if (is_null_oid(&p
->two
->oid
)) { /* deletion */
230 * Either this is a true deletion (1), or it is part
231 * of an A/D pair (2), or D/A pair (3):
233 * (1) mp->local is uninitialized; set it to null_sha1
234 * (2) mp->local is not uninitialized; don't touch it
235 * (3) mp->local is uninitialized; set it to null_sha1
236 * (will be overwritten by following addition)
238 if (oideq(&mp
->local
, &uninitialized
))
240 } else if (is_null_oid(&p
->one
->oid
)) { /* addition */
242 * Either this is a true addition (1), or it is part
243 * of an A/D pair (2), or D/A pair (3):
245 * (1) mp->local is uninitialized; set to p->two->sha1
246 * (2) mp->local is uninitialized; set to p->two->sha1
247 * (3) mp->local is null_sha1; set to p->two->sha1
249 assert(is_null_oid(&mp
->local
) ||
250 oideq(&mp
->local
, &uninitialized
));
251 oidcpy(&mp
->local
, &p
->two
->oid
);
252 } else { /* modification */
254 * This is a true modification. p->one->sha1 shall
255 * match mp->base, and mp->local shall be uninitialized.
256 * Set mp->local to p->two->sha1.
258 assert(oideq(&p
->one
->oid
, &mp
->base
));
259 assert(oideq(&mp
->local
, &uninitialized
));
260 oidcpy(&mp
->local
, &p
->two
->oid
);
262 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
263 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
264 oid_to_hex(&mp
->local
));
269 static void check_notes_merge_worktree(struct notes_merge_options
*o
)
271 if (!o
->has_worktree
) {
273 * Must establish NOTES_MERGE_WORKTREE.
274 * Abort if NOTES_MERGE_WORKTREE already exists
276 if (file_exists(git_path(NOTES_MERGE_WORKTREE
)) &&
277 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE
))) {
278 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
279 die(_("You have not concluded your previous "
280 "notes merge (%s exists).\nPlease, use "
281 "'git notes merge --commit' or 'git notes "
282 "merge --abort' to commit/abort the "
283 "previous merge before you start a new "
284 "notes merge."), git_path("NOTES_MERGE_*"));
286 die(_("You have not concluded your notes merge "
287 "(%s exists)."), git_path("NOTES_MERGE_*"));
290 if (safe_create_leading_directories_const(git_path(
291 NOTES_MERGE_WORKTREE
"/.test")))
292 die_errno("unable to create directory %s",
293 git_path(NOTES_MERGE_WORKTREE
));
295 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE
)))
296 /* NOTES_MERGE_WORKTREE should already be established */
297 die("missing '%s'. This should not happen",
298 git_path(NOTES_MERGE_WORKTREE
));
301 static void write_buf_to_worktree(const struct object_id
*obj
,
302 const char *buf
, unsigned long size
)
305 char *path
= git_pathdup(NOTES_MERGE_WORKTREE
"/%s", oid_to_hex(obj
));
306 if (safe_create_leading_directories_const(path
))
307 die_errno("unable to create directory for '%s'", path
);
309 fd
= xopen(path
, O_WRONLY
| O_EXCL
| O_CREAT
, 0666);
312 ssize_t ret
= write_in_full(fd
, buf
, size
);
317 die_errno("notes-merge");
327 static void write_note_to_worktree(const struct object_id
*obj
,
328 const struct object_id
*note
)
330 enum object_type type
;
332 void *buf
= repo_read_object_file(the_repository
, note
, &type
, &size
);
335 die("cannot read note %s for object %s",
336 oid_to_hex(note
), oid_to_hex(obj
));
337 if (type
!= OBJ_BLOB
)
338 die("blob expected in note %s for object %s",
339 oid_to_hex(note
), oid_to_hex(obj
));
340 write_buf_to_worktree(obj
, buf
, size
);
344 static int ll_merge_in_worktree(struct notes_merge_options
*o
,
345 struct notes_merge_pair
*p
)
347 mmbuffer_t result_buf
;
348 mmfile_t base
, local
, remote
;
349 enum ll_merge_result status
;
351 read_mmblob(&base
, &p
->base
);
352 read_mmblob(&local
, &p
->local
);
353 read_mmblob(&remote
, &p
->remote
);
355 status
= ll_merge(&result_buf
, oid_to_hex(&p
->obj
), &base
, NULL
,
356 &local
, o
->local_ref
, &remote
, o
->remote_ref
,
357 o
->repo
->index
, NULL
);
363 if (status
== LL_MERGE_BINARY_CONFLICT
)
364 warning("Cannot merge binary files: %s (%s vs. %s)",
365 oid_to_hex(&p
->obj
), o
->local_ref
, o
->remote_ref
);
366 if ((status
< 0) || !result_buf
.ptr
)
367 die("Failed to execute internal merge");
369 write_buf_to_worktree(&p
->obj
, result_buf
.ptr
, result_buf
.size
);
370 free(result_buf
.ptr
);
375 static int merge_one_change_manual(struct notes_merge_options
*o
,
376 struct notes_merge_pair
*p
,
377 struct notes_tree
*t
)
379 const char *lref
= o
->local_ref
? o
->local_ref
: "local version";
380 const char *rref
= o
->remote_ref
? o
->remote_ref
: "remote version";
382 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
383 "local = %.7s, remote = %.7s)\n",
384 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
385 oid_to_hex(&p
->local
), oid_to_hex(&p
->remote
));
387 /* add "Conflicts:" section to commit message first time through */
388 if (!o
->has_worktree
)
389 strbuf_addstr(&(o
->commit_msg
), "\n\nConflicts:\n");
391 strbuf_addf(&(o
->commit_msg
), "\t%s\n", oid_to_hex(&p
->obj
));
393 if (o
->verbosity
>= 2)
394 printf("Auto-merging notes for %s\n", oid_to_hex(&p
->obj
));
395 check_notes_merge_worktree(o
);
396 if (is_null_oid(&p
->local
)) {
397 /* D/F conflict, checkout p->remote */
398 assert(!is_null_oid(&p
->remote
));
399 if (o
->verbosity
>= 1)
400 printf("CONFLICT (delete/modify): Notes for object %s "
401 "deleted in %s and modified in %s. Version from %s "
403 oid_to_hex(&p
->obj
), lref
, rref
, rref
);
404 write_note_to_worktree(&p
->obj
, &p
->remote
);
405 } else if (is_null_oid(&p
->remote
)) {
406 /* D/F conflict, checkout p->local */
407 assert(!is_null_oid(&p
->local
));
408 if (o
->verbosity
>= 1)
409 printf("CONFLICT (delete/modify): Notes for object %s "
410 "deleted in %s and modified in %s. Version from %s "
412 oid_to_hex(&p
->obj
), rref
, lref
, lref
);
413 write_note_to_worktree(&p
->obj
, &p
->local
);
415 /* "regular" conflict, checkout result of ll_merge() */
416 const char *reason
= "content";
417 if (is_null_oid(&p
->base
))
419 assert(!is_null_oid(&p
->local
));
420 assert(!is_null_oid(&p
->remote
));
421 if (o
->verbosity
>= 1)
422 printf("CONFLICT (%s): Merge conflict in notes for "
423 "object %s\n", reason
,
424 oid_to_hex(&p
->obj
));
425 ll_merge_in_worktree(o
, p
);
428 trace_printf("\t\t\tremoving from partial merge result\n");
429 remove_note(t
, p
->obj
.hash
);
434 static int merge_one_change(struct notes_merge_options
*o
,
435 struct notes_merge_pair
*p
, struct notes_tree
*t
)
438 * Return 0 if change is successfully resolved (stored in notes_tree).
439 * Return 1 is change results in a conflict (NOT stored in notes_tree,
440 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
442 switch (o
->strategy
) {
443 case NOTES_MERGE_RESOLVE_MANUAL
:
444 return merge_one_change_manual(o
, p
, t
);
445 case NOTES_MERGE_RESOLVE_OURS
:
446 if (o
->verbosity
>= 2)
447 printf("Using local notes for %s\n",
448 oid_to_hex(&p
->obj
));
451 case NOTES_MERGE_RESOLVE_THEIRS
:
452 if (o
->verbosity
>= 2)
453 printf("Using remote notes for %s\n",
454 oid_to_hex(&p
->obj
));
455 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_overwrite
))
456 BUG("combine_notes_overwrite failed");
458 case NOTES_MERGE_RESOLVE_UNION
:
459 if (o
->verbosity
>= 2)
460 printf("Concatenating local and remote notes for %s\n",
461 oid_to_hex(&p
->obj
));
462 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_concatenate
))
463 die("failed to concatenate notes "
464 "(combine_notes_concatenate)");
466 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
:
467 if (o
->verbosity
>= 2)
468 printf("Concatenating unique lines in local and remote "
469 "notes for %s\n", oid_to_hex(&p
->obj
));
470 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_cat_sort_uniq
))
471 die("failed to concatenate notes "
472 "(combine_notes_cat_sort_uniq)");
475 die("Unknown strategy (%i).", o
->strategy
);
478 static int merge_changes(struct notes_merge_options
*o
,
479 struct notes_merge_pair
*changes
, int *num_changes
,
480 struct notes_tree
*t
)
482 int i
, conflicts
= 0;
484 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes
);
485 for (i
= 0; i
< *num_changes
; i
++) {
486 struct notes_merge_pair
*p
= changes
+ i
;
487 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
488 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
489 oid_to_hex(&p
->local
),
490 oid_to_hex(&p
->remote
));
492 if (oideq(&p
->base
, &p
->remote
)) {
493 /* no remote change; nothing to do */
494 trace_printf("\t\t\tskipping (no remote change)\n");
495 } else if (oideq(&p
->local
, &p
->remote
)) {
496 /* same change in local and remote; nothing to do */
497 trace_printf("\t\t\tskipping (local == remote)\n");
498 } else if (oideq(&p
->local
, &uninitialized
) ||
499 oideq(&p
->local
, &p
->base
)) {
500 /* no local change; adopt remote change */
501 trace_printf("\t\t\tno local change, adopted remote\n");
502 if (add_note(t
, &p
->obj
, &p
->remote
,
503 combine_notes_overwrite
))
504 BUG("combine_notes_overwrite failed");
506 /* need file-level merge between local and remote */
507 trace_printf("\t\t\tneed content-level merge\n");
508 conflicts
+= merge_one_change(o
, p
, t
);
515 static int merge_from_diffs(struct notes_merge_options
*o
,
516 const struct object_id
*base
,
517 const struct object_id
*local
,
518 const struct object_id
*remote
,
519 struct notes_tree
*t
)
521 struct notes_merge_pair
*changes
;
522 int num_changes
, conflicts
;
524 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
525 "remote = %.7s)\n", oid_to_hex(base
), oid_to_hex(local
),
528 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
529 diff_tree_local(o
, changes
, num_changes
, base
, local
);
531 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
534 if (o
->verbosity
>= 4)
536 "Merge result: %i unmerged notes and a dirty notes tree\n" :
537 "Merge result: %i unmerged notes and a clean notes tree\n",
540 return conflicts
? -1 : 1;
543 int notes_merge(struct notes_merge_options
*o
,
544 struct notes_tree
*local_tree
,
545 struct object_id
*result_oid
)
547 struct object_id local_oid
, remote_oid
;
548 struct commit
*local
, *remote
;
549 struct commit_list
*bases
= NULL
;
550 const struct object_id
*base_oid
, *base_tree_oid
;
553 assert(o
->local_ref
&& o
->remote_ref
);
554 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
557 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
558 o
->local_ref
, o
->remote_ref
);
560 /* Dereference o->local_ref into local_sha1 */
561 if (read_ref_full(o
->local_ref
, 0, &local_oid
, NULL
))
562 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
563 else if (!check_refname_format(o
->local_ref
, 0) &&
564 is_null_oid(&local_oid
))
565 local
= NULL
; /* local_oid == null_oid indicates unborn ref */
566 else if (!(local
= lookup_commit_reference(o
->repo
, &local_oid
)))
567 die("Could not parse local commit %s (%s)",
568 oid_to_hex(&local_oid
), o
->local_ref
);
569 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid
));
571 /* Dereference o->remote_ref into remote_oid */
572 if (repo_get_oid(the_repository
, o
->remote_ref
, &remote_oid
)) {
574 * Failed to get remote_oid. If o->remote_ref looks like an
575 * unborn ref, perform the merge using an empty notes tree.
577 if (!check_refname_format(o
->remote_ref
, 0)) {
581 die("Failed to resolve remote notes ref '%s'",
584 } else if (!(remote
= lookup_commit_reference(o
->repo
, &remote_oid
))) {
585 die("Could not parse remote commit %s (%s)",
586 oid_to_hex(&remote_oid
), o
->remote_ref
);
588 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid
));
590 if (!local
&& !remote
)
591 die("Cannot merge empty notes ref (%s) into empty notes ref "
592 "(%s)", o
->remote_ref
, o
->local_ref
);
594 /* result == remote commit */
595 oidcpy(result_oid
, &remote_oid
);
599 /* result == local commit */
600 oidcpy(result_oid
, &local_oid
);
603 assert(local
&& remote
);
605 /* Find merge bases */
606 bases
= repo_get_merge_bases(the_repository
, local
, remote
);
608 base_oid
= null_oid();
609 base_tree_oid
= the_hash_algo
->empty_tree
;
610 if (o
->verbosity
>= 4)
611 printf("No merge base found; doing history-less merge\n");
612 } else if (!bases
->next
) {
613 base_oid
= &bases
->item
->object
.oid
;
614 base_tree_oid
= get_commit_tree_oid(bases
->item
);
615 if (o
->verbosity
>= 4)
616 printf("One merge base found (%.7s)\n",
617 oid_to_hex(base_oid
));
619 /* TODO: How to handle multiple merge-bases? */
620 base_oid
= &bases
->item
->object
.oid
;
621 base_tree_oid
= get_commit_tree_oid(bases
->item
);
622 if (o
->verbosity
>= 3)
623 printf("Multiple merge bases found. Using the first "
624 "(%.7s)\n", oid_to_hex(base_oid
));
627 if (o
->verbosity
>= 4)
628 printf("Merging remote commit %.7s into local commit %.7s with "
629 "merge-base %.7s\n", oid_to_hex(&remote
->object
.oid
),
630 oid_to_hex(&local
->object
.oid
),
631 oid_to_hex(base_oid
));
633 if (oideq(&remote
->object
.oid
, base_oid
)) {
634 /* Already merged; result == local commit */
635 if (o
->verbosity
>= 2)
636 printf_ln("Already up to date.");
637 oidcpy(result_oid
, &local
->object
.oid
);
640 if (oideq(&local
->object
.oid
, base_oid
)) {
641 /* Fast-forward; result == remote commit */
642 if (o
->verbosity
>= 2)
643 printf("Fast-forward\n");
644 oidcpy(result_oid
, &remote
->object
.oid
);
648 result
= merge_from_diffs(o
, base_tree_oid
,
649 get_commit_tree_oid(local
),
650 get_commit_tree_oid(remote
), local_tree
);
652 if (result
!= 0) { /* non-trivial merge (with or without conflicts) */
653 /* Commit (partial) result */
654 struct commit_list
*parents
= NULL
;
655 commit_list_insert(remote
, &parents
); /* LIFO order */
656 commit_list_insert(local
, &parents
);
657 create_notes_commit(o
->repo
, local_tree
, parents
, o
->commit_msg
.buf
,
658 o
->commit_msg
.len
, result_oid
);
662 free_commit_list(bases
);
663 strbuf_release(&(o
->commit_msg
));
664 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
665 result
, oid_to_hex(result_oid
));
669 int notes_merge_commit(struct notes_merge_options
*o
,
670 struct notes_tree
*partial_tree
,
671 struct commit
*partial_commit
,
672 struct object_id
*result_oid
)
675 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
676 * found notes to 'partial_tree'. Write the updated notes tree to
677 * the DB, and commit the resulting tree object while reusing the
678 * commit message and parents from 'partial_commit'.
679 * Finally store the new commit object OID into 'result_oid'.
683 struct strbuf path
= STRBUF_INIT
;
684 const char *buffer
= repo_get_commit_buffer(the_repository
,
685 partial_commit
, NULL
);
686 const char *msg
= strstr(buffer
, "\n\n");
689 git_path_buf(&path
, NOTES_MERGE_WORKTREE
);
690 if (o
->verbosity
>= 3)
691 printf("Committing notes in notes merge worktree at %s\n",
694 if (!msg
|| msg
[2] == '\0')
695 die("partial notes commit has empty message");
698 dir
= opendir(path
.buf
);
700 die_errno("could not open %s", path
.buf
);
702 strbuf_addch(&path
, '/');
704 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
706 struct object_id obj_oid
, blob_oid
;
708 if (get_oid_hex(e
->d_name
, &obj_oid
)) {
709 if (o
->verbosity
>= 3)
710 printf("Skipping non-SHA1 entry '%s%s'\n",
711 path
.buf
, e
->d_name
);
715 strbuf_addstr(&path
, e
->d_name
);
716 /* write file as blob, and add to partial_tree */
717 if (stat(path
.buf
, &st
))
718 die_errno("Failed to stat '%s'", path
.buf
);
719 if (index_path(o
->repo
->index
, &blob_oid
, path
.buf
, &st
, HASH_WRITE_OBJECT
))
720 die("Failed to write blob object from '%s'", path
.buf
);
721 if (add_note(partial_tree
, &obj_oid
, &blob_oid
, NULL
))
722 die("Failed to add resolved note '%s' to notes tree",
724 if (o
->verbosity
>= 4)
725 printf("Added resolved note for object %s: %s\n",
726 oid_to_hex(&obj_oid
), oid_to_hex(&blob_oid
));
727 strbuf_setlen(&path
, baselen
);
730 create_notes_commit(o
->repo
, partial_tree
, partial_commit
->parents
, msg
,
731 strlen(msg
), result_oid
);
732 repo_unuse_commit_buffer(the_repository
, partial_commit
, buffer
);
733 if (o
->verbosity
>= 4)
734 printf("Finalized notes merge commit: %s\n",
735 oid_to_hex(result_oid
));
736 strbuf_release(&path
);
741 int notes_merge_abort(struct notes_merge_options
*o
)
744 * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
745 * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
746 * the current working directory of the user.
748 struct strbuf buf
= STRBUF_INIT
;
751 git_path_buf(&buf
, NOTES_MERGE_WORKTREE
);
752 if (o
->verbosity
>= 3)
753 printf("Removing notes merge worktree at %s/*\n", buf
.buf
);
754 ret
= remove_dir_recursively(&buf
, REMOVE_DIR_KEEP_TOPLEVEL
);
755 strbuf_release(&buf
);