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"
22 static const char * const git_notes_usage
[] = {
23 "git notes [--ref <notes_ref>] [list [<object>]]",
24 "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
25 "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
26 "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
27 "git notes [--ref <notes_ref>] edit [<object>]",
28 "git notes [--ref <notes_ref>] show [<object>]",
29 "git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>",
30 "git notes [--ref <notes_ref>] remove [<object>]",
31 "git notes [--ref <notes_ref>] prune [-n | -v]",
35 static const char * const git_notes_list_usage
[] = {
36 "git notes [list [<object>]]",
40 static const char * const git_notes_add_usage
[] = {
41 "git notes add [<options>] [<object>]",
45 static const char * const git_notes_copy_usage
[] = {
46 "git notes copy [<options>] <from-object> <to-object>",
47 "git notes copy --stdin [<from-object> <to-object>]...",
51 static const char * const git_notes_append_usage
[] = {
52 "git notes append [<options>] [<object>]",
56 static const char * const git_notes_edit_usage
[] = {
57 "git notes edit [<object>]",
61 static const char * const git_notes_show_usage
[] = {
62 "git notes show [<object>]",
66 static const char * const git_notes_merge_usage
[] = {
67 "git notes merge [<options>] <notes_ref>",
71 static const char * const git_notes_remove_usage
[] = {
72 "git notes remove [<object>]",
76 static const char * const git_notes_prune_usage
[] = {
77 "git notes prune [<options>]",
81 static const char note_template
[] =
84 "# Write/edit the notes for the following object:\n"
93 static void expand_notes_ref(struct strbuf
*sb
)
95 if (!prefixcmp(sb
->buf
, "refs/notes/"))
96 return; /* we're happy */
97 else if (!prefixcmp(sb
->buf
, "notes/"))
98 strbuf_insert(sb
, 0, "refs/", 5);
100 strbuf_insert(sb
, 0, "refs/notes/", 11);
103 static int list_each_note(const unsigned char *object_sha1
,
104 const unsigned char *note_sha1
, char *note_path
,
107 printf("%s %s\n", sha1_to_hex(note_sha1
), sha1_to_hex(object_sha1
));
111 static void write_note_data(int fd
, const unsigned char *sha1
)
114 enum object_type type
;
115 char *buf
= read_sha1_file(sha1
, &type
, &size
);
118 write_or_die(fd
, buf
, size
);
123 static void write_commented_object(int fd
, const unsigned char *object
)
125 const char *show_args
[5] =
126 {"show", "--stat", "--no-notes", sha1_to_hex(object
), NULL
};
127 struct child_process show
;
128 struct strbuf buf
= STRBUF_INIT
;
131 /* Invoke "git show --stat --no-notes $object" */
132 memset(&show
, 0, sizeof(show
));
133 show
.argv
= show_args
;
138 if (start_command(&show
))
139 die("unable to start 'show' for object '%s'",
140 sha1_to_hex(object
));
142 /* Open the output as FILE* so strbuf_getline() can be used. */
143 show_out
= xfdopen(show
.out
, "r");
144 if (show_out
== NULL
)
145 die_errno("can't fdopen 'show' output fd");
147 /* Prepend "# " to each output line and write result to 'fd' */
148 while (strbuf_getline(&buf
, show_out
, '\n') != EOF
) {
149 write_or_die(fd
, "# ", 2);
150 write_or_die(fd
, buf
.buf
, buf
.len
);
151 write_or_die(fd
, "\n", 1);
153 strbuf_release(&buf
);
154 if (fclose(show_out
))
155 die_errno("failed to close pipe to 'show' for object '%s'",
156 sha1_to_hex(object
));
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 msg_arg
*msg
,
163 int append_only
, const unsigned char *prev
,
164 unsigned char *result
)
168 if (msg
->use_editor
|| !msg
->given
) {
171 /* write the template message before editing: */
172 path
= git_pathdup("NOTES_EDITMSG");
173 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
175 die_errno("could not create file '%s'", path
);
178 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
179 else if (prev
&& !append_only
)
180 write_note_data(fd
, prev
);
181 write_or_die(fd
, note_template
, strlen(note_template
));
183 write_commented_object(fd
, object
);
186 strbuf_reset(&(msg
->buf
));
188 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
189 die("Please supply the note contents using either -m" \
192 stripspace(&(msg
->buf
), 1);
195 if (prev
&& append_only
) {
196 /* Append buf to previous note contents */
198 enum object_type type
;
199 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
201 strbuf_grow(&(msg
->buf
), size
+ 1);
202 if (msg
->buf
.len
&& prev_buf
&& size
)
203 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
204 if (prev_buf
&& size
)
205 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
210 fprintf(stderr
, "Removing note for object %s\n",
211 sha1_to_hex(object
));
214 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
215 error("unable to write note object");
217 error("The note contents has been left in %s",
224 unlink_or_warn(path
);
229 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
231 struct msg_arg
*msg
= opt
->value
;
233 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
235 strbuf_addch(&(msg
->buf
), '\n');
236 strbuf_addstr(&(msg
->buf
), arg
);
237 stripspace(&(msg
->buf
), 0);
243 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
245 struct msg_arg
*msg
= opt
->value
;
248 strbuf_addch(&(msg
->buf
), '\n');
249 if (!strcmp(arg
, "-")) {
250 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
251 die_errno("cannot read '%s'", arg
);
252 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
253 die_errno("could not open or read '%s'", arg
);
254 stripspace(&(msg
->buf
), 0);
260 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
262 struct msg_arg
*msg
= opt
->value
;
264 unsigned char object
[20];
265 enum object_type type
;
269 strbuf_addch(&(msg
->buf
), '\n');
271 if (get_sha1(arg
, object
))
272 die("Failed to resolve '%s' as a valid ref.", arg
);
273 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
275 die("Failed to read 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 void commit_notes(struct notes_tree
*t
, const char *msg
)
293 struct strbuf buf
= STRBUF_INIT
;
294 unsigned char commit_sha1
[20];
297 t
= &default_notes_tree
;
298 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
299 die("Cannot commit uninitialized/unreferenced notes tree");
301 return; /* don't have to commit an unchanged tree */
303 /* Prepare commit message and reflog message */
304 strbuf_addstr(&buf
, "notes: "); /* commit message starts at index 7 */
305 strbuf_addstr(&buf
, msg
);
306 if (buf
.buf
[buf
.len
- 1] != '\n')
307 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
309 create_notes_commit(t
, NULL
, buf
.buf
+ 7, commit_sha1
);
310 update_ref(buf
.buf
, t
->ref
, commit_sha1
, NULL
, 0, DIE_ON_ERR
);
312 strbuf_release(&buf
);
315 combine_notes_fn
parse_combine_notes_fn(const char *v
)
317 if (!strcasecmp(v
, "overwrite"))
318 return combine_notes_overwrite
;
319 else if (!strcasecmp(v
, "ignore"))
320 return combine_notes_ignore
;
321 else if (!strcasecmp(v
, "concatenate"))
322 return combine_notes_concatenate
;
327 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
329 struct notes_rewrite_cfg
*c
= cb
;
330 if (!prefixcmp(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
331 c
->enabled
= git_config_bool(k
, v
);
333 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
335 config_error_nonbool(k
);
336 c
->combine
= parse_combine_notes_fn(v
);
338 error("Bad notes.rewriteMode value: '%s'", v
);
342 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
343 /* note that a refs/ prefix is implied in the
344 * underlying for_each_glob_ref */
345 if (!prefixcmp(v
, "refs/notes/"))
346 string_list_add_refs_by_glob(c
->refs
, v
);
348 warning("Refusing to rewrite notes in %s"
349 " (outside of refs/notes/)", v
);
357 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
359 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
360 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
361 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
364 c
->combine
= combine_notes_concatenate
;
365 c
->refs
= xcalloc(1, sizeof(struct string_list
));
366 c
->refs
->strdup_strings
= 1;
367 c
->refs_from_env
= 0;
368 c
->mode_from_env
= 0;
369 if (rewrite_mode_env
) {
370 c
->mode_from_env
= 1;
371 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
373 error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
374 " value: '%s'", rewrite_mode_env
);
376 if (rewrite_refs_env
) {
377 c
->refs_from_env
= 1;
378 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
380 git_config(notes_rewrite_config
, c
);
381 if (!c
->enabled
|| !c
->refs
->nr
) {
382 string_list_clear(c
->refs
, 0);
387 c
->trees
= load_notes_trees(c
->refs
);
388 string_list_clear(c
->refs
, 0);
393 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
394 const unsigned char *from_obj
, const unsigned char *to_obj
)
398 for (i
= 0; c
->trees
[i
]; i
++)
399 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
403 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg
*c
)
406 for (i
= 0; c
->trees
[i
]; i
++) {
407 commit_notes(c
->trees
[i
], "Notes added by 'git notes copy'");
408 free_notes(c
->trees
[i
]);
414 int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
416 struct strbuf buf
= STRBUF_INIT
;
417 struct notes_rewrite_cfg
*c
= NULL
;
418 struct notes_tree
*t
= NULL
;
422 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
426 init_notes(NULL
, NULL
, NULL
, 0);
427 t
= &default_notes_tree
;
430 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
431 unsigned char from_obj
[20], to_obj
[20];
432 struct strbuf
**split
;
435 split
= strbuf_split(&buf
, ' ');
436 if (!split
[0] || !split
[1])
437 die("Malformed input line: '%s'.", buf
.buf
);
438 strbuf_rtrim(split
[0]);
439 strbuf_rtrim(split
[1]);
440 if (get_sha1(split
[0]->buf
, from_obj
))
441 die("Failed to resolve '%s' as a valid ref.", split
[0]->buf
);
442 if (get_sha1(split
[1]->buf
, to_obj
))
443 die("Failed to resolve '%s' as a valid ref.", split
[1]->buf
);
446 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
448 err
= copy_note(t
, from_obj
, to_obj
, force
,
449 combine_notes_overwrite
);
452 error("Failed to copy notes from '%s' to '%s'",
453 split
[0]->buf
, split
[1]->buf
);
457 strbuf_list_free(split
);
461 commit_notes(t
, "Notes added by 'git notes copy'");
464 finish_copy_notes_for_rewrite(c
);
469 static struct notes_tree
*init_notes_check(const char *subcommand
)
471 struct notes_tree
*t
;
472 init_notes(NULL
, NULL
, NULL
, 0);
473 t
= &default_notes_tree
;
475 if (prefixcmp(t
->ref
, "refs/notes/"))
476 die("Refusing to %s notes in %s (outside of refs/notes/)",
481 static int list(int argc
, const char **argv
, const char *prefix
)
483 struct notes_tree
*t
;
484 unsigned char object
[20];
485 const unsigned char *note
;
487 struct option options
[] = {
492 argc
= parse_options(argc
, argv
, prefix
, options
,
493 git_notes_list_usage
, 0);
496 error("too many parameters");
497 usage_with_options(git_notes_list_usage
, options
);
500 t
= init_notes_check("list");
502 if (get_sha1(argv
[0], object
))
503 die("Failed to resolve '%s' as a valid ref.", argv
[0]);
504 note
= get_note(t
, object
);
506 puts(sha1_to_hex(note
));
509 retval
= error("No note found for object %s.",
510 sha1_to_hex(object
));
512 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
518 static int add(int argc
, const char **argv
, const char *prefix
)
520 int retval
= 0, force
= 0;
521 const char *object_ref
;
522 struct notes_tree
*t
;
523 unsigned char object
[20], new_note
[20];
525 const unsigned char *note
;
526 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
527 struct option options
[] = {
528 { OPTION_CALLBACK
, 'm', "message", &msg
, "MSG",
529 "note contents as a string", PARSE_OPT_NONEG
,
531 { OPTION_CALLBACK
, 'F', "file", &msg
, "FILE",
532 "note contents in a file", PARSE_OPT_NONEG
,
534 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, "OBJECT",
535 "reuse and edit specified note object", PARSE_OPT_NONEG
,
537 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, "OBJECT",
538 "reuse specified note object", PARSE_OPT_NONEG
,
540 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
544 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
548 error("too many parameters");
549 usage_with_options(git_notes_add_usage
, options
);
552 object_ref
= argc
? argv
[0] : "HEAD";
554 if (get_sha1(object_ref
, object
))
555 die("Failed to resolve '%s' as a valid ref.", object_ref
);
557 t
= init_notes_check("add");
558 note
= get_note(t
, object
);
562 retval
= error("Cannot add notes. Found existing notes "
563 "for object %s. Use '-f' to overwrite "
564 "existing notes", sha1_to_hex(object
));
567 fprintf(stderr
, "Overwriting existing notes for object %s\n",
568 sha1_to_hex(object
));
571 create_note(object
, &msg
, 0, note
, new_note
);
573 if (is_null_sha1(new_note
))
574 remove_note(t
, object
);
575 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
576 die("BUG: combine_notes_overwrite failed");
578 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
579 is_null_sha1(new_note
) ? "removed" : "added", "add");
580 commit_notes(t
, logmsg
);
583 strbuf_release(&(msg
.buf
));
587 static int copy(int argc
, const char **argv
, const char *prefix
)
589 int retval
= 0, force
= 0, from_stdin
= 0;
590 const unsigned char *from_note
, *note
;
591 const char *object_ref
;
592 unsigned char object
[20], from_obj
[20];
593 struct notes_tree
*t
;
594 const char *rewrite_cmd
= NULL
;
595 struct option options
[] = {
596 OPT_BOOLEAN('f', "force", &force
, "replace existing notes"),
597 OPT_BOOLEAN(0, "stdin", &from_stdin
, "read objects from stdin"),
598 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, "command",
599 "load rewriting config for <command> (implies "
604 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
607 if (from_stdin
|| rewrite_cmd
) {
609 error("too many parameters");
610 usage_with_options(git_notes_copy_usage
, options
);
612 return notes_copy_from_stdin(force
, rewrite_cmd
);
617 error("too few parameters");
618 usage_with_options(git_notes_copy_usage
, options
);
621 error("too many parameters");
622 usage_with_options(git_notes_copy_usage
, options
);
625 if (get_sha1(argv
[0], from_obj
))
626 die("Failed to resolve '%s' as a valid ref.", argv
[0]);
628 object_ref
= 1 < argc
? argv
[1] : "HEAD";
630 if (get_sha1(object_ref
, object
))
631 die("Failed to resolve '%s' as a valid ref.", object_ref
);
633 t
= init_notes_check("copy");
634 note
= get_note(t
, object
);
638 retval
= error("Cannot copy notes. Found existing "
639 "notes for object %s. Use '-f' to "
640 "overwrite existing notes",
641 sha1_to_hex(object
));
644 fprintf(stderr
, "Overwriting existing notes for object %s\n",
645 sha1_to_hex(object
));
648 from_note
= get_note(t
, from_obj
);
650 retval
= error("Missing notes on source object %s. Cannot "
651 "copy.", sha1_to_hex(from_obj
));
655 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
656 die("BUG: combine_notes_overwrite failed");
657 commit_notes(t
, "Notes added by 'git notes copy'");
663 static int append_edit(int argc
, const char **argv
, const char *prefix
)
665 const char *object_ref
;
666 struct notes_tree
*t
;
667 unsigned char object
[20], new_note
[20];
668 const unsigned char *note
;
670 const char * const *usage
;
671 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
672 struct option options
[] = {
673 { OPTION_CALLBACK
, 'm', "message", &msg
, "MSG",
674 "note contents as a string", PARSE_OPT_NONEG
,
676 { OPTION_CALLBACK
, 'F', "file", &msg
, "FILE",
677 "note contents in a file", PARSE_OPT_NONEG
,
679 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, "OBJECT",
680 "reuse and edit specified note object", PARSE_OPT_NONEG
,
682 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, "OBJECT",
683 "reuse specified note object", PARSE_OPT_NONEG
,
687 int edit
= !strcmp(argv
[0], "edit");
689 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
690 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
691 PARSE_OPT_KEEP_ARGV0
);
694 error("too many parameters");
695 usage_with_options(usage
, options
);
698 if (msg
.given
&& edit
)
699 fprintf(stderr
, "The -m/-F/-c/-C options have been deprecated "
700 "for the 'edit' subcommand.\n"
701 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
703 object_ref
= 1 < argc
? argv
[1] : "HEAD";
705 if (get_sha1(object_ref
, object
))
706 die("Failed to resolve '%s' as a valid ref.", object_ref
);
708 t
= init_notes_check(argv
[0]);
709 note
= get_note(t
, object
);
711 create_note(object
, &msg
, !edit
, note
, new_note
);
713 if (is_null_sha1(new_note
))
714 remove_note(t
, object
);
715 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
716 die("BUG: combine_notes_overwrite failed");
718 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
719 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
720 commit_notes(t
, logmsg
);
722 strbuf_release(&(msg
.buf
));
726 static int show(int argc
, const char **argv
, const char *prefix
)
728 const char *object_ref
;
729 struct notes_tree
*t
;
730 unsigned char object
[20];
731 const unsigned char *note
;
733 struct option options
[] = {
737 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
741 error("too many parameters");
742 usage_with_options(git_notes_show_usage
, options
);
745 object_ref
= argc
? argv
[0] : "HEAD";
747 if (get_sha1(object_ref
, object
))
748 die("Failed to resolve '%s' as a valid ref.", object_ref
);
750 t
= init_notes_check("show");
751 note
= get_note(t
, object
);
754 retval
= error("No note found for object %s.",
755 sha1_to_hex(object
));
757 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
758 retval
= execv_git_cmd(show_args
);
764 static int merge(int argc
, const char **argv
, const char *prefix
)
766 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
767 unsigned char result_sha1
[20];
768 struct notes_tree
*t
;
769 struct notes_merge_options o
;
770 int verbosity
= 0, result
;
771 struct option options
[] = {
772 OPT__VERBOSITY(&verbosity
),
776 argc
= parse_options(argc
, argv
, prefix
, options
,
777 git_notes_merge_usage
, 0);
780 error("Must specify a notes ref to merge");
781 usage_with_options(git_notes_merge_usage
, options
);
784 init_notes_merge_options(&o
);
785 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
787 o
.local_ref
= default_notes_ref();
788 strbuf_addstr(&remote_ref
, argv
[0]);
789 expand_notes_ref(&remote_ref
);
790 o
.remote_ref
= remote_ref
.buf
;
792 t
= init_notes_check("merge");
794 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
795 remote_ref
.buf
, default_notes_ref());
796 o
.commit_msg
= msg
.buf
+ 7; // skip "notes: " prefix
798 result
= notes_merge(&o
, t
, result_sha1
);
800 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
801 /* Update default notes ref with new commit */
802 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
806 die("'git notes merge' cannot yet handle conflicts!");
809 strbuf_release(&remote_ref
);
810 strbuf_release(&msg
);
814 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
816 struct option options
[] = {
819 const char *object_ref
;
820 struct notes_tree
*t
;
821 unsigned char object
[20];
823 argc
= parse_options(argc
, argv
, prefix
, options
,
824 git_notes_remove_usage
, 0);
827 error("too many parameters");
828 usage_with_options(git_notes_remove_usage
, options
);
831 object_ref
= argc
? argv
[0] : "HEAD";
833 if (get_sha1(object_ref
, object
))
834 die("Failed to resolve '%s' as a valid ref.", object_ref
);
836 t
= init_notes_check("remove");
838 fprintf(stderr
, "Removing note for object %s\n", sha1_to_hex(object
));
839 remove_note(t
, object
);
841 commit_notes(t
, "Notes removed by 'git notes remove'");
846 static int prune(int argc
, const char **argv
, const char *prefix
)
848 struct notes_tree
*t
;
849 int show_only
= 0, verbose
= 0;
850 struct option options
[] = {
851 OPT_BOOLEAN('n', "dry-run", &show_only
,
852 "do not remove, show only"),
853 OPT_BOOLEAN('v', "verbose", &verbose
, "report pruned notes"),
857 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
861 error("too many parameters");
862 usage_with_options(git_notes_prune_usage
, options
);
865 t
= init_notes_check("prune");
867 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
868 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
870 commit_notes(t
, "Notes removed by 'git notes prune'");
875 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
878 const char *override_notes_ref
= NULL
;
879 struct option options
[] = {
880 OPT_STRING(0, "ref", &override_notes_ref
, "notes_ref",
881 "use notes from <notes_ref>"),
885 git_config(git_default_config
, NULL
);
886 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
887 PARSE_OPT_STOP_AT_NON_OPTION
);
889 if (override_notes_ref
) {
890 struct strbuf sb
= STRBUF_INIT
;
891 strbuf_addstr(&sb
, override_notes_ref
);
892 expand_notes_ref(&sb
);
893 setenv("GIT_NOTES_REF", sb
.buf
, 1);
897 if (argc
< 1 || !strcmp(argv
[0], "list"))
898 result
= list(argc
, argv
, prefix
);
899 else if (!strcmp(argv
[0], "add"))
900 result
= add(argc
, argv
, prefix
);
901 else if (!strcmp(argv
[0], "copy"))
902 result
= copy(argc
, argv
, prefix
);
903 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
904 result
= append_edit(argc
, argv
, prefix
);
905 else if (!strcmp(argv
[0], "show"))
906 result
= show(argc
, argv
, prefix
);
907 else if (!strcmp(argv
[0], "merge"))
908 result
= merge(argc
, argv
, prefix
);
909 else if (!strcmp(argv
[0], "remove"))
910 result
= remove_cmd(argc
, argv
, prefix
);
911 else if (!strcmp(argv
[0], "prune"))
912 result
= prune(argc
, argv
, prefix
);
914 result
= error("Unknown subcommand: %s", argv
[0]);
915 usage_with_options(git_notes_usage
, options
);
918 return result
? 1 : 0;