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 void commit_notes(struct notes_tree
*t
, const char *msg
);
23 static combine_notes_fn
parse_combine_notes_fn(const char *v
);
25 static const char * const git_notes_usage
[] = {
26 N_("git notes [--ref <notes_ref>] [list [<object>]]"),
27 N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
29 N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
30 N_("git notes [--ref <notes_ref>] edit [<object>]"),
31 N_("git notes [--ref <notes_ref>] show [<object>]"),
32 N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
33 N_("git notes merge --commit [-v | -q]"),
34 N_("git notes merge --abort [-v | -q]"),
35 N_("git notes [--ref <notes_ref>] remove [<object>...]"),
36 N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
37 N_("git notes [--ref <notes_ref>] get-ref"),
41 static const char * const git_notes_list_usage
[] = {
42 N_("git notes [list [<object>]]"),
46 static const char * const git_notes_add_usage
[] = {
47 N_("git notes add [<options>] [<object>]"),
51 static const char * const git_notes_copy_usage
[] = {
52 N_("git notes copy [<options>] <from-object> <to-object>"),
53 N_("git notes copy --stdin [<from-object> <to-object>]..."),
57 static const char * const git_notes_append_usage
[] = {
58 N_("git notes append [<options>] [<object>]"),
62 static const char * const git_notes_edit_usage
[] = {
63 N_("git notes edit [<object>]"),
67 static const char * const git_notes_show_usage
[] = {
68 N_("git notes show [<object>]"),
72 static const char * const git_notes_merge_usage
[] = {
73 N_("git notes merge [<options>] <notes_ref>"),
74 N_("git notes merge --commit [<options>]"),
75 N_("git notes merge --abort [<options>]"),
79 static const char * const git_notes_remove_usage
[] = {
80 N_("git notes remove [<object>]"),
84 static const char * const git_notes_prune_usage
[] = {
85 N_("git notes prune [<options>]"),
89 static const char * const git_notes_get_ref_usage
[] = {
90 N_("git notes get-ref"),
94 static const char note_template
[] =
95 "\nWrite/edit the notes for the following object:\n";
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
;
129 struct strbuf cbuf
= 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 if (strbuf_read(&buf
, show
.out
, 0) < 0)
143 die_errno(_("could not read 'show' output"));
144 strbuf_add_commented_lines(&cbuf
, buf
.buf
, buf
.len
);
145 write_or_die(fd
, cbuf
.buf
, cbuf
.len
);
147 strbuf_release(&cbuf
);
148 strbuf_release(&buf
);
150 if (finish_command(&show
))
151 die(_("failed to finish 'show' for object '%s'"),
152 sha1_to_hex(object
));
155 static void create_note(const unsigned char *object
, struct msg_arg
*msg
,
156 int append_only
, const unsigned char *prev
,
157 unsigned char *result
)
161 if (msg
->use_editor
|| !msg
->given
) {
163 struct strbuf buf
= STRBUF_INIT
;
165 /* write the template message before editing: */
166 path
= git_pathdup("NOTES_EDITMSG");
167 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
169 die_errno(_("could not create file '%s'"), path
);
172 write_or_die(fd
, msg
->buf
.buf
, msg
->buf
.len
);
173 else if (prev
&& !append_only
)
174 write_note_data(fd
, prev
);
176 strbuf_addch(&buf
, '\n');
177 strbuf_add_commented_lines(&buf
, note_template
, strlen(note_template
));
178 strbuf_addch(&buf
, '\n');
179 write_or_die(fd
, buf
.buf
, buf
.len
);
181 write_commented_object(fd
, object
);
184 strbuf_release(&buf
);
185 strbuf_reset(&(msg
->buf
));
187 if (launch_editor(path
, &(msg
->buf
), NULL
)) {
188 die(_("Please supply the note contents using either -m" \
191 stripspace(&(msg
->buf
), 1);
194 if (prev
&& append_only
) {
195 /* Append buf to previous note contents */
197 enum object_type type
;
198 char *prev_buf
= read_sha1_file(prev
, &type
, &size
);
200 strbuf_grow(&(msg
->buf
), size
+ 1);
201 if (msg
->buf
.len
&& prev_buf
&& size
)
202 strbuf_insert(&(msg
->buf
), 0, "\n", 1);
203 if (prev_buf
&& size
)
204 strbuf_insert(&(msg
->buf
), 0, prev_buf
, size
);
209 fprintf(stderr
, _("Removing note for object %s\n"),
210 sha1_to_hex(object
));
213 if (write_sha1_file(msg
->buf
.buf
, msg
->buf
.len
, blob_type
, result
)) {
214 error(_("unable to write note object"));
216 error(_("The note contents has been left in %s"),
223 unlink_or_warn(path
);
228 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
230 struct msg_arg
*msg
= opt
->value
;
232 strbuf_grow(&(msg
->buf
), strlen(arg
) + 2);
234 strbuf_addch(&(msg
->buf
), '\n');
235 strbuf_addstr(&(msg
->buf
), arg
);
236 stripspace(&(msg
->buf
), 0);
242 static int parse_file_arg(const struct option
*opt
, const char *arg
, int unset
)
244 struct msg_arg
*msg
= opt
->value
;
247 strbuf_addch(&(msg
->buf
), '\n');
248 if (!strcmp(arg
, "-")) {
249 if (strbuf_read(&(msg
->buf
), 0, 1024) < 0)
250 die_errno(_("cannot read '%s'"), arg
);
251 } else if (strbuf_read_file(&(msg
->buf
), arg
, 1024) < 0)
252 die_errno(_("could not open or read '%s'"), arg
);
253 stripspace(&(msg
->buf
), 0);
259 static int parse_reuse_arg(const struct option
*opt
, const char *arg
, int unset
)
261 struct msg_arg
*msg
= opt
->value
;
263 unsigned char object
[20];
264 enum object_type type
;
268 strbuf_addch(&(msg
->buf
), '\n');
270 if (get_sha1(arg
, object
))
271 die(_("Failed to resolve '%s' as a valid ref."), arg
);
272 if (!(buf
= read_sha1_file(object
, &type
, &len
)) || !len
) {
274 die(_("Failed to read object '%s'."), arg
);;
276 strbuf_add(&(msg
->buf
), buf
, len
);
283 static int parse_reedit_arg(const struct option
*opt
, const char *arg
, int unset
)
285 struct msg_arg
*msg
= opt
->value
;
287 return parse_reuse_arg(opt
, arg
, unset
);
290 static void commit_notes(struct notes_tree
*t
, const char *msg
)
292 struct strbuf buf
= STRBUF_INIT
;
293 unsigned char commit_sha1
[20];
296 t
= &default_notes_tree
;
297 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
298 die(_("Cannot commit uninitialized/unreferenced notes tree"));
300 return; /* don't have to commit an unchanged tree */
302 /* Prepare commit message and reflog message */
303 strbuf_addstr(&buf
, msg
);
304 if (buf
.buf
[buf
.len
- 1] != '\n')
305 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
307 create_notes_commit(t
, NULL
, &buf
, commit_sha1
);
308 strbuf_insert(&buf
, 0, "notes: ", 7); /* commit message starts at index 7 */
309 update_ref(buf
.buf
, t
->ref
, commit_sha1
, NULL
, 0, DIE_ON_ERR
);
311 strbuf_release(&buf
);
314 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
316 if (!strcasecmp(v
, "overwrite"))
317 return combine_notes_overwrite
;
318 else if (!strcasecmp(v
, "ignore"))
319 return combine_notes_ignore
;
320 else if (!strcasecmp(v
, "concatenate"))
321 return combine_notes_concatenate
;
322 else if (!strcasecmp(v
, "cat_sort_uniq"))
323 return combine_notes_cat_sort_uniq
;
328 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
330 struct notes_rewrite_cfg
*c
= cb
;
331 if (!prefixcmp(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
332 c
->enabled
= git_config_bool(k
, v
);
334 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
336 config_error_nonbool(k
);
337 c
->combine
= parse_combine_notes_fn(v
);
339 error(_("Bad notes.rewriteMode value: '%s'"), v
);
343 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
344 /* note that a refs/ prefix is implied in the
345 * underlying for_each_glob_ref */
346 if (!prefixcmp(v
, "refs/notes/"))
347 string_list_add_refs_by_glob(c
->refs
, v
);
349 warning(_("Refusing to rewrite notes in %s"
350 " (outside of refs/notes/)"), v
);
358 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
360 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
361 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
362 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
365 c
->combine
= combine_notes_concatenate
;
366 c
->refs
= xcalloc(1, sizeof(struct string_list
));
367 c
->refs
->strdup_strings
= 1;
368 c
->refs_from_env
= 0;
369 c
->mode_from_env
= 0;
370 if (rewrite_mode_env
) {
371 c
->mode_from_env
= 1;
372 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
374 /* TRANSLATORS: The first %s is the name of the
375 environment variable, the second %s is its value */
376 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
379 if (rewrite_refs_env
) {
380 c
->refs_from_env
= 1;
381 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
383 git_config(notes_rewrite_config
, c
);
384 if (!c
->enabled
|| !c
->refs
->nr
) {
385 string_list_clear(c
->refs
, 0);
390 c
->trees
= load_notes_trees(c
->refs
);
391 string_list_clear(c
->refs
, 0);
396 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
397 const unsigned char *from_obj
, const unsigned char *to_obj
)
401 for (i
= 0; c
->trees
[i
]; i
++)
402 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
406 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg
*c
, const char *msg
)
409 for (i
= 0; c
->trees
[i
]; i
++) {
410 commit_notes(c
->trees
[i
], msg
);
411 free_notes(c
->trees
[i
]);
417 static int notes_copy_from_stdin(int force
, const char *rewrite_cmd
)
419 struct strbuf buf
= STRBUF_INIT
;
420 struct notes_rewrite_cfg
*c
= NULL
;
421 struct notes_tree
*t
= NULL
;
423 const char *msg
= "Notes added by 'git notes copy'";
426 c
= init_copy_notes_for_rewrite(rewrite_cmd
);
430 init_notes(NULL
, NULL
, NULL
, 0);
431 t
= &default_notes_tree
;
434 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
435 unsigned char from_obj
[20], to_obj
[20];
436 struct strbuf
**split
;
439 split
= strbuf_split(&buf
, ' ');
440 if (!split
[0] || !split
[1])
441 die(_("Malformed input line: '%s'."), buf
.buf
);
442 strbuf_rtrim(split
[0]);
443 strbuf_rtrim(split
[1]);
444 if (get_sha1(split
[0]->buf
, from_obj
))
445 die(_("Failed to resolve '%s' as a valid ref."), split
[0]->buf
);
446 if (get_sha1(split
[1]->buf
, to_obj
))
447 die(_("Failed to resolve '%s' as a valid ref."), split
[1]->buf
);
450 err
= copy_note_for_rewrite(c
, from_obj
, to_obj
);
452 err
= copy_note(t
, from_obj
, to_obj
, force
,
453 combine_notes_overwrite
);
456 error(_("Failed to copy notes from '%s' to '%s'"),
457 split
[0]->buf
, split
[1]->buf
);
461 strbuf_list_free(split
);
465 commit_notes(t
, msg
);
468 finish_copy_notes_for_rewrite(c
, msg
);
473 static struct notes_tree
*init_notes_check(const char *subcommand
)
475 struct notes_tree
*t
;
476 init_notes(NULL
, NULL
, NULL
, 0);
477 t
= &default_notes_tree
;
479 if (prefixcmp(t
->ref
, "refs/notes/"))
480 die("Refusing to %s notes in %s (outside of refs/notes/)",
485 static int list(int argc
, const char **argv
, const char *prefix
)
487 struct notes_tree
*t
;
488 unsigned char object
[20];
489 const unsigned char *note
;
491 struct option options
[] = {
496 argc
= parse_options(argc
, argv
, prefix
, options
,
497 git_notes_list_usage
, 0);
500 error(_("too many parameters"));
501 usage_with_options(git_notes_list_usage
, options
);
504 t
= init_notes_check("list");
506 if (get_sha1(argv
[0], object
))
507 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
508 note
= get_note(t
, object
);
510 puts(sha1_to_hex(note
));
513 retval
= error(_("No note found for object %s."),
514 sha1_to_hex(object
));
516 retval
= for_each_note(t
, 0, list_each_note
, NULL
);
522 static int append_edit(int argc
, const char **argv
, const char *prefix
);
524 static int add(int argc
, const char **argv
, const char *prefix
)
526 int retval
= 0, force
= 0;
527 const char *object_ref
;
528 struct notes_tree
*t
;
529 unsigned char object
[20], new_note
[20];
531 const unsigned char *note
;
532 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
533 struct option options
[] = {
534 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
535 N_("note contents as a string"), PARSE_OPT_NONEG
,
537 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
538 N_("note contents in a file"), PARSE_OPT_NONEG
,
540 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
541 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
543 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
544 N_("reuse specified note object"), PARSE_OPT_NONEG
,
546 OPT__FORCE(&force
, N_("replace existing notes")),
550 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_add_usage
,
551 PARSE_OPT_KEEP_ARGV0
);
554 error(_("too many parameters"));
555 usage_with_options(git_notes_add_usage
, options
);
558 object_ref
= argc
> 1 ? argv
[1] : "HEAD";
560 if (get_sha1(object_ref
, object
))
561 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
563 t
= init_notes_check("add");
564 note
= get_note(t
, object
);
570 * Redirect to "edit" subcommand.
572 * We only end up here if none of -m/-F/-c/-C
573 * or -f are given. The original args are
574 * therefore still in argv[0-1].
578 return append_edit(argc
, argv
, prefix
);
580 retval
= error(_("Cannot add notes. Found existing notes "
581 "for object %s. Use '-f' to overwrite "
582 "existing notes"), sha1_to_hex(object
));
585 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
586 sha1_to_hex(object
));
589 create_note(object
, &msg
, 0, note
, new_note
);
591 if (is_null_sha1(new_note
))
592 remove_note(t
, object
);
593 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
594 die("BUG: combine_notes_overwrite failed");
596 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
597 is_null_sha1(new_note
) ? "removed" : "added", "add");
598 commit_notes(t
, logmsg
);
601 strbuf_release(&(msg
.buf
));
605 static int copy(int argc
, const char **argv
, const char *prefix
)
607 int retval
= 0, force
= 0, from_stdin
= 0;
608 const unsigned char *from_note
, *note
;
609 const char *object_ref
;
610 unsigned char object
[20], from_obj
[20];
611 struct notes_tree
*t
;
612 const char *rewrite_cmd
= NULL
;
613 struct option options
[] = {
614 OPT__FORCE(&force
, N_("replace existing notes")),
615 OPT_BOOLEAN(0, "stdin", &from_stdin
, N_("read objects from stdin")),
616 OPT_STRING(0, "for-rewrite", &rewrite_cmd
, N_("command"),
617 N_("load rewriting config for <command> (implies "
622 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_copy_usage
,
625 if (from_stdin
|| rewrite_cmd
) {
627 error(_("too many parameters"));
628 usage_with_options(git_notes_copy_usage
, options
);
630 return notes_copy_from_stdin(force
, rewrite_cmd
);
635 error(_("too few parameters"));
636 usage_with_options(git_notes_copy_usage
, options
);
639 error(_("too many parameters"));
640 usage_with_options(git_notes_copy_usage
, options
);
643 if (get_sha1(argv
[0], from_obj
))
644 die(_("Failed to resolve '%s' as a valid ref."), argv
[0]);
646 object_ref
= 1 < argc
? argv
[1] : "HEAD";
648 if (get_sha1(object_ref
, object
))
649 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
651 t
= init_notes_check("copy");
652 note
= get_note(t
, object
);
656 retval
= error(_("Cannot copy notes. Found existing "
657 "notes for object %s. Use '-f' to "
658 "overwrite existing notes"),
659 sha1_to_hex(object
));
662 fprintf(stderr
, _("Overwriting existing notes for object %s\n"),
663 sha1_to_hex(object
));
666 from_note
= get_note(t
, from_obj
);
668 retval
= error(_("Missing notes on source object %s. Cannot "
669 "copy."), sha1_to_hex(from_obj
));
673 if (add_note(t
, object
, from_note
, combine_notes_overwrite
))
674 die("BUG: combine_notes_overwrite failed");
675 commit_notes(t
, "Notes added by 'git notes copy'");
681 static int append_edit(int argc
, const char **argv
, const char *prefix
)
683 const char *object_ref
;
684 struct notes_tree
*t
;
685 unsigned char object
[20], new_note
[20];
686 const unsigned char *note
;
688 const char * const *usage
;
689 struct msg_arg msg
= { 0, 0, STRBUF_INIT
};
690 struct option options
[] = {
691 { OPTION_CALLBACK
, 'm', "message", &msg
, N_("message"),
692 N_("note contents as a string"), PARSE_OPT_NONEG
,
694 { OPTION_CALLBACK
, 'F', "file", &msg
, N_("file"),
695 N_("note contents in a file"), PARSE_OPT_NONEG
,
697 { OPTION_CALLBACK
, 'c', "reedit-message", &msg
, N_("object"),
698 N_("reuse and edit specified note object"), PARSE_OPT_NONEG
,
700 { OPTION_CALLBACK
, 'C', "reuse-message", &msg
, N_("object"),
701 N_("reuse specified note object"), PARSE_OPT_NONEG
,
705 int edit
= !strcmp(argv
[0], "edit");
707 usage
= edit
? git_notes_edit_usage
: git_notes_append_usage
;
708 argc
= parse_options(argc
, argv
, prefix
, options
, usage
,
709 PARSE_OPT_KEEP_ARGV0
);
712 error(_("too many parameters"));
713 usage_with_options(usage
, options
);
716 if (msg
.given
&& edit
)
717 fprintf(stderr
, _("The -m/-F/-c/-C options have been deprecated "
718 "for the 'edit' subcommand.\n"
719 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
721 object_ref
= 1 < argc
? argv
[1] : "HEAD";
723 if (get_sha1(object_ref
, object
))
724 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
726 t
= init_notes_check(argv
[0]);
727 note
= get_note(t
, object
);
729 create_note(object
, &msg
, !edit
, note
, new_note
);
731 if (is_null_sha1(new_note
))
732 remove_note(t
, object
);
733 else if (add_note(t
, object
, new_note
, combine_notes_overwrite
))
734 die("BUG: combine_notes_overwrite failed");
736 snprintf(logmsg
, sizeof(logmsg
), "Notes %s by 'git notes %s'",
737 is_null_sha1(new_note
) ? "removed" : "added", argv
[0]);
738 commit_notes(t
, logmsg
);
740 strbuf_release(&(msg
.buf
));
744 static int show(int argc
, const char **argv
, const char *prefix
)
746 const char *object_ref
;
747 struct notes_tree
*t
;
748 unsigned char object
[20];
749 const unsigned char *note
;
751 struct option options
[] = {
755 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_show_usage
,
759 error(_("too many parameters"));
760 usage_with_options(git_notes_show_usage
, options
);
763 object_ref
= argc
? argv
[0] : "HEAD";
765 if (get_sha1(object_ref
, object
))
766 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
768 t
= init_notes_check("show");
769 note
= get_note(t
, object
);
772 retval
= error(_("No note found for object %s."),
773 sha1_to_hex(object
));
775 const char *show_args
[3] = {"show", sha1_to_hex(note
), NULL
};
776 retval
= execv_git_cmd(show_args
);
782 static int merge_abort(struct notes_merge_options
*o
)
787 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
788 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
791 if (delete_ref("NOTES_MERGE_PARTIAL", NULL
, 0))
792 ret
+= error("Failed to delete ref NOTES_MERGE_PARTIAL");
793 if (delete_ref("NOTES_MERGE_REF", NULL
, REF_NODEREF
))
794 ret
+= error("Failed to delete ref NOTES_MERGE_REF");
795 if (notes_merge_abort(o
))
796 ret
+= error("Failed to remove 'git notes merge' worktree");
800 static int merge_commit(struct notes_merge_options
*o
)
802 struct strbuf msg
= STRBUF_INIT
;
803 unsigned char sha1
[20], parent_sha1
[20];
804 struct notes_tree
*t
;
805 struct commit
*partial
;
806 struct pretty_print_context pretty_ctx
;
807 void *local_ref_to_free
;
811 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
812 * and target notes ref from .git/NOTES_MERGE_REF.
815 if (get_sha1("NOTES_MERGE_PARTIAL", sha1
))
816 die("Failed to read ref NOTES_MERGE_PARTIAL");
817 else if (!(partial
= lookup_commit_reference(sha1
)))
818 die("Could not find commit from NOTES_MERGE_PARTIAL.");
819 else if (parse_commit(partial
))
820 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
822 if (partial
->parents
)
823 hashcpy(parent_sha1
, partial
->parents
->item
->object
.sha1
);
825 hashclr(parent_sha1
);
827 t
= xcalloc(1, sizeof(struct notes_tree
));
828 init_notes(t
, "NOTES_MERGE_PARTIAL", combine_notes_overwrite
, 0);
830 o
->local_ref
= local_ref_to_free
=
831 resolve_refdup("NOTES_MERGE_REF", sha1
, 0, NULL
);
833 die("Failed to resolve NOTES_MERGE_REF");
835 if (notes_merge_commit(o
, t
, partial
, sha1
))
836 die("Failed to finalize notes merge");
838 /* Reuse existing commit message in reflog message */
839 memset(&pretty_ctx
, 0, sizeof(pretty_ctx
));
840 format_commit_message(partial
, "%s", &msg
, &pretty_ctx
);
842 strbuf_insert(&msg
, 0, "notes: ", 7);
843 update_ref(msg
.buf
, o
->local_ref
, sha1
,
844 is_null_sha1(parent_sha1
) ? NULL
: parent_sha1
,
848 strbuf_release(&msg
);
849 ret
= merge_abort(o
);
850 free(local_ref_to_free
);
854 static int merge(int argc
, const char **argv
, const char *prefix
)
856 struct strbuf remote_ref
= STRBUF_INIT
, msg
= STRBUF_INIT
;
857 unsigned char result_sha1
[20];
858 struct notes_tree
*t
;
859 struct notes_merge_options o
;
860 int do_merge
= 0, do_commit
= 0, do_abort
= 0;
861 int verbosity
= 0, result
;
862 const char *strategy
= NULL
;
863 struct option options
[] = {
864 OPT_GROUP(N_("General options")),
865 OPT__VERBOSITY(&verbosity
),
866 OPT_GROUP(N_("Merge options")),
867 OPT_STRING('s', "strategy", &strategy
, N_("strategy"),
868 N_("resolve notes conflicts using the given strategy "
869 "(manual/ours/theirs/union/cat_sort_uniq)")),
870 OPT_GROUP(N_("Committing unmerged notes")),
871 { OPTION_BOOLEAN
, 0, "commit", &do_commit
, NULL
,
872 N_("finalize notes merge by committing unmerged notes"),
873 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
874 OPT_GROUP(N_("Aborting notes merge resolution")),
875 { OPTION_BOOLEAN
, 0, "abort", &do_abort
, NULL
,
876 N_("abort notes merge"),
877 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
881 argc
= parse_options(argc
, argv
, prefix
, options
,
882 git_notes_merge_usage
, 0);
884 if (strategy
|| do_commit
+ do_abort
== 0)
886 if (do_merge
+ do_commit
+ do_abort
!= 1) {
887 error("cannot mix --commit, --abort or -s/--strategy");
888 usage_with_options(git_notes_merge_usage
, options
);
891 if (do_merge
&& argc
!= 1) {
892 error("Must specify a notes ref to merge");
893 usage_with_options(git_notes_merge_usage
, options
);
894 } else if (!do_merge
&& argc
) {
895 error("too many parameters");
896 usage_with_options(git_notes_merge_usage
, options
);
899 init_notes_merge_options(&o
);
900 o
.verbosity
= verbosity
+ NOTES_MERGE_VERBOSITY_DEFAULT
;
903 return merge_abort(&o
);
905 return merge_commit(&o
);
907 o
.local_ref
= default_notes_ref();
908 strbuf_addstr(&remote_ref
, argv
[0]);
909 expand_notes_ref(&remote_ref
);
910 o
.remote_ref
= remote_ref
.buf
;
913 if (!strcmp(strategy
, "manual"))
914 o
.strategy
= NOTES_MERGE_RESOLVE_MANUAL
;
915 else if (!strcmp(strategy
, "ours"))
916 o
.strategy
= NOTES_MERGE_RESOLVE_OURS
;
917 else if (!strcmp(strategy
, "theirs"))
918 o
.strategy
= NOTES_MERGE_RESOLVE_THEIRS
;
919 else if (!strcmp(strategy
, "union"))
920 o
.strategy
= NOTES_MERGE_RESOLVE_UNION
;
921 else if (!strcmp(strategy
, "cat_sort_uniq"))
922 o
.strategy
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
924 error("Unknown -s/--strategy: %s", strategy
);
925 usage_with_options(git_notes_merge_usage
, options
);
929 t
= init_notes_check("merge");
931 strbuf_addf(&msg
, "notes: Merged notes from %s into %s",
932 remote_ref
.buf
, default_notes_ref());
933 strbuf_add(&(o
.commit_msg
), msg
.buf
+ 7, msg
.len
- 7); /* skip "notes: " */
935 result
= notes_merge(&o
, t
, result_sha1
);
937 if (result
>= 0) /* Merge resulted (trivially) in result_sha1 */
938 /* Update default notes ref with new commit */
939 update_ref(msg
.buf
, default_notes_ref(), result_sha1
, NULL
,
941 else { /* Merge has unresolved conflicts */
942 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
943 update_ref(msg
.buf
, "NOTES_MERGE_PARTIAL", result_sha1
, NULL
,
945 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
946 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL
))
947 die("Failed to store link to current notes ref (%s)",
948 default_notes_ref());
949 printf("Automatic notes merge failed. Fix conflicts in %s and "
950 "commit the result with 'git notes merge --commit', or "
951 "abort the merge with 'git notes merge --abort'.\n",
952 git_path(NOTES_MERGE_WORKTREE
));
956 strbuf_release(&remote_ref
);
957 strbuf_release(&msg
);
958 return result
< 0; /* return non-zero on conflicts */
961 #define IGNORE_MISSING 1
963 static int remove_one_note(struct notes_tree
*t
, const char *name
, unsigned flag
)
966 unsigned char sha1
[20];
967 if (get_sha1(name
, sha1
))
968 return error(_("Failed to resolve '%s' as a valid ref."), name
);
969 status
= remove_note(t
, sha1
);
971 fprintf(stderr
, _("Object %s has no note\n"), name
);
973 fprintf(stderr
, _("Removing note for object %s\n"), name
);
974 return (flag
& IGNORE_MISSING
) ? 0 : status
;
977 static int remove_cmd(int argc
, const char **argv
, const char *prefix
)
981 struct option options
[] = {
982 OPT_BIT(0, "ignore-missing", &flag
,
983 N_("attempt to remove non-existent note is not an error"),
985 OPT_BOOLEAN(0, "stdin", &from_stdin
,
986 N_("read object names from the standard input")),
989 struct notes_tree
*t
;
992 argc
= parse_options(argc
, argv
, prefix
, options
,
993 git_notes_remove_usage
, 0);
995 t
= init_notes_check("remove");
997 if (!argc
&& !from_stdin
) {
998 retval
= remove_one_note(t
, "HEAD", flag
);
1001 retval
|= remove_one_note(t
, *argv
, flag
);
1006 struct strbuf sb
= STRBUF_INIT
;
1007 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1009 retval
|= remove_one_note(t
, sb
.buf
, flag
);
1011 strbuf_release(&sb
);
1014 commit_notes(t
, "Notes removed by 'git notes remove'");
1019 static int prune(int argc
, const char **argv
, const char *prefix
)
1021 struct notes_tree
*t
;
1022 int show_only
= 0, verbose
= 0;
1023 struct option options
[] = {
1024 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
1025 OPT__VERBOSE(&verbose
, "report pruned notes"),
1029 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_prune_usage
,
1033 error(_("too many parameters"));
1034 usage_with_options(git_notes_prune_usage
, options
);
1037 t
= init_notes_check("prune");
1039 prune_notes(t
, (verbose
? NOTES_PRUNE_VERBOSE
: 0) |
1040 (show_only
? NOTES_PRUNE_VERBOSE
|NOTES_PRUNE_DRYRUN
: 0) );
1042 commit_notes(t
, "Notes removed by 'git notes prune'");
1047 static int get_ref(int argc
, const char **argv
, const char *prefix
)
1049 struct option options
[] = { OPT_END() };
1050 argc
= parse_options(argc
, argv
, prefix
, options
,
1051 git_notes_get_ref_usage
, 0);
1054 error("too many parameters");
1055 usage_with_options(git_notes_get_ref_usage
, options
);
1058 puts(default_notes_ref());
1062 int cmd_notes(int argc
, const char **argv
, const char *prefix
)
1065 const char *override_notes_ref
= NULL
;
1066 struct option options
[] = {
1067 OPT_STRING(0, "ref", &override_notes_ref
, N_("notes_ref"),
1068 N_("use notes from <notes_ref>")),
1072 git_config(git_default_config
, NULL
);
1073 argc
= parse_options(argc
, argv
, prefix
, options
, git_notes_usage
,
1074 PARSE_OPT_STOP_AT_NON_OPTION
);
1076 if (override_notes_ref
) {
1077 struct strbuf sb
= STRBUF_INIT
;
1078 strbuf_addstr(&sb
, override_notes_ref
);
1079 expand_notes_ref(&sb
);
1080 setenv("GIT_NOTES_REF", sb
.buf
, 1);
1081 strbuf_release(&sb
);
1084 if (argc
< 1 || !strcmp(argv
[0], "list"))
1085 result
= list(argc
, argv
, prefix
);
1086 else if (!strcmp(argv
[0], "add"))
1087 result
= add(argc
, argv
, prefix
);
1088 else if (!strcmp(argv
[0], "copy"))
1089 result
= copy(argc
, argv
, prefix
);
1090 else if (!strcmp(argv
[0], "append") || !strcmp(argv
[0], "edit"))
1091 result
= append_edit(argc
, argv
, prefix
);
1092 else if (!strcmp(argv
[0], "show"))
1093 result
= show(argc
, argv
, prefix
);
1094 else if (!strcmp(argv
[0], "merge"))
1095 result
= merge(argc
, argv
, prefix
);
1096 else if (!strcmp(argv
[0], "remove"))
1097 result
= remove_cmd(argc
, argv
, prefix
);
1098 else if (!strcmp(argv
[0], "prune"))
1099 result
= prune(argc
, argv
, prefix
);
1100 else if (!strcmp(argv
[0], "get-ref"))
1101 result
= get_ref(argc
, argv
, prefix
);
1103 result
= error(_("Unknown subcommand: %s"), argv
[0]);
1104 usage_with_options(git_notes_usage
, options
);
1107 return result
? 1 : 0;