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-name.h"
17 #include "object-store.h"
18 #include "repository.h"
23 #include "run-command.h"
24 #include "parse-options.h"
25 #include "string-list.h"
26 #include "notes-merge.h"
27 #include "notes-utils.h"
29 #include "write-or-die.h"
31 static const char * const git_notes_usage
[] = {
32 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
33 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
34 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
35 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
36 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
37 N_("git notes [--ref <notes-ref>] show [<object>]"),
38 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
39 "git notes merge --commit [-v | -q]",
40 "git notes merge --abort [-v | -q]",
41 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
42 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
43 N_("git notes [--ref <notes-ref>] get-ref"),
47 static const char * const git_notes_list_usage
[] = {
48 N_("git notes [list [<object>]]"),
52 static const char * const git_notes_add_usage
[] = {
53 N_("git notes add [<options>] [<object>]"),
57 static const char * const git_notes_copy_usage
[] = {
58 N_("git notes copy [<options>] <from-object> <to-object>"),
59 N_("git notes copy --stdin [<from-object> <to-object>]..."),
63 static const char * const git_notes_append_usage
[] = {
64 N_("git notes append [<options>] [<object>]"),
68 static const char * const git_notes_edit_usage
[] = {
69 N_("git notes edit [<object>]"),
73 static const char * const git_notes_show_usage
[] = {
74 N_("git notes show [<object>]"),
78 static const char * const git_notes_merge_usage
[] = {
79 N_("git notes merge [<options>] <notes-ref>"),
80 N_("git notes merge --commit [<options>]"),
81 N_("git notes merge --abort [<options>]"),
85 static const char * const git_notes_remove_usage
[] = {
86 N_("git notes remove [<object>]"),
90 static const char * const git_notes_prune_usage
[] = {
91 N_("git notes prune [<options>]"),
95 static const char * const git_notes_get_ref_usage
[] = {
100 static const char note_template
[] =
101 N_("Write/edit the notes for the following object:");
110 static void free_note_data(struct note_data
*d
)
113 unlink_or_warn(d
->edit_path
);
116 strbuf_release(&d
->buf
);
119 static int list_each_note(const struct object_id
*object_oid
,
120 const struct object_id
*note_oid
,
121 char *note_path UNUSED
,
122 void *cb_data UNUSED
)
124 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
128 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
131 enum object_type type
;
132 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
135 write_or_die(fd
, buf
, size
);
140 static void write_commented_object(int fd
, const struct object_id
*object
)
142 struct child_process show
= CHILD_PROCESS_INIT
;
143 struct strbuf buf
= STRBUF_INIT
;
144 struct strbuf cbuf
= STRBUF_INIT
;
146 /* Invoke "git show --stat --no-notes $object" */
147 strvec_pushl(&show
.args
, "show", "--stat", "--no-notes",
148 oid_to_hex(object
), NULL
);
153 if (start_command(&show
))
154 die(_("unable to start 'show' for object '%s'"),
157 if (strbuf_read(&buf
, show
.out
, 0) < 0)
158 die_errno(_("could not read 'show' output"));
159 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
160 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
162 strbuf_release(&cbuf
);
163 strbuf_release(&buf
);
165 if (finish_command(&show
))
166 die(_("failed to finish 'show' for object '%s'"),
170 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
171 const struct object_id
*old_note
)
173 if (d
->use_editor
|| !d
->given
) {
175 struct strbuf buf
= STRBUF_INIT
;
177 /* write the template message before editing: */
178 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
179 fd
= xopen(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
182 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
184 copy_obj_to_fd(fd
, old_note
);
186 strbuf_addch(&buf
, '\n');
187 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
188 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)));
189 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
190 write_or_die(fd
, buf
.buf
, buf
.len
);
192 write_commented_object(fd
, object
);
195 strbuf_release(&buf
);
196 strbuf_reset(&d
->buf
);
198 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
199 die(_("please supply the note contents using either -m or -F option"));
201 strbuf_stripspace(&d
->buf
, 1);
205 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
207 if (write_object_file(d
->buf
.buf
, d
->buf
.len
, OBJ_BLOB
, oid
)) {
208 int status
= die_message(_("unable to write note object"));
211 die_message(_("the note contents have been left in %s"),
217 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
219 struct note_data
*d
= opt
->value
;
221 BUG_ON_OPT_NEG(unset
);
223 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
225 strbuf_addch(&d
->buf
, '\n');
226 strbuf_addstr(&d
->buf
, arg
);
227 strbuf_stripspace(&d
->buf
, 0);
233 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
235 struct note_data
*d
= opt
->value
;
237 BUG_ON_OPT_NEG(unset
);
240 strbuf_addch(&d
->buf
, '\n');
241 if (!strcmp(arg
, "-")) {
242 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
243 die_errno(_("cannot read '%s'"), arg
);
244 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
245 die_errno(_("could not open or read '%s'"), arg
);
246 strbuf_stripspace(&d
->buf
, 0);
252 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
254 struct note_data
*d
= opt
->value
;
256 struct object_id object
;
257 enum object_type type
;
260 BUG_ON_OPT_NEG(unset
);
263 strbuf_addch(&d
->buf
, '\n');
265 if (repo_get_oid(the_repository
, arg
, &object
))
266 die(_("failed to resolve '%s' as a valid ref."), arg
);
267 if (!(buf
= repo_read_object_file(the_repository
, &object
, &type
, &len
)))
268 die(_("failed to read object '%s'."), arg
);
269 if (type
!= OBJ_BLOB
) {
271 die(_("cannot read note data from non-blob object '%s'."), arg
);
273 strbuf_add(&d
->buf
, buf
, len
);
280 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
282 struct note_data
*d
= opt
->value
;
283 BUG_ON_OPT_NEG(unset
);
285 return parse_reuse_arg(opt
, arg
, unset
);
288 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
290 struct strbuf buf
= STRBUF_INIT
;
291 struct notes_rewrite_cfg
*c
= NULL
;
292 struct notes_tree
*t
= NULL
;
294 const char *msg
= "Notes added by 'git notes copy'";
297 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
301 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
302 t
= &default_notes_tree
;
305 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
306 struct object_id from_obj
, to_obj
;
307 struct strbuf
**split
;
310 split
= strbuf_split(&buf
, ' ');
311 if (!split
[0] || !split
[1])
312 die(_("malformed input line: '%s'."), buf
.buf
);
313 strbuf_rtrim(split
[0]);
314 strbuf_rtrim(split
[1]);
315 if (repo_get_oid(the_repository
, split
[0]->buf
, &from_obj
))
316 die(_("failed to resolve '%s' as a valid ref."), split
[0]->buf
);
317 if (repo_get_oid(the_repository
, split
[1]->buf
, &to_obj
))
318 die(_("failed to resolve '%s' as a valid ref."), split
[1]->buf
);
321 err
= copy_note_for_rewrite(c
, &from_obj
, &to_obj
);
323 err
= copy_note(t
, &from_obj
, &to_obj
, force
,
324 combine_notes_overwrite
);
327 error(_("failed to copy notes from '%s' to '%s'"),
328 split
[0]->buf
, split
[1]->buf
);
332 strbuf_list_free(split
);
336 commit_notes(the_repository
, t
, msg
);
339 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
341 strbuf_release(&buf
);
345 static struct notes_tree
*init_notes_check(const char *subcommand
,
348 struct notes_tree
*t
;
350 init_notes(NULL
, NULL
, NULL
, flags
);
351 t
= &default_notes_tree
;
353 ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->update_ref
: t
->ref
;
354 if (!starts_with(ref
, "refs/notes/"))
356 * TRANSLATORS: the first %s will be replaced by a git
357 * notes command: 'add', 'merge', 'remove', etc.
359 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
364 static int list(int argc
, const char **argv
, const char *prefix
)
366 struct notes_tree
*t
;
367 struct object_id object
;
368 const struct object_id
*note
;
370 struct option options
[] = {
375 argc
= parse_options(argc
, argv
, prefix
, options
,
376 git_notes_list_usage
, 0);
379 error(_("too many arguments"));
380 usage_with_options(git_notes_list_usage
, options
);
383 t
= init_notes_check("list", 0);
385 if (repo_get_oid(the_repository
, argv
[0], &object
))
386 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
387 note
= get_note(t
, &object
);
389 puts(oid_to_hex(note
));
392 retval
= error(_("no note found for object %s."),
393 oid_to_hex(&object
));
395 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
401 static int append_edit(int argc
, const char **argv
, const char *prefix
);
403 static int add(int argc
, const char **argv
, const char *prefix
)
405 int force
= 0, allow_empty
= 0;
406 const char *object_ref
;
407 struct notes_tree
*t
;
408 struct object_id object
, new_note
;
409 const struct object_id
*note
;
410 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
411 struct option options
[] = {
412 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
413 N_("note contents as a string"), PARSE_OPT_NONEG
,
415 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
416 N_("note contents in a file"), PARSE_OPT_NONEG
,
418 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
419 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
421 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
422 N_("reuse specified note object"), PARSE_OPT_NONEG
,
424 OPT_BOOL(0, "allow-empty", &allow_empty
,
425 N_("allow storing empty note")),
426 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
430 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
431 PARSE_OPT_KEEP_ARGV0
);
434 error(_("too many arguments"));
435 usage_with_options(git_notes_add_usage
, options
);
438 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
440 if (repo_get_oid(the_repository
, object_ref
, &object
))
441 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
443 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
444 note
= get_note(t
, &object
);
451 return error(_("Cannot add notes. "
452 "Found existing notes for object %s. "
453 "Use '-f' to overwrite existing notes"),
454 oid_to_hex(&object
));
457 * Redirect to "edit" subcommand.
459 * We only end up here if none of -m/-F/-c/-C or -f are
460 * given. The original args are therefore still in
464 return append_edit(argc
, argv
, prefix
);
466 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
467 oid_to_hex(&object
));
470 prepare_note_data(&object
, &d
, note
);
471 if (d
.buf
.len
|| allow_empty
) {
472 write_note_data(&d
, &new_note
);
473 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
474 BUG("combine_notes_overwrite failed");
475 commit_notes(the_repository
, t
,
476 "Notes added by 'git notes add'");
478 fprintf(stderr
, _("Removing note for object %s\n"),
479 oid_to_hex(&object
));
480 remove_note(t
, object
.hash
);
481 commit_notes(the_repository
, t
,
482 "Notes removed by 'git notes add'");
490 static int copy(int argc
, const char **argv
, const char *prefix
)
492 int retval
= 0, force
= 0, from_stdin
= 0;
493 const struct object_id
*from_note
, *note
;
494 const char *object_ref
;
495 struct object_id object
, from_obj
;
496 struct notes_tree
*t
;
497 const char *rewrite_cmd
= NULL
;
498 struct option options
[] = {
499 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
500 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
501 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
502 N_("load rewriting config for <command> (implies "
507 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
510 if (from_stdin
|| rewrite_cmd
) {
512 error(_("too many arguments"));
513 usage_with_options(git_notes_copy_usage
, options
);
515 return notes_copy_from_stdin(force
, rewrite_cmd
);
520 error(_("too few arguments"));
521 usage_with_options(git_notes_copy_usage
, options
);
524 error(_("too many arguments"));
525 usage_with_options(git_notes_copy_usage
, options
);
528 if (repo_get_oid(the_repository
, argv
[0], &from_obj
))
529 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
531 object_ref
= 1 < argc
? argv
[1] : "HEAD";
533 if (repo_get_oid(the_repository
, object_ref
, &object
))
534 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
536 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
537 note
= get_note(t
, &object
);
541 retval
= error(_("Cannot copy notes. Found existing "
542 "notes for object %s. Use '-f' to "
543 "overwrite existing notes"),
544 oid_to_hex(&object
));
547 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
548 oid_to_hex(&object
));
551 from_note
= get_note(t
, &from_obj
);
553 retval
= error(_("missing notes on source object %s. Cannot "
554 "copy."), oid_to_hex(&from_obj
));
558 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
559 BUG("combine_notes_overwrite failed");
560 commit_notes(the_repository
, t
,
561 "Notes added by 'git notes copy'");
567 static int append_edit(int argc
, const char **argv
, const char *prefix
)
570 const char *object_ref
;
571 struct notes_tree
*t
;
572 struct object_id object
, new_note
;
573 const struct object_id
*note
;
575 const char * const *usage
;
576 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
577 struct option options
[] = {
578 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
579 N_("note contents as a string"), PARSE_OPT_NONEG
,
581 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
582 N_("note contents in a file"), PARSE_OPT_NONEG
,
584 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
585 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
587 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
588 N_("reuse specified note object"), PARSE_OPT_NONEG
,
590 OPT_BOOL(0, "allow-empty", &allow_empty
,
591 N_("allow storing empty note")),
594 int edit
= !strcmp(argv
[0], "edit");
596 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
597 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
598 PARSE_OPT_KEEP_ARGV0
);
601 error(_("too many arguments"));
602 usage_with_options(usage
, options
);
606 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
607 "for the 'edit' subcommand.\n"
608 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
610 object_ref
= 1 < argc
? argv
[1] : "HEAD";
612 if (repo_get_oid(the_repository
, object_ref
, &object
))
613 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
615 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
616 note
= get_note(t
, &object
);
618 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
621 /* Append buf to previous note contents */
623 enum object_type type
;
624 char *prev_buf
= repo_read_object_file(the_repository
, note
,
627 strbuf_grow(&d
.buf
, size
+ 1);
628 if (d
.buf
.len
&& prev_buf
&& size
)
629 strbuf_insertstr(&d
.buf
, 0, "\n");
630 if (prev_buf
&& size
)
631 strbuf_insert(&d
.buf
, 0, prev_buf
, size
);
635 if (d
.buf
.len
|| allow_empty
) {
636 write_note_data(&d
, &new_note
);
637 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
638 BUG("combine_notes_overwrite failed");
639 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
641 fprintf(stderr
, _("Removing note for object %s\n"),
642 oid_to_hex(&object
));
643 remove_note(t
, object
.hash
);
644 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
646 commit_notes(the_repository
, t
, logmsg
);
654 static int show(int argc
, const char **argv
, const char *prefix
)
656 const char *object_ref
;
657 struct notes_tree
*t
;
658 struct object_id object
;
659 const struct object_id
*note
;
661 struct option options
[] = {
665 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
669 error(_("too many arguments"));
670 usage_with_options(git_notes_show_usage
, options
);
673 object_ref
= argc
? argv
[0] : "HEAD";
675 if (repo_get_oid(the_repository
, object_ref
, &object
))
676 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
678 t
= init_notes_check("show", 0);
679 note
= get_note(t
, &object
);
682 retval
= error(_("no note found for object %s."),
683 oid_to_hex(&object
));
685 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
686 retval
= execv_git_cmd(show_args
);
692 static int merge_abort(struct notes_merge_options
*o
)
697 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
698 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
701 if (delete_ref(NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
702 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
703 if (delete_ref(NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
704 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
705 if (notes_merge_abort(o
))
706 ret
+= error(_("failed to remove 'git notes merge' worktree"));
710 static int merge_commit(struct notes_merge_options
*o
)
712 struct strbuf msg
= STRBUF_INIT
;
713 struct object_id oid
, parent_oid
;
714 struct notes_tree
*t
;
715 struct commit
*partial
;
716 struct pretty_print_context pretty_ctx
;
717 void *local_ref_to_free
;
721 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
722 * and target notes ref from .git/NOTES_MERGE_REF.
725 if (repo_get_oid(the_repository
, "NOTES_MERGE_PARTIAL", &oid
))
726 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
727 else if (!(partial
= lookup_commit_reference(the_repository
, &oid
)))
728 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
729 else if (repo_parse_commit(the_repository
, partial
))
730 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
732 if (partial
->parents
)
733 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
738 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
740 o
->local_ref
= local_ref_to_free
=
741 resolve_refdup("NOTES_MERGE_REF", 0, &oid
, NULL
);
743 die(_("failed to resolve NOTES_MERGE_REF"));
745 if (notes_merge_commit(o
, t
, partial
, &oid
))
746 die(_("failed to finalize notes merge"));
748 /* Reuse existing commit message in reflog message */
749 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
750 repo_format_commit_message(the_repository
, partial
, "%s", &msg
,
753 strbuf_insertstr(&msg
, 0, "notes: ");
754 update_ref(msg
.buf
, o
->local_ref
, &oid
,
755 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
756 0, UPDATE_REFS_DIE_ON_ERR
);
759 strbuf_release(&msg
);
760 ret
= merge_abort(o
);
761 free(local_ref_to_free
);
765 static int git_config_get_notes_strategy(const char *key
,
766 enum notes_merge_strategy
*strategy
)
770 if (git_config_get_string(key
, &value
))
772 if (parse_notes_merge_strategy(value
, strategy
))
773 git_die_config(key
, _("unknown notes merge strategy %s"), value
);
779 static int merge(int argc
, const char **argv
, const char *prefix
)
781 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
782 struct object_id result_oid
;
783 struct notes_tree
*t
;
784 struct notes_merge_options o
;
785 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
786 int verbosity
= 0, result
;
787 const char *strategy
= NULL
;
788 struct option options
[] = {
789 OPT_GROUP(N_("General options")),
790 OPT__VERBOSITY(&verbosity
),
791 OPT_GROUP(N_("Merge options")),
792 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
793 N_("resolve notes conflicts using the given strategy "
794 "(manual/ours/theirs/union/cat_sort_uniq)")),
795 OPT_GROUP(N_("Committing unmerged notes")),
796 OPT_SET_INT_F(0, "commit", &do_commit
,
797 N_("finalize notes merge by committing unmerged notes"),
799 OPT_GROUP(N_("Aborting notes merge resolution")),
800 OPT_SET_INT_F(0, "abort", &do_abort
,
801 N_("abort notes merge"),
806 argc
= parse_options(argc
, argv
, prefix
, options
,
807 git_notes_merge_usage
, 0);
809 if (strategy
|| do_commit
+ do_abort
== 0)
811 if (do_merge
+ do_commit
+ do_abort
!= 1) {
812 error(_("cannot mix --commit, --abort or -s/--strategy"));
813 usage_with_options(git_notes_merge_usage
, options
);
816 if (do_merge
&& argc
!= 1) {
817 error(_("must specify a notes ref to merge"));
818 usage_with_options(git_notes_merge_usage
, options
);
819 } else if (!do_merge
&& argc
) {
820 error(_("too many arguments"));
821 usage_with_options(git_notes_merge_usage
, options
);
824 init_notes_merge_options(the_repository
, &o
);
825 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
828 return merge_abort(&o
);
830 return merge_commit(&o
);
832 o
.local_ref
= default_notes_ref();
833 strbuf_addstr(&remote_ref
, argv
[0]);
834 expand_loose_notes_ref(&remote_ref
);
835 o
.remote_ref
= remote_ref
.buf
;
837 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
840 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
841 error(_("unknown -s/--strategy: %s"), strategy
);
842 usage_with_options(git_notes_merge_usage
, options
);
845 struct strbuf merge_key
= STRBUF_INIT
;
846 const char *short_ref
= NULL
;
848 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
849 BUG("local ref %s is outside of refs/notes/",
852 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
854 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
855 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
857 strbuf_release(&merge_key
);
860 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
861 remote_ref
.buf
, default_notes_ref());
862 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
864 result
= notes_merge(&o
, t
, &result_oid
);
866 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
867 /* Update default notes ref with new commit */
868 update_ref(msg
.buf
, default_notes_ref(), &result_oid
, NULL
, 0,
869 UPDATE_REFS_DIE_ON_ERR
);
870 else { /* Merge has unresolved conflicts */
871 struct worktree
**worktrees
;
872 const struct worktree
*wt
;
873 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
874 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
875 0, UPDATE_REFS_DIE_ON_ERR
);
876 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
877 worktrees
= get_worktrees();
878 wt
= find_shared_symref(worktrees
, "NOTES_MERGE_REF",
879 default_notes_ref());
881 die(_("a notes merge into %s is already in-progress at %s"),
882 default_notes_ref(), wt
->path
);
883 free_worktrees(worktrees
);
884 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
885 die(_("failed to store link to current notes ref (%s)"),
886 default_notes_ref());
887 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
888 "and commit the result with 'git notes merge --commit', "
889 "or abort the merge with 'git notes merge --abort'.\n"),
890 git_path(NOTES_MERGE_WORKTREE
));
894 strbuf_release(&remote_ref
);
895 strbuf_release(&msg
);
896 return result
< 0; /* return non-zero on conflicts */
899 #define IGNORE_MISSING 1
901 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
904 struct object_id oid
;
905 if (repo_get_oid(the_repository
, name
, &oid
))
906 return error(_("Failed to resolve '%s' as a valid ref."), name
);
907 status
= remove_note(t
, oid
.hash
);
909 fprintf(stderr
, _("Object %s has no note\n"), name
);
911 fprintf(stderr
, _("Removing note for object %s\n"), name
);
912 return (flag
& IGNORE_MISSING
) ? 0 : status
;
915 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
919 struct option options
[] = {
920 OPT_BIT(0, "ignore-missing", &flag
,
921 N_("attempt to remove non-existent note is not an error"),
923 OPT_BOOL(0, "stdin", &from_stdin
,
924 N_("read object names from the standard input")),
927 struct notes_tree
*t
;
930 argc
= parse_options(argc
, argv
, prefix
, options
,
931 git_notes_remove_usage
, 0);
933 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
935 if (!argc
&& !from_stdin
) {
936 retval
= remove_one_note(t
, "HEAD", flag
);
939 retval
|= remove_one_note(t
, *argv
, flag
);
944 struct strbuf sb
= STRBUF_INIT
;
945 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
947 retval
|= remove_one_note(t
, sb
.buf
, flag
);
952 commit_notes(the_repository
, t
,
953 "Notes removed by 'git notes remove'");
958 static int prune(int argc
, const char **argv
, const char *prefix
)
960 struct notes_tree
*t
;
961 int show_only
= 0, verbose
= 0;
962 struct option options
[] = {
963 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
964 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
968 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
972 error(_("too many arguments"));
973 usage_with_options(git_notes_prune_usage
, options
);
976 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
978 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
979 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
981 commit_notes(the_repository
, t
,
982 "Notes removed by 'git notes prune'");
987 static int get_ref(int argc
, const char **argv
, const char *prefix
)
989 struct option options
[] = { OPT_END() };
990 argc
= parse_options(argc
, argv
, prefix
, options
,
991 git_notes_get_ref_usage
, 0);
994 error(_("too many arguments"));
995 usage_with_options(git_notes_get_ref_usage
, options
);
998 puts(default_notes_ref());
1002 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
1004 const char *override_notes_ref
= NULL
;
1005 parse_opt_subcommand_fn
*fn
= NULL
;
1006 struct option options
[] = {
1007 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
1008 N_("use notes from <notes-ref>")),
1009 OPT_SUBCOMMAND("list", &fn
, list
),
1010 OPT_SUBCOMMAND("add", &fn
, add
),
1011 OPT_SUBCOMMAND("copy", &fn
, copy
),
1012 OPT_SUBCOMMAND("append", &fn
, append_edit
),
1013 OPT_SUBCOMMAND("edit", &fn
, append_edit
),
1014 OPT_SUBCOMMAND("show", &fn
, show
),
1015 OPT_SUBCOMMAND("merge", &fn
, merge
),
1016 OPT_SUBCOMMAND("remove", &fn
, remove_cmd
),
1017 OPT_SUBCOMMAND("prune", &fn
, prune
),
1018 OPT_SUBCOMMAND("get-ref", &fn
, get_ref
),
1022 git_config(git_default_config
, NULL
);
1023 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1024 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1027 error(_("unknown subcommand: `%s'"), argv
[0]);
1028 usage_with_options(git_notes_usage
, options
);
1033 if (override_notes_ref
) {
1034 struct strbuf sb
= STRBUF_INIT
;
1035 strbuf_addstr(&sb
, override_notes_ref
);
1036 expand_notes_ref(&sb
);
1037 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1038 strbuf_release(&sb
);
1041 return !!fn(argc
, argv
, prefix
);