1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
9 #include "object-file.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
13 #include "repository.h"
17 #include "xdiff-interface.h"
21 #include "notes-merge.h"
24 #include "notes-utils.h"
25 #include "commit-reach.h"
27 struct notes_merge_pair
{
28 struct object_id obj
, base
, local
, remote
;
31 void init_notes_merge_options(struct repository
*r
,
32 struct notes_merge_options
*o
)
34 memset(o
, 0, sizeof(struct notes_merge_options
));
35 strbuf_init(&(o
->commit_msg
), 0);
36 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
40 static int path_to_oid(const char *path
, struct object_id
*oid
)
42 char hex_oid
[GIT_MAX_HEXSZ
];
44 while (*path
&& i
< the_hash_algo
->hexsz
) {
49 if (*path
|| i
!= the_hash_algo
->hexsz
)
51 return get_oid_hex(hex_oid
, oid
);
54 static int verify_notes_filepair(struct diff_filepair
*p
, struct object_id
*oid
)
57 case DIFF_STATUS_MODIFIED
:
58 assert(p
->one
->mode
== p
->two
->mode
);
59 assert(!is_null_oid(&p
->one
->oid
));
60 assert(!is_null_oid(&p
->two
->oid
));
62 case DIFF_STATUS_ADDED
:
63 assert(is_null_oid(&p
->one
->oid
));
65 case DIFF_STATUS_DELETED
:
66 assert(is_null_oid(&p
->two
->oid
));
71 assert(!strcmp(p
->one
->path
, p
->two
->path
));
72 return path_to_oid(p
->one
->path
, oid
);
75 static struct notes_merge_pair
*find_notes_merge_pair_pos(
76 struct notes_merge_pair
*list
, int len
, struct object_id
*obj
,
77 int insert_new
, int *occupied
)
80 * Both diff_tree_remote() and diff_tree_local() tend to process
81 * merge_pairs in ascending order. Therefore, cache last returned
82 * index, and search sequentially from there until the appropriate
85 * Since inserts only happen from diff_tree_remote() (which mainly
86 * _appends_), we don't care that inserting into the middle of the
87 * list is expensive (using memmove()).
89 static int last_index
;
90 int i
= last_index
< len
? last_index
: len
- 1;
91 int prev_cmp
= 0, cmp
= -1;
92 while (i
>= 0 && i
< len
) {
93 cmp
= oidcmp(obj
, &list
[i
].obj
);
94 if (!cmp
) /* obj belongs @ i */
96 else if (cmp
< 0 && prev_cmp
<= 0) /* obj belongs < i */
98 else if (cmp
< 0) /* obj belongs between i-1 and i */
100 else if (cmp
> 0 && prev_cmp
>= 0) /* obj belongs > i */
102 else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
110 /* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
116 if (insert_new
&& i
< len
) {
117 MOVE_ARRAY(list
+ i
+ 1, list
+ i
, len
- i
);
118 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
125 static struct object_id uninitialized
= {
127 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
128 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
131 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
132 const struct object_id
*base
,
133 const struct object_id
*remote
,
136 struct diff_options opt
;
137 struct notes_merge_pair
*changes
;
140 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
141 oid_to_hex(base
), oid_to_hex(remote
));
143 repo_diff_setup(o
->repo
, &opt
);
144 opt
.flags
.recursive
= 1;
145 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
146 diff_setup_done(&opt
);
147 diff_tree_oid(base
, remote
, "", &opt
);
150 CALLOC_ARRAY(changes
, diff_queued_diff
.nr
);
152 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
153 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
154 struct notes_merge_pair
*mp
;
156 struct object_id obj
;
158 if (verify_notes_filepair(p
, &obj
)) {
159 trace_printf("\t\tCannot merge entry '%s' (%c): "
160 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
161 p
->status
, oid_to_hex(&p
->one
->oid
),
162 oid_to_hex(&p
->two
->oid
));
165 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 1, &occupied
);
167 /* We've found an addition/deletion pair */
168 assert(oideq(&mp
->obj
, &obj
));
169 if (is_null_oid(&p
->one
->oid
)) { /* addition */
170 assert(is_null_oid(&mp
->remote
));
171 oidcpy(&mp
->remote
, &p
->two
->oid
);
172 } else if (is_null_oid(&p
->two
->oid
)) { /* deletion */
173 assert(is_null_oid(&mp
->base
));
174 oidcpy(&mp
->base
, &p
->one
->oid
);
176 assert(!"Invalid existing change recorded");
178 oidcpy(&mp
->obj
, &obj
);
179 oidcpy(&mp
->base
, &p
->one
->oid
);
180 oidcpy(&mp
->local
, &uninitialized
);
181 oidcpy(&mp
->remote
, &p
->two
->oid
);
184 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
185 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
186 oid_to_hex(&mp
->remote
));
194 static void diff_tree_local(struct notes_merge_options
*o
,
195 struct notes_merge_pair
*changes
, int len
,
196 const struct object_id
*base
,
197 const struct object_id
*local
)
199 struct diff_options opt
;
202 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
203 len
, oid_to_hex(base
), oid_to_hex(local
));
205 repo_diff_setup(o
->repo
, &opt
);
206 opt
.flags
.recursive
= 1;
207 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
208 diff_setup_done(&opt
);
209 diff_tree_oid(base
, local
, "", &opt
);
212 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
213 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
214 struct notes_merge_pair
*mp
;
216 struct object_id obj
;
218 if (verify_notes_filepair(p
, &obj
)) {
219 trace_printf("\t\tCannot merge entry '%s' (%c): "
220 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
221 p
->status
, oid_to_hex(&p
->one
->oid
),
222 oid_to_hex(&p
->two
->oid
));
225 mp
= find_notes_merge_pair_pos(changes
, len
, &obj
, 0, &match
);
227 trace_printf("\t\tIgnoring local-only change for %s: "
228 "%.7s -> %.7s\n", oid_to_hex(&obj
),
229 oid_to_hex(&p
->one
->oid
),
230 oid_to_hex(&p
->two
->oid
));
234 assert(oideq(&mp
->obj
, &obj
));
235 if (is_null_oid(&p
->two
->oid
)) { /* deletion */
237 * Either this is a true deletion (1), or it is part
238 * of an A/D pair (2), or D/A pair (3):
240 * (1) mp->local is uninitialized; set it to null_sha1
241 * (2) mp->local is not uninitialized; don't touch it
242 * (3) mp->local is uninitialized; set it to null_sha1
243 * (will be overwritten by following addition)
245 if (oideq(&mp
->local
, &uninitialized
))
246 oidclr(&mp
->local
, the_repository
->hash_algo
);
247 } else if (is_null_oid(&p
->one
->oid
)) { /* addition */
249 * Either this is a true addition (1), or it is part
250 * of an A/D pair (2), or D/A pair (3):
252 * (1) mp->local is uninitialized; set to p->two->sha1
253 * (2) mp->local is uninitialized; set to p->two->sha1
254 * (3) mp->local is null_sha1; set to p->two->sha1
256 assert(is_null_oid(&mp
->local
) ||
257 oideq(&mp
->local
, &uninitialized
));
258 oidcpy(&mp
->local
, &p
->two
->oid
);
259 } else { /* modification */
261 * This is a true modification. p->one->sha1 shall
262 * match mp->base, and mp->local shall be uninitialized.
263 * Set mp->local to p->two->sha1.
265 assert(oideq(&p
->one
->oid
, &mp
->base
));
266 assert(oideq(&mp
->local
, &uninitialized
));
267 oidcpy(&mp
->local
, &p
->two
->oid
);
269 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
270 oid_to_hex(&mp
->obj
), oid_to_hex(&mp
->base
),
271 oid_to_hex(&mp
->local
));
276 static void check_notes_merge_worktree(struct notes_merge_options
*o
)
278 if (!o
->has_worktree
) {
280 * Must establish NOTES_MERGE_WORKTREE.
281 * Abort if NOTES_MERGE_WORKTREE already exists
283 if (file_exists(git_path(NOTES_MERGE_WORKTREE
)) &&
284 !is_empty_dir(git_path(NOTES_MERGE_WORKTREE
))) {
285 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
286 die(_("You have not concluded your previous "
287 "notes merge (%s exists).\nPlease, use "
288 "'git notes merge --commit' or 'git notes "
289 "merge --abort' to commit/abort the "
290 "previous merge before you start a new "
291 "notes merge."), git_path("NOTES_MERGE_*"));
293 die(_("You have not concluded your notes merge "
294 "(%s exists)."), git_path("NOTES_MERGE_*"));
297 if (safe_create_leading_directories_const(git_path(
298 NOTES_MERGE_WORKTREE
"/.test")))
299 die_errno("unable to create directory %s",
300 git_path(NOTES_MERGE_WORKTREE
));
302 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE
)))
303 /* NOTES_MERGE_WORKTREE should already be established */
304 die("missing '%s'. This should not happen",
305 git_path(NOTES_MERGE_WORKTREE
));
308 static void write_buf_to_worktree(const struct object_id
*obj
,
309 const char *buf
, unsigned long size
)
312 char *path
= git_pathdup(NOTES_MERGE_WORKTREE
"/%s", oid_to_hex(obj
));
313 if (safe_create_leading_directories_const(path
))
314 die_errno("unable to create directory for '%s'", path
);
316 fd
= xopen(path
, O_WRONLY
| O_EXCL
| O_CREAT
, 0666);
319 ssize_t ret
= write_in_full(fd
, buf
, size
);
324 die_errno("notes-merge");
334 static void write_note_to_worktree(const struct object_id
*obj
,
335 const struct object_id
*note
)
337 enum object_type type
;
339 void *buf
= repo_read_object_file(the_repository
, note
, &type
, &size
);
342 die("cannot read note %s for object %s",
343 oid_to_hex(note
), oid_to_hex(obj
));
344 if (type
!= OBJ_BLOB
)
345 die("blob expected in note %s for object %s",
346 oid_to_hex(note
), oid_to_hex(obj
));
347 write_buf_to_worktree(obj
, buf
, size
);
351 static int ll_merge_in_worktree(struct notes_merge_options
*o
,
352 struct notes_merge_pair
*p
)
354 mmbuffer_t result_buf
;
355 mmfile_t base
, local
, remote
;
356 enum ll_merge_result status
;
358 read_mmblob(&base
, &p
->base
);
359 read_mmblob(&local
, &p
->local
);
360 read_mmblob(&remote
, &p
->remote
);
362 status
= ll_merge(&result_buf
, oid_to_hex(&p
->obj
), &base
, NULL
,
363 &local
, o
->local_ref
, &remote
, o
->remote_ref
,
364 o
->repo
->index
, NULL
);
370 if (status
== LL_MERGE_BINARY_CONFLICT
)
371 warning("Cannot merge binary files: %s (%s vs. %s)",
372 oid_to_hex(&p
->obj
), o
->local_ref
, o
->remote_ref
);
373 if ((status
< 0) || !result_buf
.ptr
)
374 die("Failed to execute internal merge");
376 write_buf_to_worktree(&p
->obj
, result_buf
.ptr
, result_buf
.size
);
377 free(result_buf
.ptr
);
382 static int merge_one_change_manual(struct notes_merge_options
*o
,
383 struct notes_merge_pair
*p
,
384 struct notes_tree
*t
)
386 const char *lref
= o
->local_ref
? o
->local_ref
: "local version";
387 const char *rref
= o
->remote_ref
? o
->remote_ref
: "remote version";
389 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
390 "local = %.7s, remote = %.7s)\n",
391 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
392 oid_to_hex(&p
->local
), oid_to_hex(&p
->remote
));
394 /* add "Conflicts:" section to commit message first time through */
395 if (!o
->has_worktree
)
396 strbuf_addstr(&(o
->commit_msg
), "\n\nConflicts:\n");
398 strbuf_addf(&(o
->commit_msg
), "\t%s\n", oid_to_hex(&p
->obj
));
400 if (o
->verbosity
>= 2)
401 printf("Auto-merging notes for %s\n", oid_to_hex(&p
->obj
));
402 check_notes_merge_worktree(o
);
403 if (is_null_oid(&p
->local
)) {
404 /* D/F conflict, checkout p->remote */
405 assert(!is_null_oid(&p
->remote
));
406 if (o
->verbosity
>= 1)
407 printf("CONFLICT (delete/modify): Notes for object %s "
408 "deleted in %s and modified in %s. Version from %s "
410 oid_to_hex(&p
->obj
), lref
, rref
, rref
);
411 write_note_to_worktree(&p
->obj
, &p
->remote
);
412 } else if (is_null_oid(&p
->remote
)) {
413 /* D/F conflict, checkout p->local */
414 assert(!is_null_oid(&p
->local
));
415 if (o
->verbosity
>= 1)
416 printf("CONFLICT (delete/modify): Notes for object %s "
417 "deleted in %s and modified in %s. Version from %s "
419 oid_to_hex(&p
->obj
), rref
, lref
, lref
);
420 write_note_to_worktree(&p
->obj
, &p
->local
);
422 /* "regular" conflict, checkout result of ll_merge() */
423 const char *reason
= "content";
424 if (is_null_oid(&p
->base
))
426 assert(!is_null_oid(&p
->local
));
427 assert(!is_null_oid(&p
->remote
));
428 if (o
->verbosity
>= 1)
429 printf("CONFLICT (%s): Merge conflict in notes for "
430 "object %s\n", reason
,
431 oid_to_hex(&p
->obj
));
432 ll_merge_in_worktree(o
, p
);
435 trace_printf("\t\t\tremoving from partial merge result\n");
436 remove_note(t
, p
->obj
.hash
);
441 static int merge_one_change(struct notes_merge_options
*o
,
442 struct notes_merge_pair
*p
, struct notes_tree
*t
)
445 * Return 0 if change is successfully resolved (stored in notes_tree).
446 * Return 1 is change results in a conflict (NOT stored in notes_tree,
447 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
449 switch (o
->strategy
) {
450 case NOTES_MERGE_RESOLVE_MANUAL
:
451 return merge_one_change_manual(o
, p
, t
);
452 case NOTES_MERGE_RESOLVE_OURS
:
453 if (o
->verbosity
>= 2)
454 printf("Using local notes for %s\n",
455 oid_to_hex(&p
->obj
));
458 case NOTES_MERGE_RESOLVE_THEIRS
:
459 if (o
->verbosity
>= 2)
460 printf("Using remote notes for %s\n",
461 oid_to_hex(&p
->obj
));
462 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_overwrite
))
463 BUG("combine_notes_overwrite failed");
465 case NOTES_MERGE_RESOLVE_UNION
:
466 if (o
->verbosity
>= 2)
467 printf("Concatenating local and remote notes for %s\n",
468 oid_to_hex(&p
->obj
));
469 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_concatenate
))
470 die("failed to concatenate notes "
471 "(combine_notes_concatenate)");
473 case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
:
474 if (o
->verbosity
>= 2)
475 printf("Concatenating unique lines in local and remote "
476 "notes for %s\n", oid_to_hex(&p
->obj
));
477 if (add_note(t
, &p
->obj
, &p
->remote
, combine_notes_cat_sort_uniq
))
478 die("failed to concatenate notes "
479 "(combine_notes_cat_sort_uniq)");
482 die("Unknown strategy (%i).", o
->strategy
);
485 static int merge_changes(struct notes_merge_options
*o
,
486 struct notes_merge_pair
*changes
, int *num_changes
,
487 struct notes_tree
*t
)
489 int i
, conflicts
= 0;
491 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes
);
492 for (i
= 0; i
< *num_changes
; i
++) {
493 struct notes_merge_pair
*p
= changes
+ i
;
494 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
495 oid_to_hex(&p
->obj
), oid_to_hex(&p
->base
),
496 oid_to_hex(&p
->local
),
497 oid_to_hex(&p
->remote
));
499 if (oideq(&p
->base
, &p
->remote
)) {
500 /* no remote change; nothing to do */
501 trace_printf("\t\t\tskipping (no remote change)\n");
502 } else if (oideq(&p
->local
, &p
->remote
)) {
503 /* same change in local and remote; nothing to do */
504 trace_printf("\t\t\tskipping (local == remote)\n");
505 } else if (oideq(&p
->local
, &uninitialized
) ||
506 oideq(&p
->local
, &p
->base
)) {
507 /* no local change; adopt remote change */
508 trace_printf("\t\t\tno local change, adopted remote\n");
509 if (add_note(t
, &p
->obj
, &p
->remote
,
510 combine_notes_overwrite
))
511 BUG("combine_notes_overwrite failed");
513 /* need file-level merge between local and remote */
514 trace_printf("\t\t\tneed content-level merge\n");
515 conflicts
+= merge_one_change(o
, p
, t
);
522 static int merge_from_diffs(struct notes_merge_options
*o
,
523 const struct object_id
*base
,
524 const struct object_id
*local
,
525 const struct object_id
*remote
,
526 struct notes_tree
*t
)
528 struct notes_merge_pair
*changes
;
529 int num_changes
, conflicts
;
531 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
532 "remote = %.7s)\n", oid_to_hex(base
), oid_to_hex(local
),
535 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
536 diff_tree_local(o
, changes
, num_changes
, base
, local
);
538 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
541 if (o
->verbosity
>= 4)
543 "Merge result: %i unmerged notes and a dirty notes tree\n" :
544 "Merge result: %i unmerged notes and a clean notes tree\n",
547 return conflicts
? -1 : 1;
550 int notes_merge(struct notes_merge_options
*o
,
551 struct notes_tree
*local_tree
,
552 struct object_id
*result_oid
)
554 struct object_id local_oid
, remote_oid
;
555 struct commit
*local
, *remote
;
556 struct commit_list
*bases
= NULL
;
557 const struct object_id
*base_oid
, *base_tree_oid
;
560 assert(o
->local_ref
&& o
->remote_ref
);
561 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
562 oidclr(result_oid
, the_repository
->hash_algo
);
564 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
565 o
->local_ref
, o
->remote_ref
);
567 /* Dereference o->local_ref into local_sha1 */
568 if (refs_read_ref_full(get_main_ref_store(the_repository
), o
->local_ref
, 0, &local_oid
, NULL
))
569 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
570 else if (!check_refname_format(o
->local_ref
, 0) &&
571 is_null_oid(&local_oid
))
572 local
= NULL
; /* local_oid == null_oid indicates unborn ref */
573 else if (!(local
= lookup_commit_reference(o
->repo
, &local_oid
)))
574 die("Could not parse local commit %s (%s)",
575 oid_to_hex(&local_oid
), o
->local_ref
);
576 trace_printf("\tlocal commit: %.7s\n", oid_to_hex(&local_oid
));
578 /* Dereference o->remote_ref into remote_oid */
579 if (repo_get_oid(the_repository
, o
->remote_ref
, &remote_oid
)) {
581 * Failed to get remote_oid. If o->remote_ref looks like an
582 * unborn ref, perform the merge using an empty notes tree.
584 if (!check_refname_format(o
->remote_ref
, 0)) {
585 oidclr(&remote_oid
, the_repository
->hash_algo
);
588 die("Failed to resolve remote notes ref '%s'",
591 } else if (!(remote
= lookup_commit_reference(o
->repo
, &remote_oid
))) {
592 die("Could not parse remote commit %s (%s)",
593 oid_to_hex(&remote_oid
), o
->remote_ref
);
595 trace_printf("\tremote commit: %.7s\n", oid_to_hex(&remote_oid
));
597 if (!local
&& !remote
)
598 die("Cannot merge empty notes ref (%s) into empty notes ref "
599 "(%s)", o
->remote_ref
, o
->local_ref
);
601 /* result == remote commit */
602 oidcpy(result_oid
, &remote_oid
);
606 /* result == local commit */
607 oidcpy(result_oid
, &local_oid
);
610 assert(local
&& remote
);
612 /* Find merge bases */
613 if (repo_get_merge_bases(the_repository
, local
, remote
, &bases
) < 0)
616 base_oid
= null_oid();
617 base_tree_oid
= the_hash_algo
->empty_tree
;
618 if (o
->verbosity
>= 4)
619 printf("No merge base found; doing history-less merge\n");
620 } else if (!bases
->next
) {
621 base_oid
= &bases
->item
->object
.oid
;
622 base_tree_oid
= get_commit_tree_oid(bases
->item
);
623 if (o
->verbosity
>= 4)
624 printf("One merge base found (%.7s)\n",
625 oid_to_hex(base_oid
));
627 /* TODO: How to handle multiple merge-bases? */
628 base_oid
= &bases
->item
->object
.oid
;
629 base_tree_oid
= get_commit_tree_oid(bases
->item
);
630 if (o
->verbosity
>= 3)
631 printf("Multiple merge bases found. Using the first "
632 "(%.7s)\n", oid_to_hex(base_oid
));
635 if (o
->verbosity
>= 4)
636 printf("Merging remote commit %.7s into local commit %.7s with "
637 "merge-base %.7s\n", oid_to_hex(&remote
->object
.oid
),
638 oid_to_hex(&local
->object
.oid
),
639 oid_to_hex(base_oid
));
641 if (oideq(&remote
->object
.oid
, base_oid
)) {
642 /* Already merged; result == local commit */
643 if (o
->verbosity
>= 2)
644 printf_ln("Already up to date.");
645 oidcpy(result_oid
, &local
->object
.oid
);
648 if (oideq(&local
->object
.oid
, base_oid
)) {
649 /* Fast-forward; result == remote commit */
650 if (o
->verbosity
>= 2)
651 printf("Fast-forward\n");
652 oidcpy(result_oid
, &remote
->object
.oid
);
656 result
= merge_from_diffs(o
, base_tree_oid
,
657 get_commit_tree_oid(local
),
658 get_commit_tree_oid(remote
), local_tree
);
660 if (result
!= 0) { /* non-trivial merge (with or without conflicts) */
661 /* Commit (partial) result */
662 struct commit_list
*parents
= NULL
;
663 commit_list_insert(remote
, &parents
); /* LIFO order */
664 commit_list_insert(local
, &parents
);
665 create_notes_commit(o
->repo
, local_tree
, parents
, o
->commit_msg
.buf
,
666 o
->commit_msg
.len
, result_oid
);
667 free_commit_list(parents
);
671 free_commit_list(bases
);
672 strbuf_release(&(o
->commit_msg
));
673 trace_printf("notes_merge(): result = %i, result_oid = %.7s\n",
674 result
, oid_to_hex(result_oid
));
678 int notes_merge_commit(struct notes_merge_options
*o
,
679 struct notes_tree
*partial_tree
,
680 struct commit
*partial_commit
,
681 struct object_id
*result_oid
)
684 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
685 * found notes to 'partial_tree'. Write the updated notes tree to
686 * the DB, and commit the resulting tree object while reusing the
687 * commit message and parents from 'partial_commit'.
688 * Finally store the new commit object OID into 'result_oid'.
692 struct strbuf path
= STRBUF_INIT
;
693 const char *buffer
= repo_get_commit_buffer(the_repository
,
694 partial_commit
, NULL
);
695 const char *msg
= strstr(buffer
, "\n\n");
698 git_path_buf(&path
, NOTES_MERGE_WORKTREE
);
699 if (o
->verbosity
>= 3)
700 printf("Committing notes in notes merge worktree at %s\n",
703 if (!msg
|| msg
[2] == '\0')
704 die("partial notes commit has empty message");
707 dir
= opendir(path
.buf
);
709 die_errno("could not open %s", path
.buf
);
711 strbuf_addch(&path
, '/');
713 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
715 struct object_id obj_oid
, blob_oid
;
717 if (get_oid_hex(e
->d_name
, &obj_oid
)) {
718 if (o
->verbosity
>= 3)
719 printf("Skipping non-SHA1 entry '%s%s'\n",
720 path
.buf
, e
->d_name
);
724 strbuf_addstr(&path
, e
->d_name
);
725 /* write file as blob, and add to partial_tree */
726 if (stat(path
.buf
, &st
))
727 die_errno("Failed to stat '%s'", path
.buf
);
728 if (index_path(o
->repo
->index
, &blob_oid
, path
.buf
, &st
, HASH_WRITE_OBJECT
))
729 die("Failed to write blob object from '%s'", path
.buf
);
730 if (add_note(partial_tree
, &obj_oid
, &blob_oid
, NULL
))
731 die("Failed to add resolved note '%s' to notes tree",
733 if (o
->verbosity
>= 4)
734 printf("Added resolved note for object %s: %s\n",
735 oid_to_hex(&obj_oid
), oid_to_hex(&blob_oid
));
736 strbuf_setlen(&path
, baselen
);
739 create_notes_commit(o
->repo
, partial_tree
, partial_commit
->parents
, msg
,
740 strlen(msg
), result_oid
);
741 repo_unuse_commit_buffer(the_repository
, partial_commit
, buffer
);
742 if (o
->verbosity
>= 4)
743 printf("Finalized notes merge commit: %s\n",
744 oid_to_hex(result_oid
));
745 strbuf_release(&path
);
750 int notes_merge_abort(struct notes_merge_options
*o
)
753 * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
754 * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
755 * the current working directory of the user.
757 struct strbuf buf
= STRBUF_INIT
;
760 git_path_buf(&buf
, NOTES_MERGE_WORKTREE
);
761 if (o
->verbosity
>= 3)
762 printf("Removing notes merge worktree at %s/*\n", buf
.buf
);
763 ret
= remove_dir_recursively(&buf
, REMOVE_DIR_KEEP_TOPLEVEL
);
764 strbuf_release(&buf
);