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] [-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 [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] edit [<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";
101 static int list_each_note(const unsigned char *object_sha1
,
102 const unsigned char *note_sha1
, char *note_path
,
105 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
109 static void write_note_data(int fd
, const unsigned char *sha1
)
112 enum object_type type
;
113 char *buf
= read_sha1_file(sha1
, &type
, &size
);
116 write_or_die(fd
, buf
, size
);
121 static void write_commented_object(int fd
, const unsigned char *object
)
123 const char *show_args
[5] =
124 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
125 struct child_process show
;
126 struct strbuf buf
= STRBUF_INIT
;
127 struct strbuf cbuf
= STRBUF_INIT
;
129 /* Invoke "git show --stat --no-notes $object" */
130 memset(&show
, 0, sizeof(show
));
131 show
.argv
= show_args
;
136 if (start_command(&show
))
137 die(_("unable to start 'show' for object '%s'"),
138 sha1_to_hex(object
));
140 if (strbuf_read(&buf
, show
.out
, 0) < 0)
141 die_errno(_("could not read 'show' output"));
142 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
143 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
145 strbuf_release(&cbuf
);
146 strbuf_release(&buf
);
148 if (finish_command(&show
))
149 die(_("failed to finish 'show' for object '%s'"),
150 sha1_to_hex(object
));
153 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
154 int append_only
, const unsigned char *prev
,
155 unsigned char *result
)
159 if (msg
->use_editor
|| !msg
->given
) {
161 struct strbuf buf
= STRBUF_INIT
;
163 /* write the template message before editing: */
164 path
= git_pathdup("NOTES_EDITMSG");
165 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
167 die_errno(_("could not create file '%s'"), path
);
170 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
171 else if (prev
&& !append_only
)
172 write_note_data(fd
, prev
);
174 strbuf_addch(&buf
, '\n');
175 strbuf_add_commented_lines(&buf
, note_template
, strlen(note_template
));
176 strbuf_addch(&buf
, '\n');
177 write_or_die(fd
, buf
.buf
, buf
.len
);
179 write_commented_object(fd
, object
);
182 strbuf_release(&buf
);
183 strbuf_reset(&(msg
->buf
));
185 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
186 die(_("Please supply the note contents using either -m" \
189 stripspace(&(msg
->buf
), 1);
192 if (prev
&& append_only
) {
193 /* Append buf to previous note contents */
195 enum object_type type
;
196 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
198 strbuf_grow(&(msg
->buf
), size
+ 1);
199 if (msg
->buf
.len
&& prev_buf
&& size
)
200 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
201 if (prev_buf
&& size
)
202 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
207 fprintf(stderr
, _("Removing note for object %s\n"),
208 sha1_to_hex(object
));
211 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
212 error(_("unable to write note object"));
214 error(_("The note contents has been left in %s"),
221 unlink_or_warn(path
);
226 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
228 struct msg_arg
*msg
= opt
->value
;
230 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
232 strbuf_addch(&(msg
->buf
), '\n');
233 strbuf_addstr(&(msg
->buf
), arg
);
234 stripspace(&(msg
->buf
), 0);
240 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
242 struct msg_arg
*msg
= opt
->value
;
245 strbuf_addch(&(msg
->buf
), '\n');
246 if (!strcmp(arg
, "-")) {
247 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
248 die_errno(_("cannot read '%s'"), arg
);
249 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
250 die_errno(_("could not open or read '%s'"), arg
);
251 stripspace(&(msg
->buf
), 0);
257 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
259 struct msg_arg
*msg
= opt
->value
;
261 unsigned char object
[20];
262 enum object_type type
;
266 strbuf_addch(&(msg
->buf
), '\n');
268 if (get_sha1(arg
, object
))
269 die(_("Failed to resolve '%s' as a valid ref."), arg
);
270 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
272 die(_("Failed to read object '%s'."), arg
);;
274 strbuf_add(&(msg
->buf
), buf
, len
);
281 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
283 struct msg_arg
*msg
= opt
->value
;
285 return parse_reuse_arg(opt
, arg
, unset
);
288 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
290 struct strbuf buf
= STRBUF_INIT
;
291 struct notes_rewrite_cfg
*c
= NULL
;
292 struct notes_tree
*t
= NULL
;
294 const char *msg
= "Notes added by 'git notes copy'";
297 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
301 init_notes(NULL
, NULL
, NULL
, 0);
302 t
= &default_notes_tree
;
305 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
306 unsigned char from_obj
[20], to_obj
[20];
307 struct strbuf
**split
;
310 split
= strbuf_split(&buf
, ' ');
311 if (!split
[0] || !split
[1])
312 die(_("Malformed input line: '%s'."), buf
.buf
);
313 strbuf_rtrim(split
[0]);
314 strbuf_rtrim(split
[1]);
315 if (get_sha1(split
[0]->buf
, from_obj
))
316 die(_("Failed to resolve '%s' as a valid ref."), split
[0]->buf
);
317 if (get_sha1(split
[1]->buf
, to_obj
))
318 die(_("Failed to resolve '%s' as a valid ref."), split
[1]->buf
);
321 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
323 err
= copy_note(t
, from_obj
, to_obj
, force
,
324 combine_notes_overwrite
);
327 error(_("Failed to copy notes from '%s' to '%s'"),
328 split
[0]->buf
, split
[1]->buf
);
332 strbuf_list_free(split
);
336 commit_notes(t
, msg
);
339 finish_copy_notes_for_rewrite(c
, msg
);
344 static struct notes_tree
*init_notes_check(const char *subcommand
)
346 struct notes_tree
*t
;
347 init_notes(NULL
, NULL
, NULL
, 0);
348 t
= &default_notes_tree
;
350 if (prefixcmp(t
->ref
, "refs/notes/"))
351 die("Refusing to %s notes in %s (outside of refs/notes/)",
356 static int list(int argc
, const char **argv
, const char *prefix
)
358 struct notes_tree
*t
;
359 unsigned char object
[20];
360 const unsigned char *note
;
362 struct option options
[] = {
367 argc
= parse_options(argc
, argv
, prefix
, options
,
368 git_notes_list_usage
, 0);
371 error(_("too many parameters"));
372 usage_with_options(git_notes_list_usage
, options
);
375 t
= init_notes_check("list");
377 if (get_sha1(argv
[0], object
))
378 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
379 note
= get_note(t
, object
);
381 puts(sha1_to_hex(note
));
384 retval
= error(_("No note found for object %s."),
385 sha1_to_hex(object
));
387 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
393 static int append_edit(int argc
, const char **argv
, const char *prefix
);
395 static int add(int argc
, const char **argv
, const char *prefix
)
397 int retval
= 0, force
= 0;
398 const char *object_ref
;
399 struct notes_tree
*t
;
400 unsigned char object
[20], new_note
[20];
402 const unsigned char *note
;
403 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
404 struct option options
[] = {
405 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
406 N_("note contents as a string"), PARSE_OPT_NONEG
,
408 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
409 N_("note contents in a file"), PARSE_OPT_NONEG
,
411 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
412 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
414 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
415 N_("reuse specified note object"), PARSE_OPT_NONEG
,
417 OPT__FORCE(&force
, N_("replace existing notes")),
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_sha1(object_ref
, object
))
432 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
434 t
= init_notes_check("add");
435 note
= get_note(t
, object
);
441 * Redirect to "edit" subcommand.
443 * We only end up here if none of -m/-F/-c/-C
444 * or -f are given. The original args are
445 * therefore still in argv[0-1].
449 return append_edit(argc
, argv
, prefix
);
451 retval
= error(_("Cannot add notes. Found existing notes "
452 "for object %s. Use '-f' to overwrite "
453 "existing notes"), sha1_to_hex(object
));
456 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
457 sha1_to_hex(object
));
460 create_note(object
, &msg
, 0, note
, new_note
);
462 if (is_null_sha1(new_note
))
463 remove_note(t
, object
);
464 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
465 die("BUG: combine_notes_overwrite failed");
467 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
468 is_null_sha1(new_note
) ? "removed" : "added", "add");
469 commit_notes(t
, logmsg
);
472 strbuf_release(&(msg
.buf
));
476 static int copy(int argc
, const char **argv
, const char *prefix
)
478 int retval
= 0, force
= 0, from_stdin
= 0;
479 const unsigned char *from_note
, *note
;
480 const char *object_ref
;
481 unsigned char object
[20], from_obj
[20];
482 struct notes_tree
*t
;
483 const char *rewrite_cmd
= NULL
;
484 struct option options
[] = {
485 OPT__FORCE(&force
, N_("replace existing notes")),
486 OPT_BOOLEAN(0, "stdin", &from_stdin
, N_("read objects from stdin")),
487 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
488 N_("load rewriting config for <command> (implies "
493 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
496 if (from_stdin
|| rewrite_cmd
) {
498 error(_("too many parameters"));
499 usage_with_options(git_notes_copy_usage
, options
);
501 return notes_copy_from_stdin(force
, rewrite_cmd
);
506 error(_("too few parameters"));
507 usage_with_options(git_notes_copy_usage
, options
);
510 error(_("too many parameters"));
511 usage_with_options(git_notes_copy_usage
, options
);
514 if (get_sha1(argv
[0], from_obj
))
515 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
517 object_ref
= 1 < argc
? argv
[1] : "HEAD";
519 if (get_sha1(object_ref
, object
))
520 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
522 t
= init_notes_check("copy");
523 note
= get_note(t
, object
);
527 retval
= error(_("Cannot copy notes. Found existing "
528 "notes for object %s. Use '-f' to "
529 "overwrite existing notes"),
530 sha1_to_hex(object
));
533 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
534 sha1_to_hex(object
));
537 from_note
= get_note(t
, from_obj
);
539 retval
= error(_("Missing notes on source object %s. Cannot "
540 "copy."), sha1_to_hex(from_obj
));
544 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
545 die("BUG: combine_notes_overwrite failed");
546 commit_notes(t
, "Notes added by 'git notes copy'");
552 static int append_edit(int argc
, const char **argv
, const char *prefix
)
554 const char *object_ref
;
555 struct notes_tree
*t
;
556 unsigned char object
[20], new_note
[20];
557 const unsigned char *note
;
559 const char * const *usage
;
560 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
561 struct option options
[] = {
562 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
563 N_("note contents as a string"), PARSE_OPT_NONEG
,
565 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
566 N_("note contents in a file"), PARSE_OPT_NONEG
,
568 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
569 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
571 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
572 N_("reuse specified note object"), PARSE_OPT_NONEG
,
576 int edit
= !strcmp(argv
[0], "edit");
578 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
579 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
580 PARSE_OPT_KEEP_ARGV0
);
583 error(_("too many parameters"));
584 usage_with_options(usage
, options
);
587 if (msg
.given
&& edit
)
588 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
589 "for the 'edit' subcommand.\n"
590 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
592 object_ref
= 1 < argc
? argv
[1] : "HEAD";
594 if (get_sha1(object_ref
, object
))
595 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
597 t
= init_notes_check(argv
[0]);
598 note
= get_note(t
, object
);
600 create_note(object
, &msg
, !edit
, note
, new_note
);
602 if (is_null_sha1(new_note
))
603 remove_note(t
, object
);
604 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
605 die("BUG: combine_notes_overwrite failed");
607 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
608 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
609 commit_notes(t
, logmsg
);
611 strbuf_release(&(msg
.buf
));
615 static int show(int argc
, const char **argv
, const char *prefix
)
617 const char *object_ref
;
618 struct notes_tree
*t
;
619 unsigned char object
[20];
620 const unsigned char *note
;
622 struct option options
[] = {
626 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
630 error(_("too many parameters"));
631 usage_with_options(git_notes_show_usage
, options
);
634 object_ref
= argc
? argv
[0] : "HEAD";
636 if (get_sha1(object_ref
, object
))
637 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
639 t
= init_notes_check("show");
640 note
= get_note(t
, object
);
643 retval
= error(_("No note found for object %s."),
644 sha1_to_hex(object
));
646 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
647 retval
= execv_git_cmd(show_args
);
653 static int merge_abort(struct notes_merge_options
*o
)
658 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
659 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
662 if (delete_ref("NOTES_MERGE_PARTIAL", NULL
, 0))
663 ret
+= error("Failed to delete ref NOTES_MERGE_PARTIAL");
664 if (delete_ref("NOTES_MERGE_REF", NULL
, REF_NODEREF
))
665 ret
+= error("Failed to delete ref NOTES_MERGE_REF");
666 if (notes_merge_abort(o
))
667 ret
+= error("Failed to remove 'git notes merge' worktree");
671 static int merge_commit(struct notes_merge_options
*o
)
673 struct strbuf msg
= STRBUF_INIT
;
674 unsigned char sha1
[20], parent_sha1
[20];
675 struct notes_tree
*t
;
676 struct commit
*partial
;
677 struct pretty_print_context pretty_ctx
;
678 void *local_ref_to_free
;
682 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
683 * and target notes ref from .git/NOTES_MERGE_REF.
686 if (get_sha1("NOTES_MERGE_PARTIAL", sha1
))
687 die("Failed to read ref NOTES_MERGE_PARTIAL");
688 else if (!(partial
= lookup_commit_reference(sha1
)))
689 die("Could not find commit from NOTES_MERGE_PARTIAL.");
690 else if (parse_commit(partial
))
691 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
693 if (partial
->parents
)
694 hashcpy(parent_sha1
, partial
->parents
->item
->object
.sha1
);
696 hashclr(parent_sha1
);
698 t
= xcalloc(1, sizeof(struct notes_tree
));
699 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
701 o
->local_ref
= local_ref_to_free
=
702 resolve_refdup("NOTES_MERGE_REF", sha1
, 0, NULL
);
704 die("Failed to resolve NOTES_MERGE_REF");
706 if (notes_merge_commit(o
, t
, partial
, sha1
))
707 die("Failed to finalize notes merge");
709 /* Reuse existing commit message in reflog message */
710 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
711 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
713 strbuf_insert(&msg
, 0, "notes: ", 7);
714 update_ref(msg
.buf
, o
->local_ref
, sha1
,
715 is_null_sha1(parent_sha1
) ? NULL
: parent_sha1
,
719 strbuf_release(&msg
);
720 ret
= merge_abort(o
);
721 free(local_ref_to_free
);
725 static int merge(int argc
, const char **argv
, const char *prefix
)
727 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
728 unsigned char result_sha1
[20];
729 struct notes_tree
*t
;
730 struct notes_merge_options o
;
731 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
732 int verbosity
= 0, result
;
733 const char *strategy
= NULL
;
734 struct option options
[] = {
735 OPT_GROUP(N_("General options")),
736 OPT__VERBOSITY(&verbosity
),
737 OPT_GROUP(N_("Merge options")),
738 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
739 N_("resolve notes conflicts using the given strategy "
740 "(manual/ours/theirs/union/cat_sort_uniq)")),
741 OPT_GROUP(N_("Committing unmerged notes")),
742 { OPTION_BOOLEAN
, 0, "commit", &do_commit
, NULL
,
743 N_("finalize notes merge by committing unmerged notes"),
744 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
745 OPT_GROUP(N_("Aborting notes merge resolution")),
746 { OPTION_BOOLEAN
, 0, "abort", &do_abort
, NULL
,
747 N_("abort notes merge"),
748 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
752 argc
= parse_options(argc
, argv
, prefix
, options
,
753 git_notes_merge_usage
, 0);
755 if (strategy
|| do_commit
+ do_abort
== 0)
757 if (do_merge
+ do_commit
+ do_abort
!= 1) {
758 error("cannot mix --commit, --abort or -s/--strategy");
759 usage_with_options(git_notes_merge_usage
, options
);
762 if (do_merge
&& argc
!= 1) {
763 error("Must specify a notes ref to merge");
764 usage_with_options(git_notes_merge_usage
, options
);
765 } else if (!do_merge
&& argc
) {
766 error("too many parameters");
767 usage_with_options(git_notes_merge_usage
, options
);
770 init_notes_merge_options(&o
);
771 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
774 return merge_abort(&o
);
776 return merge_commit(&o
);
778 o
.local_ref
= default_notes_ref();
779 strbuf_addstr(&remote_ref
, argv
[0]);
780 expand_notes_ref(&remote_ref
);
781 o
.remote_ref
= remote_ref
.buf
;
784 if (!strcmp(strategy
, "manual"))
785 o
.strategy
= NOTES_MERGE_RESOLVE_MANUAL
;
786 else if (!strcmp(strategy
, "ours"))
787 o
.strategy
= NOTES_MERGE_RESOLVE_OURS
;
788 else if (!strcmp(strategy
, "theirs"))
789 o
.strategy
= NOTES_MERGE_RESOLVE_THEIRS
;
790 else if (!strcmp(strategy
, "union"))
791 o
.strategy
= NOTES_MERGE_RESOLVE_UNION
;
792 else if (!strcmp(strategy
, "cat_sort_uniq"))
793 o
.strategy
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
795 error("Unknown -s/--strategy: %s", strategy
);
796 usage_with_options(git_notes_merge_usage
, options
);
800 t
= init_notes_check("merge");
802 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
803 remote_ref
.buf
, default_notes_ref());
804 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
806 result
= notes_merge(&o
, t
, result_sha1
);
808 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
809 /* Update default notes ref with new commit */
810 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
812 else { /* Merge has unresolved conflicts */
813 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
814 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", result_sha1
, NULL
,
816 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
817 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
818 die("Failed to store link to current notes ref (%s)",
819 default_notes_ref());
820 printf("Automatic notes merge failed. Fix conflicts in %s and "
821 "commit the result with 'git notes merge --commit', or "
822 "abort the merge with 'git notes merge --abort'.\n",
823 git_path(NOTES_MERGE_WORKTREE
));
827 strbuf_release(&remote_ref
);
828 strbuf_release(&msg
);
829 return result
< 0; /* return non-zero on conflicts */
832 #define IGNORE_MISSING 1
834 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
837 unsigned char sha1
[20];
838 if (get_sha1(name
, sha1
))
839 return error(_("Failed to resolve '%s' as a valid ref."), name
);
840 status
= remove_note(t
, sha1
);
842 fprintf(stderr
, _("Object %s has no note\n"), name
);
844 fprintf(stderr
, _("Removing note for object %s\n"), name
);
845 return (flag
& IGNORE_MISSING
) ? 0 : status
;
848 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
852 struct option options
[] = {
853 OPT_BIT(0, "ignore-missing", &flag
,
854 N_("attempt to remove non-existent note is not an error"),
856 OPT_BOOLEAN(0, "stdin", &from_stdin
,
857 N_("read object names from the standard input")),
860 struct notes_tree
*t
;
863 argc
= parse_options(argc
, argv
, prefix
, options
,
864 git_notes_remove_usage
, 0);
866 t
= init_notes_check("remove");
868 if (!argc
&& !from_stdin
) {
869 retval
= remove_one_note(t
, "HEAD", flag
);
872 retval
|= remove_one_note(t
, *argv
, flag
);
877 struct strbuf sb
= STRBUF_INIT
;
878 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
880 retval
|= remove_one_note(t
, sb
.buf
, flag
);
885 commit_notes(t
, "Notes removed by 'git notes remove'");
890 static int prune(int argc
, const char **argv
, const char *prefix
)
892 struct notes_tree
*t
;
893 int show_only
= 0, verbose
= 0;
894 struct option options
[] = {
895 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
896 OPT__VERBOSE(&verbose
, "report pruned notes"),
900 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
904 error(_("too many parameters"));
905 usage_with_options(git_notes_prune_usage
, options
);
908 t
= init_notes_check("prune");
910 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
911 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
913 commit_notes(t
, "Notes removed by 'git notes prune'");
918 static int get_ref(int argc
, const char **argv
, const char *prefix
)
920 struct option options
[] = { OPT_END() };
921 argc
= parse_options(argc
, argv
, prefix
, options
,
922 git_notes_get_ref_usage
, 0);
925 error("too many parameters");
926 usage_with_options(git_notes_get_ref_usage
, options
);
929 puts(default_notes_ref());
933 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
936 const char *override_notes_ref
= NULL
;
937 struct option options
[] = {
938 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes_ref"),
939 N_("use notes from <notes_ref>")),
943 git_config(git_default_config
, NULL
);
944 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
945 PARSE_OPT_STOP_AT_NON_OPTION
);
947 if (override_notes_ref
) {
948 struct strbuf sb
= STRBUF_INIT
;
949 strbuf_addstr(&sb
, override_notes_ref
);
950 expand_notes_ref(&sb
);
951 setenv("GIT_NOTES_REF", sb
.buf
, 1);
955 if (argc
< 1 || !strcmp(argv
[0], "list"))
956 result
= list(argc
, argv
, prefix
);
957 else if (!strcmp(argv
[0], "add"))
958 result
= add(argc
, argv
, prefix
);
959 else if (!strcmp(argv
[0], "copy"))
960 result
= copy(argc
, argv
, prefix
);
961 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
962 result
= append_edit(argc
, argv
, prefix
);
963 else if (!strcmp(argv
[0], "show"))
964 result
= show(argc
, argv
, prefix
);
965 else if (!strcmp(argv
[0], "merge"))
966 result
= merge(argc
, argv
, prefix
);
967 else if (!strcmp(argv
[0], "remove"))
968 result
= remove_cmd(argc
, argv
, prefix
);
969 else if (!strcmp(argv
[0], "prune"))
970 result
= prune(argc
, argv
, prefix
);
971 else if (!strcmp(argv
[0], "get-ref"))
972 result
= get_ref(argc
, argv
, prefix
);
974 result
= error(_("Unknown subcommand: %s"), argv
[0]);
975 usage_with_options(git_notes_usage
, options
);
978 return result
? 1 : 0;