4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
6 * Based on git-notes.sh by Johannes Schindelin,
7 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "string-list.h"
20 #include "notes-merge.h"
21 #include "notes-utils.h"
23 static const char * const git_notes_usage
[] = {
24 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
25 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
26 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
27 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
29 N_("git notes [--ref <notes-ref>] show [<object>]"),
30 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
31 N_("git notes merge --commit [-v | -q]"),
32 N_("git notes merge --abort [-v | -q]"),
33 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
34 N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
35 N_("git notes [--ref <notes-ref>] get-ref"),
39 static const char * const git_notes_list_usage
[] = {
40 N_("git notes [list [<object>]]"),
44 static const char * const git_notes_add_usage
[] = {
45 N_("git notes add [<options>] [<object>]"),
49 static const char * const git_notes_copy_usage
[] = {
50 N_("git notes copy [<options>] <from-object> <to-object>"),
51 N_("git notes copy --stdin [<from-object> <to-object>]..."),
55 static const char * const git_notes_append_usage
[] = {
56 N_("git notes append [<options>] [<object>]"),
60 static const char * const git_notes_edit_usage
[] = {
61 N_("git notes edit [<object>]"),
65 static const char * const git_notes_show_usage
[] = {
66 N_("git notes show [<object>]"),
70 static const char * const git_notes_merge_usage
[] = {
71 N_("git notes merge [<options>] <notes-ref>"),
72 N_("git notes merge --commit [<options>]"),
73 N_("git notes merge --abort [<options>]"),
77 static const char * const git_notes_remove_usage
[] = {
78 N_("git notes remove [<object>]"),
82 static const char * const git_notes_prune_usage
[] = {
83 N_("git notes prune [<options>]"),
87 static const char * const git_notes_get_ref_usage
[] = {
88 N_("git notes get-ref"),
92 static const char note_template
[] =
93 "\nWrite/edit the notes for the following object:\n";
102 static void free_note_data(struct note_data
*d
)
105 unlink_or_warn(d
->edit_path
);
108 strbuf_release(&d
->buf
);
111 static int list_each_note(const unsigned char *object_sha1
,
112 const unsigned char *note_sha1
, char *note_path
,
115 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
119 static void copy_obj_to_fd(int fd
, const unsigned char *sha1
)
122 enum object_type type
;
123 char *buf
= read_sha1_file(sha1
, &type
, &size
);
126 write_or_die(fd
, buf
, size
);
131 static void write_commented_object(int fd
, const unsigned char *object
)
133 const char *show_args
[5] =
134 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
135 struct child_process show
= CHILD_PROCESS_INIT
;
136 struct strbuf buf
= STRBUF_INIT
;
137 struct strbuf cbuf
= STRBUF_INIT
;
139 /* Invoke "git show --stat --no-notes $object" */
140 show
.argv
= show_args
;
145 if (start_command(&show
))
146 die(_("unable to start 'show' for object '%s'"),
147 sha1_to_hex(object
));
149 if (strbuf_read(&buf
, show
.out
, 0) < 0)
150 die_errno(_("could not read 'show' output"));
151 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
152 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
154 strbuf_release(&cbuf
);
155 strbuf_release(&buf
);
157 if (finish_command(&show
))
158 die(_("failed to finish 'show' for object '%s'"),
159 sha1_to_hex(object
));
162 static void prepare_note_data(const unsigned char *object
, struct note_data
*d
,
163 const unsigned char *old_note
)
165 if (d
->use_editor
|| !d
->given
) {
167 struct strbuf buf
= STRBUF_INIT
;
169 /* write the template message before editing: */
170 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
171 fd
= open(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
173 die_errno(_("could not create file '%s'"), d
->edit_path
);
176 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
178 copy_obj_to_fd(fd
, old_note
);
180 strbuf_addch(&buf
, '\n');
181 strbuf_add_commented_lines(&buf
, note_template
, strlen(note_template
));
182 strbuf_addch(&buf
, '\n');
183 write_or_die(fd
, buf
.buf
, buf
.len
);
185 write_commented_object(fd
, object
);
188 strbuf_release(&buf
);
189 strbuf_reset(&d
->buf
);
191 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
192 die(_("Please supply the note contents using either -m or -F option"));
194 stripspace(&d
->buf
, 1);
198 static void write_note_data(struct note_data
*d
, unsigned char *sha1
)
200 if (write_sha1_file(d
->buf
.buf
, d
->buf
.len
, blob_type
, sha1
)) {
201 error(_("unable to write note object"));
203 error(_("The note contents have been left in %s"),
209 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
211 struct note_data
*d
= opt
->value
;
213 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
215 strbuf_addch(&d
->buf
, '\n');
216 strbuf_addstr(&d
->buf
, arg
);
217 stripspace(&d
->buf
, 0);
223 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
225 struct note_data
*d
= opt
->value
;
228 strbuf_addch(&d
->buf
, '\n');
229 if (!strcmp(arg
, "-")) {
230 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
231 die_errno(_("cannot read '%s'"), arg
);
232 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
233 die_errno(_("could not open or read '%s'"), arg
);
234 stripspace(&d
->buf
, 0);
240 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
242 struct note_data
*d
= opt
->value
;
244 unsigned char object
[20];
245 enum object_type type
;
249 strbuf_addch(&d
->buf
, '\n');
251 if (get_sha1(arg
, object
))
252 die(_("Failed to resolve '%s' as a valid ref."), arg
);
253 if (!(buf
= read_sha1_file(object
, &type
, &len
))) {
255 die(_("Failed to read object '%s'."), arg
);
257 if (type
!= OBJ_BLOB
) {
259 die(_("Cannot read note data from non-blob object '%s'."), arg
);
261 strbuf_add(&d
->buf
, buf
, len
);
268 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
270 struct note_data
*d
= opt
->value
;
272 return parse_reuse_arg(opt
, arg
, unset
);
275 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
277 struct strbuf buf
= STRBUF_INIT
;
278 struct notes_rewrite_cfg
*c
= NULL
;
279 struct notes_tree
*t
= NULL
;
281 const char *msg
= "Notes added by 'git notes copy'";
284 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
288 init_notes(NULL
, NULL
, NULL
, 0);
289 t
= &default_notes_tree
;
292 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
293 unsigned char from_obj
[20], to_obj
[20];
294 struct strbuf
**split
;
297 split
= strbuf_split(&buf
, ' ');
298 if (!split
[0] || !split
[1])
299 die(_("Malformed input line: '%s'."), buf
.buf
);
300 strbuf_rtrim(split
[0]);
301 strbuf_rtrim(split
[1]);
302 if (get_sha1(split
[0]->buf
, from_obj
))
303 die(_("Failed to resolve '%s' as a valid ref."), split
[0]->buf
);
304 if (get_sha1(split
[1]->buf
, to_obj
))
305 die(_("Failed to resolve '%s' as a valid ref."), split
[1]->buf
);
308 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
310 err
= copy_note(t
, from_obj
, to_obj
, force
,
311 combine_notes_overwrite
);
314 error(_("Failed to copy notes from '%s' to '%s'"),
315 split
[0]->buf
, split
[1]->buf
);
319 strbuf_list_free(split
);
323 commit_notes(t
, msg
);
326 finish_copy_notes_for_rewrite(c
, msg
);
331 static struct notes_tree
*init_notes_check(const char *subcommand
)
333 struct notes_tree
*t
;
334 init_notes(NULL
, NULL
, NULL
, 0);
335 t
= &default_notes_tree
;
337 if (!starts_with(t
->ref
, "refs/notes/"))
338 die("Refusing to %s notes in %s (outside of refs/notes/)",
343 static int list(int argc
, const char **argv
, const char *prefix
)
345 struct notes_tree
*t
;
346 unsigned char object
[20];
347 const unsigned char *note
;
349 struct option options
[] = {
354 argc
= parse_options(argc
, argv
, prefix
, options
,
355 git_notes_list_usage
, 0);
358 error(_("too many parameters"));
359 usage_with_options(git_notes_list_usage
, options
);
362 t
= init_notes_check("list");
364 if (get_sha1(argv
[0], object
))
365 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
366 note
= get_note(t
, object
);
368 puts(sha1_to_hex(note
));
371 retval
= error(_("No note found for object %s."),
372 sha1_to_hex(object
));
374 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
380 static int append_edit(int argc
, const char **argv
, const char *prefix
);
382 static int add(int argc
, const char **argv
, const char *prefix
)
384 int force
= 0, allow_empty
= 0;
385 const char *object_ref
;
386 struct notes_tree
*t
;
387 unsigned char object
[20], new_note
[20];
388 const unsigned char *note
;
389 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
390 struct option options
[] = {
391 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
392 N_("note contents as a string"), PARSE_OPT_NONEG
,
394 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
395 N_("note contents in a file"), PARSE_OPT_NONEG
,
397 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
398 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
400 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
401 N_("reuse specified note object"), PARSE_OPT_NONEG
,
403 OPT_BOOL(0, "allow-empty", &allow_empty
,
404 N_("allow storing empty note")),
405 OPT__FORCE(&force
, N_("replace existing notes")),
409 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
410 PARSE_OPT_KEEP_ARGV0
);
413 error(_("too many parameters"));
414 usage_with_options(git_notes_add_usage
, options
);
417 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
419 if (get_sha1(object_ref
, object
))
420 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
422 t
= init_notes_check("add");
423 note
= get_note(t
, object
);
430 return error(_("Cannot add notes. "
431 "Found existing notes for object %s. "
432 "Use '-f' to overwrite existing notes"),
433 sha1_to_hex(object
));
436 * Redirect to "edit" subcommand.
438 * We only end up here if none of -m/-F/-c/-C or -f are
439 * given. The original args are therefore still in
443 return append_edit(argc
, argv
, prefix
);
445 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
446 sha1_to_hex(object
));
449 prepare_note_data(object
, &d
, note
);
450 if (d
.buf
.len
|| allow_empty
) {
451 write_note_data(&d
, new_note
);
452 if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
453 die("BUG: combine_notes_overwrite failed");
454 commit_notes(t
, "Notes added by 'git notes add'");
456 fprintf(stderr
, _("Removing note for object %s\n"),
457 sha1_to_hex(object
));
458 remove_note(t
, object
);
459 commit_notes(t
, "Notes removed by 'git notes add'");
467 static int copy(int argc
, const char **argv
, const char *prefix
)
469 int retval
= 0, force
= 0, from_stdin
= 0;
470 const unsigned char *from_note
, *note
;
471 const char *object_ref
;
472 unsigned char object
[20], from_obj
[20];
473 struct notes_tree
*t
;
474 const char *rewrite_cmd
= NULL
;
475 struct option options
[] = {
476 OPT__FORCE(&force
, N_("replace existing notes")),
477 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
478 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
479 N_("load rewriting config for <command> (implies "
484 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
487 if (from_stdin
|| rewrite_cmd
) {
489 error(_("too many parameters"));
490 usage_with_options(git_notes_copy_usage
, options
);
492 return notes_copy_from_stdin(force
, rewrite_cmd
);
497 error(_("too few parameters"));
498 usage_with_options(git_notes_copy_usage
, options
);
501 error(_("too many parameters"));
502 usage_with_options(git_notes_copy_usage
, options
);
505 if (get_sha1(argv
[0], from_obj
))
506 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
508 object_ref
= 1 < argc
? argv
[1] : "HEAD";
510 if (get_sha1(object_ref
, object
))
511 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
513 t
= init_notes_check("copy");
514 note
= get_note(t
, object
);
518 retval
= error(_("Cannot copy notes. Found existing "
519 "notes for object %s. Use '-f' to "
520 "overwrite existing notes"),
521 sha1_to_hex(object
));
524 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
525 sha1_to_hex(object
));
528 from_note
= get_note(t
, from_obj
);
530 retval
= error(_("Missing notes on source object %s. Cannot "
531 "copy."), sha1_to_hex(from_obj
));
535 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
536 die("BUG: combine_notes_overwrite failed");
537 commit_notes(t
, "Notes added by 'git notes copy'");
543 static int append_edit(int argc
, const char **argv
, const char *prefix
)
546 const char *object_ref
;
547 struct notes_tree
*t
;
548 unsigned char object
[20], new_note
[20];
549 const unsigned char *note
;
551 const char * const *usage
;
552 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
553 struct option options
[] = {
554 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
555 N_("note contents as a string"), PARSE_OPT_NONEG
,
557 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
558 N_("note contents in a file"), PARSE_OPT_NONEG
,
560 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
561 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
563 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
564 N_("reuse specified note object"), PARSE_OPT_NONEG
,
566 OPT_BOOL(0, "allow-empty", &allow_empty
,
567 N_("allow storing empty note")),
570 int edit
= !strcmp(argv
[0], "edit");
572 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
573 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
574 PARSE_OPT_KEEP_ARGV0
);
577 error(_("too many parameters"));
578 usage_with_options(usage
, options
);
582 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
583 "for the 'edit' subcommand.\n"
584 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
586 object_ref
= 1 < argc
? argv
[1] : "HEAD";
588 if (get_sha1(object_ref
, object
))
589 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
591 t
= init_notes_check(argv
[0]);
592 note
= get_note(t
, object
);
594 prepare_note_data(object
, &d
, edit
? note
: NULL
);
597 /* Append buf to previous note contents */
599 enum object_type type
;
600 char *prev_buf
= read_sha1_file(note
, &type
, &size
);
602 strbuf_grow(&d
.buf
, size
+ 1);
603 if (d
.buf
.len
&& prev_buf
&& size
)
604 strbuf_insert(&d
.buf
, 0, "\n", 1);
605 if (prev_buf
&& size
)
606 strbuf_insert(&d
.buf
, 0, prev_buf
, size
);
610 if (d
.buf
.len
|| allow_empty
) {
611 write_note_data(&d
, new_note
);
612 if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
613 die("BUG: combine_notes_overwrite failed");
614 snprintf(logmsg
, sizeof(logmsg
), "Notes added by 'git notes %s'",
617 fprintf(stderr
, _("Removing note for object %s\n"),
618 sha1_to_hex(object
));
619 remove_note(t
, object
);
620 snprintf(logmsg
, sizeof(logmsg
), "Notes removed by 'git notes %s'",
623 commit_notes(t
, logmsg
);
630 static int show(int argc
, const char **argv
, const char *prefix
)
632 const char *object_ref
;
633 struct notes_tree
*t
;
634 unsigned char object
[20];
635 const unsigned char *note
;
637 struct option options
[] = {
641 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
645 error(_("too many parameters"));
646 usage_with_options(git_notes_show_usage
, options
);
649 object_ref
= argc
? argv
[0] : "HEAD";
651 if (get_sha1(object_ref
, object
))
652 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
654 t
= init_notes_check("show");
655 note
= get_note(t
, object
);
658 retval
= error(_("No note found for object %s."),
659 sha1_to_hex(object
));
661 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
662 retval
= execv_git_cmd(show_args
);
668 static int merge_abort(struct notes_merge_options
*o
)
673 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
674 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
677 if (delete_ref("NOTES_MERGE_PARTIAL", NULL
, 0))
678 ret
+= error("Failed to delete ref NOTES_MERGE_PARTIAL");
679 if (delete_ref("NOTES_MERGE_REF", NULL
, REF_NODEREF
))
680 ret
+= error("Failed to delete ref NOTES_MERGE_REF");
681 if (notes_merge_abort(o
))
682 ret
+= error("Failed to remove 'git notes merge' worktree");
686 static int merge_commit(struct notes_merge_options
*o
)
688 struct strbuf msg
= STRBUF_INIT
;
689 unsigned char sha1
[20], parent_sha1
[20];
690 struct notes_tree
*t
;
691 struct commit
*partial
;
692 struct pretty_print_context pretty_ctx
;
693 void *local_ref_to_free
;
697 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
698 * and target notes ref from .git/NOTES_MERGE_REF.
701 if (get_sha1("NOTES_MERGE_PARTIAL", sha1
))
702 die("Failed to read ref NOTES_MERGE_PARTIAL");
703 else if (!(partial
= lookup_commit_reference(sha1
)))
704 die("Could not find commit from NOTES_MERGE_PARTIAL.");
705 else if (parse_commit(partial
))
706 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
708 if (partial
->parents
)
709 hashcpy(parent_sha1
, partial
->parents
->item
->object
.sha1
);
711 hashclr(parent_sha1
);
713 t
= xcalloc(1, sizeof(struct notes_tree
));
714 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
716 o
->local_ref
= local_ref_to_free
=
717 resolve_refdup("NOTES_MERGE_REF", 0, sha1
, NULL
);
719 die("Failed to resolve NOTES_MERGE_REF");
721 if (notes_merge_commit(o
, t
, partial
, sha1
))
722 die("Failed to finalize notes merge");
724 /* Reuse existing commit message in reflog message */
725 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
726 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
728 strbuf_insert(&msg
, 0, "notes: ", 7);
729 update_ref(msg
.buf
, o
->local_ref
, sha1
,
730 is_null_sha1(parent_sha1
) ? NULL
: parent_sha1
,
731 0, UPDATE_REFS_DIE_ON_ERR
);
734 strbuf_release(&msg
);
735 ret
= merge_abort(o
);
736 free(local_ref_to_free
);
740 static int merge(int argc
, const char **argv
, const char *prefix
)
742 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
743 unsigned char result_sha1
[20];
744 struct notes_tree
*t
;
745 struct notes_merge_options o
;
746 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
747 int verbosity
= 0, result
;
748 const char *strategy
= NULL
;
749 struct option options
[] = {
750 OPT_GROUP(N_("General options")),
751 OPT__VERBOSITY(&verbosity
),
752 OPT_GROUP(N_("Merge options")),
753 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
754 N_("resolve notes conflicts using the given strategy "
755 "(manual/ours/theirs/union/cat_sort_uniq)")),
756 OPT_GROUP(N_("Committing unmerged notes")),
757 { OPTION_SET_INT
, 0, "commit", &do_commit
, NULL
,
758 N_("finalize notes merge by committing unmerged notes"),
759 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
760 OPT_GROUP(N_("Aborting notes merge resolution")),
761 { OPTION_SET_INT
, 0, "abort", &do_abort
, NULL
,
762 N_("abort notes merge"),
763 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
767 argc
= parse_options(argc
, argv
, prefix
, options
,
768 git_notes_merge_usage
, 0);
770 if (strategy
|| do_commit
+ do_abort
== 0)
772 if (do_merge
+ do_commit
+ do_abort
!= 1) {
773 error("cannot mix --commit, --abort or -s/--strategy");
774 usage_with_options(git_notes_merge_usage
, options
);
777 if (do_merge
&& argc
!= 1) {
778 error("Must specify a notes ref to merge");
779 usage_with_options(git_notes_merge_usage
, options
);
780 } else if (!do_merge
&& argc
) {
781 error("too many parameters");
782 usage_with_options(git_notes_merge_usage
, options
);
785 init_notes_merge_options(&o
);
786 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
789 return merge_abort(&o
);
791 return merge_commit(&o
);
793 o
.local_ref
= default_notes_ref();
794 strbuf_addstr(&remote_ref
, argv
[0]);
795 expand_notes_ref(&remote_ref
);
796 o
.remote_ref
= remote_ref
.buf
;
799 if (parse_notes_merge_strategy(strategy
, &o
.strategy
)) {
800 error("Unknown -s/--strategy: %s", strategy
);
801 usage_with_options(git_notes_merge_usage
, options
);
805 t
= init_notes_check("merge");
807 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
808 remote_ref
.buf
, default_notes_ref());
809 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
811 result
= notes_merge(&o
, t
, result_sha1
);
813 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
814 /* Update default notes ref with new commit */
815 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
816 0, UPDATE_REFS_DIE_ON_ERR
);
817 else { /* Merge has unresolved conflicts */
818 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
819 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", result_sha1
, NULL
,
820 0, UPDATE_REFS_DIE_ON_ERR
);
821 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
822 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
823 die("Failed to store link to current notes ref (%s)",
824 default_notes_ref());
825 printf("Automatic notes merge failed. Fix conflicts in %s and "
826 "commit the result with 'git notes merge --commit', or "
827 "abort the merge with 'git notes merge --abort'.\n",
828 git_path(NOTES_MERGE_WORKTREE
));
832 strbuf_release(&remote_ref
);
833 strbuf_release(&msg
);
834 return result
< 0; /* return non-zero on conflicts */
837 #define IGNORE_MISSING 1
839 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
842 unsigned char sha1
[20];
843 if (get_sha1(name
, sha1
))
844 return error(_("Failed to resolve '%s' as a valid ref."), name
);
845 status
= remove_note(t
, sha1
);
847 fprintf(stderr
, _("Object %s has no note\n"), name
);
849 fprintf(stderr
, _("Removing note for object %s\n"), name
);
850 return (flag
& IGNORE_MISSING
) ? 0 : status
;
853 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
857 struct option options
[] = {
858 OPT_BIT(0, "ignore-missing", &flag
,
859 N_("attempt to remove non-existent note is not an error"),
861 OPT_BOOL(0, "stdin", &from_stdin
,
862 N_("read object names from the standard input")),
865 struct notes_tree
*t
;
868 argc
= parse_options(argc
, argv
, prefix
, options
,
869 git_notes_remove_usage
, 0);
871 t
= init_notes_check("remove");
873 if (!argc
&& !from_stdin
) {
874 retval
= remove_one_note(t
, "HEAD", flag
);
877 retval
|= remove_one_note(t
, *argv
, flag
);
882 struct strbuf sb
= STRBUF_INIT
;
883 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
885 retval
|= remove_one_note(t
, sb
.buf
, flag
);
890 commit_notes(t
, "Notes removed by 'git notes remove'");
895 static int prune(int argc
, const char **argv
, const char *prefix
)
897 struct notes_tree
*t
;
898 int show_only
= 0, verbose
= 0;
899 struct option options
[] = {
900 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
901 OPT__VERBOSE(&verbose
, "report pruned notes"),
905 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
909 error(_("too many parameters"));
910 usage_with_options(git_notes_prune_usage
, options
);
913 t
= init_notes_check("prune");
915 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
916 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
918 commit_notes(t
, "Notes removed by 'git notes prune'");
923 static int get_ref(int argc
, const char **argv
, const char *prefix
)
925 struct option options
[] = { OPT_END() };
926 argc
= parse_options(argc
, argv
, prefix
, options
,
927 git_notes_get_ref_usage
, 0);
930 error("too many parameters");
931 usage_with_options(git_notes_get_ref_usage
, options
);
934 puts(default_notes_ref());
938 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
941 const char *override_notes_ref
= NULL
;
942 struct option options
[] = {
943 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
944 N_("use notes from <notes-ref>")),
948 git_config(git_default_config
, NULL
);
949 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
950 PARSE_OPT_STOP_AT_NON_OPTION
);
952 if (override_notes_ref
) {
953 struct strbuf sb
= STRBUF_INIT
;
954 strbuf_addstr(&sb
, override_notes_ref
);
955 expand_notes_ref(&sb
);
956 setenv("GIT_NOTES_REF", sb
.buf
, 1);
960 if (argc
< 1 || !strcmp(argv
[0], "list"))
961 result
= list(argc
, argv
, prefix
);
962 else if (!strcmp(argv
[0], "add"))
963 result
= add(argc
, argv
, prefix
);
964 else if (!strcmp(argv
[0], "copy"))
965 result
= copy(argc
, argv
, prefix
);
966 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
967 result
= append_edit(argc
, argv
, prefix
);
968 else if (!strcmp(argv
[0], "show"))
969 result
= show(argc
, argv
, prefix
);
970 else if (!strcmp(argv
[0], "merge"))
971 result
= merge(argc
, argv
, prefix
);
972 else if (!strcmp(argv
[0], "remove"))
973 result
= remove_cmd(argc
, argv
, prefix
);
974 else if (!strcmp(argv
[0], "prune"))
975 result
= prune(argc
, argv
, prefix
);
976 else if (!strcmp(argv
[0], "get-ref"))
977 result
= get_ref(argc
, argv
, prefix
);
979 result
= error(_("Unknown subcommand: %s"), argv
[0]);
980 usage_with_options(git_notes_usage
, options
);
983 return result
? 1 : 0;