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 "object-store.h"
19 #include "run-command.h"
20 #include "parse-options.h"
21 #include "string-list.h"
22 #include "notes-merge.h"
23 #include "notes-utils.h"
26 static const char * const git_notes_usage
[] = {
27 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
28 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
29 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
30 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
31 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
32 N_("git notes [--ref <notes-ref>] show [<object>]"),
33 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
34 N_("git notes merge --commit [-v | -q]"),
35 N_("git notes merge --abort [-v | -q]"),
36 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
37 N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
38 N_("git notes [--ref <notes-ref>] get-ref"),
42 static const char * const git_notes_list_usage
[] = {
43 N_("git notes [list [<object>]]"),
47 static const char * const git_notes_add_usage
[] = {
48 N_("git notes add [<options>] [<object>]"),
52 static const char * const git_notes_copy_usage
[] = {
53 N_("git notes copy [<options>] <from-object> <to-object>"),
54 N_("git notes copy --stdin [<from-object> <to-object>]..."),
58 static const char * const git_notes_append_usage
[] = {
59 N_("git notes append [<options>] [<object>]"),
63 static const char * const git_notes_edit_usage
[] = {
64 N_("git notes edit [<object>]"),
68 static const char * const git_notes_show_usage
[] = {
69 N_("git notes show [<object>]"),
73 static const char * const git_notes_merge_usage
[] = {
74 N_("git notes merge [<options>] <notes-ref>"),
75 N_("git notes merge --commit [<options>]"),
76 N_("git notes merge --abort [<options>]"),
80 static const char * const git_notes_remove_usage
[] = {
81 N_("git notes remove [<object>]"),
85 static const char * const git_notes_prune_usage
[] = {
86 N_("git notes prune [<options>]"),
90 static const char * const git_notes_get_ref_usage
[] = {
91 N_("git notes get-ref"),
95 static const char note_template
[] =
96 N_("Write/edit the notes for the following object:");
105 static void free_note_data(struct note_data
*d
)
108 unlink_or_warn(d
->edit_path
);
111 strbuf_release(&d
->buf
);
114 static int list_each_note(const struct object_id
*object_oid
,
115 const struct object_id
*note_oid
, char *note_path
,
118 printf("%s %s\n", oid_to_hex(note_oid
), oid_to_hex(object_oid
));
122 static void copy_obj_to_fd(int fd
, const struct object_id
*oid
)
125 enum object_type type
;
126 char *buf
= read_object_file(oid
, &type
, &size
);
129 write_or_die(fd
, buf
, size
);
134 static void write_commented_object(int fd
, const struct object_id
*object
)
136 const char *show_args
[5] =
137 {"show", "--stat", "--no-notes", oid_to_hex(object
), NULL
};
138 struct child_process show
= CHILD_PROCESS_INIT
;
139 struct strbuf buf
= STRBUF_INIT
;
140 struct strbuf cbuf
= STRBUF_INIT
;
142 /* Invoke "git show --stat --no-notes $object" */
143 show
.argv
= show_args
;
148 if (start_command(&show
))
149 die(_("unable to start 'show' for object '%s'"),
152 if (strbuf_read(&buf
, show
.out
, 0) < 0)
153 die_errno(_("could not read 'show' output"));
154 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
155 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
157 strbuf_release(&cbuf
);
158 strbuf_release(&buf
);
160 if (finish_command(&show
))
161 die(_("failed to finish 'show' for object '%s'"),
165 static void prepare_note_data(const struct object_id
*object
, struct note_data
*d
,
166 const struct object_id
*old_note
)
168 if (d
->use_editor
|| !d
->given
) {
170 struct strbuf buf
= STRBUF_INIT
;
172 /* write the template message before editing: */
173 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
174 fd
= open(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
176 die_errno(_("could not create file '%s'"), d
->edit_path
);
179 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
181 copy_obj_to_fd(fd
, old_note
);
183 strbuf_addch(&buf
, '\n');
184 strbuf_add_commented_lines(&buf
, "\n", strlen("\n"));
185 strbuf_add_commented_lines(&buf
, _(note_template
), strlen(_(note_template
)));
186 strbuf_addch(&buf
, '\n');
187 write_or_die(fd
, buf
.buf
, buf
.len
);
189 write_commented_object(fd
, object
);
192 strbuf_release(&buf
);
193 strbuf_reset(&d
->buf
);
195 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
196 die(_("please supply the note contents using either -m or -F option"));
198 strbuf_stripspace(&d
->buf
, 1);
202 static void write_note_data(struct note_data
*d
, struct object_id
*oid
)
204 if (write_object_file(d
->buf
.buf
, d
->buf
.len
, blob_type
, oid
)) {
205 error(_("unable to write note object"));
207 error(_("the note contents have been left in %s"),
213 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
215 struct note_data
*d
= opt
->value
;
217 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
219 strbuf_addch(&d
->buf
, '\n');
220 strbuf_addstr(&d
->buf
, arg
);
221 strbuf_stripspace(&d
->buf
, 0);
227 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
229 struct note_data
*d
= opt
->value
;
232 strbuf_addch(&d
->buf
, '\n');
233 if (!strcmp(arg
, "-")) {
234 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
235 die_errno(_("cannot read '%s'"), arg
);
236 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
237 die_errno(_("could not open or read '%s'"), arg
);
238 strbuf_stripspace(&d
->buf
, 0);
244 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
246 struct note_data
*d
= opt
->value
;
248 struct object_id object
;
249 enum object_type type
;
253 strbuf_addch(&d
->buf
, '\n');
255 if (get_oid(arg
, &object
))
256 die(_("failed to resolve '%s' as a valid ref."), arg
);
257 if (!(buf
= read_object_file(&object
, &type
, &len
))) {
259 die(_("failed to read object '%s'."), arg
);
261 if (type
!= OBJ_BLOB
) {
263 die(_("cannot read note data from non-blob object '%s'."), arg
);
265 strbuf_add(&d
->buf
, buf
, len
);
272 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
274 struct note_data
*d
= opt
->value
;
276 return parse_reuse_arg(opt
, arg
, unset
);
279 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
281 struct strbuf buf
= STRBUF_INIT
;
282 struct notes_rewrite_cfg
*c
= NULL
;
283 struct notes_tree
*t
= NULL
;
285 const char *msg
= "Notes added by 'git notes copy'";
288 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
292 init_notes(NULL
, NULL
, NULL
, NOTES_INIT_WRITABLE
);
293 t
= &default_notes_tree
;
296 while (strbuf_getline_lf(&buf
, stdin
) != EOF
) {
297 struct object_id from_obj
, to_obj
;
298 struct strbuf
**split
;
301 split
= strbuf_split(&buf
, ' ');
302 if (!split
[0] || !split
[1])
303 die(_("malformed input line: '%s'."), buf
.buf
);
304 strbuf_rtrim(split
[0]);
305 strbuf_rtrim(split
[1]);
306 if (get_oid(split
[0]->buf
, &from_obj
))
307 die(_("failed to resolve '%s' as a valid ref."), split
[0]->buf
);
308 if (get_oid(split
[1]->buf
, &to_obj
))
309 die(_("failed to resolve '%s' as a valid ref."), split
[1]->buf
);
312 err
= copy_note_for_rewrite(c
, &from_obj
, &to_obj
);
314 err
= copy_note(t
, &from_obj
, &to_obj
, force
,
315 combine_notes_overwrite
);
318 error(_("failed to copy notes from '%s' to '%s'"),
319 split
[0]->buf
, split
[1]->buf
);
323 strbuf_list_free(split
);
327 commit_notes(t
, msg
);
330 finish_copy_notes_for_rewrite(c
, msg
);
332 strbuf_release(&buf
);
336 static struct notes_tree
*init_notes_check(const char *subcommand
,
339 struct notes_tree
*t
;
341 init_notes(NULL
, NULL
, NULL
, flags
);
342 t
= &default_notes_tree
;
344 ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->update_ref
: t
->ref
;
345 if (!starts_with(ref
, "refs/notes/"))
347 * TRANSLATORS: the first %s will be replaced by a git
348 * notes command: 'add', 'merge', 'remove', etc.
350 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
355 static int list(int argc
, const char **argv
, const char *prefix
)
357 struct notes_tree
*t
;
358 struct object_id object
;
359 const struct object_id
*note
;
361 struct option options
[] = {
366 argc
= parse_options(argc
, argv
, prefix
, options
,
367 git_notes_list_usage
, 0);
370 error(_("too many parameters"));
371 usage_with_options(git_notes_list_usage
, options
);
374 t
= init_notes_check("list", 0);
376 if (get_oid(argv
[0], &object
))
377 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
378 note
= get_note(t
, &object
);
380 puts(oid_to_hex(note
));
383 retval
= error(_("no note found for object %s."),
384 oid_to_hex(&object
));
386 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
392 static int append_edit(int argc
, const char **argv
, const char *prefix
);
394 static int add(int argc
, const char **argv
, const char *prefix
)
396 int force
= 0, allow_empty
= 0;
397 const char *object_ref
;
398 struct notes_tree
*t
;
399 struct object_id object
, new_note
;
400 const struct object_id
*note
;
401 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
402 struct option options
[] = {
403 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
404 N_("note contents as a string"), PARSE_OPT_NONEG
,
406 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
407 N_("note contents in a file"), PARSE_OPT_NONEG
,
409 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
410 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
412 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
413 N_("reuse specified note object"), PARSE_OPT_NONEG
,
415 OPT_BOOL(0, "allow-empty", &allow_empty
,
416 N_("allow storing empty note")),
417 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
421 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
422 PARSE_OPT_KEEP_ARGV0
);
425 error(_("too many parameters"));
426 usage_with_options(git_notes_add_usage
, options
);
429 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
431 if (get_oid(object_ref
, &object
))
432 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
434 t
= init_notes_check("add", NOTES_INIT_WRITABLE
);
435 note
= get_note(t
, &object
);
442 return error(_("Cannot add notes. "
443 "Found existing notes for object %s. "
444 "Use '-f' to overwrite existing notes"),
445 oid_to_hex(&object
));
448 * Redirect to "edit" subcommand.
450 * We only end up here if none of -m/-F/-c/-C or -f are
451 * given. The original args are therefore still in
455 return append_edit(argc
, argv
, prefix
);
457 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
458 oid_to_hex(&object
));
461 prepare_note_data(&object
, &d
, note
);
462 if (d
.buf
.len
|| allow_empty
) {
463 write_note_data(&d
, &new_note
);
464 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
465 BUG("combine_notes_overwrite failed");
466 commit_notes(t
, "Notes added by 'git notes add'");
468 fprintf(stderr
, _("Removing note for object %s\n"),
469 oid_to_hex(&object
));
470 remove_note(t
, object
.hash
);
471 commit_notes(t
, "Notes removed by 'git notes add'");
479 static int copy(int argc
, const char **argv
, const char *prefix
)
481 int retval
= 0, force
= 0, from_stdin
= 0;
482 const struct object_id
*from_note
, *note
;
483 const char *object_ref
;
484 struct object_id object
, from_obj
;
485 struct notes_tree
*t
;
486 const char *rewrite_cmd
= NULL
;
487 struct option options
[] = {
488 OPT__FORCE(&force
, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE
),
489 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
490 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
491 N_("load rewriting config for <command> (implies "
496 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
499 if (from_stdin
|| rewrite_cmd
) {
501 error(_("too many parameters"));
502 usage_with_options(git_notes_copy_usage
, options
);
504 return notes_copy_from_stdin(force
, rewrite_cmd
);
509 error(_("too few parameters"));
510 usage_with_options(git_notes_copy_usage
, options
);
513 error(_("too many parameters"));
514 usage_with_options(git_notes_copy_usage
, options
);
517 if (get_oid(argv
[0], &from_obj
))
518 die(_("failed to resolve '%s' as a valid ref."), argv
[0]);
520 object_ref
= 1 < argc
? argv
[1] : "HEAD";
522 if (get_oid(object_ref
, &object
))
523 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
525 t
= init_notes_check("copy", NOTES_INIT_WRITABLE
);
526 note
= get_note(t
, &object
);
530 retval
= error(_("Cannot copy notes. Found existing "
531 "notes for object %s. Use '-f' to "
532 "overwrite existing notes"),
533 oid_to_hex(&object
));
536 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
537 oid_to_hex(&object
));
540 from_note
= get_note(t
, &from_obj
);
542 retval
= error(_("missing notes on source object %s. Cannot "
543 "copy."), oid_to_hex(&from_obj
));
547 if (add_note(t
, &object
, from_note
, combine_notes_overwrite
))
548 BUG("combine_notes_overwrite failed");
549 commit_notes(t
, "Notes added by 'git notes copy'");
555 static int append_edit(int argc
, const char **argv
, const char *prefix
)
558 const char *object_ref
;
559 struct notes_tree
*t
;
560 struct object_id object
, new_note
;
561 const struct object_id
*note
;
563 const char * const *usage
;
564 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
565 struct option options
[] = {
566 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
567 N_("note contents as a string"), PARSE_OPT_NONEG
,
569 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
570 N_("note contents in a file"), PARSE_OPT_NONEG
,
572 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
573 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
575 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
576 N_("reuse specified note object"), PARSE_OPT_NONEG
,
578 OPT_BOOL(0, "allow-empty", &allow_empty
,
579 N_("allow storing empty note")),
582 int edit
= !strcmp(argv
[0], "edit");
584 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
585 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
586 PARSE_OPT_KEEP_ARGV0
);
589 error(_("too many parameters"));
590 usage_with_options(usage
, options
);
594 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
595 "for the 'edit' subcommand.\n"
596 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
598 object_ref
= 1 < argc
? argv
[1] : "HEAD";
600 if (get_oid(object_ref
, &object
))
601 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
603 t
= init_notes_check(argv
[0], NOTES_INIT_WRITABLE
);
604 note
= get_note(t
, &object
);
606 prepare_note_data(&object
, &d
, edit
&& note
? note
: NULL
);
609 /* Append buf to previous note contents */
611 enum object_type type
;
612 char *prev_buf
= read_object_file(note
, &type
, &size
);
614 strbuf_grow(&d
.buf
, size
+ 1);
615 if (d
.buf
.len
&& prev_buf
&& size
)
616 strbuf_insert(&d
.buf
, 0, "\n", 1);
617 if (prev_buf
&& size
)
618 strbuf_insert(&d
.buf
, 0, prev_buf
, size
);
622 if (d
.buf
.len
|| allow_empty
) {
623 write_note_data(&d
, &new_note
);
624 if (add_note(t
, &object
, &new_note
, combine_notes_overwrite
))
625 BUG("combine_notes_overwrite failed");
626 logmsg
= xstrfmt("Notes added by 'git notes %s'", argv
[0]);
628 fprintf(stderr
, _("Removing note for object %s\n"),
629 oid_to_hex(&object
));
630 remove_note(t
, object
.hash
);
631 logmsg
= xstrfmt("Notes removed by 'git notes %s'", argv
[0]);
633 commit_notes(t
, logmsg
);
641 static int show(int argc
, const char **argv
, const char *prefix
)
643 const char *object_ref
;
644 struct notes_tree
*t
;
645 struct object_id object
;
646 const struct object_id
*note
;
648 struct option options
[] = {
652 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
656 error(_("too many parameters"));
657 usage_with_options(git_notes_show_usage
, options
);
660 object_ref
= argc
? argv
[0] : "HEAD";
662 if (get_oid(object_ref
, &object
))
663 die(_("failed to resolve '%s' as a valid ref."), object_ref
);
665 t
= init_notes_check("show", 0);
666 note
= get_note(t
, &object
);
669 retval
= error(_("no note found for object %s."),
670 oid_to_hex(&object
));
672 const char *show_args
[3] = {"show", oid_to_hex(note
), NULL
};
673 retval
= execv_git_cmd(show_args
);
679 static int merge_abort(struct notes_merge_options
*o
)
684 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
685 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
688 if (delete_ref(NULL
, "NOTES_MERGE_PARTIAL", NULL
, 0))
689 ret
+= error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
690 if (delete_ref(NULL
, "NOTES_MERGE_REF", NULL
, REF_NO_DEREF
))
691 ret
+= error(_("failed to delete ref NOTES_MERGE_REF"));
692 if (notes_merge_abort(o
))
693 ret
+= error(_("failed to remove 'git notes merge' worktree"));
697 static int merge_commit(struct notes_merge_options
*o
)
699 struct strbuf msg
= STRBUF_INIT
;
700 struct object_id oid
, parent_oid
;
701 struct notes_tree
*t
;
702 struct commit
*partial
;
703 struct pretty_print_context pretty_ctx
;
704 void *local_ref_to_free
;
708 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
709 * and target notes ref from .git/NOTES_MERGE_REF.
712 if (get_oid("NOTES_MERGE_PARTIAL", &oid
))
713 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
714 else if (!(partial
= lookup_commit_reference(&oid
)))
715 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
716 else if (parse_commit(partial
))
717 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
719 if (partial
->parents
)
720 oidcpy(&parent_oid
, &partial
->parents
->item
->object
.oid
);
724 t
= xcalloc(1, sizeof(struct notes_tree
));
725 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
727 o
->local_ref
= local_ref_to_free
=
728 resolve_refdup("NOTES_MERGE_REF", 0, &oid
, NULL
);
730 die(_("failed to resolve NOTES_MERGE_REF"));
732 if (notes_merge_commit(o
, t
, partial
, &oid
))
733 die(_("failed to finalize notes merge"));
735 /* Reuse existing commit message in reflog message */
736 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
737 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
739 strbuf_insert(&msg
, 0, "notes: ", 7);
740 update_ref(msg
.buf
, o
->local_ref
, &oid
,
741 is_null_oid(&parent_oid
) ? NULL
: &parent_oid
,
742 0, UPDATE_REFS_DIE_ON_ERR
);
745 strbuf_release(&msg
);
746 ret
= merge_abort(o
);
747 free(local_ref_to_free
);
751 static int git_config_get_notes_strategy(const char *key
,
752 enum notes_merge_strategy
*strategy
)
756 if (git_config_get_string(key
, &value
))
758 if (parse_notes_merge_strategy(value
, strategy
))
759 git_die_config(key
, _("unknown notes merge strategy %s"), value
);
765 static int merge(int argc
, const char **argv
, const char *prefix
)
767 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
768 struct object_id result_oid
;
769 struct notes_tree
*t
;
770 struct notes_merge_options o
;
771 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
772 int verbosity
= 0, result
;
773 const char *strategy
= NULL
;
774 struct option options
[] = {
775 OPT_GROUP(N_("General options")),
776 OPT__VERBOSITY(&verbosity
),
777 OPT_GROUP(N_("Merge options")),
778 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
779 N_("resolve notes conflicts using the given strategy "
780 "(manual/ours/theirs/union/cat_sort_uniq)")),
781 OPT_GROUP(N_("Committing unmerged notes")),
782 OPT_SET_INT_F(0, "commit", &do_commit
,
783 N_("finalize notes merge by committing unmerged notes"),
785 OPT_GROUP(N_("Aborting notes merge resolution")),
786 OPT_SET_INT_F(0, "abort", &do_abort
,
787 N_("abort notes merge"),
792 argc
= parse_options(argc
, argv
, prefix
, options
,
793 git_notes_merge_usage
, 0);
795 if (strategy
|| do_commit
+ do_abort
== 0)
797 if (do_merge
+ do_commit
+ do_abort
!= 1) {
798 error(_("cannot mix --commit, --abort or -s/--strategy"));
799 usage_with_options(git_notes_merge_usage
, options
);
802 if (do_merge
&& argc
!= 1) {
803 error(_("must specify a notes ref to merge"));
804 usage_with_options(git_notes_merge_usage
, options
);
805 } else if (!do_merge
&& argc
) {
806 error(_("too many parameters"));
807 usage_with_options(git_notes_merge_usage
, options
);
810 init_notes_merge_options(&o
);
811 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
814 return merge_abort(&o
);
816 return merge_commit(&o
);
818 o
.local_ref
= default_notes_ref();
819 strbuf_addstr(&remote_ref
, argv
[0]);
820 expand_loose_notes_ref(&remote_ref
);
821 o
.remote_ref
= remote_ref
.buf
;
823 t
= init_notes_check("merge", NOTES_INIT_WRITABLE
);
826 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
827 error(_("unknown -s/--strategy: %s"), strategy
);
828 usage_with_options(git_notes_merge_usage
, options
);
831 struct strbuf merge_key
= STRBUF_INIT
;
832 const char *short_ref
= NULL
;
834 if (!skip_prefix(o
.local_ref
, "refs/notes/", &short_ref
))
835 BUG("local ref %s is outside of refs/notes/",
838 strbuf_addf(&merge_key
, "notes.%s.mergeStrategy", short_ref
);
840 if (git_config_get_notes_strategy(merge_key
.buf
, &o
.strategy
))
841 git_config_get_notes_strategy("notes.mergeStrategy", &o
.strategy
);
843 strbuf_release(&merge_key
);
846 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
847 remote_ref
.buf
, default_notes_ref());
848 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
850 result
= notes_merge(&o
, t
, &result_oid
);
852 if (result
>= 0) /* Merge resulted (trivially) in result_oid */
853 /* Update default notes ref with new commit */
854 update_ref(msg
.buf
, default_notes_ref(), &result_oid
, NULL
, 0,
855 UPDATE_REFS_DIE_ON_ERR
);
856 else { /* Merge has unresolved conflicts */
857 const struct worktree
*wt
;
858 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
859 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", &result_oid
, NULL
,
860 0, UPDATE_REFS_DIE_ON_ERR
);
861 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
862 wt
= find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
864 die(_("a notes merge into %s is already in-progress at %s"),
865 default_notes_ref(), wt
->path
);
866 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
867 die(_("failed to store link to current notes ref (%s)"),
868 default_notes_ref());
869 fprintf(stderr
, _("Automatic notes merge failed. Fix conflicts in %s "
870 "and commit the result with 'git notes merge --commit', "
871 "or abort the merge with 'git notes merge --abort'.\n"),
872 git_path(NOTES_MERGE_WORKTREE
));
876 strbuf_release(&remote_ref
);
877 strbuf_release(&msg
);
878 return result
< 0; /* return non-zero on conflicts */
881 #define IGNORE_MISSING 1
883 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
886 struct object_id oid
;
887 if (get_oid(name
, &oid
))
888 return error(_("Failed to resolve '%s' as a valid ref."), name
);
889 status
= remove_note(t
, oid
.hash
);
891 fprintf(stderr
, _("Object %s has no note\n"), name
);
893 fprintf(stderr
, _("Removing note for object %s\n"), name
);
894 return (flag
& IGNORE_MISSING
) ? 0 : status
;
897 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
901 struct option options
[] = {
902 OPT_BIT(0, "ignore-missing", &flag
,
903 N_("attempt to remove non-existent note is not an error"),
905 OPT_BOOL(0, "stdin", &from_stdin
,
906 N_("read object names from the standard input")),
909 struct notes_tree
*t
;
912 argc
= parse_options(argc
, argv
, prefix
, options
,
913 git_notes_remove_usage
, 0);
915 t
= init_notes_check("remove", NOTES_INIT_WRITABLE
);
917 if (!argc
&& !from_stdin
) {
918 retval
= remove_one_note(t
, "HEAD", flag
);
921 retval
|= remove_one_note(t
, *argv
, flag
);
926 struct strbuf sb
= STRBUF_INIT
;
927 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
929 retval
|= remove_one_note(t
, sb
.buf
, flag
);
934 commit_notes(t
, "Notes removed by 'git notes remove'");
939 static int prune(int argc
, const char **argv
, const char *prefix
)
941 struct notes_tree
*t
;
942 int show_only
= 0, verbose
= 0;
943 struct option options
[] = {
944 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
945 OPT__VERBOSE(&verbose
, N_("report pruned notes")),
949 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
953 error(_("too many parameters"));
954 usage_with_options(git_notes_prune_usage
, options
);
957 t
= init_notes_check("prune", NOTES_INIT_WRITABLE
);
959 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
960 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
962 commit_notes(t
, "Notes removed by 'git notes prune'");
967 static int get_ref(int argc
, const char **argv
, const char *prefix
)
969 struct option options
[] = { OPT_END() };
970 argc
= parse_options(argc
, argv
, prefix
, options
,
971 git_notes_get_ref_usage
, 0);
974 error(_("too many parameters"));
975 usage_with_options(git_notes_get_ref_usage
, options
);
978 puts(default_notes_ref());
982 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
985 const char *override_notes_ref
= NULL
;
986 struct option options
[] = {
987 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
988 N_("use notes from <notes-ref>")),
992 git_config(git_default_config
, NULL
);
993 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
994 PARSE_OPT_STOP_AT_NON_OPTION
);
996 if (override_notes_ref
) {
997 struct strbuf sb
= STRBUF_INIT
;
998 strbuf_addstr(&sb
, override_notes_ref
);
999 expand_notes_ref(&sb
);
1000 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1001 strbuf_release(&sb
);
1004 if (argc
< 1 || !strcmp(argv
[0], "list"))
1005 result
= list(argc
, argv
, prefix
);
1006 else if (!strcmp(argv
[0], "add"))
1007 result
= add(argc
, argv
, prefix
);
1008 else if (!strcmp(argv
[0], "copy"))
1009 result
= copy(argc
, argv
, prefix
);
1010 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
1011 result
= append_edit(argc
, argv
, prefix
);
1012 else if (!strcmp(argv
[0], "show"))
1013 result
= show(argc
, argv
, prefix
);
1014 else if (!strcmp(argv
[0], "merge"))
1015 result
= merge(argc
, argv
, prefix
);
1016 else if (!strcmp(argv
[0], "remove"))
1017 result
= remove_cmd(argc
, argv
, prefix
);
1018 else if (!strcmp(argv
[0], "prune"))
1019 result
= prune(argc
, argv
, prefix
);
1020 else if (!strcmp(argv
[0], "get-ref"))
1021 result
= get_ref(argc
, argv
, prefix
);
1023 result
= error(_("unknown subcommand: %s"), argv
[0]);
1024 usage_with_options(git_notes_usage
, options
);
1027 return result
? 1 : 0;