6 #include "xdiff-interface.h"
10 #include "notes-merge.h"
13 struct notes_merge_pair
{
14 unsigned char obj
[20], base
[20], local
[20], remote
[20];
17 void init_notes_merge_options(struct notes_merge_options
*o
)
19 memset(o
, 0, sizeof(struct notes_merge_options
));
20 strbuf_init(&(o
->commit_msg
), 0);
21 o
->verbosity
= NOTES_MERGE_VERBOSITY_DEFAULT
;
24 #define OUTPUT(o, v, ...) \
26 if ((o)->verbosity >= (v)) { \
27 printf(__VA_ARGS__); \
32 static int path_to_sha1(const char *path
, unsigned char *sha1
)
36 while (*path
&& i
< 40) {
38 hex_sha1
[i
++] = *path
;
43 return get_sha1_hex(hex_sha1
, sha1
);
46 static int verify_notes_filepair(struct diff_filepair
*p
, unsigned char *sha1
)
49 case DIFF_STATUS_MODIFIED
:
50 assert(p
->one
->mode
== p
->two
->mode
);
51 assert(!is_null_sha1(p
->one
->sha1
));
52 assert(!is_null_sha1(p
->two
->sha1
));
54 case DIFF_STATUS_ADDED
:
55 assert(is_null_sha1(p
->one
->sha1
));
57 case DIFF_STATUS_DELETED
:
58 assert(is_null_sha1(p
->two
->sha1
));
63 assert(!strcmp(p
->one
->path
, p
->two
->path
));
64 return path_to_sha1(p
->one
->path
, sha1
);
67 static struct notes_merge_pair
*find_notes_merge_pair_pos(
68 struct notes_merge_pair
*list
, int len
, unsigned char *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
= hashcmp(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 memmove(list
+ i
+ 1, list
+ i
,
110 (len
- i
) * sizeof(struct notes_merge_pair
));
111 memset(list
+ i
, 0, sizeof(struct notes_merge_pair
));
118 static unsigned char uninitialized
[20] =
119 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
120 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
122 static struct notes_merge_pair
*diff_tree_remote(struct notes_merge_options
*o
,
123 const unsigned char *base
,
124 const unsigned char *remote
,
127 struct diff_options opt
;
128 struct notes_merge_pair
*changes
;
131 trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
132 sha1_to_hex(base
), sha1_to_hex(remote
));
135 DIFF_OPT_SET(&opt
, RECURSIVE
);
136 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
137 if (diff_setup_done(&opt
) < 0)
138 die("diff_setup_done failed");
139 diff_tree_sha1(base
, remote
, "", &opt
);
142 changes
= xcalloc(diff_queued_diff
.nr
, sizeof(struct notes_merge_pair
));
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 unsigned char obj
[20];
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
, sha1_to_hex(p
->one
->sha1
),
154 sha1_to_hex(p
->two
->sha1
));
157 mp
= find_notes_merge_pair_pos(changes
, len
, obj
, 1, &occupied
);
159 /* We've found an addition/deletion pair */
160 assert(!hashcmp(mp
->obj
, obj
));
161 if (is_null_sha1(p
->one
->sha1
)) { /* addition */
162 assert(is_null_sha1(mp
->remote
));
163 hashcpy(mp
->remote
, p
->two
->sha1
);
164 } else if (is_null_sha1(p
->two
->sha1
)) { /* deletion */
165 assert(is_null_sha1(mp
->base
));
166 hashcpy(mp
->base
, p
->one
->sha1
);
168 assert(!"Invalid existing change recorded");
170 hashcpy(mp
->obj
, obj
);
171 hashcpy(mp
->base
, p
->one
->sha1
);
172 hashcpy(mp
->local
, uninitialized
);
173 hashcpy(mp
->remote
, p
->two
->sha1
);
176 trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
177 sha1_to_hex(mp
->obj
), sha1_to_hex(mp
->base
),
178 sha1_to_hex(mp
->remote
));
181 diff_tree_release_paths(&opt
);
187 static void diff_tree_local(struct notes_merge_options
*o
,
188 struct notes_merge_pair
*changes
, int len
,
189 const unsigned char *base
,
190 const unsigned char *local
)
192 struct diff_options opt
;
195 trace_printf("\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\n",
196 len
, sha1_to_hex(base
), sha1_to_hex(local
));
199 DIFF_OPT_SET(&opt
, RECURSIVE
);
200 opt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
201 if (diff_setup_done(&opt
) < 0)
202 die("diff_setup_done failed");
203 diff_tree_sha1(base
, local
, "", &opt
);
206 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
207 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
208 struct notes_merge_pair
*mp
;
210 unsigned char obj
[20];
212 if (verify_notes_filepair(p
, obj
)) {
213 trace_printf("\t\tCannot merge entry '%s' (%c): "
214 "%.7s -> %.7s. Skipping!\n", p
->one
->path
,
215 p
->status
, sha1_to_hex(p
->one
->sha1
),
216 sha1_to_hex(p
->two
->sha1
));
219 mp
= find_notes_merge_pair_pos(changes
, len
, obj
, 0, &match
);
221 trace_printf("\t\tIgnoring local-only change for %s: "
222 "%.7s -> %.7s\n", sha1_to_hex(obj
),
223 sha1_to_hex(p
->one
->sha1
),
224 sha1_to_hex(p
->two
->sha1
));
228 assert(!hashcmp(mp
->obj
, obj
));
229 if (is_null_sha1(p
->two
->sha1
)) { /* deletion */
231 * Either this is a true deletion (1), or it is part
232 * of an A/D pair (2), or D/A pair (3):
234 * (1) mp->local is uninitialized; set it to null_sha1
235 * (2) mp->local is not uninitialized; don't touch it
236 * (3) mp->local is uninitialized; set it to null_sha1
237 * (will be overwritten by following addition)
239 if (!hashcmp(mp
->local
, uninitialized
))
241 } else if (is_null_sha1(p
->one
->sha1
)) { /* addition */
243 * Either this is a true addition (1), or it is part
244 * of an A/D pair (2), or D/A pair (3):
246 * (1) mp->local is uninitialized; set to p->two->sha1
247 * (2) mp->local is uninitialized; set to p->two->sha1
248 * (3) mp->local is null_sha1; set to p->two->sha1
250 assert(is_null_sha1(mp
->local
) ||
251 !hashcmp(mp
->local
, uninitialized
));
252 hashcpy(mp
->local
, p
->two
->sha1
);
253 } else { /* modification */
255 * This is a true modification. p->one->sha1 shall
256 * match mp->base, and mp->local shall be uninitialized.
257 * Set mp->local to p->two->sha1.
259 assert(!hashcmp(p
->one
->sha1
, mp
->base
));
260 assert(!hashcmp(mp
->local
, uninitialized
));
261 hashcpy(mp
->local
, p
->two
->sha1
);
263 trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
264 sha1_to_hex(mp
->obj
), sha1_to_hex(mp
->base
),
265 sha1_to_hex(mp
->local
));
268 diff_tree_release_paths(&opt
);
271 static void check_notes_merge_worktree(struct notes_merge_options
*o
)
273 if (!o
->has_worktree
) {
275 * Must establish NOTES_MERGE_WORKTREE.
276 * Abort if NOTES_MERGE_WORKTREE already exists
278 if (file_exists(git_path(NOTES_MERGE_WORKTREE
))) {
279 if (advice_resolve_conflict
)
280 die("You have not concluded your previous "
281 "notes merge (%s exists).\nPlease, use "
282 "'git notes merge --commit' or 'git notes "
283 "merge --abort' to commit/abort the "
284 "previous merge before you start a new "
285 "notes merge.", git_path("NOTES_MERGE_*"));
287 die("You have not concluded your notes merge "
288 "(%s exists).", git_path("NOTES_MERGE_*"));
291 if (safe_create_leading_directories(git_path(
292 NOTES_MERGE_WORKTREE
"/.test")))
293 die_errno("unable to create directory %s",
294 git_path(NOTES_MERGE_WORKTREE
));
296 } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE
)))
297 /* NOTES_MERGE_WORKTREE should already be established */
298 die("missing '%s'. This should not happen",
299 git_path(NOTES_MERGE_WORKTREE
));
302 static void write_buf_to_worktree(const unsigned char *obj
,
303 const char *buf
, unsigned long size
)
306 char *path
= git_path(NOTES_MERGE_WORKTREE
"/%s", sha1_to_hex(obj
));
307 if (safe_create_leading_directories(path
))
308 die_errno("unable to create directory for '%s'", path
);
309 if (file_exists(path
))
310 die("found existing file at '%s'", path
);
312 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0666);
314 die_errno("failed to open '%s'", path
);
317 long ret
= write_in_full(fd
, buf
, size
);
322 die_errno("notes-merge");
324 die("notes-merge: disk full?");
333 static void write_note_to_worktree(const unsigned char *obj
,
334 const unsigned char *note
)
336 enum object_type type
;
338 void *buf
= read_sha1_file(note
, &type
, &size
);
341 die("cannot read note %s for object %s",
342 sha1_to_hex(note
), sha1_to_hex(obj
));
343 if (type
!= OBJ_BLOB
)
344 die("blob expected in note %s for object %s",
345 sha1_to_hex(note
), sha1_to_hex(obj
));
346 write_buf_to_worktree(obj
, buf
, size
);
350 static int ll_merge_in_worktree(struct notes_merge_options
*o
,
351 struct notes_merge_pair
*p
)
353 mmbuffer_t result_buf
;
354 mmfile_t base
, local
, remote
;
357 read_mmblob(&base
, p
->base
);
358 read_mmblob(&local
, p
->local
);
359 read_mmblob(&remote
, p
->remote
);
361 status
= ll_merge(&result_buf
, sha1_to_hex(p
->obj
), &base
, NULL
,
362 &local
, o
->local_ref
, &remote
, o
->remote_ref
, 0);
368 if ((status
< 0) || !result_buf
.ptr
)
369 die("Failed to execute internal merge");
371 write_buf_to_worktree(p
->obj
, result_buf
.ptr
, result_buf
.size
);
372 free(result_buf
.ptr
);
377 static int merge_one_change_manual(struct notes_merge_options
*o
,
378 struct notes_merge_pair
*p
,
379 struct notes_tree
*t
)
381 const char *lref
= o
->local_ref
? o
->local_ref
: "local version";
382 const char *rref
= o
->remote_ref
? o
->remote_ref
: "remote version";
384 trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
385 "local = %.7s, remote = %.7s)\n",
386 sha1_to_hex(p
->obj
), sha1_to_hex(p
->base
),
387 sha1_to_hex(p
->local
), sha1_to_hex(p
->remote
));
389 /* add "Conflicts:" section to commit message first time through */
390 if (!o
->has_worktree
)
391 strbuf_addstr(&(o
->commit_msg
), "\n\nConflicts:\n");
393 strbuf_addf(&(o
->commit_msg
), "\t%s\n", sha1_to_hex(p
->obj
));
395 OUTPUT(o
, 2, "Auto-merging notes for %s", sha1_to_hex(p
->obj
));
396 check_notes_merge_worktree(o
);
397 if (is_null_sha1(p
->local
)) {
398 /* D/F conflict, checkout p->remote */
399 assert(!is_null_sha1(p
->remote
));
400 OUTPUT(o
, 1, "CONFLICT (delete/modify): Notes for object %s "
401 "deleted in %s and modified in %s. Version from %s "
402 "left in tree.", sha1_to_hex(p
->obj
), lref
, rref
, rref
);
403 write_note_to_worktree(p
->obj
, p
->remote
);
404 } else if (is_null_sha1(p
->remote
)) {
405 /* D/F conflict, checkout p->local */
406 assert(!is_null_sha1(p
->local
));
407 OUTPUT(o
, 1, "CONFLICT (delete/modify): Notes for object %s "
408 "deleted in %s and modified in %s. Version from %s "
409 "left in tree.", sha1_to_hex(p
->obj
), rref
, lref
, lref
);
410 write_note_to_worktree(p
->obj
, p
->local
);
412 /* "regular" conflict, checkout result of ll_merge() */
413 const char *reason
= "content";
414 if (is_null_sha1(p
->base
))
416 assert(!is_null_sha1(p
->local
));
417 assert(!is_null_sha1(p
->remote
));
418 OUTPUT(o
, 1, "CONFLICT (%s): Merge conflict in notes for "
419 "object %s", reason
, sha1_to_hex(p
->obj
));
420 ll_merge_in_worktree(o
, p
);
423 trace_printf("\t\t\tremoving from partial merge result\n");
424 remove_note(t
, p
->obj
);
429 static int merge_one_change(struct notes_merge_options
*o
,
430 struct notes_merge_pair
*p
, struct notes_tree
*t
)
433 * Return 0 if change is successfully resolved (stored in notes_tree).
434 * Return 1 is change results in a conflict (NOT stored in notes_tree,
435 * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
437 switch (o
->strategy
) {
438 case NOTES_MERGE_RESOLVE_MANUAL
:
439 return merge_one_change_manual(o
, p
, t
);
440 case NOTES_MERGE_RESOLVE_OURS
:
441 OUTPUT(o
, 2, "Using local notes for %s", sha1_to_hex(p
->obj
));
444 case NOTES_MERGE_RESOLVE_THEIRS
:
445 OUTPUT(o
, 2, "Using remote notes for %s", sha1_to_hex(p
->obj
));
446 if (add_note(t
, p
->obj
, p
->remote
, combine_notes_overwrite
))
447 die("BUG: combine_notes_overwrite failed");
449 case NOTES_MERGE_RESOLVE_UNION
:
450 OUTPUT(o
, 2, "Concatenating local and remote notes for %s",
451 sha1_to_hex(p
->obj
));
452 if (add_note(t
, p
->obj
, p
->remote
, combine_notes_concatenate
))
453 die("failed to concatenate notes "
454 "(combine_notes_concatenate)");
457 die("Unknown strategy (%i).", o
->strategy
);
460 static int merge_changes(struct notes_merge_options
*o
,
461 struct notes_merge_pair
*changes
, int *num_changes
,
462 struct notes_tree
*t
)
464 int i
, conflicts
= 0;
466 trace_printf("\tmerge_changes(num_changes = %i)\n", *num_changes
);
467 for (i
= 0; i
< *num_changes
; i
++) {
468 struct notes_merge_pair
*p
= changes
+ i
;
469 trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
470 sha1_to_hex(p
->obj
), sha1_to_hex(p
->base
),
471 sha1_to_hex(p
->local
), sha1_to_hex(p
->remote
));
473 if (!hashcmp(p
->base
, p
->remote
)) {
474 /* no remote change; nothing to do */
475 trace_printf("\t\t\tskipping (no remote change)\n");
476 } else if (!hashcmp(p
->local
, p
->remote
)) {
477 /* same change in local and remote; nothing to do */
478 trace_printf("\t\t\tskipping (local == remote)\n");
479 } else if (!hashcmp(p
->local
, uninitialized
) ||
480 !hashcmp(p
->local
, p
->base
)) {
481 /* no local change; adopt remote change */
482 trace_printf("\t\t\tno local change, adopted remote\n");
483 if (add_note(t
, p
->obj
, p
->remote
,
484 combine_notes_overwrite
))
485 die("BUG: combine_notes_overwrite failed");
487 /* need file-level merge between local and remote */
488 trace_printf("\t\t\tneed content-level merge\n");
489 conflicts
+= merge_one_change(o
, p
, t
);
496 static int merge_from_diffs(struct notes_merge_options
*o
,
497 const unsigned char *base
,
498 const unsigned char *local
,
499 const unsigned char *remote
, struct notes_tree
*t
)
501 struct notes_merge_pair
*changes
;
502 int num_changes
, conflicts
;
504 trace_printf("\tmerge_from_diffs(base = %.7s, local = %.7s, "
505 "remote = %.7s)\n", sha1_to_hex(base
), sha1_to_hex(local
),
506 sha1_to_hex(remote
));
508 changes
= diff_tree_remote(o
, base
, remote
, &num_changes
);
509 diff_tree_local(o
, changes
, num_changes
, base
, local
);
511 conflicts
= merge_changes(o
, changes
, &num_changes
, t
);
514 OUTPUT(o
, 4, "Merge result: %i unmerged notes and a %s notes tree",
515 conflicts
, t
->dirty
? "dirty" : "clean");
517 return conflicts
? -1 : 1;
520 void create_notes_commit(struct notes_tree
*t
, struct commit_list
*parents
,
521 const char *msg
, unsigned char *result_sha1
)
523 unsigned char tree_sha1
[20];
525 assert(t
->initialized
);
527 if (write_notes_tree(t
, tree_sha1
))
528 die("Failed to write notes tree to database");
531 /* Deduce parent commit from t->ref */
532 unsigned char parent_sha1
[20];
533 if (!read_ref(t
->ref
, parent_sha1
)) {
534 struct commit
*parent
= lookup_commit(parent_sha1
);
535 if (!parent
|| parse_commit(parent
))
536 die("Failed to find/parse commit %s", t
->ref
);
537 commit_list_insert(parent
, &parents
);
539 /* else: t->ref points to nothing, assume root/orphan commit */
542 if (commit_tree(msg
, tree_sha1
, parents
, result_sha1
, NULL
))
543 die("Failed to commit notes tree to database");
546 int notes_merge(struct notes_merge_options
*o
,
547 struct notes_tree
*local_tree
,
548 unsigned char *result_sha1
)
550 unsigned char local_sha1
[20], remote_sha1
[20];
551 struct commit
*local
, *remote
;
552 struct commit_list
*bases
= NULL
;
553 const unsigned char *base_sha1
, *base_tree_sha1
;
556 assert(o
->local_ref
&& o
->remote_ref
);
557 assert(!strcmp(o
->local_ref
, local_tree
->ref
));
558 hashclr(result_sha1
);
560 trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
561 o
->local_ref
, o
->remote_ref
);
563 /* Dereference o->local_ref into local_sha1 */
564 if (!resolve_ref(o
->local_ref
, local_sha1
, 0, NULL
))
565 die("Failed to resolve local notes ref '%s'", o
->local_ref
);
566 else if (!check_ref_format(o
->local_ref
) && is_null_sha1(local_sha1
))
567 local
= NULL
; /* local_sha1 == null_sha1 indicates unborn ref */
568 else if (!(local
= lookup_commit_reference(local_sha1
)))
569 die("Could not parse local commit %s (%s)",
570 sha1_to_hex(local_sha1
), o
->local_ref
);
571 trace_printf("\tlocal commit: %.7s\n", sha1_to_hex(local_sha1
));
573 /* Dereference o->remote_ref into remote_sha1 */
574 if (get_sha1(o
->remote_ref
, remote_sha1
)) {
576 * Failed to get remote_sha1. If o->remote_ref looks like an
577 * unborn ref, perform the merge using an empty notes tree.
579 if (!check_ref_format(o
->remote_ref
)) {
580 hashclr(remote_sha1
);
583 die("Failed to resolve remote notes ref '%s'",
586 } else if (!(remote
= lookup_commit_reference(remote_sha1
))) {
587 die("Could not parse remote commit %s (%s)",
588 sha1_to_hex(remote_sha1
), o
->remote_ref
);
590 trace_printf("\tremote commit: %.7s\n", sha1_to_hex(remote_sha1
));
592 if (!local
&& !remote
)
593 die("Cannot merge empty notes ref (%s) into empty notes ref "
594 "(%s)", o
->remote_ref
, o
->local_ref
);
596 /* result == remote commit */
597 hashcpy(result_sha1
, remote_sha1
);
601 /* result == local commit */
602 hashcpy(result_sha1
, local_sha1
);
605 assert(local
&& remote
);
607 /* Find merge bases */
608 bases
= get_merge_bases(local
, remote
, 1);
610 base_sha1
= null_sha1
;
611 base_tree_sha1
= (unsigned char *)EMPTY_TREE_SHA1_BIN
;
612 OUTPUT(o
, 4, "No merge base found; doing history-less merge");
613 } else if (!bases
->next
) {
614 base_sha1
= bases
->item
->object
.sha1
;
615 base_tree_sha1
= bases
->item
->tree
->object
.sha1
;
616 OUTPUT(o
, 4, "One merge base found (%.7s)",
617 sha1_to_hex(base_sha1
));
619 /* TODO: How to handle multiple merge-bases? */
620 base_sha1
= bases
->item
->object
.sha1
;
621 base_tree_sha1
= bases
->item
->tree
->object
.sha1
;
622 OUTPUT(o
, 3, "Multiple merge bases found. Using the first "
623 "(%.7s)", sha1_to_hex(base_sha1
));
626 OUTPUT(o
, 4, "Merging remote commit %.7s into local commit %.7s with "
627 "merge-base %.7s", sha1_to_hex(remote
->object
.sha1
),
628 sha1_to_hex(local
->object
.sha1
), sha1_to_hex(base_sha1
));
630 if (!hashcmp(remote
->object
.sha1
, base_sha1
)) {
631 /* Already merged; result == local commit */
632 OUTPUT(o
, 2, "Already up-to-date!");
633 hashcpy(result_sha1
, local
->object
.sha1
);
636 if (!hashcmp(local
->object
.sha1
, base_sha1
)) {
637 /* Fast-forward; result == remote commit */
638 OUTPUT(o
, 2, "Fast-forward");
639 hashcpy(result_sha1
, remote
->object
.sha1
);
643 result
= merge_from_diffs(o
, base_tree_sha1
, local
->tree
->object
.sha1
,
644 remote
->tree
->object
.sha1
, local_tree
);
646 if (result
!= 0) { /* non-trivial merge (with or without conflicts) */
647 /* Commit (partial) result */
648 struct commit_list
*parents
= NULL
;
649 commit_list_insert(remote
, &parents
); /* LIFO order */
650 commit_list_insert(local
, &parents
);
651 create_notes_commit(local_tree
, parents
, o
->commit_msg
.buf
,
656 free_commit_list(bases
);
657 strbuf_release(&(o
->commit_msg
));
658 trace_printf("notes_merge(): result = %i, result_sha1 = %.7s\n",
659 result
, sha1_to_hex(result_sha1
));
663 int notes_merge_commit(struct notes_merge_options
*o
,
664 struct notes_tree
*partial_tree
,
665 struct commit
*partial_commit
,
666 unsigned char *result_sha1
)
669 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
670 * found notes to 'partial_tree'. Write the updates notes tree to
671 * the DB, and commit the resulting tree object while reusing the
672 * commit message and parents from 'partial_commit'.
673 * Finally store the new commit object SHA1 into 'result_sha1'.
675 struct dir_struct dir
;
676 const char *path
= git_path(NOTES_MERGE_WORKTREE
"/");
677 int path_len
= strlen(path
), i
;
678 const char *msg
= strstr(partial_commit
->buffer
, "\n\n");
680 OUTPUT(o
, 3, "Committing notes in notes merge worktree at %.*s",
683 if (!msg
|| msg
[2] == '\0')
684 die("partial notes commit has empty message");
687 memset(&dir
, 0, sizeof(dir
));
688 read_directory(&dir
, path
, path_len
, NULL
);
689 for (i
= 0; i
< dir
.nr
; i
++) {
690 struct dir_entry
*ent
= dir
.entries
[i
];
692 const char *relpath
= ent
->name
+ path_len
;
693 unsigned char obj_sha1
[20], blob_sha1
[20];
695 if (ent
->len
- path_len
!= 40 || get_sha1_hex(relpath
, obj_sha1
)) {
696 OUTPUT(o
, 3, "Skipping non-SHA1 entry '%s'", ent
->name
);
700 /* write file as blob, and add to partial_tree */
701 if (stat(ent
->name
, &st
))
702 die_errno("Failed to stat '%s'", ent
->name
);
703 if (index_path(blob_sha1
, ent
->name
, &st
, 1))
704 die("Failed to write blob object from '%s'", ent
->name
);
705 if (add_note(partial_tree
, obj_sha1
, blob_sha1
, NULL
))
706 die("Failed to add resolved note '%s' to notes tree",
708 OUTPUT(o
, 4, "Added resolved note for object %s: %s",
709 sha1_to_hex(obj_sha1
), sha1_to_hex(blob_sha1
));
712 create_notes_commit(partial_tree
, partial_commit
->parents
, msg
,
714 OUTPUT(o
, 4, "Finalized notes merge commit: %s",
715 sha1_to_hex(result_sha1
));
719 int notes_merge_abort(struct notes_merge_options
*o
)
721 /* Remove .git/NOTES_MERGE_WORKTREE directory and all files within */
722 struct strbuf buf
= STRBUF_INIT
;
725 strbuf_addstr(&buf
, git_path(NOTES_MERGE_WORKTREE
));
726 OUTPUT(o
, 3, "Removing notes merge worktree at %s", buf
.buf
);
727 ret
= remove_dir_recursively(&buf
, 0);
728 strbuf_release(&buf
);