4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
6 * Based on git-notes.sh by Johannes Schindelin,
7 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
16 #include "object-store.h"
17 #include "repository.h"
22 #include "run-command.h"
23 #include "parse-options.h"
24 #include "string-list.h"
25 #include "notes-merge.h"
26 #include "notes-utils.h"
28 #include "write-or-die.h"
30 static const char * const git_notes_usage
[] = {
31 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
32 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
33 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
34 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
35 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
36 N_("git notes [--ref <notes-ref>] show [<object>]"),
37 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
38 "git notes merge --commit [-v | -q]",
39 "git notes merge --abort [-v | -q]",
40 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
41 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
42 N_("git notes [--ref <notes-ref>] get-ref"),
46 static const char * const git_notes_list_usage
[] = {
47 N_("git notes [list [<object>]]"),
51 static const char * const git_notes_add_usage
[] = {
52 N_("git notes add [<options>] [<object>]"),
56 static const char * const git_notes_copy_usage
[] = {
57 N_("git notes copy [<options>] <from-object> <to-object>"),
58 N_("git notes copy --stdin [<from-object> <to-object>]..."),
62 static const char * const git_notes_append_usage
[] = {
63 N_("git notes append [<options>] [<object>]"),
67 static const char * const git_notes_edit_usage
[] = {
68 N_("git notes edit [<object>]"),
72 static const char * const git_notes_show_usage
[] = {
73 N_("git notes show [<object>]"),
77 static const char * const git_notes_merge_usage
[] = {
78 N_("git notes merge [<options>] <notes-ref>"),
79 N_("git notes merge --commit [<options>]"),
80 N_("git notes merge --abort [<options>]"),
84 static const char * const git_notes_remove_usage
[] = {
85 N_("git notes remove [<object>]"),
89 static const char * const git_notes_prune_usage
[] = {
90 N_("git notes prune [<options>]"),
94 static const char * const git_notes_get_ref_usage
[] = {
99 static const char note_template
[] =
100 N_("Write/edit the notes for the following object:");
109 static void free_note_data(struct note_data
*d
)
112 unlink_or_warn(d
->edit_path
);
115 strbuf_release(&d
->buf
);
118 static int list_each_note(const struct object_id
*object_oid
,
119 const struct object_id
*note_oid
,
120 char *note_path UNUSED
,
121 void *cb_data UNUSED
)
123 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
127 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
130 enum object_type type
;
131 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
134 write_or_die(fd
, buf
, size
);
139 static void write_commented_object(int fd
, const struct object_id
*object
)
141 struct child_process show
= CHILD_PROCESS_INIT
;
142 struct strbuf buf
= STRBUF_INIT
;
143 struct strbuf cbuf
= STRBUF_INIT
;
145 /* Invoke "git show --stat --no-notes $object" */
146 strvec_pushl(&show
.args
, "show", "--stat", "--no-notes",
147 oid_to_hex(object
), NULL
);
152 if (start_command(&show
))
153 die(_("unable to start 'show' for object '%s'"),
156 if (strbuf_read(&buf
, show
.out
, 0) < 0)
157 die_errno(_("could not read 'show' output"));
158 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
159 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
161 strbuf_release(&cbuf
);
162 strbuf_release(&buf
);
164 if (finish_command(&show
))
165 die(_("failed to finish 'show' for object '%s'"),
169 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
170 const struct object_id
*old_note
)
172 if (d
->use_editor
|| !d
->given
) {
174 struct strbuf buf
= STRBUF_INIT
;
176 /* write the template message before editing: */
177 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
178 fd
= xopen(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
181 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
183 copy_obj_to_fd(fd
, old_note
);
185 strbuf_addch(&buf
, '\n');
186 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
187 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)));
188 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
189 write_or_die(fd
, buf
.buf
, buf
.len
);
191 write_commented_object(fd
, object
);
194 strbuf_release(&buf
);
195 strbuf_reset(&d
->buf
);
197 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
198 die(_("please supply the note contents using either -m or -F option"));
200 strbuf_stripspace(&d
->buf
, 1);
204 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
206 if (write_object_file(d
->buf
.buf
, d
->buf
.len
, OBJ_BLOB
, oid
)) {
207 int status
= die_message(_("unable to write note object"));
210 die_message(_("the note contents have been left in %s"),
216 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
218 struct note_data
*d
= opt
->value
;
220 BUG_ON_OPT_NEG(unset
);
222 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
224 strbuf_addch(&d
->buf
, '\n');
225 strbuf_addstr(&d
->buf
, arg
);
226 strbuf_stripspace(&d
->buf
, 0);
232 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
234 struct note_data
*d
= opt
->value
;
236 BUG_ON_OPT_NEG(unset
);
239 strbuf_addch(&d
->buf
, '\n');
240 if (!strcmp(arg
, "-")) {
241 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
242 die_errno(_("cannot read '%s'"), arg
);
243 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
244 die_errno(_("could not open or read '%s'"), arg
);
245 strbuf_stripspace(&d
->buf
, 0);
251 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
253 struct note_data
*d
= opt
->value
;
255 struct object_id object
;
256 enum object_type type
;
259 BUG_ON_OPT_NEG(unset
);
262 strbuf_addch(&d
->buf
, '\n');
264 if (repo_get_oid(the_repository
, arg
, &object
))
265 die(_("failed to resolve '%s' as a valid ref."), arg
);
266 if (!(buf
= repo_read_object_file(the_repository
, &object
, &type
, &len
)))
267 die(_("failed to read object '%s'."), arg
);
268 if (type
!= OBJ_BLOB
) {
270 die(_("cannot read note data from non-blob object '%s'."), arg
);
272 strbuf_add(&d
->buf
, buf
, len
);
279 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
281 struct note_data
*d
= opt
->value
;
282 BUG_ON_OPT_NEG(unset
);
284 return parse_reuse_arg(opt
, arg
, unset
);
287 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
289 struct strbuf buf
= STRBUF_INIT
;
290 struct notes_rewrite_cfg
*c
= NULL
;
291 struct notes_tree
*t
= NULL
;
293 const char *msg
= "Notes added by 'git notes copy'";
296 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
300 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
301 t
= &default_notes_tree
;
304 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
305 struct object_id from_obj
, to_obj
;
306 struct strbuf
**split
;
309 split
= strbuf_split(&buf
, ' ');
310 if (!split
[0] || !split
[1])
311 die(_("malformed input line: '%s'."), buf
.buf
);
312 strbuf_rtrim(split
[0]);
313 strbuf_rtrim(split
[1]);
314 if (repo_get_oid(the_repository
, split
[0]->buf
, &from_obj
))
315 die(_("failed to resolve '%s' as a valid ref."), split
[0]->buf
);
316 if (repo_get_oid(the_repository
, split
[1]->buf
, &to_obj
))
317 die(_("failed to resolve '%s' as a valid ref."), split
[1]->buf
);
320 err
= copy_note_for_rewrite(c
, &from_obj
, &to_obj
);
322 err
= copy_note(t
, &from_obj
, &to_obj
, force
,
323 combine_notes_overwrite
);
326 error(_("failed to copy notes from '%s' to '%s'"),
327 split
[0]->buf
, split
[1]->buf
);
331 strbuf_list_free(split
);
335 commit_notes(the_repository
, t
, msg
);
338 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
340 strbuf_release(&buf
);
344 static struct notes_tree
*init_notes_check(const char *subcommand
,
347 struct notes_tree
*t
;
349 init_notes(NULL
, NULL
, NULL
, flags
);
350 t
= &default_notes_tree
;
352 ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->update_ref
: t
->ref
;
353 if (!starts_with(ref
, "refs/notes/"))
355 * TRANSLATORS: the first %s will be replaced by a git
356 * notes command: 'add', 'merge', 'remove', etc.
358 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
363 static int list(int argc
, const char **argv
, const char *prefix
)
365 struct notes_tree
*t
;
366 struct object_id object
;
367 const struct object_id
*note
;
369 struct option options
[] = {
374 argc
= parse_options(argc
, argv
, prefix
, options
,
375 git_notes_list_usage
, 0);
378 error(_("too many arguments"));
379 usage_with_options(git_notes_list_usage
, options
);
382 t
= init_notes_check("list", 0);
384 if (repo_get_oid(the_repository
, argv
[0], &object
))
385 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
386 note
= get_note(t
, &object
);
388 puts(oid_to_hex(note
));
391 retval
= error(_("no note found for object %s."),
392 oid_to_hex(&object
));
394 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
400 static int append_edit(int argc
, const char **argv
, const char *prefix
);
402 static int add(int argc
, const char **argv
, const char *prefix
)
404 int force
= 0, allow_empty
= 0;
405 const char *object_ref
;
406 struct notes_tree
*t
;
407 struct object_id object
, new_note
;
408 const struct object_id
*note
;
409 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
410 struct option options
[] = {
411 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
412 N_("note contents as a string"), PARSE_OPT_NONEG
,
414 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
415 N_("note contents in a file"), PARSE_OPT_NONEG
,
417 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
418 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
420 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
421 N_("reuse specified note object"), PARSE_OPT_NONEG
,
423 OPT_BOOL(0, "allow-empty", &allow_empty
,
424 N_("allow storing empty note")),
425 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
429 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
430 PARSE_OPT_KEEP_ARGV0
);
433 error(_("too many arguments"));
434 usage_with_options(git_notes_add_usage
, options
);
437 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
439 if (repo_get_oid(the_repository
, object_ref
, &object
))
440 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
442 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
443 note
= get_note(t
, &object
);
450 return error(_("Cannot add notes. "
451 "Found existing notes for object %s. "
452 "Use '-f' to overwrite existing notes"),
453 oid_to_hex(&object
));
456 * Redirect to "edit" subcommand.
458 * We only end up here if none of -m/-F/-c/-C or -f are
459 * given. The original args are therefore still in
463 return append_edit(argc
, argv
, prefix
);
465 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
466 oid_to_hex(&object
));
469 prepare_note_data(&object
, &d
, note
);
470 if (d
.buf
.len
|| allow_empty
) {
471 write_note_data(&d
, &new_note
);
472 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
473 BUG("combine_notes_overwrite failed");
474 commit_notes(the_repository
, t
,
475 "Notes added by 'git notes add'");
477 fprintf(stderr
, _("Removing note for object %s\n"),
478 oid_to_hex(&object
));
479 remove_note(t
, object
.hash
);
480 commit_notes(the_repository
, t
,
481 "Notes removed by 'git notes add'");
489 static int copy(int argc
, const char **argv
, const char *prefix
)
491 int retval
= 0, force
= 0, from_stdin
= 0;
492 const struct object_id
*from_note
, *note
;
493 const char *object_ref
;
494 struct object_id object
, from_obj
;
495 struct notes_tree
*t
;
496 const char *rewrite_cmd
= NULL
;
497 struct option options
[] = {
498 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
499 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
500 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
501 N_("load rewriting config for <command> (implies "
506 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
509 if (from_stdin
|| rewrite_cmd
) {
511 error(_("too many arguments"));
512 usage_with_options(git_notes_copy_usage
, options
);
514 return notes_copy_from_stdin(force
, rewrite_cmd
);
519 error(_("too few arguments"));
520 usage_with_options(git_notes_copy_usage
, options
);
523 error(_("too many arguments"));
524 usage_with_options(git_notes_copy_usage
, options
);
527 if (repo_get_oid(the_repository
, argv
[0], &from_obj
))
528 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
530 object_ref
= 1 < argc
? argv
[1] : "HEAD";
532 if (repo_get_oid(the_repository
, object_ref
, &object
))
533 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
535 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
536 note
= get_note(t
, &object
);
540 retval
= error(_("Cannot copy notes. Found existing "
541 "notes for object %s. Use '-f' to "
542 "overwrite existing notes"),
543 oid_to_hex(&object
));
546 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
547 oid_to_hex(&object
));
550 from_note
= get_note(t
, &from_obj
);
552 retval
= error(_("missing notes on source object %s. Cannot "
553 "copy."), oid_to_hex(&from_obj
));
557 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
558 BUG("combine_notes_overwrite failed");
559 commit_notes(the_repository
, t
,
560 "Notes added by 'git notes copy'");
566 static int append_edit(int argc
, const char **argv
, const char *prefix
)
569 const char *object_ref
;
570 struct notes_tree
*t
;
571 struct object_id object
, new_note
;
572 const struct object_id
*note
;
574 const char * const *usage
;
575 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
576 struct option options
[] = {
577 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
578 N_("note contents as a string"), PARSE_OPT_NONEG
,
580 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
581 N_("note contents in a file"), PARSE_OPT_NONEG
,
583 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
584 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
586 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
587 N_("reuse specified note object"), PARSE_OPT_NONEG
,
589 OPT_BOOL(0, "allow-empty", &allow_empty
,
590 N_("allow storing empty note")),
593 int edit
= !strcmp(argv
[0], "edit");
595 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
596 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
597 PARSE_OPT_KEEP_ARGV0
);
600 error(_("too many arguments"));
601 usage_with_options(usage
, options
);
605 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
606 "for the 'edit' subcommand.\n"
607 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
609 object_ref
= 1 < argc
? argv
[1] : "HEAD";
611 if (repo_get_oid(the_repository
, object_ref
, &object
))
612 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
614 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
615 note
= get_note(t
, &object
);
617 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
620 /* Append buf to previous note contents */
622 enum object_type type
;
623 char *prev_buf
= repo_read_object_file(the_repository
, note
,
626 strbuf_grow(&d
.buf
, size
+ 1);
627 if (d
.buf
.len
&& prev_buf
&& size
)
628 strbuf_insertstr(&d
.buf
, 0, "\n");
629 if (prev_buf
&& size
)
630 strbuf_insert(&d
.buf
, 0, prev_buf
, size
);
634 if (d
.buf
.len
|| allow_empty
) {
635 write_note_data(&d
, &new_note
);
636 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
637 BUG("combine_notes_overwrite failed");
638 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
640 fprintf(stderr
, _("Removing note for object %s\n"),
641 oid_to_hex(&object
));
642 remove_note(t
, object
.hash
);
643 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
645 commit_notes(the_repository
, t
, logmsg
);
653 static int show(int argc
, const char **argv
, const char *prefix
)
655 const char *object_ref
;
656 struct notes_tree
*t
;
657 struct object_id object
;
658 const struct object_id
*note
;
660 struct option options
[] = {
664 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
668 error(_("too many arguments"));
669 usage_with_options(git_notes_show_usage
, options
);
672 object_ref
= argc
? argv
[0] : "HEAD";
674 if (repo_get_oid(the_repository
, object_ref
, &object
))
675 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
677 t
= init_notes_check("show", 0);
678 note
= get_note(t
, &object
);
681 retval
= error(_("no note found for object %s."),
682 oid_to_hex(&object
));
684 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
685 retval
= execv_git_cmd(show_args
);
691 static int merge_abort(struct notes_merge_options
*o
)
696 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
697 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
700 if (delete_ref(NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
701 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
702 if (delete_ref(NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
703 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
704 if (notes_merge_abort(o
))
705 ret
+= error(_("failed to remove 'git notes merge' worktree"));
709 static int merge_commit(struct notes_merge_options
*o
)
711 struct strbuf msg
= STRBUF_INIT
;
712 struct object_id oid
, parent_oid
;
713 struct notes_tree
*t
;
714 struct commit
*partial
;
715 struct pretty_print_context pretty_ctx
;
716 void *local_ref_to_free
;
720 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
721 * and target notes ref from .git/NOTES_MERGE_REF.
724 if (repo_get_oid(the_repository
, "NOTES_MERGE_PARTIAL", &oid
))
725 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
726 else if (!(partial
= lookup_commit_reference(the_repository
, &oid
)))
727 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
728 else if (repo_parse_commit(the_repository
, partial
))
729 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
731 if (partial
->parents
)
732 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
737 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
739 o
->local_ref
= local_ref_to_free
=
740 resolve_refdup("NOTES_MERGE_REF", 0, &oid
, NULL
);
742 die(_("failed to resolve NOTES_MERGE_REF"));
744 if (notes_merge_commit(o
, t
, partial
, &oid
))
745 die(_("failed to finalize notes merge"));
747 /* Reuse existing commit message in reflog message */
748 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
749 repo_format_commit_message(the_repository
, partial
, "%s", &msg
,
752 strbuf_insertstr(&msg
, 0, "notes: ");
753 update_ref(msg
.buf
, o
->local_ref
, &oid
,
754 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
755 0, UPDATE_REFS_DIE_ON_ERR
);
758 strbuf_release(&msg
);
759 ret
= merge_abort(o
);
760 free(local_ref_to_free
);
764 static int git_config_get_notes_strategy(const char *key
,
765 enum notes_merge_strategy
*strategy
)
769 if (git_config_get_string(key
, &value
))
771 if (parse_notes_merge_strategy(value
, strategy
))
772 git_die_config(key
, _("unknown notes merge strategy %s"), value
);
778 static int merge(int argc
, const char **argv
, const char *prefix
)
780 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
781 struct object_id result_oid
;
782 struct notes_tree
*t
;
783 struct notes_merge_options o
;
784 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
785 int verbosity
= 0, result
;
786 const char *strategy
= NULL
;
787 struct option options
[] = {
788 OPT_GROUP(N_("General options")),
789 OPT__VERBOSITY(&verbosity
),
790 OPT_GROUP(N_("Merge options")),
791 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
792 N_("resolve notes conflicts using the given strategy "
793 "(manual/ours/theirs/union/cat_sort_uniq)")),
794 OPT_GROUP(N_("Committing unmerged notes")),
795 OPT_SET_INT_F(0, "commit", &do_commit
,
796 N_("finalize notes merge by committing unmerged notes"),
798 OPT_GROUP(N_("Aborting notes merge resolution")),
799 OPT_SET_INT_F(0, "abort", &do_abort
,
800 N_("abort notes merge"),
805 argc
= parse_options(argc
, argv
, prefix
, options
,
806 git_notes_merge_usage
, 0);
808 if (strategy
|| do_commit
+ do_abort
== 0)
810 if (do_merge
+ do_commit
+ do_abort
!= 1) {
811 error(_("cannot mix --commit, --abort or -s/--strategy"));
812 usage_with_options(git_notes_merge_usage
, options
);
815 if (do_merge
&& argc
!= 1) {
816 error(_("must specify a notes ref to merge"));
817 usage_with_options(git_notes_merge_usage
, options
);
818 } else if (!do_merge
&& argc
) {
819 error(_("too many arguments"));
820 usage_with_options(git_notes_merge_usage
, options
);
823 init_notes_merge_options(the_repository
, &o
);
824 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
827 return merge_abort(&o
);
829 return merge_commit(&o
);
831 o
.local_ref
= default_notes_ref();
832 strbuf_addstr(&remote_ref
, argv
[0]);
833 expand_loose_notes_ref(&remote_ref
);
834 o
.remote_ref
= remote_ref
.buf
;
836 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
839 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
840 error(_("unknown -s/--strategy: %s"), strategy
);
841 usage_with_options(git_notes_merge_usage
, options
);
844 struct strbuf merge_key
= STRBUF_INIT
;
845 const char *short_ref
= NULL
;
847 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
848 BUG("local ref %s is outside of refs/notes/",
851 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
853 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
854 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
856 strbuf_release(&merge_key
);
859 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
860 remote_ref
.buf
, default_notes_ref());
861 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
863 result
= notes_merge(&o
, t
, &result_oid
);
865 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
866 /* Update default notes ref with new commit */
867 update_ref(msg
.buf
, default_notes_ref(), &result_oid
, NULL
, 0,
868 UPDATE_REFS_DIE_ON_ERR
);
869 else { /* Merge has unresolved conflicts */
870 struct worktree
**worktrees
;
871 const struct worktree
*wt
;
872 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
873 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
874 0, UPDATE_REFS_DIE_ON_ERR
);
875 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
876 worktrees
= get_worktrees();
877 wt
= find_shared_symref(worktrees
, "NOTES_MERGE_REF",
878 default_notes_ref());
880 die(_("a notes merge into %s is already in-progress at %s"),
881 default_notes_ref(), wt
->path
);
882 free_worktrees(worktrees
);
883 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
884 die(_("failed to store link to current notes ref (%s)"),
885 default_notes_ref());
886 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
887 "and commit the result with 'git notes merge --commit', "
888 "or abort the merge with 'git notes merge --abort'.\n"),
889 git_path(NOTES_MERGE_WORKTREE
));
893 strbuf_release(&remote_ref
);
894 strbuf_release(&msg
);
895 return result
< 0; /* return non-zero on conflicts */
898 #define IGNORE_MISSING 1
900 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
903 struct object_id oid
;
904 if (repo_get_oid(the_repository
, name
, &oid
))
905 return error(_("Failed to resolve '%s' as a valid ref."), name
);
906 status
= remove_note(t
, oid
.hash
);
908 fprintf(stderr
, _("Object %s has no note\n"), name
);
910 fprintf(stderr
, _("Removing note for object %s\n"), name
);
911 return (flag
& IGNORE_MISSING
) ? 0 : status
;
914 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
918 struct option options
[] = {
919 OPT_BIT(0, "ignore-missing", &flag
,
920 N_("attempt to remove non-existent note is not an error"),
922 OPT_BOOL(0, "stdin", &from_stdin
,
923 N_("read object names from the standard input")),
926 struct notes_tree
*t
;
929 argc
= parse_options(argc
, argv
, prefix
, options
,
930 git_notes_remove_usage
, 0);
932 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
934 if (!argc
&& !from_stdin
) {
935 retval
= remove_one_note(t
, "HEAD", flag
);
938 retval
|= remove_one_note(t
, *argv
, flag
);
943 struct strbuf sb
= STRBUF_INIT
;
944 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
946 retval
|= remove_one_note(t
, sb
.buf
, flag
);
951 commit_notes(the_repository
, t
,
952 "Notes removed by 'git notes remove'");
957 static int prune(int argc
, const char **argv
, const char *prefix
)
959 struct notes_tree
*t
;
960 int show_only
= 0, verbose
= 0;
961 struct option options
[] = {
962 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
963 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
967 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
971 error(_("too many arguments"));
972 usage_with_options(git_notes_prune_usage
, options
);
975 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
977 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
978 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
980 commit_notes(the_repository
, t
,
981 "Notes removed by 'git notes prune'");
986 static int get_ref(int argc
, const char **argv
, const char *prefix
)
988 struct option options
[] = { OPT_END() };
989 argc
= parse_options(argc
, argv
, prefix
, options
,
990 git_notes_get_ref_usage
, 0);
993 error(_("too many arguments"));
994 usage_with_options(git_notes_get_ref_usage
, options
);
997 puts(default_notes_ref());
1001 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
1003 const char *override_notes_ref
= NULL
;
1004 parse_opt_subcommand_fn
*fn
= NULL
;
1005 struct option options
[] = {
1006 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
1007 N_("use notes from <notes-ref>")),
1008 OPT_SUBCOMMAND("list", &fn
, list
),
1009 OPT_SUBCOMMAND("add", &fn
, add
),
1010 OPT_SUBCOMMAND("copy", &fn
, copy
),
1011 OPT_SUBCOMMAND("append", &fn
, append_edit
),
1012 OPT_SUBCOMMAND("edit", &fn
, append_edit
),
1013 OPT_SUBCOMMAND("show", &fn
, show
),
1014 OPT_SUBCOMMAND("merge", &fn
, merge
),
1015 OPT_SUBCOMMAND("remove", &fn
, remove_cmd
),
1016 OPT_SUBCOMMAND("prune", &fn
, prune
),
1017 OPT_SUBCOMMAND("get-ref", &fn
, get_ref
),
1021 git_config(git_default_config
, NULL
);
1022 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1023 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1026 error(_("unknown subcommand: `%s'"), argv
[0]);
1027 usage_with_options(git_notes_usage
, options
);
1032 if (override_notes_ref
) {
1033 struct strbuf sb
= STRBUF_INIT
;
1034 strbuf_addstr(&sb
, override_notes_ref
);
1035 expand_notes_ref(&sb
);
1036 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1037 strbuf_release(&sb
);
1040 return !!fn(argc
, argv
, prefix
);