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.
9 #define USE_THE_REPOSITORY_VARIABLE
13 #include "environment.h"
17 #include "object-file.h"
18 #include "object-name.h"
25 #include "run-command.h"
26 #include "parse-options.h"
27 #include "string-list.h"
28 #include "notes-merge.h"
29 #include "notes-utils.h"
31 #include "write-or-die.h"
33 static const char *separator
= "\n";
34 static const char * const git_notes_usage
[] = {
35 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
36 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>] [-e]"),
37 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
38 N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"),
39 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
40 N_("git notes [--ref <notes-ref>] show [<object>]"),
41 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
42 "git notes merge --commit [-v | -q]",
43 "git notes merge --abort [-v | -q]",
44 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
45 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
46 N_("git notes [--ref <notes-ref>] get-ref"),
50 static const char * const git_notes_list_usage
[] = {
51 N_("git notes [list [<object>]]"),
55 static const char * const git_notes_add_usage
[] = {
56 N_("git notes add [<options>] [<object>]"),
60 static const char * const git_notes_copy_usage
[] = {
61 N_("git notes copy [<options>] <from-object> <to-object>"),
62 N_("git notes copy --stdin [<from-object> <to-object>]..."),
66 static const char * const git_notes_append_usage
[] = {
67 N_("git notes append [<options>] [<object>]"),
71 static const char * const git_notes_edit_usage
[] = {
72 N_("git notes edit [<object>]"),
76 static const char * const git_notes_show_usage
[] = {
77 N_("git notes show [<object>]"),
81 static const char * const git_notes_merge_usage
[] = {
82 N_("git notes merge [<options>] <notes-ref>"),
83 N_("git notes merge --commit [<options>]"),
84 N_("git notes merge --abort [<options>]"),
88 static const char * const git_notes_remove_usage
[] = {
89 N_("git notes remove [<object>]"),
93 static const char * const git_notes_prune_usage
[] = {
94 N_("git notes prune [<options>]"),
98 static const char * const git_notes_get_ref_usage
[] = {
103 static const char note_template
[] =
104 N_("Write/edit the notes for the following object:");
106 enum notes_stripspace
{
113 enum notes_stripspace stripspace
;
122 struct note_msg
**messages
;
127 static void free_note_data(struct note_data
*d
)
130 unlink_or_warn(d
->edit_path
);
133 strbuf_release(&d
->buf
);
135 while (d
->msg_nr
--) {
136 strbuf_release(&d
->messages
[d
->msg_nr
]->buf
);
137 free(d
->messages
[d
->msg_nr
]);
142 static int list_each_note(const struct object_id
*object_oid
,
143 const struct object_id
*note_oid
,
144 char *note_path UNUSED
,
145 void *cb_data UNUSED
)
147 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
151 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
154 enum object_type type
;
155 char *buf
= odb_read_object(the_repository
->objects
, oid
, &type
, &size
);
158 write_or_die(fd
, buf
, size
);
163 static void write_commented_object(int fd
, const struct object_id
*object
)
165 struct child_process show
= CHILD_PROCESS_INIT
;
166 struct strbuf buf
= STRBUF_INIT
;
167 struct strbuf cbuf
= STRBUF_INIT
;
169 /* Invoke "git show --stat --no-notes $object" */
170 strvec_pushl(&show
.args
, "show", "--stat", "--no-notes",
171 oid_to_hex(object
), NULL
);
176 if (start_command(&show
))
177 die(_("unable to start 'show' for object '%s'"),
180 if (strbuf_read(&buf
, show
.out
, 0) < 0)
181 die_errno(_("could not read 'show' output"));
182 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
, comment_line_str
);
183 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
185 strbuf_release(&cbuf
);
186 strbuf_release(&buf
);
188 if (finish_command(&show
))
189 die(_("failed to finish 'show' for object '%s'"),
193 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
194 const struct object_id
*old_note
)
196 if (d
->use_editor
|| !d
->msg_nr
) {
198 struct strbuf buf
= STRBUF_INIT
;
200 /* write the template message before editing: */
201 d
->edit_path
= repo_git_path(the_repository
, "NOTES_EDITMSG");
202 fd
= xopen(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
205 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
207 copy_obj_to_fd(fd
, old_note
);
209 strbuf_addch(&buf
, '\n');
210 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"), comment_line_str
);
211 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)),
213 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"), comment_line_str
);
214 write_or_die(fd
, buf
.buf
, buf
.len
);
216 write_commented_object(fd
, object
);
219 strbuf_release(&buf
);
220 strbuf_reset(&d
->buf
);
222 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
223 die(_("please supply the note contents using either -m or -F option"));
226 strbuf_stripspace(&d
->buf
, comment_line_str
);
230 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
232 if (odb_write_object(the_repository
->objects
, d
->buf
.buf
,
233 d
->buf
.len
, OBJ_BLOB
, oid
)) {
234 int status
= die_message(_("unable to write note object"));
237 die_message(_("the note contents have been left in %s"),
243 static void append_separator(struct strbuf
*message
)
249 else if ((sep_len
= strlen(separator
)) && separator
[sep_len
- 1] == '\n')
250 strbuf_addstr(message
, separator
);
252 strbuf_addf(message
, "%s%s", separator
, "\n");
255 static void concat_messages(struct note_data
*d
)
257 struct strbuf msg
= STRBUF_INIT
;
260 for (i
= 0; i
< d
->msg_nr
; i
++) {
262 append_separator(&d
->buf
);
263 strbuf_add(&msg
, d
->messages
[i
]->buf
.buf
, d
->messages
[i
]->buf
.len
);
264 strbuf_addbuf(&d
->buf
, &msg
);
265 if ((d
->stripspace
== UNSPECIFIED
&&
266 d
->messages
[i
]->stripspace
== STRIPSPACE
) ||
267 d
->stripspace
== STRIPSPACE
)
268 strbuf_stripspace(&d
->buf
, NULL
);
271 strbuf_release(&msg
);
274 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
276 struct note_data
*d
= opt
->value
;
277 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
279 BUG_ON_OPT_NEG(unset
);
281 strbuf_init(&msg
->buf
, strlen(arg
));
282 strbuf_addstr(&msg
->buf
, arg
);
283 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
284 d
->messages
[d
->msg_nr
- 1] = msg
;
285 msg
->stripspace
= STRIPSPACE
;
289 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
291 struct note_data
*d
= opt
->value
;
292 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
294 BUG_ON_OPT_NEG(unset
);
296 strbuf_init(&msg
->buf
, 0);
297 if (!strcmp(arg
, "-")) {
298 if (strbuf_read(&msg
->buf
, 0, 1024) < 0)
299 die_errno(_("cannot read '%s'"), arg
);
300 } else if (strbuf_read_file(&msg
->buf
, arg
, 1024) < 0)
301 die_errno(_("could not open or read '%s'"), arg
);
303 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
304 d
->messages
[d
->msg_nr
- 1] = msg
;
305 msg
->stripspace
= STRIPSPACE
;
309 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
311 struct note_data
*d
= opt
->value
;
312 struct note_msg
*msg
= xmalloc(sizeof(*msg
));
314 struct object_id object
;
315 enum object_type type
;
318 BUG_ON_OPT_NEG(unset
);
320 strbuf_init(&msg
->buf
, 0);
321 if (repo_get_oid(the_repository
, arg
, &object
))
322 die(_("failed to resolve '%s' as a valid ref."), arg
);
323 if (!(value
= odb_read_object(the_repository
->objects
, &object
, &type
, &len
)))
324 die(_("failed to read object '%s'."), arg
);
325 if (type
!= OBJ_BLOB
) {
326 strbuf_release(&msg
->buf
);
329 die(_("cannot read note data from non-blob object '%s'."), arg
);
332 strbuf_add(&msg
->buf
, value
, len
);
336 ALLOC_GROW_BY(d
->messages
, d
->msg_nr
, 1, d
->msg_alloc
);
337 d
->messages
[d
->msg_nr
- 1] = msg
;
338 msg
->stripspace
= NO_STRIPSPACE
;
342 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
344 struct note_data
*d
= opt
->value
;
345 BUG_ON_OPT_NEG(unset
);
347 return parse_reuse_arg(opt
, arg
, unset
);
350 static int parse_separator_arg(const struct option
*opt
, const char *arg
,
354 *(const char **)opt
->value
= NULL
;
356 *(const char **)opt
->value
= arg
? arg
: "\n";
360 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
362 struct strbuf buf
= STRBUF_INIT
;
363 struct notes_rewrite_cfg
*c
= NULL
;
364 struct notes_tree
*t
= NULL
;
366 const char *msg
= "Notes added by 'git notes copy'";
369 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
373 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
374 t
= &default_notes_tree
;
377 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
378 struct object_id from_obj
, to_obj
;
379 struct string_list split
= STRING_LIST_INIT_NODUP
;
382 string_list_split_in_place_f(&split
, buf
.buf
, " ", -1,
383 STRING_LIST_SPLIT_TRIM
);
385 die(_("malformed input line: '%s'."), buf
.buf
);
386 if (repo_get_oid(the_repository
, split
.items
[0].string
, &from_obj
))
387 die(_("failed to resolve '%s' as a valid ref."),
388 split
.items
[0].string
);
389 if (repo_get_oid(the_repository
, split
.items
[1].string
, &to_obj
))
390 die(_("failed to resolve '%s' as a valid ref."),
391 split
.items
[1].string
);
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
.items
[0].string
, split
.items
[1].string
);
405 string_list_clear(&split
, 0);
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
,
438 struct repository
*repo UNUSED
)
440 struct notes_tree
*t
;
441 struct object_id object
;
442 const struct object_id
*note
;
444 struct option options
[] = {
449 argc
= parse_options(argc
, argv
, prefix
, options
,
450 git_notes_list_usage
, 0);
453 error(_("too many arguments"));
454 usage_with_options(git_notes_list_usage
, options
);
457 t
= init_notes_check("list", 0);
459 if (repo_get_oid(the_repository
, argv
[0], &object
))
460 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
461 note
= get_note(t
, &object
);
463 puts(oid_to_hex(note
));
466 retval
= error(_("no note found for object %s."),
467 oid_to_hex(&object
));
469 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
475 static int append_edit(int argc
, const char **argv
, const char *prefix
,
476 struct repository
*repo UNUSED
);
478 static int add(int argc
, const char **argv
, const char *prefix
,
479 struct repository
*repo
)
481 int force
= 0, allow_empty
= 0;
482 const char *object_ref
;
483 struct notes_tree
*t
;
484 struct object_id object
, new_note
;
485 const struct object_id
*note
;
486 struct note_data d
= { .buf
= STRBUF_INIT
, .stripspace
= UNSPECIFIED
};
488 struct option options
[] = {
489 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
490 N_("note contents as a string"), PARSE_OPT_NONEG
,
492 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
493 N_("note contents in a file"), PARSE_OPT_NONEG
,
495 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
496 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
498 OPT_BOOL('e', "edit", &d
.use_editor
,
499 N_("edit note message in editor")),
500 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
501 N_("reuse specified note object"), PARSE_OPT_NONEG
,
503 OPT_BOOL(0, "allow-empty", &allow_empty
,
504 N_("allow storing empty note")),
505 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
506 OPT_CALLBACK_F(0, "separator", &separator
,
507 N_("<paragraph-break>"),
508 N_("insert <paragraph-break> between paragraphs"),
509 PARSE_OPT_OPTARG
, parse_separator_arg
),
510 OPT_BOOL(0, "stripspace", &d
.stripspace
,
511 N_("remove unnecessary whitespace")),
515 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
516 PARSE_OPT_KEEP_ARGV0
);
519 error(_("too many arguments"));
520 usage_with_options(git_notes_add_usage
, options
);
526 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
528 if (repo_get_oid(the_repository
, object_ref
, &object
))
529 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
531 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
532 note
= get_note(t
, &object
);
539 return error(_("Cannot add notes. "
540 "Found existing notes for object %s. "
541 "Use '-f' to overwrite existing notes"),
542 oid_to_hex(&object
));
545 * Redirect to "edit" subcommand.
547 * We only end up here if none of -m/-F/-c/-C or -f are
548 * given. The original args are therefore still in
552 return append_edit(argc
, argv
, prefix
, repo
);
554 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
555 oid_to_hex(&object
));
558 prepare_note_data(&object
, &d
, note
);
559 if (d
.buf
.len
|| allow_empty
) {
560 write_note_data(&d
, &new_note
);
561 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
562 BUG("combine_notes_overwrite failed");
563 commit_notes(the_repository
, t
,
564 "Notes added by 'git notes add'");
566 fprintf(stderr
, _("Removing note for object %s\n"),
567 oid_to_hex(&object
));
568 remove_note(t
, object
.hash
);
569 commit_notes(the_repository
, t
,
570 "Notes removed by 'git notes add'");
578 static int copy(int argc
, const char **argv
, const char *prefix
,
579 struct repository
*repo UNUSED
)
581 int retval
= 0, force
= 0, from_stdin
= 0;
582 const struct object_id
*from_note
, *note
;
583 const char *object_ref
;
584 struct object_id object
, from_obj
;
585 struct notes_tree
*t
;
586 const char *rewrite_cmd
= NULL
;
587 struct option options
[] = {
588 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
589 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
590 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
591 N_("load rewriting config for <command> (implies "
596 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
599 if (from_stdin
|| rewrite_cmd
) {
601 error(_("too many arguments"));
602 usage_with_options(git_notes_copy_usage
, options
);
604 return notes_copy_from_stdin(force
, rewrite_cmd
);
609 error(_("too few arguments"));
610 usage_with_options(git_notes_copy_usage
, options
);
613 error(_("too many arguments"));
614 usage_with_options(git_notes_copy_usage
, options
);
617 if (repo_get_oid(the_repository
, argv
[0], &from_obj
))
618 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
620 object_ref
= 1 < argc
? argv
[1] : "HEAD";
622 if (repo_get_oid(the_repository
, object_ref
, &object
))
623 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
625 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
626 note
= get_note(t
, &object
);
630 retval
= error(_("Cannot copy notes. Found existing "
631 "notes for object %s. Use '-f' to "
632 "overwrite existing notes"),
633 oid_to_hex(&object
));
636 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
637 oid_to_hex(&object
));
640 from_note
= get_note(t
, &from_obj
);
642 retval
= error(_("missing notes on source object %s. Cannot "
643 "copy."), oid_to_hex(&from_obj
));
647 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
648 BUG("combine_notes_overwrite failed");
649 commit_notes(the_repository
, t
,
650 "Notes added by 'git notes copy'");
656 static int append_edit(int argc
, const char **argv
, const char *prefix
,
657 struct repository
*repo UNUSED
)
660 const char *object_ref
;
661 struct notes_tree
*t
;
662 struct object_id object
, new_note
;
663 const struct object_id
*note
;
665 const char * const *usage
;
666 struct note_data d
= { .buf
= STRBUF_INIT
, .stripspace
= UNSPECIFIED
};
667 struct option options
[] = {
668 OPT_CALLBACK_F('m', "message", &d
, N_("message"),
669 N_("note contents as a string"), PARSE_OPT_NONEG
,
671 OPT_CALLBACK_F('F', "file", &d
, N_("file"),
672 N_("note contents in a file"), PARSE_OPT_NONEG
,
674 OPT_CALLBACK_F('c', "reedit-message", &d
, N_("object"),
675 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
677 OPT_CALLBACK_F('C', "reuse-message", &d
, N_("object"),
678 N_("reuse specified note object"), PARSE_OPT_NONEG
,
680 OPT_BOOL('e', "edit", &d
.use_editor
,
681 N_("edit note message in editor")),
682 OPT_BOOL(0, "allow-empty", &allow_empty
,
683 N_("allow storing empty note")),
684 OPT_CALLBACK_F(0, "separator", &separator
,
685 N_("<paragraph-break>"),
686 N_("insert <paragraph-break> between paragraphs"),
687 PARSE_OPT_OPTARG
, parse_separator_arg
),
688 OPT_BOOL(0, "stripspace", &d
.stripspace
,
689 N_("remove unnecessary whitespace")),
692 int edit
= !strcmp(argv
[0], "edit");
694 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
695 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
696 PARSE_OPT_KEEP_ARGV0
);
699 error(_("too many arguments"));
700 usage_with_options(usage
, options
);
706 fprintf(stderr
, _("The -m/-F/-c/-C options have been "
707 "deprecated for the 'edit' subcommand.\n"
708 "Please use 'git notes add -f -m/-F/-c/-C' "
712 object_ref
= 1 < argc
? argv
[1] : "HEAD";
714 if (repo_get_oid(the_repository
, object_ref
, &object
))
715 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
717 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
718 note
= get_note(t
, &object
);
720 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
723 /* Append buf to previous note contents */
725 enum object_type type
;
726 struct strbuf buf
= STRBUF_INIT
;
727 char *prev_buf
= odb_read_object(the_repository
->objects
, note
, &type
, &size
);
730 die(_("unable to read %s"), oid_to_hex(note
));
732 strbuf_add(&buf
, prev_buf
, size
);
733 if (d
.buf
.len
&& size
)
734 append_separator(&buf
);
735 strbuf_insert(&d
.buf
, 0, buf
.buf
, buf
.len
);
738 strbuf_release(&buf
);
741 if (d
.buf
.len
|| allow_empty
) {
742 write_note_data(&d
, &new_note
);
743 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
744 BUG("combine_notes_overwrite failed");
745 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
747 fprintf(stderr
, _("Removing note for object %s\n"),
748 oid_to_hex(&object
));
749 remove_note(t
, object
.hash
);
750 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
752 commit_notes(the_repository
, t
, logmsg
);
760 static int show(int argc
, const char **argv
, const char *prefix
,
761 struct repository
*repo UNUSED
)
763 const char *object_ref
;
764 struct notes_tree
*t
;
765 struct object_id object
;
766 const struct object_id
*note
;
768 struct option options
[] = {
772 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
776 error(_("too many arguments"));
777 usage_with_options(git_notes_show_usage
, options
);
780 object_ref
= argc
? argv
[0] : "HEAD";
782 if (repo_get_oid(the_repository
, object_ref
, &object
))
783 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
785 t
= init_notes_check("show", 0);
786 note
= get_note(t
, &object
);
789 retval
= error(_("no note found for object %s."),
790 oid_to_hex(&object
));
792 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
793 retval
= execv_git_cmd(show_args
);
799 static int merge_abort(struct notes_merge_options
*o
)
804 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
805 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
808 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
809 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
810 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
811 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
812 if (notes_merge_abort(o
))
813 ret
+= error(_("failed to remove 'git notes merge' worktree"));
817 static int merge_commit(struct notes_merge_options
*o
)
819 struct strbuf msg
= STRBUF_INIT
;
820 struct object_id oid
, parent_oid
;
821 struct notes_tree t
= {0};
822 struct commit
*partial
;
823 struct pretty_print_context pretty_ctx
;
824 void *local_ref_to_free
;
828 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
829 * and target notes ref from .git/NOTES_MERGE_REF.
832 if (repo_get_oid(the_repository
, "NOTES_MERGE_PARTIAL", &oid
))
833 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
834 else if (!(partial
= lookup_commit_reference(the_repository
, &oid
)))
835 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
836 else if (repo_parse_commit(the_repository
, partial
))
837 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
839 if (partial
->parents
)
840 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
842 oidclr(&parent_oid
, the_repository
->hash_algo
);
844 init_notes(&t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
846 o
->local_ref
= local_ref_to_free
=
847 refs_resolve_refdup(get_main_ref_store(the_repository
),
848 "NOTES_MERGE_REF", 0, &oid
, NULL
);
850 die(_("failed to resolve NOTES_MERGE_REF"));
852 if (notes_merge_commit(o
, &t
, partial
, &oid
))
853 die(_("failed to finalize notes merge"));
855 /* Reuse existing commit message in reflog message */
856 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
857 repo_format_commit_message(the_repository
, partial
, "%s", &msg
,
860 strbuf_insertstr(&msg
, 0, "notes: ");
861 refs_update_ref(get_main_ref_store(the_repository
), msg
.buf
,
863 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
864 0, UPDATE_REFS_DIE_ON_ERR
);
867 strbuf_release(&msg
);
868 ret
= merge_abort(o
);
869 free(local_ref_to_free
);
873 static int git_config_get_notes_strategy(const char *key
,
874 enum notes_merge_strategy
*strategy
)
878 if (repo_config_get_string(the_repository
, key
, &value
))
880 if (parse_notes_merge_strategy(value
, strategy
))
881 git_die_config(the_repository
, key
, _("unknown notes merge strategy %s"), value
);
887 static int merge(int argc
, const char **argv
, const char *prefix
,
888 struct repository
*repo UNUSED
)
890 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
891 struct object_id result_oid
;
892 struct notes_tree
*t
;
893 struct notes_merge_options o
;
894 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
895 int verbosity
= 0, result
;
896 const char *strategy
= NULL
;
897 struct option options
[] = {
898 OPT_GROUP(N_("General options")),
899 OPT__VERBOSITY(&verbosity
),
900 OPT_GROUP(N_("Merge options")),
901 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
902 N_("resolve notes conflicts using the given strategy "
903 "(manual/ours/theirs/union/cat_sort_uniq)")),
904 OPT_GROUP(N_("Committing unmerged notes")),
905 OPT_SET_INT_F(0, "commit", &do_commit
,
906 N_("finalize notes merge by committing unmerged notes"),
908 OPT_GROUP(N_("Aborting notes merge resolution")),
909 OPT_SET_INT_F(0, "abort", &do_abort
,
910 N_("abort notes merge"),
916 argc
= parse_options(argc
, argv
, prefix
, options
,
917 git_notes_merge_usage
, 0);
919 if (strategy
|| do_commit
+ do_abort
== 0)
921 if (do_merge
+ do_commit
+ do_abort
!= 1) {
922 error(_("cannot mix --commit, --abort or -s/--strategy"));
923 usage_with_options(git_notes_merge_usage
, options
);
926 if (do_merge
&& argc
!= 1) {
927 error(_("must specify a notes ref to merge"));
928 usage_with_options(git_notes_merge_usage
, options
);
929 } else if (!do_merge
&& argc
) {
930 error(_("too many arguments"));
931 usage_with_options(git_notes_merge_usage
, options
);
934 init_notes_merge_options(the_repository
, &o
);
935 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
938 return merge_abort(&o
);
940 return merge_commit(&o
);
942 notes_ref
= default_notes_ref(the_repository
);
943 o
.local_ref
= notes_ref
;
944 strbuf_addstr(&remote_ref
, argv
[0]);
945 expand_loose_notes_ref(&remote_ref
);
946 o
.remote_ref
= remote_ref
.buf
;
948 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
951 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
952 error(_("unknown -s/--strategy: %s"), strategy
);
953 usage_with_options(git_notes_merge_usage
, options
);
956 struct strbuf merge_key
= STRBUF_INIT
;
957 const char *short_ref
= NULL
;
959 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
960 BUG("local ref %s is outside of refs/notes/",
963 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
965 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
966 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
968 strbuf_release(&merge_key
);
971 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
972 remote_ref
.buf
, notes_ref
);
973 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
975 result
= notes_merge(&o
, t
, &result_oid
);
977 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
978 /* Update default notes ref with new commit */
979 refs_update_ref(get_main_ref_store(the_repository
), msg
.buf
,
980 notes_ref
, &result_oid
, NULL
, 0,
981 UPDATE_REFS_DIE_ON_ERR
);
982 else { /* Merge has unresolved conflicts */
983 struct worktree
**worktrees
;
984 const struct worktree
*wt
;
987 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
988 refs_update_ref(get_main_ref_store(the_repository
), msg
.buf
,
989 "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
990 0, UPDATE_REFS_DIE_ON_ERR
);
991 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
992 worktrees
= get_worktrees();
993 wt
= find_shared_symref(worktrees
, "NOTES_MERGE_REF",
996 die(_("a notes merge into %s is already in-progress at %s"),
997 notes_ref
, wt
->path
);
998 free_worktrees(worktrees
);
999 if (refs_update_symref(get_main_ref_store(the_repository
), "NOTES_MERGE_REF", notes_ref
, NULL
))
1000 die(_("failed to store link to current notes ref (%s)"),
1003 path
= repo_git_path(the_repository
, NOTES_MERGE_WORKTREE
);
1004 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
1005 "and commit the result with 'git notes merge --commit', "
1006 "or abort the merge with 'git notes merge --abort'.\n"),
1013 strbuf_release(&remote_ref
);
1014 strbuf_release(&msg
);
1015 return result
< 0; /* return non-zero on conflicts */
1018 #define IGNORE_MISSING 1
1020 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
1023 struct object_id oid
;
1024 if (repo_get_oid(the_repository
, name
, &oid
))
1025 return error(_("Failed to resolve '%s' as a valid ref."), name
);
1026 status
= remove_note(t
, oid
.hash
);
1028 fprintf(stderr
, _("Object %s has no note\n"), name
);
1030 fprintf(stderr
, _("Removing note for object %s\n"), name
);
1031 return (flag
& IGNORE_MISSING
) ? 0 : status
;
1034 static int remove_cmd(int argc
, const char **argv
, const char *prefix
,
1035 struct repository
*repo UNUSED
)
1039 struct option options
[] = {
1040 OPT_BIT(0, "ignore-missing", &flag
,
1041 N_("attempt to remove non-existent note is not an error"),
1043 OPT_BOOL(0, "stdin", &from_stdin
,
1044 N_("read object names from the standard input")),
1047 struct notes_tree
*t
;
1050 argc
= parse_options(argc
, argv
, prefix
, options
,
1051 git_notes_remove_usage
, 0);
1053 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
1055 if (!argc
&& !from_stdin
) {
1056 retval
= remove_one_note(t
, "HEAD", flag
);
1059 retval
|= remove_one_note(t
, *argv
, flag
);
1064 struct strbuf sb
= STRBUF_INIT
;
1065 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1067 retval
|= remove_one_note(t
, sb
.buf
, flag
);
1069 strbuf_release(&sb
);
1072 commit_notes(the_repository
, t
,
1073 "Notes removed by 'git notes remove'");
1078 static int prune(int argc
, const char **argv
, const char *prefix
,
1079 struct repository
*repo UNUSED
)
1081 struct notes_tree
*t
;
1082 int show_only
= 0, verbose
= 0;
1083 struct option options
[] = {
1084 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
1085 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
1089 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
1093 error(_("too many arguments"));
1094 usage_with_options(git_notes_prune_usage
, options
);
1097 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
1099 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
1100 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
1102 commit_notes(the_repository
, t
,
1103 "Notes removed by 'git notes prune'");
1108 static int get_ref(int argc
, const char **argv
, const char *prefix
,
1109 struct repository
*repo UNUSED
)
1111 struct option options
[] = { OPT_END() };
1113 argc
= parse_options(argc
, argv
, prefix
, options
,
1114 git_notes_get_ref_usage
, 0);
1117 error(_("too many arguments"));
1118 usage_with_options(git_notes_get_ref_usage
, options
);
1121 notes_ref
= default_notes_ref(the_repository
);
1127 int cmd_notes(int argc
,
1130 struct repository
*repo
)
1132 const char *override_notes_ref
= NULL
;
1133 parse_opt_subcommand_fn
*fn
= NULL
;
1134 struct option options
[] = {
1135 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
1136 N_("use notes from <notes-ref>")),
1137 OPT_SUBCOMMAND("list", &fn
, list
),
1138 OPT_SUBCOMMAND("add", &fn
, add
),
1139 OPT_SUBCOMMAND("copy", &fn
, copy
),
1140 OPT_SUBCOMMAND("append", &fn
, append_edit
),
1141 OPT_SUBCOMMAND("edit", &fn
, append_edit
),
1142 OPT_SUBCOMMAND("show", &fn
, show
),
1143 OPT_SUBCOMMAND("merge", &fn
, merge
),
1144 OPT_SUBCOMMAND("remove", &fn
, remove_cmd
),
1145 OPT_SUBCOMMAND("prune", &fn
, prune
),
1146 OPT_SUBCOMMAND("get-ref", &fn
, get_ref
),
1150 repo_config(the_repository
, git_default_config
, NULL
);
1151 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1152 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1155 error(_("unknown subcommand: `%s'"), argv
[0]);
1156 usage_with_options(git_notes_usage
, options
);
1161 if (override_notes_ref
) {
1162 struct strbuf sb
= STRBUF_INIT
;
1163 strbuf_addstr(&sb
, override_notes_ref
);
1164 expand_notes_ref(&sb
);
1165 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1166 strbuf_release(&sb
);
1169 return !!fn(argc
, argv
, prefix
, repo
);