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.
17 #include "object-name.h"
18 #include "object-store.h"
19 #include "repository.h"
24 #include "run-command.h"
25 #include "parse-options.h"
26 #include "string-list.h"
27 #include "notes-merge.h"
28 #include "notes-utils.h"
30 #include "write-or-die.h"
32 static const char * const git_notes_usage
[] = {
33 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
34 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
35 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
36 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
37 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
38 N_("git notes [--ref <notes-ref>] show [<object>]"),
39 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
40 "git notes merge --commit [-v | -q]",
41 "git notes merge --abort [-v | -q]",
42 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
43 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
44 N_("git notes [--ref <notes-ref>] get-ref"),
48 static const char * const git_notes_list_usage
[] = {
49 N_("git notes [list [<object>]]"),
53 static const char * const git_notes_add_usage
[] = {
54 N_("git notes add [<options>] [<object>]"),
58 static const char * const git_notes_copy_usage
[] = {
59 N_("git notes copy [<options>] <from-object> <to-object>"),
60 N_("git notes copy --stdin [<from-object> <to-object>]..."),
64 static const char * const git_notes_append_usage
[] = {
65 N_("git notes append [<options>] [<object>]"),
69 static const char * const git_notes_edit_usage
[] = {
70 N_("git notes edit [<object>]"),
74 static const char * const git_notes_show_usage
[] = {
75 N_("git notes show [<object>]"),
79 static const char * const git_notes_merge_usage
[] = {
80 N_("git notes merge [<options>] <notes-ref>"),
81 N_("git notes merge --commit [<options>]"),
82 N_("git notes merge --abort [<options>]"),
86 static const char * const git_notes_remove_usage
[] = {
87 N_("git notes remove [<object>]"),
91 static const char * const git_notes_prune_usage
[] = {
92 N_("git notes prune [<options>]"),
96 static const char * const git_notes_get_ref_usage
[] = {
101 static const char note_template
[] =
102 N_("Write/edit the notes for the following object:");
111 static void free_note_data(struct note_data
*d
)
114 unlink_or_warn(d
->edit_path
);
117 strbuf_release(&d
->buf
);
120 static int list_each_note(const struct object_id
*object_oid
,
121 const struct object_id
*note_oid
,
122 char *note_path UNUSED
,
123 void *cb_data UNUSED
)
125 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
129 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
132 enum object_type type
;
133 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
136 write_or_die(fd
, buf
, size
);
141 static void write_commented_object(int fd
, const struct object_id
*object
)
143 struct child_process show
= CHILD_PROCESS_INIT
;
144 struct strbuf buf
= STRBUF_INIT
;
145 struct strbuf cbuf
= STRBUF_INIT
;
147 /* Invoke "git show --stat --no-notes $object" */
148 strvec_pushl(&show
.args
, "show", "--stat", "--no-notes",
149 oid_to_hex(object
), NULL
);
154 if (start_command(&show
))
155 die(_("unable to start 'show' for object '%s'"),
158 if (strbuf_read(&buf
, show
.out
, 0) < 0)
159 die_errno(_("could not read 'show' output"));
160 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
161 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
163 strbuf_release(&cbuf
);
164 strbuf_release(&buf
);
166 if (finish_command(&show
))
167 die(_("failed to finish 'show' for object '%s'"),
171 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
172 const struct object_id
*old_note
)
174 if (d
->use_editor
|| !d
->given
) {
176 struct strbuf buf
= STRBUF_INIT
;
178 /* write the template message before editing: */
179 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
180 fd
= xopen(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
183 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
185 copy_obj_to_fd(fd
, old_note
);
187 strbuf_addch(&buf
, '\n');
188 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
189 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)));
190 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
191 write_or_die(fd
, buf
.buf
, buf
.len
);
193 write_commented_object(fd
, object
);
196 strbuf_release(&buf
);
197 strbuf_reset(&d
->buf
);
199 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
200 die(_("please supply the note contents using either -m or -F option"));
202 strbuf_stripspace(&d
->buf
, 1);
206 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
208 if (write_object_file(d
->buf
.buf
, d
->buf
.len
, OBJ_BLOB
, oid
)) {
209 int status
= die_message(_("unable to write note object"));
212 die_message(_("the note contents have been left in %s"),
218 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
220 struct note_data
*d
= opt
->value
;
222 BUG_ON_OPT_NEG(unset
);
224 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
226 strbuf_addch(&d
->buf
, '\n');
227 strbuf_addstr(&d
->buf
, arg
);
228 strbuf_stripspace(&d
->buf
, 0);
234 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
236 struct note_data
*d
= opt
->value
;
238 BUG_ON_OPT_NEG(unset
);
241 strbuf_addch(&d
->buf
, '\n');
242 if (!strcmp(arg
, "-")) {
243 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
244 die_errno(_("cannot read '%s'"), arg
);
245 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
246 die_errno(_("could not open or read '%s'"), arg
);
247 strbuf_stripspace(&d
->buf
, 0);
253 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
255 struct note_data
*d
= opt
->value
;
257 struct object_id object
;
258 enum object_type type
;
261 BUG_ON_OPT_NEG(unset
);
264 strbuf_addch(&d
->buf
, '\n');
266 if (repo_get_oid(the_repository
, arg
, &object
))
267 die(_("failed to resolve '%s' as a valid ref."), arg
);
268 if (!(buf
= repo_read_object_file(the_repository
, &object
, &type
, &len
)))
269 die(_("failed to read object '%s'."), arg
);
270 if (type
!= OBJ_BLOB
) {
272 die(_("cannot read note data from non-blob object '%s'."), arg
);
274 strbuf_add(&d
->buf
, buf
, len
);
281 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
283 struct note_data
*d
= opt
->value
;
284 BUG_ON_OPT_NEG(unset
);
286 return parse_reuse_arg(opt
, arg
, unset
);
289 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
291 struct strbuf buf
= STRBUF_INIT
;
292 struct notes_rewrite_cfg
*c
= NULL
;
293 struct notes_tree
*t
= NULL
;
295 const char *msg
= "Notes added by 'git notes copy'";
298 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
302 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
303 t
= &default_notes_tree
;
306 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
307 struct object_id from_obj
, to_obj
;
308 struct strbuf
**split
;
311 split
= strbuf_split(&buf
, ' ');
312 if (!split
[0] || !split
[1])
313 die(_("malformed input line: '%s'."), buf
.buf
);
314 strbuf_rtrim(split
[0]);
315 strbuf_rtrim(split
[1]);
316 if (repo_get_oid(the_repository
, split
[0]->buf
, &from_obj
))
317 die(_("failed to resolve '%s' as a valid ref."), split
[0]->buf
);
318 if (repo_get_oid(the_repository
, split
[1]->buf
, &to_obj
))
319 die(_("failed to resolve '%s' as a valid ref."), split
[1]->buf
);
322 err
= copy_note_for_rewrite(c
, &from_obj
, &to_obj
);
324 err
= copy_note(t
, &from_obj
, &to_obj
, force
,
325 combine_notes_overwrite
);
328 error(_("failed to copy notes from '%s' to '%s'"),
329 split
[0]->buf
, split
[1]->buf
);
333 strbuf_list_free(split
);
337 commit_notes(the_repository
, t
, msg
);
340 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
342 strbuf_release(&buf
);
346 static struct notes_tree
*init_notes_check(const char *subcommand
,
349 struct notes_tree
*t
;
351 init_notes(NULL
, NULL
, NULL
, flags
);
352 t
= &default_notes_tree
;
354 ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->update_ref
: t
->ref
;
355 if (!starts_with(ref
, "refs/notes/"))
357 * TRANSLATORS: the first %s will be replaced by a git
358 * notes command: 'add', 'merge', 'remove', etc.
360 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
365 static int list(int argc
, const char **argv
, const char *prefix
)
367 struct notes_tree
*t
;
368 struct object_id object
;
369 const struct object_id
*note
;
371 struct option options
[] = {
376 argc
= parse_options(argc
, argv
, prefix
, options
,
377 git_notes_list_usage
, 0);
380 error(_("too many arguments"));
381 usage_with_options(git_notes_list_usage
, options
);
384 t
= init_notes_check("list", 0);
386 if (repo_get_oid(the_repository
, argv
[0], &object
))
387 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
388 note
= get_note(t
, &object
);
390 puts(oid_to_hex(note
));
393 retval
= error(_("no note found for object %s."),
394 oid_to_hex(&object
));
396 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
402 static int append_edit(int argc
, const char **argv
, const char *prefix
);
404 static int add(int argc
, const char **argv
, const char *prefix
)
406 int force
= 0, allow_empty
= 0;
407 const char *object_ref
;
408 struct notes_tree
*t
;
409 struct object_id object
, new_note
;
410 const struct object_id
*note
;
411 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
412 struct option options
[] = {
413 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
414 N_("note contents as a string"), PARSE_OPT_NONEG
,
416 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
417 N_("note contents in a file"), PARSE_OPT_NONEG
,
419 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
420 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
422 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
423 N_("reuse specified note object"), PARSE_OPT_NONEG
,
425 OPT_BOOL(0, "allow-empty", &allow_empty
,
426 N_("allow storing empty note")),
427 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
431 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
432 PARSE_OPT_KEEP_ARGV0
);
435 error(_("too many arguments"));
436 usage_with_options(git_notes_add_usage
, options
);
439 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
441 if (repo_get_oid(the_repository
, object_ref
, &object
))
442 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
444 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
445 note
= get_note(t
, &object
);
452 return error(_("Cannot add notes. "
453 "Found existing notes for object %s. "
454 "Use '-f' to overwrite existing notes"),
455 oid_to_hex(&object
));
458 * Redirect to "edit" subcommand.
460 * We only end up here if none of -m/-F/-c/-C or -f are
461 * given. The original args are therefore still in
465 return append_edit(argc
, argv
, prefix
);
467 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
468 oid_to_hex(&object
));
471 prepare_note_data(&object
, &d
, note
);
472 if (d
.buf
.len
|| allow_empty
) {
473 write_note_data(&d
, &new_note
);
474 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
475 BUG("combine_notes_overwrite failed");
476 commit_notes(the_repository
, t
,
477 "Notes added by 'git notes add'");
479 fprintf(stderr
, _("Removing note for object %s\n"),
480 oid_to_hex(&object
));
481 remove_note(t
, object
.hash
);
482 commit_notes(the_repository
, t
,
483 "Notes removed by 'git notes add'");
491 static int copy(int argc
, const char **argv
, const char *prefix
)
493 int retval
= 0, force
= 0, from_stdin
= 0;
494 const struct object_id
*from_note
, *note
;
495 const char *object_ref
;
496 struct object_id object
, from_obj
;
497 struct notes_tree
*t
;
498 const char *rewrite_cmd
= NULL
;
499 struct option options
[] = {
500 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
501 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
502 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
503 N_("load rewriting config for <command> (implies "
508 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
511 if (from_stdin
|| rewrite_cmd
) {
513 error(_("too many arguments"));
514 usage_with_options(git_notes_copy_usage
, options
);
516 return notes_copy_from_stdin(force
, rewrite_cmd
);
521 error(_("too few arguments"));
522 usage_with_options(git_notes_copy_usage
, options
);
525 error(_("too many arguments"));
526 usage_with_options(git_notes_copy_usage
, options
);
529 if (repo_get_oid(the_repository
, argv
[0], &from_obj
))
530 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
532 object_ref
= 1 < argc
? argv
[1] : "HEAD";
534 if (repo_get_oid(the_repository
, object_ref
, &object
))
535 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
537 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
538 note
= get_note(t
, &object
);
542 retval
= error(_("Cannot copy notes. Found existing "
543 "notes for object %s. Use '-f' to "
544 "overwrite existing notes"),
545 oid_to_hex(&object
));
548 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
549 oid_to_hex(&object
));
552 from_note
= get_note(t
, &from_obj
);
554 retval
= error(_("missing notes on source object %s. Cannot "
555 "copy."), oid_to_hex(&from_obj
));
559 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
560 BUG("combine_notes_overwrite failed");
561 commit_notes(the_repository
, t
,
562 "Notes added by 'git notes copy'");
568 static int append_edit(int argc
, const char **argv
, const char *prefix
)
571 const char *object_ref
;
572 struct notes_tree
*t
;
573 struct object_id object
, new_note
;
574 const struct object_id
*note
;
576 const char * const *usage
;
577 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
578 struct option options
[] = {
579 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
580 N_("note contents as a string"), PARSE_OPT_NONEG
,
582 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
583 N_("note contents in a file"), PARSE_OPT_NONEG
,
585 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
586 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
588 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
589 N_("reuse specified note object"), PARSE_OPT_NONEG
,
591 OPT_BOOL(0, "allow-empty", &allow_empty
,
592 N_("allow storing empty note")),
595 int edit
= !strcmp(argv
[0], "edit");
597 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
598 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
599 PARSE_OPT_KEEP_ARGV0
);
602 error(_("too many arguments"));
603 usage_with_options(usage
, options
);
607 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
608 "for the 'edit' subcommand.\n"
609 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
611 object_ref
= 1 < argc
? argv
[1] : "HEAD";
613 if (repo_get_oid(the_repository
, object_ref
, &object
))
614 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
616 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
617 note
= get_note(t
, &object
);
619 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
622 /* Append buf to previous note contents */
624 enum object_type type
;
625 char *prev_buf
= repo_read_object_file(the_repository
, note
,
628 strbuf_grow(&d
.buf
, size
+ 1);
629 if (d
.buf
.len
&& prev_buf
&& size
)
630 strbuf_insertstr(&d
.buf
, 0, "\n");
631 if (prev_buf
&& size
)
632 strbuf_insert(&d
.buf
, 0, prev_buf
, size
);
636 if (d
.buf
.len
|| allow_empty
) {
637 write_note_data(&d
, &new_note
);
638 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
639 BUG("combine_notes_overwrite failed");
640 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
642 fprintf(stderr
, _("Removing note for object %s\n"),
643 oid_to_hex(&object
));
644 remove_note(t
, object
.hash
);
645 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
647 commit_notes(the_repository
, t
, logmsg
);
655 static int show(int argc
, const char **argv
, const char *prefix
)
657 const char *object_ref
;
658 struct notes_tree
*t
;
659 struct object_id object
;
660 const struct object_id
*note
;
662 struct option options
[] = {
666 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
670 error(_("too many arguments"));
671 usage_with_options(git_notes_show_usage
, options
);
674 object_ref
= argc
? argv
[0] : "HEAD";
676 if (repo_get_oid(the_repository
, object_ref
, &object
))
677 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
679 t
= init_notes_check("show", 0);
680 note
= get_note(t
, &object
);
683 retval
= error(_("no note found for object %s."),
684 oid_to_hex(&object
));
686 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
687 retval
= execv_git_cmd(show_args
);
693 static int merge_abort(struct notes_merge_options
*o
)
698 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
699 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
702 if (delete_ref(NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
703 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
704 if (delete_ref(NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
705 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
706 if (notes_merge_abort(o
))
707 ret
+= error(_("failed to remove 'git notes merge' worktree"));
711 static int merge_commit(struct notes_merge_options
*o
)
713 struct strbuf msg
= STRBUF_INIT
;
714 struct object_id oid
, parent_oid
;
715 struct notes_tree
*t
;
716 struct commit
*partial
;
717 struct pretty_print_context pretty_ctx
;
718 void *local_ref_to_free
;
722 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
723 * and target notes ref from .git/NOTES_MERGE_REF.
726 if (repo_get_oid(the_repository
, "NOTES_MERGE_PARTIAL", &oid
))
727 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
728 else if (!(partial
= lookup_commit_reference(the_repository
, &oid
)))
729 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
730 else if (repo_parse_commit(the_repository
, partial
))
731 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
733 if (partial
->parents
)
734 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
739 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
741 o
->local_ref
= local_ref_to_free
=
742 resolve_refdup("NOTES_MERGE_REF", 0, &oid
, NULL
);
744 die(_("failed to resolve NOTES_MERGE_REF"));
746 if (notes_merge_commit(o
, t
, partial
, &oid
))
747 die(_("failed to finalize notes merge"));
749 /* Reuse existing commit message in reflog message */
750 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
751 repo_format_commit_message(the_repository
, partial
, "%s", &msg
,
754 strbuf_insertstr(&msg
, 0, "notes: ");
755 update_ref(msg
.buf
, o
->local_ref
, &oid
,
756 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
757 0, UPDATE_REFS_DIE_ON_ERR
);
760 strbuf_release(&msg
);
761 ret
= merge_abort(o
);
762 free(local_ref_to_free
);
766 static int git_config_get_notes_strategy(const char *key
,
767 enum notes_merge_strategy
*strategy
)
771 if (git_config_get_string(key
, &value
))
773 if (parse_notes_merge_strategy(value
, strategy
))
774 git_die_config(key
, _("unknown notes merge strategy %s"), value
);
780 static int merge(int argc
, const char **argv
, const char *prefix
)
782 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
783 struct object_id result_oid
;
784 struct notes_tree
*t
;
785 struct notes_merge_options o
;
786 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
787 int verbosity
= 0, result
;
788 const char *strategy
= NULL
;
789 struct option options
[] = {
790 OPT_GROUP(N_("General options")),
791 OPT__VERBOSITY(&verbosity
),
792 OPT_GROUP(N_("Merge options")),
793 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
794 N_("resolve notes conflicts using the given strategy "
795 "(manual/ours/theirs/union/cat_sort_uniq)")),
796 OPT_GROUP(N_("Committing unmerged notes")),
797 OPT_SET_INT_F(0, "commit", &do_commit
,
798 N_("finalize notes merge by committing unmerged notes"),
800 OPT_GROUP(N_("Aborting notes merge resolution")),
801 OPT_SET_INT_F(0, "abort", &do_abort
,
802 N_("abort notes merge"),
807 argc
= parse_options(argc
, argv
, prefix
, options
,
808 git_notes_merge_usage
, 0);
810 if (strategy
|| do_commit
+ do_abort
== 0)
812 if (do_merge
+ do_commit
+ do_abort
!= 1) {
813 error(_("cannot mix --commit, --abort or -s/--strategy"));
814 usage_with_options(git_notes_merge_usage
, options
);
817 if (do_merge
&& argc
!= 1) {
818 error(_("must specify a notes ref to merge"));
819 usage_with_options(git_notes_merge_usage
, options
);
820 } else if (!do_merge
&& argc
) {
821 error(_("too many arguments"));
822 usage_with_options(git_notes_merge_usage
, options
);
825 init_notes_merge_options(the_repository
, &o
);
826 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
829 return merge_abort(&o
);
831 return merge_commit(&o
);
833 o
.local_ref
= default_notes_ref();
834 strbuf_addstr(&remote_ref
, argv
[0]);
835 expand_loose_notes_ref(&remote_ref
);
836 o
.remote_ref
= remote_ref
.buf
;
838 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
841 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
842 error(_("unknown -s/--strategy: %s"), strategy
);
843 usage_with_options(git_notes_merge_usage
, options
);
846 struct strbuf merge_key
= STRBUF_INIT
;
847 const char *short_ref
= NULL
;
849 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
850 BUG("local ref %s is outside of refs/notes/",
853 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
855 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
856 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
858 strbuf_release(&merge_key
);
861 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
862 remote_ref
.buf
, default_notes_ref());
863 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
865 result
= notes_merge(&o
, t
, &result_oid
);
867 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
868 /* Update default notes ref with new commit */
869 update_ref(msg
.buf
, default_notes_ref(), &result_oid
, NULL
, 0,
870 UPDATE_REFS_DIE_ON_ERR
);
871 else { /* Merge has unresolved conflicts */
872 struct worktree
**worktrees
;
873 const struct worktree
*wt
;
874 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
875 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
876 0, UPDATE_REFS_DIE_ON_ERR
);
877 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
878 worktrees
= get_worktrees();
879 wt
= find_shared_symref(worktrees
, "NOTES_MERGE_REF",
880 default_notes_ref());
882 die(_("a notes merge into %s is already in-progress at %s"),
883 default_notes_ref(), wt
->path
);
884 free_worktrees(worktrees
);
885 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
886 die(_("failed to store link to current notes ref (%s)"),
887 default_notes_ref());
888 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
889 "and commit the result with 'git notes merge --commit', "
890 "or abort the merge with 'git notes merge --abort'.\n"),
891 git_path(NOTES_MERGE_WORKTREE
));
895 strbuf_release(&remote_ref
);
896 strbuf_release(&msg
);
897 return result
< 0; /* return non-zero on conflicts */
900 #define IGNORE_MISSING 1
902 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
905 struct object_id oid
;
906 if (repo_get_oid(the_repository
, name
, &oid
))
907 return error(_("Failed to resolve '%s' as a valid ref."), name
);
908 status
= remove_note(t
, oid
.hash
);
910 fprintf(stderr
, _("Object %s has no note\n"), name
);
912 fprintf(stderr
, _("Removing note for object %s\n"), name
);
913 return (flag
& IGNORE_MISSING
) ? 0 : status
;
916 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
920 struct option options
[] = {
921 OPT_BIT(0, "ignore-missing", &flag
,
922 N_("attempt to remove non-existent note is not an error"),
924 OPT_BOOL(0, "stdin", &from_stdin
,
925 N_("read object names from the standard input")),
928 struct notes_tree
*t
;
931 argc
= parse_options(argc
, argv
, prefix
, options
,
932 git_notes_remove_usage
, 0);
934 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
936 if (!argc
&& !from_stdin
) {
937 retval
= remove_one_note(t
, "HEAD", flag
);
940 retval
|= remove_one_note(t
, *argv
, flag
);
945 struct strbuf sb
= STRBUF_INIT
;
946 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
948 retval
|= remove_one_note(t
, sb
.buf
, flag
);
953 commit_notes(the_repository
, t
,
954 "Notes removed by 'git notes remove'");
959 static int prune(int argc
, const char **argv
, const char *prefix
)
961 struct notes_tree
*t
;
962 int show_only
= 0, verbose
= 0;
963 struct option options
[] = {
964 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
965 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
969 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
973 error(_("too many arguments"));
974 usage_with_options(git_notes_prune_usage
, options
);
977 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
979 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
980 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
982 commit_notes(the_repository
, t
,
983 "Notes removed by 'git notes prune'");
988 static int get_ref(int argc
, const char **argv
, const char *prefix
)
990 struct option options
[] = { OPT_END() };
991 argc
= parse_options(argc
, argv
, prefix
, options
,
992 git_notes_get_ref_usage
, 0);
995 error(_("too many arguments"));
996 usage_with_options(git_notes_get_ref_usage
, options
);
999 puts(default_notes_ref());
1003 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
1005 const char *override_notes_ref
= NULL
;
1006 parse_opt_subcommand_fn
*fn
= NULL
;
1007 struct option options
[] = {
1008 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
1009 N_("use notes from <notes-ref>")),
1010 OPT_SUBCOMMAND("list", &fn
, list
),
1011 OPT_SUBCOMMAND("add", &fn
, add
),
1012 OPT_SUBCOMMAND("copy", &fn
, copy
),
1013 OPT_SUBCOMMAND("append", &fn
, append_edit
),
1014 OPT_SUBCOMMAND("edit", &fn
, append_edit
),
1015 OPT_SUBCOMMAND("show", &fn
, show
),
1016 OPT_SUBCOMMAND("merge", &fn
, merge
),
1017 OPT_SUBCOMMAND("remove", &fn
, remove_cmd
),
1018 OPT_SUBCOMMAND("prune", &fn
, prune
),
1019 OPT_SUBCOMMAND("get-ref", &fn
, get_ref
),
1023 git_config(git_default_config
, NULL
);
1024 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1025 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1028 error(_("unknown subcommand: `%s'"), argv
[0]);
1029 usage_with_options(git_notes_usage
, options
);
1034 if (override_notes_ref
) {
1035 struct strbuf sb
= STRBUF_INIT
;
1036 strbuf_addstr(&sb
, override_notes_ref
);
1037 expand_notes_ref(&sb
);
1038 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1039 strbuf_release(&sb
);
1042 return !!fn(argc
, argv
, prefix
);