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.
14 #include "environment.h"
18 #include "object-name.h"
19 #include "object-store-ll.h"
21 #include "repository.h"
26 #include "run-command.h"
27 #include "parse-options.h"
28 #include "string-list.h"
29 #include "notes-merge.h"
30 #include "notes-utils.h"
32 #include "write-or-die.h"
34 static const char *separator
= "\n";
35 static const char * const git_notes_usage
[] = {
36 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
37 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
38 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
39 N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
40 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
41 N_("git notes [--ref <notes-ref>] show [<object>]"),
42 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
43 "git notes merge --commit [-v | -q]",
44 "git notes merge --abort [-v | -q]",
45 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
46 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
47 N_("git notes [--ref <notes-ref>] get-ref"),
51 static const char * const git_notes_list_usage
[] = {
52 N_("git notes [list [<object>]]"),
56 static const char * const git_notes_add_usage
[] = {
57 N_("git notes add [<options>] [<object>]"),
61 static const char * const git_notes_copy_usage
[] = {
62 N_("git notes copy [<options>] <from-object> <to-object>"),
63 N_("git notes copy --stdin [<from-object> <to-object>]..."),
67 static const char * const git_notes_append_usage
[] = {
68 N_("git notes append [<options>] [<object>]"),
72 static const char * const git_notes_edit_usage
[] = {
73 N_("git notes edit [<object>]"),
77 static const char * const git_notes_show_usage
[] = {
78 N_("git notes show [<object>]"),
82 static const char * const git_notes_merge_usage
[] = {
83 N_("git notes merge [<options>] <notes-ref>"),
84 N_("git notes merge --commit [<options>]"),
85 N_("git notes merge --abort [<options>]"),
89 static const char * const git_notes_remove_usage
[] = {
90 N_("git notes remove [<object>]"),
94 static const char * const git_notes_prune_usage
[] = {
95 N_("git notes prune [<options>]"),
99 static const char * const git_notes_get_ref_usage
[] = {
104 static const char note_template
[] =
105 N_("Write/edit the notes for the following object:");
107 enum notes_stripspace
{
114 enum notes_stripspace stripspace
;
124 struct note_msg
**messages
;
129 static void free_note_data(struct note_data
*d
)
132 unlink_or_warn(d
->edit_path
);
135 strbuf_release(&d
->buf
);
137 while (d
->msg_nr
--) {
138 strbuf_release(&d
->messages
[d
->msg_nr
]->buf
);
139 free(d
->messages
[d
->msg_nr
]);
144 static int list_each_note(const struct object_id
*object_oid
,
145 const struct object_id
*note_oid
,
146 char *note_path UNUSED
,
147 void *cb_data UNUSED
)
149 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
153 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
156 enum object_type type
;
157 char *buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
160 write_or_die(fd
, buf
, size
);
165 static void write_commented_object(int fd
, const struct object_id
*object
)
167 struct child_process show
= CHILD_PROCESS_INIT
;
168 struct strbuf buf
= STRBUF_INIT
;
169 struct strbuf cbuf
= STRBUF_INIT
;
171 /* Invoke "git show --stat --no-notes $object" */
172 strvec_pushl(&show
.args
, "show", "--stat", "--no-notes",
173 oid_to_hex(object
), NULL
);
178 if (start_command(&show
))
179 die(_("unable to start 'show' for object '%s'"),
182 if (strbuf_read(&buf
, show
.out
, 0) < 0)
183 die_errno(_("could not read 'show' output"));
184 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
, comment_line_char
);
185 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
187 strbuf_release(&cbuf
);
188 strbuf_release(&buf
);
190 if (finish_command(&show
))
191 die(_("failed to finish 'show' for object '%s'"),
195 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
196 const struct object_id
*old_note
)
198 if (d
->use_editor
|| !d
->given
) {
200 struct strbuf buf
= STRBUF_INIT
;
202 /* write the template message before editing: */
203 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
204 fd
= xopen(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
207 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
209 copy_obj_to_fd(fd
, old_note
);
211 strbuf_addch(&buf
, '\n');
212 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"), comment_line_char
);
213 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)),
215 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"), comment_line_char
);
216 write_or_die(fd
, buf
.buf
, buf
.len
);
218 write_commented_object(fd
, object
);
221 strbuf_release(&buf
);
222 strbuf_reset(&d
->buf
);
224 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
225 die(_("please supply the note contents using either -m or -F option"));
228 strbuf_stripspace(&d
->buf
, comment_line_char
);
232 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
234 if (write_object_file(d
->buf
.buf
, d
->buf
.len
, OBJ_BLOB
, oid
)) {
235 int status
= die_message(_("unable to write note object"));
238 die_message(_("the note contents have been left in %s"),
244 static void append_separator(struct strbuf
*message
)
250 else if ((sep_len
= strlen(separator
)) && separator
[sep_len
- 1] == '\n')
251 strbuf_addstr(message
, separator
);
253 strbuf_addf(message
, "%s%s", separator
, "\n");
256 static void concat_messages(struct note_data
*d
)
258 struct strbuf msg
= STRBUF_INIT
;
261 for (i
= 0; i
< d
->msg_nr
; i
++) {
263 append_separator(&d
->buf
);
264 strbuf_add(&msg
, d
->messages
[i
]->buf
.buf
, d
->messages
[i
]->buf
.len
);
265 strbuf_addbuf(&d
->buf
, &msg
);
266 if ((d
->stripspace
== UNSPECIFIED
&&
267 d
->messages
[i
]->stripspace
== STRIPSPACE
) ||
268 d
->stripspace
== STRIPSPACE
)
269 strbuf_stripspace(&d
->buf
, 0);
272 strbuf_release(&msg
);
275 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
277 struct note_data
*d
= opt
->value
;
278 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
280 BUG_ON_OPT_NEG(unset
);
282 strbuf_init(&msg
->buf
, strlen(arg
));
283 strbuf_addstr(&msg
->buf
, arg
);
284 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
285 d
->messages
[d
->msg_nr
- 1] = msg
;
286 msg
->stripspace
= STRIPSPACE
;
290 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
292 struct note_data
*d
= opt
->value
;
293 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
295 BUG_ON_OPT_NEG(unset
);
297 strbuf_init(&msg
->buf
, 0);
298 if (!strcmp(arg
, "-")) {
299 if (strbuf_read(&msg
->buf
, 0, 1024) < 0)
300 die_errno(_("cannot read '%s'"), arg
);
301 } else if (strbuf_read_file(&msg
->buf
, arg
, 1024) < 0)
302 die_errno(_("could not open or read '%s'"), arg
);
304 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
305 d
->messages
[d
->msg_nr
- 1] = msg
;
306 msg
->stripspace
= STRIPSPACE
;
310 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
312 struct note_data
*d
= opt
->value
;
313 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
315 struct object_id object
;
316 enum object_type type
;
319 BUG_ON_OPT_NEG(unset
);
321 strbuf_init(&msg
->buf
, 0);
322 if (repo_get_oid(the_repository
, arg
, &object
))
323 die(_("failed to resolve '%s' as a valid ref."), arg
);
324 if (!(value
= repo_read_object_file(the_repository
, &object
, &type
, &len
)))
325 die(_("failed to read object '%s'."), arg
);
326 if (type
!= OBJ_BLOB
) {
327 strbuf_release(&msg
->buf
);
330 die(_("cannot read note data from non-blob object '%s'."), arg
);
333 strbuf_add(&msg
->buf
, value
, len
);
337 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
338 d
->messages
[d
->msg_nr
- 1] = msg
;
339 msg
->stripspace
= NO_STRIPSPACE
;
343 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
345 struct note_data
*d
= opt
->value
;
346 BUG_ON_OPT_NEG(unset
);
348 return parse_reuse_arg(opt
, arg
, unset
);
351 static int parse_separator_arg(const struct option
*opt
, const char *arg
,
355 *(const char **)opt
->value
= NULL
;
357 *(const char **)opt
->value
= arg
? arg
: "\n";
361 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
363 struct strbuf buf
= STRBUF_INIT
;
364 struct notes_rewrite_cfg
*c
= NULL
;
365 struct notes_tree
*t
= NULL
;
367 const char *msg
= "Notes added by 'git notes copy'";
370 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
374 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
375 t
= &default_notes_tree
;
378 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
379 struct object_id from_obj
, to_obj
;
380 struct strbuf
**split
;
383 split
= strbuf_split(&buf
, ' ');
384 if (!split
[0] || !split
[1])
385 die(_("malformed input line: '%s'."), buf
.buf
);
386 strbuf_rtrim(split
[0]);
387 strbuf_rtrim(split
[1]);
388 if (repo_get_oid(the_repository
, split
[0]->buf
, &from_obj
))
389 die(_("failed to resolve '%s' as a valid ref."), split
[0]->buf
);
390 if (repo_get_oid(the_repository
, split
[1]->buf
, &to_obj
))
391 die(_("failed to resolve '%s' as a valid ref."), split
[1]->buf
);
394 err
= copy_note_for_rewrite(c
, &from_obj
, &to_obj
);
396 err
= copy_note(t
, &from_obj
, &to_obj
, force
,
397 combine_notes_overwrite
);
400 error(_("failed to copy notes from '%s' to '%s'"),
401 split
[0]->buf
, split
[1]->buf
);
405 strbuf_list_free(split
);
409 commit_notes(the_repository
, t
, msg
);
412 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
414 strbuf_release(&buf
);
418 static struct notes_tree
*init_notes_check(const char *subcommand
,
421 struct notes_tree
*t
;
423 init_notes(NULL
, NULL
, NULL
, flags
);
424 t
= &default_notes_tree
;
426 ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->update_ref
: t
->ref
;
427 if (!starts_with(ref
, "refs/notes/"))
429 * TRANSLATORS: the first %s will be replaced by a git
430 * notes command: 'add', 'merge', 'remove', etc.
432 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
437 static int list(int argc
, const char **argv
, const char *prefix
)
439 struct notes_tree
*t
;
440 struct object_id object
;
441 const struct object_id
*note
;
443 struct option options
[] = {
448 argc
= parse_options(argc
, argv
, prefix
, options
,
449 git_notes_list_usage
, 0);
452 error(_("too many arguments"));
453 usage_with_options(git_notes_list_usage
, options
);
456 t
= init_notes_check("list", 0);
458 if (repo_get_oid(the_repository
, argv
[0], &object
))
459 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
460 note
= get_note(t
, &object
);
462 puts(oid_to_hex(note
));
465 retval
= error(_("no note found for object %s."),
466 oid_to_hex(&object
));
468 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
474 static int append_edit(int argc
, const char **argv
, const char *prefix
);
476 static int add(int argc
, const char **argv
, const char *prefix
)
478 int force
= 0, allow_empty
= 0;
479 const char *object_ref
;
480 struct notes_tree
*t
;
481 struct object_id object
, new_note
;
482 const struct object_id
*note
;
483 struct note_data d
= { .buf
= STRBUF_INIT
, .stripspace
= UNSPECIFIED
};
485 struct option options
[] = {
486 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
487 N_("note contents as a string"), PARSE_OPT_NONEG
,
489 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
490 N_("note contents in a file"), PARSE_OPT_NONEG
,
492 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
493 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
495 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
496 N_("reuse specified note object"), PARSE_OPT_NONEG
,
498 OPT_BOOL(0, "allow-empty", &allow_empty
,
499 N_("allow storing empty note")),
500 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
501 OPT_CALLBACK_F(0, "separator", &separator
,
502 N_("<paragraph-break>"),
503 N_("insert <paragraph-break> between paragraphs"),
504 PARSE_OPT_OPTARG
, parse_separator_arg
),
505 OPT_BOOL(0, "stripspace", &d
.stripspace
,
506 N_("remove unnecessary whitespace")),
510 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
511 PARSE_OPT_KEEP_ARGV0
);
514 error(_("too many arguments"));
515 usage_with_options(git_notes_add_usage
, options
);
520 d
.given
= !!d
.buf
.len
;
522 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
524 if (repo_get_oid(the_repository
, object_ref
, &object
))
525 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
527 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
528 note
= get_note(t
, &object
);
535 return error(_("Cannot add notes. "
536 "Found existing notes for object %s. "
537 "Use '-f' to overwrite existing notes"),
538 oid_to_hex(&object
));
541 * Redirect to "edit" subcommand.
543 * We only end up here if none of -m/-F/-c/-C or -f are
544 * given. The original args are therefore still in
548 return append_edit(argc
, argv
, prefix
);
550 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
551 oid_to_hex(&object
));
554 prepare_note_data(&object
, &d
, note
);
555 if (d
.buf
.len
|| allow_empty
) {
556 write_note_data(&d
, &new_note
);
557 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
558 BUG("combine_notes_overwrite failed");
559 commit_notes(the_repository
, t
,
560 "Notes added by 'git notes add'");
562 fprintf(stderr
, _("Removing note for object %s\n"),
563 oid_to_hex(&object
));
564 remove_note(t
, object
.hash
);
565 commit_notes(the_repository
, t
,
566 "Notes removed by 'git notes add'");
574 static int copy(int argc
, const char **argv
, const char *prefix
)
576 int retval
= 0, force
= 0, from_stdin
= 0;
577 const struct object_id
*from_note
, *note
;
578 const char *object_ref
;
579 struct object_id object
, from_obj
;
580 struct notes_tree
*t
;
581 const char *rewrite_cmd
= NULL
;
582 struct option options
[] = {
583 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
584 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
585 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
586 N_("load rewriting config for <command> (implies "
591 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
594 if (from_stdin
|| rewrite_cmd
) {
596 error(_("too many arguments"));
597 usage_with_options(git_notes_copy_usage
, options
);
599 return notes_copy_from_stdin(force
, rewrite_cmd
);
604 error(_("too few arguments"));
605 usage_with_options(git_notes_copy_usage
, options
);
608 error(_("too many arguments"));
609 usage_with_options(git_notes_copy_usage
, options
);
612 if (repo_get_oid(the_repository
, argv
[0], &from_obj
))
613 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
615 object_ref
= 1 < argc
? argv
[1] : "HEAD";
617 if (repo_get_oid(the_repository
, object_ref
, &object
))
618 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
620 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
621 note
= get_note(t
, &object
);
625 retval
= error(_("Cannot copy notes. Found existing "
626 "notes for object %s. Use '-f' to "
627 "overwrite existing notes"),
628 oid_to_hex(&object
));
631 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
632 oid_to_hex(&object
));
635 from_note
= get_note(t
, &from_obj
);
637 retval
= error(_("missing notes on source object %s. Cannot "
638 "copy."), oid_to_hex(&from_obj
));
642 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
643 BUG("combine_notes_overwrite failed");
644 commit_notes(the_repository
, t
,
645 "Notes added by 'git notes copy'");
651 static int append_edit(int argc
, const char **argv
, const char *prefix
)
654 const char *object_ref
;
655 struct notes_tree
*t
;
656 struct object_id object
, new_note
;
657 const struct object_id
*note
;
659 const char * const *usage
;
660 struct note_data d
= { .buf
= STRBUF_INIT
, .stripspace
= UNSPECIFIED
};
661 struct option options
[] = {
662 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
663 N_("note contents as a string"), PARSE_OPT_NONEG
,
665 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
666 N_("note contents in a file"), PARSE_OPT_NONEG
,
668 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
669 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
671 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
672 N_("reuse specified note object"), PARSE_OPT_NONEG
,
674 OPT_BOOL(0, "allow-empty", &allow_empty
,
675 N_("allow storing empty note")),
676 OPT_CALLBACK_F(0, "separator", &separator
,
677 N_("<paragraph-break>"),
678 N_("insert <paragraph-break> between paragraphs"),
679 PARSE_OPT_OPTARG
, parse_separator_arg
),
680 OPT_BOOL(0, "stripspace", &d
.stripspace
,
681 N_("remove unnecessary whitespace")),
684 int edit
= !strcmp(argv
[0], "edit");
686 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
687 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
688 PARSE_OPT_KEEP_ARGV0
);
691 error(_("too many arguments"));
692 usage_with_options(usage
, options
);
697 d
.given
= !!d
.buf
.len
;
700 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
701 "for the 'edit' subcommand.\n"
702 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
704 object_ref
= 1 < argc
? argv
[1] : "HEAD";
706 if (repo_get_oid(the_repository
, object_ref
, &object
))
707 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
709 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
710 note
= get_note(t
, &object
);
712 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
715 /* Append buf to previous note contents */
717 enum object_type type
;
718 struct strbuf buf
= STRBUF_INIT
;
719 char *prev_buf
= repo_read_object_file(the_repository
, note
, &type
, &size
);
721 if (prev_buf
&& size
)
722 strbuf_add(&buf
, prev_buf
, size
);
723 if (d
.buf
.len
&& prev_buf
&& size
)
724 append_separator(&buf
);
725 strbuf_insert(&d
.buf
, 0, buf
.buf
, buf
.len
);
728 strbuf_release(&buf
);
731 if (d
.buf
.len
|| allow_empty
) {
732 write_note_data(&d
, &new_note
);
733 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
734 BUG("combine_notes_overwrite failed");
735 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
737 fprintf(stderr
, _("Removing note for object %s\n"),
738 oid_to_hex(&object
));
739 remove_note(t
, object
.hash
);
740 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
742 commit_notes(the_repository
, t
, logmsg
);
750 static int show(int argc
, const char **argv
, const char *prefix
)
752 const char *object_ref
;
753 struct notes_tree
*t
;
754 struct object_id object
;
755 const struct object_id
*note
;
757 struct option options
[] = {
761 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
765 error(_("too many arguments"));
766 usage_with_options(git_notes_show_usage
, options
);
769 object_ref
= argc
? argv
[0] : "HEAD";
771 if (repo_get_oid(the_repository
, object_ref
, &object
))
772 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
774 t
= init_notes_check("show", 0);
775 note
= get_note(t
, &object
);
778 retval
= error(_("no note found for object %s."),
779 oid_to_hex(&object
));
781 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
782 retval
= execv_git_cmd(show_args
);
788 static int merge_abort(struct notes_merge_options
*o
)
793 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
794 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
797 if (delete_ref(NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
798 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
799 if (delete_ref(NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
800 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
801 if (notes_merge_abort(o
))
802 ret
+= error(_("failed to remove 'git notes merge' worktree"));
806 static int merge_commit(struct notes_merge_options
*o
)
808 struct strbuf msg
= STRBUF_INIT
;
809 struct object_id oid
, parent_oid
;
810 struct notes_tree
*t
;
811 struct commit
*partial
;
812 struct pretty_print_context pretty_ctx
;
813 void *local_ref_to_free
;
817 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
818 * and target notes ref from .git/NOTES_MERGE_REF.
821 if (repo_get_oid(the_repository
, "NOTES_MERGE_PARTIAL", &oid
))
822 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
823 else if (!(partial
= lookup_commit_reference(the_repository
, &oid
)))
824 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
825 else if (repo_parse_commit(the_repository
, partial
))
826 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
828 if (partial
->parents
)
829 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
834 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
836 o
->local_ref
= local_ref_to_free
=
837 resolve_refdup("NOTES_MERGE_REF", 0, &oid
, NULL
);
839 die(_("failed to resolve NOTES_MERGE_REF"));
841 if (notes_merge_commit(o
, t
, partial
, &oid
))
842 die(_("failed to finalize notes merge"));
844 /* Reuse existing commit message in reflog message */
845 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
846 repo_format_commit_message(the_repository
, partial
, "%s", &msg
,
849 strbuf_insertstr(&msg
, 0, "notes: ");
850 update_ref(msg
.buf
, o
->local_ref
, &oid
,
851 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
852 0, UPDATE_REFS_DIE_ON_ERR
);
855 strbuf_release(&msg
);
856 ret
= merge_abort(o
);
857 free(local_ref_to_free
);
861 static int git_config_get_notes_strategy(const char *key
,
862 enum notes_merge_strategy
*strategy
)
866 if (git_config_get_string(key
, &value
))
868 if (parse_notes_merge_strategy(value
, strategy
))
869 git_die_config(key
, _("unknown notes merge strategy %s"), value
);
875 static int merge(int argc
, const char **argv
, const char *prefix
)
877 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
878 struct object_id result_oid
;
879 struct notes_tree
*t
;
880 struct notes_merge_options o
;
881 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
882 int verbosity
= 0, result
;
883 const char *strategy
= NULL
;
884 struct option options
[] = {
885 OPT_GROUP(N_("General options")),
886 OPT__VERBOSITY(&verbosity
),
887 OPT_GROUP(N_("Merge options")),
888 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
889 N_("resolve notes conflicts using the given strategy "
890 "(manual/ours/theirs/union/cat_sort_uniq)")),
891 OPT_GROUP(N_("Committing unmerged notes")),
892 OPT_SET_INT_F(0, "commit", &do_commit
,
893 N_("finalize notes merge by committing unmerged notes"),
895 OPT_GROUP(N_("Aborting notes merge resolution")),
896 OPT_SET_INT_F(0, "abort", &do_abort
,
897 N_("abort notes merge"),
902 argc
= parse_options(argc
, argv
, prefix
, options
,
903 git_notes_merge_usage
, 0);
905 if (strategy
|| do_commit
+ do_abort
== 0)
907 if (do_merge
+ do_commit
+ do_abort
!= 1) {
908 error(_("cannot mix --commit, --abort or -s/--strategy"));
909 usage_with_options(git_notes_merge_usage
, options
);
912 if (do_merge
&& argc
!= 1) {
913 error(_("must specify a notes ref to merge"));
914 usage_with_options(git_notes_merge_usage
, options
);
915 } else if (!do_merge
&& argc
) {
916 error(_("too many arguments"));
917 usage_with_options(git_notes_merge_usage
, options
);
920 init_notes_merge_options(the_repository
, &o
);
921 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
924 return merge_abort(&o
);
926 return merge_commit(&o
);
928 o
.local_ref
= default_notes_ref();
929 strbuf_addstr(&remote_ref
, argv
[0]);
930 expand_loose_notes_ref(&remote_ref
);
931 o
.remote_ref
= remote_ref
.buf
;
933 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
936 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
937 error(_("unknown -s/--strategy: %s"), strategy
);
938 usage_with_options(git_notes_merge_usage
, options
);
941 struct strbuf merge_key
= STRBUF_INIT
;
942 const char *short_ref
= NULL
;
944 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
945 BUG("local ref %s is outside of refs/notes/",
948 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
950 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
951 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
953 strbuf_release(&merge_key
);
956 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
957 remote_ref
.buf
, default_notes_ref());
958 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
960 result
= notes_merge(&o
, t
, &result_oid
);
962 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
963 /* Update default notes ref with new commit */
964 update_ref(msg
.buf
, default_notes_ref(), &result_oid
, NULL
, 0,
965 UPDATE_REFS_DIE_ON_ERR
);
966 else { /* Merge has unresolved conflicts */
967 struct worktree
**worktrees
;
968 const struct worktree
*wt
;
969 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
970 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
971 0, UPDATE_REFS_DIE_ON_ERR
);
972 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
973 worktrees
= get_worktrees();
974 wt
= find_shared_symref(worktrees
, "NOTES_MERGE_REF",
975 default_notes_ref());
977 die(_("a notes merge into %s is already in-progress at %s"),
978 default_notes_ref(), wt
->path
);
979 free_worktrees(worktrees
);
980 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
981 die(_("failed to store link to current notes ref (%s)"),
982 default_notes_ref());
983 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
984 "and commit the result with 'git notes merge --commit', "
985 "or abort the merge with 'git notes merge --abort'.\n"),
986 git_path(NOTES_MERGE_WORKTREE
));
990 strbuf_release(&remote_ref
);
991 strbuf_release(&msg
);
992 return result
< 0; /* return non-zero on conflicts */
995 #define IGNORE_MISSING 1
997 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
1000 struct object_id oid
;
1001 if (repo_get_oid(the_repository
, name
, &oid
))
1002 return error(_("Failed to resolve '%s' as a valid ref."), name
);
1003 status
= remove_note(t
, oid
.hash
);
1005 fprintf(stderr
, _("Object %s has no note\n"), name
);
1007 fprintf(stderr
, _("Removing note for object %s\n"), name
);
1008 return (flag
& IGNORE_MISSING
) ? 0 : status
;
1011 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
1015 struct option options
[] = {
1016 OPT_BIT(0, "ignore-missing", &flag
,
1017 N_("attempt to remove non-existent note is not an error"),
1019 OPT_BOOL(0, "stdin", &from_stdin
,
1020 N_("read object names from the standard input")),
1023 struct notes_tree
*t
;
1026 argc
= parse_options(argc
, argv
, prefix
, options
,
1027 git_notes_remove_usage
, 0);
1029 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
1031 if (!argc
&& !from_stdin
) {
1032 retval
= remove_one_note(t
, "HEAD", flag
);
1035 retval
|= remove_one_note(t
, *argv
, flag
);
1040 struct strbuf sb
= STRBUF_INIT
;
1041 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1043 retval
|= remove_one_note(t
, sb
.buf
, flag
);
1045 strbuf_release(&sb
);
1048 commit_notes(the_repository
, t
,
1049 "Notes removed by 'git notes remove'");
1054 static int prune(int argc
, const char **argv
, const char *prefix
)
1056 struct notes_tree
*t
;
1057 int show_only
= 0, verbose
= 0;
1058 struct option options
[] = {
1059 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
1060 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
1064 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
1068 error(_("too many arguments"));
1069 usage_with_options(git_notes_prune_usage
, options
);
1072 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
1074 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
1075 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
1077 commit_notes(the_repository
, t
,
1078 "Notes removed by 'git notes prune'");
1083 static int get_ref(int argc
, const char **argv
, const char *prefix
)
1085 struct option options
[] = { OPT_END() };
1086 argc
= parse_options(argc
, argv
, prefix
, options
,
1087 git_notes_get_ref_usage
, 0);
1090 error(_("too many arguments"));
1091 usage_with_options(git_notes_get_ref_usage
, options
);
1094 puts(default_notes_ref());
1098 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
1100 const char *override_notes_ref
= NULL
;
1101 parse_opt_subcommand_fn
*fn
= NULL
;
1102 struct option options
[] = {
1103 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
1104 N_("use notes from <notes-ref>")),
1105 OPT_SUBCOMMAND("list", &fn
, list
),
1106 OPT_SUBCOMMAND("add", &fn
, add
),
1107 OPT_SUBCOMMAND("copy", &fn
, copy
),
1108 OPT_SUBCOMMAND("append", &fn
, append_edit
),
1109 OPT_SUBCOMMAND("edit", &fn
, append_edit
),
1110 OPT_SUBCOMMAND("show", &fn
, show
),
1111 OPT_SUBCOMMAND("merge", &fn
, merge
),
1112 OPT_SUBCOMMAND("remove", &fn
, remove_cmd
),
1113 OPT_SUBCOMMAND("prune", &fn
, prune
),
1114 OPT_SUBCOMMAND("get-ref", &fn
, get_ref
),
1118 git_config(git_default_config
, NULL
);
1119 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1120 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1123 error(_("unknown subcommand: `%s'"), argv
[0]);
1124 usage_with_options(git_notes_usage
, options
);
1129 if (override_notes_ref
) {
1130 struct strbuf sb
= STRBUF_INIT
;
1131 strbuf_addstr(&sb
, override_notes_ref
);
1132 expand_notes_ref(&sb
);
1133 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1134 strbuf_release(&sb
);
1137 return !!fn(argc
, argv
, prefix
);