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
= CHILD_PROCESS_INIT
;
126 struct strbuf buf
= STRBUF_INIT
;
127 struct strbuf cbuf
= STRBUF_INIT
;
129 /* Invoke "git show --stat --no-notes $object" */
130 show
.argv
= show_args
;
135 if (start_command(&show
))
136 die(_("unable to start 'show' for object '%s'"),
137 sha1_to_hex(object
));
139 if (strbuf_read(&buf
, show
.out
, 0) < 0)
140 die_errno(_("could not read 'show' output"));
141 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
142 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
144 strbuf_release(&cbuf
);
145 strbuf_release(&buf
);
147 if (finish_command(&show
))
148 die(_("failed to finish 'show' for object '%s'"),
149 sha1_to_hex(object
));
152 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
153 int append_only
, const unsigned char *prev
,
154 unsigned char *result
)
158 if (msg
->use_editor
|| !msg
->given
) {
160 struct strbuf buf
= STRBUF_INIT
;
162 /* write the template message before editing: */
163 path
= git_pathdup("NOTES_EDITMSG");
164 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
166 die_errno(_("could not create file '%s'"), path
);
169 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
170 else if (prev
&& !append_only
)
171 write_note_data(fd
, prev
);
173 strbuf_addch(&buf
, '\n');
174 strbuf_add_commented_lines(&buf
, note_template
, strlen(note_template
));
175 strbuf_addch(&buf
, '\n');
176 write_or_die(fd
, buf
.buf
, buf
.len
);
178 write_commented_object(fd
, object
);
181 strbuf_release(&buf
);
182 strbuf_reset(&(msg
->buf
));
184 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
185 die(_("Please supply the note contents using either -m" \
188 stripspace(&(msg
->buf
), 1);
191 if (prev
&& append_only
) {
192 /* Append buf to previous note contents */
194 enum object_type type
;
195 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
197 strbuf_grow(&(msg
->buf
), size
+ 1);
198 if (msg
->buf
.len
&& prev_buf
&& size
)
199 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
200 if (prev_buf
&& size
)
201 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
206 fprintf(stderr
, _("Removing note for object %s\n"),
207 sha1_to_hex(object
));
210 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
211 error(_("unable to write note object"));
213 error(_("The note contents have been left in %s"),
220 unlink_or_warn(path
);
225 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
227 struct msg_arg
*msg
= opt
->value
;
229 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
231 strbuf_addch(&(msg
->buf
), '\n');
232 strbuf_addstr(&(msg
->buf
), arg
);
233 stripspace(&(msg
->buf
), 0);
239 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
241 struct msg_arg
*msg
= opt
->value
;
244 strbuf_addch(&(msg
->buf
), '\n');
245 if (!strcmp(arg
, "-")) {
246 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
247 die_errno(_("cannot read '%s'"), arg
);
248 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
249 die_errno(_("could not open or read '%s'"), arg
);
250 stripspace(&(msg
->buf
), 0);
256 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
258 struct msg_arg
*msg
= opt
->value
;
260 unsigned char object
[20];
261 enum object_type type
;
265 strbuf_addch(&(msg
->buf
), '\n');
267 if (get_sha1(arg
, object
))
268 die(_("Failed to resolve '%s' as a valid ref."), arg
);
269 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
271 die(_("Failed to read object '%s'."), arg
);
273 if (type
!= OBJ_BLOB
) {
275 die(_("Cannot read note data from non-blob object '%s'."), arg
);
277 strbuf_add(&(msg
->buf
), buf
, len
);
284 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
286 struct msg_arg
*msg
= opt
->value
;
288 return parse_reuse_arg(opt
, arg
, unset
);
291 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
293 struct strbuf buf
= STRBUF_INIT
;
294 struct notes_rewrite_cfg
*c
= NULL
;
295 struct notes_tree
*t
= NULL
;
297 const char *msg
= "Notes added by 'git notes copy'";
300 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
304 init_notes(NULL
, NULL
, NULL
, 0);
305 t
= &default_notes_tree
;
308 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
309 unsigned char from_obj
[20], to_obj
[20];
310 struct strbuf
**split
;
313 split
= strbuf_split(&buf
, ' ');
314 if (!split
[0] || !split
[1])
315 die(_("Malformed input line: '%s'."), buf
.buf
);
316 strbuf_rtrim(split
[0]);
317 strbuf_rtrim(split
[1]);
318 if (get_sha1(split
[0]->buf
, from_obj
))
319 die(_("Failed to resolve '%s' as a valid ref."), split
[0]->buf
);
320 if (get_sha1(split
[1]->buf
, to_obj
))
321 die(_("Failed to resolve '%s' as a valid ref."), split
[1]->buf
);
324 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
326 err
= copy_note(t
, from_obj
, to_obj
, force
,
327 combine_notes_overwrite
);
330 error(_("Failed to copy notes from '%s' to '%s'"),
331 split
[0]->buf
, split
[1]->buf
);
335 strbuf_list_free(split
);
339 commit_notes(t
, msg
);
342 finish_copy_notes_for_rewrite(c
, msg
);
347 static struct notes_tree
*init_notes_check(const char *subcommand
)
349 struct notes_tree
*t
;
350 init_notes(NULL
, NULL
, NULL
, 0);
351 t
= &default_notes_tree
;
353 if (!starts_with(t
->ref
, "refs/notes/"))
354 die("Refusing to %s notes in %s (outside of refs/notes/)",
359 static int list(int argc
, const char **argv
, const char *prefix
)
361 struct notes_tree
*t
;
362 unsigned char object
[20];
363 const unsigned char *note
;
365 struct option options
[] = {
370 argc
= parse_options(argc
, argv
, prefix
, options
,
371 git_notes_list_usage
, 0);
374 error(_("too many parameters"));
375 usage_with_options(git_notes_list_usage
, options
);
378 t
= init_notes_check("list");
380 if (get_sha1(argv
[0], object
))
381 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
382 note
= get_note(t
, object
);
384 puts(sha1_to_hex(note
));
387 retval
= error(_("No note found for object %s."),
388 sha1_to_hex(object
));
390 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
396 static int append_edit(int argc
, const char **argv
, const char *prefix
);
398 static int add(int argc
, const char **argv
, const char *prefix
)
400 int retval
= 0, force
= 0;
401 const char *object_ref
;
402 struct notes_tree
*t
;
403 unsigned char object
[20], new_note
[20];
405 const unsigned char *note
;
406 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
407 struct option options
[] = {
408 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
409 N_("note contents as a string"), PARSE_OPT_NONEG
,
411 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
412 N_("note contents in a file"), PARSE_OPT_NONEG
,
414 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
415 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
417 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
418 N_("reuse specified note object"), PARSE_OPT_NONEG
,
420 OPT__FORCE(&force
, N_("replace existing notes")),
424 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
425 PARSE_OPT_KEEP_ARGV0
);
428 error(_("too many parameters"));
429 usage_with_options(git_notes_add_usage
, options
);
432 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
434 if (get_sha1(object_ref
, object
))
435 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
437 t
= init_notes_check("add");
438 note
= get_note(t
, object
);
444 * Redirect to "edit" subcommand.
446 * We only end up here if none of -m/-F/-c/-C
447 * or -f are given. The original args are
448 * therefore still in argv[0-1].
452 return append_edit(argc
, argv
, prefix
);
454 retval
= error(_("Cannot add notes. Found existing notes "
455 "for object %s. Use '-f' to overwrite "
456 "existing notes"), sha1_to_hex(object
));
459 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
460 sha1_to_hex(object
));
463 create_note(object
, &msg
, 0, note
, new_note
);
465 if (is_null_sha1(new_note
))
466 remove_note(t
, object
);
467 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
468 die("BUG: combine_notes_overwrite failed");
470 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
471 is_null_sha1(new_note
) ? "removed" : "added", "add");
472 commit_notes(t
, logmsg
);
475 strbuf_release(&(msg
.buf
));
479 static int copy(int argc
, const char **argv
, const char *prefix
)
481 int retval
= 0, force
= 0, from_stdin
= 0;
482 const unsigned char *from_note
, *note
;
483 const char *object_ref
;
484 unsigned char object
[20], from_obj
[20];
485 struct notes_tree
*t
;
486 const char *rewrite_cmd
= NULL
;
487 struct option options
[] = {
488 OPT__FORCE(&force
, N_("replace existing notes")),
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_sha1(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_sha1(object_ref
, object
))
523 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
525 t
= init_notes_check("copy");
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 sha1_to_hex(object
));
536 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
537 sha1_to_hex(object
));
540 from_note
= get_note(t
, from_obj
);
542 retval
= error(_("Missing notes on source object %s. Cannot "
543 "copy."), sha1_to_hex(from_obj
));
547 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
548 die("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
)
557 const char *object_ref
;
558 struct notes_tree
*t
;
559 unsigned char object
[20], new_note
[20];
560 const unsigned char *note
;
562 const char * const *usage
;
563 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
564 struct option options
[] = {
565 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
566 N_("note contents as a string"), PARSE_OPT_NONEG
,
568 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
569 N_("note contents in a file"), PARSE_OPT_NONEG
,
571 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
572 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
574 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
575 N_("reuse specified note object"), PARSE_OPT_NONEG
,
579 int edit
= !strcmp(argv
[0], "edit");
581 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
582 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
583 PARSE_OPT_KEEP_ARGV0
);
586 error(_("too many parameters"));
587 usage_with_options(usage
, options
);
590 if (msg
.given
&& edit
)
591 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
592 "for the 'edit' subcommand.\n"
593 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
595 object_ref
= 1 < argc
? argv
[1] : "HEAD";
597 if (get_sha1(object_ref
, object
))
598 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
600 t
= init_notes_check(argv
[0]);
601 note
= get_note(t
, object
);
603 create_note(object
, &msg
, !edit
, note
, new_note
);
605 if (is_null_sha1(new_note
))
606 remove_note(t
, object
);
607 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
608 die("BUG: combine_notes_overwrite failed");
610 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
611 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
612 commit_notes(t
, logmsg
);
614 strbuf_release(&(msg
.buf
));
618 static int show(int argc
, const char **argv
, const char *prefix
)
620 const char *object_ref
;
621 struct notes_tree
*t
;
622 unsigned char object
[20];
623 const unsigned char *note
;
625 struct option options
[] = {
629 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
633 error(_("too many parameters"));
634 usage_with_options(git_notes_show_usage
, options
);
637 object_ref
= argc
? argv
[0] : "HEAD";
639 if (get_sha1(object_ref
, object
))
640 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
642 t
= init_notes_check("show");
643 note
= get_note(t
, object
);
646 retval
= error(_("No note found for object %s."),
647 sha1_to_hex(object
));
649 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
650 retval
= execv_git_cmd(show_args
);
656 static int merge_abort(struct notes_merge_options
*o
)
661 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
662 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
665 if (delete_ref("NOTES_MERGE_PARTIAL", NULL
, 0))
666 ret
+= error("Failed to delete ref NOTES_MERGE_PARTIAL");
667 if (delete_ref("NOTES_MERGE_REF", NULL
, REF_NODEREF
))
668 ret
+= error("Failed to delete ref NOTES_MERGE_REF");
669 if (notes_merge_abort(o
))
670 ret
+= error("Failed to remove 'git notes merge' worktree");
674 static int merge_commit(struct notes_merge_options
*o
)
676 struct strbuf msg
= STRBUF_INIT
;
677 unsigned char sha1
[20], parent_sha1
[20];
678 struct notes_tree
*t
;
679 struct commit
*partial
;
680 struct pretty_print_context pretty_ctx
;
681 void *local_ref_to_free
;
685 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
686 * and target notes ref from .git/NOTES_MERGE_REF.
689 if (get_sha1("NOTES_MERGE_PARTIAL", sha1
))
690 die("Failed to read ref NOTES_MERGE_PARTIAL");
691 else if (!(partial
= lookup_commit_reference(sha1
)))
692 die("Could not find commit from NOTES_MERGE_PARTIAL.");
693 else if (parse_commit(partial
))
694 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
696 if (partial
->parents
)
697 hashcpy(parent_sha1
, partial
->parents
->item
->object
.sha1
);
699 hashclr(parent_sha1
);
701 t
= xcalloc(1, sizeof(struct notes_tree
));
702 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
704 o
->local_ref
= local_ref_to_free
=
705 resolve_refdup("NOTES_MERGE_REF", sha1
, 0, NULL
);
707 die("Failed to resolve NOTES_MERGE_REF");
709 if (notes_merge_commit(o
, t
, partial
, sha1
))
710 die("Failed to finalize notes merge");
712 /* Reuse existing commit message in reflog message */
713 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
714 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
716 strbuf_insert(&msg
, 0, "notes: ", 7);
717 update_ref(msg
.buf
, o
->local_ref
, sha1
,
718 is_null_sha1(parent_sha1
) ? NULL
: parent_sha1
,
719 0, UPDATE_REFS_DIE_ON_ERR
);
722 strbuf_release(&msg
);
723 ret
= merge_abort(o
);
724 free(local_ref_to_free
);
728 static int merge(int argc
, const char **argv
, const char *prefix
)
730 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
731 unsigned char result_sha1
[20];
732 struct notes_tree
*t
;
733 struct notes_merge_options o
;
734 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
735 int verbosity
= 0, result
;
736 const char *strategy
= NULL
;
737 struct option options
[] = {
738 OPT_GROUP(N_("General options")),
739 OPT__VERBOSITY(&verbosity
),
740 OPT_GROUP(N_("Merge options")),
741 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
742 N_("resolve notes conflicts using the given strategy "
743 "(manual/ours/theirs/union/cat_sort_uniq)")),
744 OPT_GROUP(N_("Committing unmerged notes")),
745 { OPTION_SET_INT
, 0, "commit", &do_commit
, NULL
,
746 N_("finalize notes merge by committing unmerged notes"),
747 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
748 OPT_GROUP(N_("Aborting notes merge resolution")),
749 { OPTION_SET_INT
, 0, "abort", &do_abort
, NULL
,
750 N_("abort notes merge"),
751 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
755 argc
= parse_options(argc
, argv
, prefix
, options
,
756 git_notes_merge_usage
, 0);
758 if (strategy
|| do_commit
+ do_abort
== 0)
760 if (do_merge
+ do_commit
+ do_abort
!= 1) {
761 error("cannot mix --commit, --abort or -s/--strategy");
762 usage_with_options(git_notes_merge_usage
, options
);
765 if (do_merge
&& argc
!= 1) {
766 error("Must specify a notes ref to merge");
767 usage_with_options(git_notes_merge_usage
, options
);
768 } else if (!do_merge
&& argc
) {
769 error("too many parameters");
770 usage_with_options(git_notes_merge_usage
, options
);
773 init_notes_merge_options(&o
);
774 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
777 return merge_abort(&o
);
779 return merge_commit(&o
);
781 o
.local_ref
= default_notes_ref();
782 strbuf_addstr(&remote_ref
, argv
[0]);
783 expand_notes_ref(&remote_ref
);
784 o
.remote_ref
= remote_ref
.buf
;
787 if (!strcmp(strategy
, "manual"))
788 o
.strategy
= NOTES_MERGE_RESOLVE_MANUAL
;
789 else if (!strcmp(strategy
, "ours"))
790 o
.strategy
= NOTES_MERGE_RESOLVE_OURS
;
791 else if (!strcmp(strategy
, "theirs"))
792 o
.strategy
= NOTES_MERGE_RESOLVE_THEIRS
;
793 else if (!strcmp(strategy
, "union"))
794 o
.strategy
= NOTES_MERGE_RESOLVE_UNION
;
795 else if (!strcmp(strategy
, "cat_sort_uniq"))
796 o
.strategy
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
798 error("Unknown -s/--strategy: %s", strategy
);
799 usage_with_options(git_notes_merge_usage
, options
);
803 t
= init_notes_check("merge");
805 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
806 remote_ref
.buf
, default_notes_ref());
807 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
809 result
= notes_merge(&o
, t
, result_sha1
);
811 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
812 /* Update default notes ref with new commit */
813 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
814 0, UPDATE_REFS_DIE_ON_ERR
);
815 else { /* Merge has unresolved conflicts */
816 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
817 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", result_sha1
, NULL
,
818 0, UPDATE_REFS_DIE_ON_ERR
);
819 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
820 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
821 die("Failed to store link to current notes ref (%s)",
822 default_notes_ref());
823 printf("Automatic notes merge failed. Fix conflicts in %s and "
824 "commit the result with 'git notes merge --commit', or "
825 "abort the merge with 'git notes merge --abort'.\n",
826 git_path(NOTES_MERGE_WORKTREE
));
830 strbuf_release(&remote_ref
);
831 strbuf_release(&msg
);
832 return result
< 0; /* return non-zero on conflicts */
835 #define IGNORE_MISSING 1
837 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
840 unsigned char sha1
[20];
841 if (get_sha1(name
, sha1
))
842 return error(_("Failed to resolve '%s' as a valid ref."), name
);
843 status
= remove_note(t
, sha1
);
845 fprintf(stderr
, _("Object %s has no note\n"), name
);
847 fprintf(stderr
, _("Removing note for object %s\n"), name
);
848 return (flag
& IGNORE_MISSING
) ? 0 : status
;
851 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
855 struct option options
[] = {
856 OPT_BIT(0, "ignore-missing", &flag
,
857 N_("attempt to remove non-existent note is not an error"),
859 OPT_BOOL(0, "stdin", &from_stdin
,
860 N_("read object names from the standard input")),
863 struct notes_tree
*t
;
866 argc
= parse_options(argc
, argv
, prefix
, options
,
867 git_notes_remove_usage
, 0);
869 t
= init_notes_check("remove");
871 if (!argc
&& !from_stdin
) {
872 retval
= remove_one_note(t
, "HEAD", flag
);
875 retval
|= remove_one_note(t
, *argv
, flag
);
880 struct strbuf sb
= STRBUF_INIT
;
881 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
883 retval
|= remove_one_note(t
, sb
.buf
, flag
);
888 commit_notes(t
, "Notes removed by 'git notes remove'");
893 static int prune(int argc
, const char **argv
, const char *prefix
)
895 struct notes_tree
*t
;
896 int show_only
= 0, verbose
= 0;
897 struct option options
[] = {
898 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
899 OPT__VERBOSE(&verbose
, "report pruned notes"),
903 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
907 error(_("too many parameters"));
908 usage_with_options(git_notes_prune_usage
, options
);
911 t
= init_notes_check("prune");
913 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
914 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
916 commit_notes(t
, "Notes removed by 'git notes prune'");
921 static int get_ref(int argc
, const char **argv
, const char *prefix
)
923 struct option options
[] = { OPT_END() };
924 argc
= parse_options(argc
, argv
, prefix
, options
,
925 git_notes_get_ref_usage
, 0);
928 error("too many parameters");
929 usage_with_options(git_notes_get_ref_usage
, options
);
932 puts(default_notes_ref());
936 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
939 const char *override_notes_ref
= NULL
;
940 struct option options
[] = {
941 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
942 N_("use notes from <notes_ref>")),
946 git_config(git_default_config
, NULL
);
947 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
948 PARSE_OPT_STOP_AT_NON_OPTION
);
950 if (override_notes_ref
) {
951 struct strbuf sb
= STRBUF_INIT
;
952 strbuf_addstr(&sb
, override_notes_ref
);
953 expand_notes_ref(&sb
);
954 setenv("GIT_NOTES_REF", sb
.buf
, 1);
958 if (argc
< 1 || !strcmp(argv
[0], "list"))
959 result
= list(argc
, argv
, prefix
);
960 else if (!strcmp(argv
[0], "add"))
961 result
= add(argc
, argv
, prefix
);
962 else if (!strcmp(argv
[0], "copy"))
963 result
= copy(argc
, argv
, prefix
);
964 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
965 result
= append_edit(argc
, argv
, prefix
);
966 else if (!strcmp(argv
[0], "show"))
967 result
= show(argc
, argv
, prefix
);
968 else if (!strcmp(argv
[0], "merge"))
969 result
= merge(argc
, argv
, prefix
);
970 else if (!strcmp(argv
[0], "remove"))
971 result
= remove_cmd(argc
, argv
, prefix
);
972 else if (!strcmp(argv
[0], "prune"))
973 result
= prune(argc
, argv
, prefix
);
974 else if (!strcmp(argv
[0], "get-ref"))
975 result
= get_ref(argc
, argv
, prefix
);
977 result
= error(_("Unknown subcommand: %s"), argv
[0]);
978 usage_with_options(git_notes_usage
, options
);
981 return result
? 1 : 0;