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";
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 create_note(const unsigned char *object
, struct note_data
*d
,
163 int append_only
, const unsigned char *prev
,
164 unsigned char *result
)
166 if (d
->use_editor
|| !d
->given
) {
168 struct strbuf buf
= STRBUF_INIT
;
170 /* write the template message before editing: */
171 d
->edit_path
= git_pathdup("NOTES_EDITMSG");
172 fd
= open(d
->edit_path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
174 die_errno(_("could not create file '%s'"), d
->edit_path
);
177 write_or_die(fd
, d
->buf
.buf
, d
->buf
.len
);
178 else if (prev
&& !append_only
)
179 copy_obj_to_fd(fd
, prev
);
181 strbuf_addch(&buf
, '\n');
182 strbuf_add_commented_lines(&buf
, note_template
, strlen(note_template
));
183 strbuf_addch(&buf
, '\n');
184 write_or_die(fd
, buf
.buf
, buf
.len
);
186 write_commented_object(fd
, object
);
189 strbuf_release(&buf
);
190 strbuf_reset(&d
->buf
);
192 if (launch_editor(d
->edit_path
, &d
->buf
, NULL
)) {
193 die(_("Please supply the note contents using either -m or -F option"));
195 stripspace(&d
->buf
, 1);
198 if (prev
&& append_only
) {
199 /* Append buf to previous note contents */
201 enum object_type type
;
202 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
204 strbuf_grow(&d
->buf
, size
+ 1);
205 if (d
->buf
.len
&& prev_buf
&& size
)
206 strbuf_insert(&d
->buf
, 0, "\n", 1);
207 if (prev_buf
&& size
)
208 strbuf_insert(&d
->buf
, 0, prev_buf
, size
);
213 fprintf(stderr
, _("Removing note for object %s\n"),
214 sha1_to_hex(object
));
217 if (write_sha1_file(d
->buf
.buf
, d
->buf
.len
, blob_type
, result
)) {
218 error(_("unable to write note object"));
220 error(_("The note contents have been left in %s"),
227 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
229 struct note_data
*d
= opt
->value
;
231 strbuf_grow(&d
->buf
, strlen(arg
) + 2);
233 strbuf_addch(&d
->buf
, '\n');
234 strbuf_addstr(&d
->buf
, arg
);
235 stripspace(&d
->buf
, 0);
241 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
243 struct note_data
*d
= opt
->value
;
246 strbuf_addch(&d
->buf
, '\n');
247 if (!strcmp(arg
, "-")) {
248 if (strbuf_read(&d
->buf
, 0, 1024) < 0)
249 die_errno(_("cannot read '%s'"), arg
);
250 } else if (strbuf_read_file(&d
->buf
, arg
, 1024) < 0)
251 die_errno(_("could not open or read '%s'"), arg
);
252 stripspace(&d
->buf
, 0);
258 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
260 struct note_data
*d
= opt
->value
;
262 unsigned char object
[20];
263 enum object_type type
;
267 strbuf_addch(&d
->buf
, '\n');
269 if (get_sha1(arg
, object
))
270 die(_("Failed to resolve '%s' as a valid ref."), arg
);
271 if (!(buf
= read_sha1_file(object
, &type
, &len
))) {
273 die(_("Failed to read object '%s'."), arg
);
275 if (type
!= OBJ_BLOB
) {
277 die(_("Cannot read note data from non-blob object '%s'."), arg
);
279 strbuf_add(&d
->buf
, buf
, len
);
286 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
288 struct note_data
*d
= opt
->value
;
290 return parse_reuse_arg(opt
, arg
, unset
);
293 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
295 struct strbuf buf
= STRBUF_INIT
;
296 struct notes_rewrite_cfg
*c
= NULL
;
297 struct notes_tree
*t
= NULL
;
299 const char *msg
= "Notes added by 'git notes copy'";
302 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
306 init_notes(NULL
, NULL
, NULL
, 0);
307 t
= &default_notes_tree
;
310 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
311 unsigned char from_obj
[20], to_obj
[20];
312 struct strbuf
**split
;
315 split
= strbuf_split(&buf
, ' ');
316 if (!split
[0] || !split
[1])
317 die(_("Malformed input line: '%s'."), buf
.buf
);
318 strbuf_rtrim(split
[0]);
319 strbuf_rtrim(split
[1]);
320 if (get_sha1(split
[0]->buf
, from_obj
))
321 die(_("Failed to resolve '%s' as a valid ref."), split
[0]->buf
);
322 if (get_sha1(split
[1]->buf
, to_obj
))
323 die(_("Failed to resolve '%s' as a valid ref."), split
[1]->buf
);
326 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
328 err
= copy_note(t
, from_obj
, to_obj
, force
,
329 combine_notes_overwrite
);
332 error(_("Failed to copy notes from '%s' to '%s'"),
333 split
[0]->buf
, split
[1]->buf
);
337 strbuf_list_free(split
);
341 commit_notes(t
, msg
);
344 finish_copy_notes_for_rewrite(c
, msg
);
349 static struct notes_tree
*init_notes_check(const char *subcommand
)
351 struct notes_tree
*t
;
352 init_notes(NULL
, NULL
, NULL
, 0);
353 t
= &default_notes_tree
;
355 if (!starts_with(t
->ref
, "refs/notes/"))
356 die("Refusing to %s notes in %s (outside of refs/notes/)",
361 static int list(int argc
, const char **argv
, const char *prefix
)
363 struct notes_tree
*t
;
364 unsigned char object
[20];
365 const unsigned char *note
;
367 struct option options
[] = {
372 argc
= parse_options(argc
, argv
, prefix
, options
,
373 git_notes_list_usage
, 0);
376 error(_("too many parameters"));
377 usage_with_options(git_notes_list_usage
, options
);
380 t
= init_notes_check("list");
382 if (get_sha1(argv
[0], object
))
383 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
384 note
= get_note(t
, object
);
386 puts(sha1_to_hex(note
));
389 retval
= error(_("No note found for object %s."),
390 sha1_to_hex(object
));
392 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
398 static int append_edit(int argc
, const char **argv
, const char *prefix
);
400 static int add(int argc
, const char **argv
, const char *prefix
)
402 int retval
= 0, force
= 0;
403 const char *object_ref
;
404 struct notes_tree
*t
;
405 unsigned char object
[20], new_note
[20];
407 const unsigned char *note
;
408 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
409 struct option options
[] = {
410 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
411 N_("note contents as a string"), PARSE_OPT_NONEG
,
413 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
414 N_("note contents in a file"), PARSE_OPT_NONEG
,
416 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
417 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
419 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
420 N_("reuse specified note object"), PARSE_OPT_NONEG
,
422 OPT__FORCE(&force
, N_("replace existing notes")),
426 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
427 PARSE_OPT_KEEP_ARGV0
);
430 error(_("too many parameters"));
431 usage_with_options(git_notes_add_usage
, options
);
434 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
436 if (get_sha1(object_ref
, object
))
437 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
439 t
= init_notes_check("add");
440 note
= get_note(t
, object
);
446 * Redirect to "edit" subcommand.
448 * We only end up here if none of -m/-F/-c/-C
449 * or -f are given. The original args are
450 * therefore still in argv[0-1].
455 return append_edit(argc
, argv
, prefix
);
457 retval
= error(_("Cannot add notes. Found existing notes "
458 "for object %s. Use '-f' to overwrite "
459 "existing notes"), sha1_to_hex(object
));
462 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
463 sha1_to_hex(object
));
466 create_note(object
, &d
, 0, note
, new_note
);
469 if (is_null_sha1(new_note
))
470 remove_note(t
, object
);
471 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
472 die("BUG: combine_notes_overwrite failed");
474 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
475 is_null_sha1(new_note
) ? "removed" : "added", "add");
476 commit_notes(t
, logmsg
);
482 static int copy(int argc
, const char **argv
, const char *prefix
)
484 int retval
= 0, force
= 0, from_stdin
= 0;
485 const unsigned char *from_note
, *note
;
486 const char *object_ref
;
487 unsigned char object
[20], from_obj
[20];
488 struct notes_tree
*t
;
489 const char *rewrite_cmd
= NULL
;
490 struct option options
[] = {
491 OPT__FORCE(&force
, N_("replace existing notes")),
492 OPT_BOOL(0, "stdin", &from_stdin
, N_("read objects from stdin")),
493 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
494 N_("load rewriting config for <command> (implies "
499 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
502 if (from_stdin
|| rewrite_cmd
) {
504 error(_("too many parameters"));
505 usage_with_options(git_notes_copy_usage
, options
);
507 return notes_copy_from_stdin(force
, rewrite_cmd
);
512 error(_("too few parameters"));
513 usage_with_options(git_notes_copy_usage
, options
);
516 error(_("too many parameters"));
517 usage_with_options(git_notes_copy_usage
, options
);
520 if (get_sha1(argv
[0], from_obj
))
521 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
523 object_ref
= 1 < argc
? argv
[1] : "HEAD";
525 if (get_sha1(object_ref
, object
))
526 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
528 t
= init_notes_check("copy");
529 note
= get_note(t
, object
);
533 retval
= error(_("Cannot copy notes. Found existing "
534 "notes for object %s. Use '-f' to "
535 "overwrite existing notes"),
536 sha1_to_hex(object
));
539 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
540 sha1_to_hex(object
));
543 from_note
= get_note(t
, from_obj
);
545 retval
= error(_("Missing notes on source object %s. Cannot "
546 "copy."), sha1_to_hex(from_obj
));
550 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
551 die("BUG: combine_notes_overwrite failed");
552 commit_notes(t
, "Notes added by 'git notes copy'");
558 static int append_edit(int argc
, const char **argv
, const char *prefix
)
560 const char *object_ref
;
561 struct notes_tree
*t
;
562 unsigned char object
[20], new_note
[20];
563 const unsigned char *note
;
565 const char * const *usage
;
566 struct note_data d
= { 0, 0, NULL
, STRBUF_INIT
};
567 struct option options
[] = {
568 { OPTION_CALLBACK
, 'm', "message", &d
, N_("message"),
569 N_("note contents as a string"), PARSE_OPT_NONEG
,
571 { OPTION_CALLBACK
, 'F', "file", &d
, N_("file"),
572 N_("note contents in a file"), PARSE_OPT_NONEG
,
574 { OPTION_CALLBACK
, 'c', "reedit-message", &d
, N_("object"),
575 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
577 { OPTION_CALLBACK
, 'C', "reuse-message", &d
, N_("object"),
578 N_("reuse specified note object"), PARSE_OPT_NONEG
,
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_sha1(object_ref
, object
))
601 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
603 t
= init_notes_check(argv
[0]);
604 note
= get_note(t
, object
);
606 create_note(object
, &d
, !edit
, note
, new_note
);
609 if (is_null_sha1(new_note
))
610 remove_note(t
, object
);
611 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
612 die("BUG: combine_notes_overwrite failed");
614 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
615 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
616 commit_notes(t
, logmsg
);
621 static int show(int argc
, const char **argv
, const char *prefix
)
623 const char *object_ref
;
624 struct notes_tree
*t
;
625 unsigned char object
[20];
626 const unsigned char *note
;
628 struct option options
[] = {
632 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
636 error(_("too many parameters"));
637 usage_with_options(git_notes_show_usage
, options
);
640 object_ref
= argc
? argv
[0] : "HEAD";
642 if (get_sha1(object_ref
, object
))
643 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
645 t
= init_notes_check("show");
646 note
= get_note(t
, object
);
649 retval
= error(_("No note found for object %s."),
650 sha1_to_hex(object
));
652 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
653 retval
= execv_git_cmd(show_args
);
659 static int merge_abort(struct notes_merge_options
*o
)
664 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
665 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
668 if (delete_ref("NOTES_MERGE_PARTIAL", NULL
, 0))
669 ret
+= error("Failed to delete ref NOTES_MERGE_PARTIAL");
670 if (delete_ref("NOTES_MERGE_REF", NULL
, REF_NODEREF
))
671 ret
+= error("Failed to delete ref NOTES_MERGE_REF");
672 if (notes_merge_abort(o
))
673 ret
+= error("Failed to remove 'git notes merge' worktree");
677 static int merge_commit(struct notes_merge_options
*o
)
679 struct strbuf msg
= STRBUF_INIT
;
680 unsigned char sha1
[20], parent_sha1
[20];
681 struct notes_tree
*t
;
682 struct commit
*partial
;
683 struct pretty_print_context pretty_ctx
;
684 void *local_ref_to_free
;
688 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
689 * and target notes ref from .git/NOTES_MERGE_REF.
692 if (get_sha1("NOTES_MERGE_PARTIAL", sha1
))
693 die("Failed to read ref NOTES_MERGE_PARTIAL");
694 else if (!(partial
= lookup_commit_reference(sha1
)))
695 die("Could not find commit from NOTES_MERGE_PARTIAL.");
696 else if (parse_commit(partial
))
697 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
699 if (partial
->parents
)
700 hashcpy(parent_sha1
, partial
->parents
->item
->object
.sha1
);
702 hashclr(parent_sha1
);
704 t
= xcalloc(1, sizeof(struct notes_tree
));
705 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
707 o
->local_ref
= local_ref_to_free
=
708 resolve_refdup("NOTES_MERGE_REF", 0, sha1
, NULL
);
710 die("Failed to resolve NOTES_MERGE_REF");
712 if (notes_merge_commit(o
, t
, partial
, sha1
))
713 die("Failed to finalize notes merge");
715 /* Reuse existing commit message in reflog message */
716 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
717 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
719 strbuf_insert(&msg
, 0, "notes: ", 7);
720 update_ref(msg
.buf
, o
->local_ref
, sha1
,
721 is_null_sha1(parent_sha1
) ? NULL
: parent_sha1
,
722 0, UPDATE_REFS_DIE_ON_ERR
);
725 strbuf_release(&msg
);
726 ret
= merge_abort(o
);
727 free(local_ref_to_free
);
731 static int merge(int argc
, const char **argv
, const char *prefix
)
733 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
734 unsigned char result_sha1
[20];
735 struct notes_tree
*t
;
736 struct notes_merge_options o
;
737 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
738 int verbosity
= 0, result
;
739 const char *strategy
= NULL
;
740 struct option options
[] = {
741 OPT_GROUP(N_("General options")),
742 OPT__VERBOSITY(&verbosity
),
743 OPT_GROUP(N_("Merge options")),
744 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
745 N_("resolve notes conflicts using the given strategy "
746 "(manual/ours/theirs/union/cat_sort_uniq)")),
747 OPT_GROUP(N_("Committing unmerged notes")),
748 { OPTION_SET_INT
, 0, "commit", &do_commit
, NULL
,
749 N_("finalize notes merge by committing unmerged notes"),
750 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
751 OPT_GROUP(N_("Aborting notes merge resolution")),
752 { OPTION_SET_INT
, 0, "abort", &do_abort
, NULL
,
753 N_("abort notes merge"),
754 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
758 argc
= parse_options(argc
, argv
, prefix
, options
,
759 git_notes_merge_usage
, 0);
761 if (strategy
|| do_commit
+ do_abort
== 0)
763 if (do_merge
+ do_commit
+ do_abort
!= 1) {
764 error("cannot mix --commit, --abort or -s/--strategy");
765 usage_with_options(git_notes_merge_usage
, options
);
768 if (do_merge
&& argc
!= 1) {
769 error("Must specify a notes ref to merge");
770 usage_with_options(git_notes_merge_usage
, options
);
771 } else if (!do_merge
&& argc
) {
772 error("too many parameters");
773 usage_with_options(git_notes_merge_usage
, options
);
776 init_notes_merge_options(&o
);
777 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
780 return merge_abort(&o
);
782 return merge_commit(&o
);
784 o
.local_ref
= default_notes_ref();
785 strbuf_addstr(&remote_ref
, argv
[0]);
786 expand_notes_ref(&remote_ref
);
787 o
.remote_ref
= remote_ref
.buf
;
790 if (!strcmp(strategy
, "manual"))
791 o
.strategy
= NOTES_MERGE_RESOLVE_MANUAL
;
792 else if (!strcmp(strategy
, "ours"))
793 o
.strategy
= NOTES_MERGE_RESOLVE_OURS
;
794 else if (!strcmp(strategy
, "theirs"))
795 o
.strategy
= NOTES_MERGE_RESOLVE_THEIRS
;
796 else if (!strcmp(strategy
, "union"))
797 o
.strategy
= NOTES_MERGE_RESOLVE_UNION
;
798 else if (!strcmp(strategy
, "cat_sort_uniq"))
799 o
.strategy
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
801 error("Unknown -s/--strategy: %s", strategy
);
802 usage_with_options(git_notes_merge_usage
, options
);
806 t
= init_notes_check("merge");
808 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
809 remote_ref
.buf
, default_notes_ref());
810 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
812 result
= notes_merge(&o
, t
, result_sha1
);
814 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
815 /* Update default notes ref with new commit */
816 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
817 0, UPDATE_REFS_DIE_ON_ERR
);
818 else { /* Merge has unresolved conflicts */
819 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
820 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", result_sha1
, NULL
,
821 0, UPDATE_REFS_DIE_ON_ERR
);
822 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
823 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
824 die("Failed to store link to current notes ref (%s)",
825 default_notes_ref());
826 printf("Automatic notes merge failed. Fix conflicts in %s and "
827 "commit the result with 'git notes merge --commit', or "
828 "abort the merge with 'git notes merge --abort'.\n",
829 git_path(NOTES_MERGE_WORKTREE
));
833 strbuf_release(&remote_ref
);
834 strbuf_release(&msg
);
835 return result
< 0; /* return non-zero on conflicts */
838 #define IGNORE_MISSING 1
840 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
843 unsigned char sha1
[20];
844 if (get_sha1(name
, sha1
))
845 return error(_("Failed to resolve '%s' as a valid ref."), name
);
846 status
= remove_note(t
, sha1
);
848 fprintf(stderr
, _("Object %s has no note\n"), name
);
850 fprintf(stderr
, _("Removing note for object %s\n"), name
);
851 return (flag
& IGNORE_MISSING
) ? 0 : status
;
854 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
858 struct option options
[] = {
859 OPT_BIT(0, "ignore-missing", &flag
,
860 N_("attempt to remove non-existent note is not an error"),
862 OPT_BOOL(0, "stdin", &from_stdin
,
863 N_("read object names from the standard input")),
866 struct notes_tree
*t
;
869 argc
= parse_options(argc
, argv
, prefix
, options
,
870 git_notes_remove_usage
, 0);
872 t
= init_notes_check("remove");
874 if (!argc
&& !from_stdin
) {
875 retval
= remove_one_note(t
, "HEAD", flag
);
878 retval
|= remove_one_note(t
, *argv
, flag
);
883 struct strbuf sb
= STRBUF_INIT
;
884 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
886 retval
|= remove_one_note(t
, sb
.buf
, flag
);
891 commit_notes(t
, "Notes removed by 'git notes remove'");
896 static int prune(int argc
, const char **argv
, const char *prefix
)
898 struct notes_tree
*t
;
899 int show_only
= 0, verbose
= 0;
900 struct option options
[] = {
901 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
902 OPT__VERBOSE(&verbose
, "report pruned notes"),
906 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
910 error(_("too many parameters"));
911 usage_with_options(git_notes_prune_usage
, options
);
914 t
= init_notes_check("prune");
916 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
917 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
919 commit_notes(t
, "Notes removed by 'git notes prune'");
924 static int get_ref(int argc
, const char **argv
, const char *prefix
)
926 struct option options
[] = { OPT_END() };
927 argc
= parse_options(argc
, argv
, prefix
, options
,
928 git_notes_get_ref_usage
, 0);
931 error("too many parameters");
932 usage_with_options(git_notes_get_ref_usage
, options
);
935 puts(default_notes_ref());
939 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
942 const char *override_notes_ref
= NULL
;
943 struct option options
[] = {
944 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes-ref"),
945 N_("use notes from <notes_ref>")),
949 git_config(git_default_config
, NULL
);
950 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
951 PARSE_OPT_STOP_AT_NON_OPTION
);
953 if (override_notes_ref
) {
954 struct strbuf sb
= STRBUF_INIT
;
955 strbuf_addstr(&sb
, override_notes_ref
);
956 expand_notes_ref(&sb
);
957 setenv("GIT_NOTES_REF", sb
.buf
, 1);
961 if (argc
< 1 || !strcmp(argv
[0], "list"))
962 result
= list(argc
, argv
, prefix
);
963 else if (!strcmp(argv
[0], "add"))
964 result
= add(argc
, argv
, prefix
);
965 else if (!strcmp(argv
[0], "copy"))
966 result
= copy(argc
, argv
, prefix
);
967 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
968 result
= append_edit(argc
, argv
, prefix
);
969 else if (!strcmp(argv
[0], "show"))
970 result
= show(argc
, argv
, prefix
);
971 else if (!strcmp(argv
[0], "merge"))
972 result
= merge(argc
, argv
, prefix
);
973 else if (!strcmp(argv
[0], "remove"))
974 result
= remove_cmd(argc
, argv
, prefix
);
975 else if (!strcmp(argv
[0], "prune"))
976 result
= prune(argc
, argv
, prefix
);
977 else if (!strcmp(argv
[0], "get-ref"))
978 result
= get_ref(argc
, argv
, prefix
);
980 result
= error(_("Unknown subcommand: %s"), argv
[0]);
981 usage_with_options(git_notes_usage
, options
);
984 return result
? 1 : 0;