Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git notes" |
| 3 | * |
| 4 | * Copyright (c) 2010 Johan Herland <johan@herland.net> |
| 5 | * |
| 6 | * Based on git-notes.sh by Johannes Schindelin, |
Phil Hord | 09b7e22 | 2013-06-18 13:44:58 -0400 | [diff] [blame] | 7 | * and builtin/tag.c by Kristian Høgsberg and Carlos Rica. |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "cache.h" |
| 11 | #include "builtin.h" |
| 12 | #include "notes.h" |
| 13 | #include "blob.h" |
| 14 | #include "commit.h" |
| 15 | #include "refs.h" |
| 16 | #include "exec_cmd.h" |
| 17 | #include "run-command.h" |
| 18 | #include "parse-options.h" |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 19 | #include "string-list.h" |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 20 | #include "notes-merge.h" |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 21 | #include "notes-utils.h" |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 22 | #include "worktree.h" |
Junio C Hamano | f50fee4 | 2012-09-15 13:56:07 -0700 | [diff] [blame] | 23 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 24 | static const char * const git_notes_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 25 | N_("git notes [--ref <notes-ref>] [list [<object>]]"), |
| 26 | N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), |
| 27 | N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"), |
| 28 | N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), |
| 29 | N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"), |
| 30 | N_("git notes [--ref <notes-ref>] show [<object>]"), |
| 31 | N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 32 | N_("git notes merge --commit [-v | -q]"), |
| 33 | N_("git notes merge --abort [-v | -q]"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 34 | N_("git notes [--ref <notes-ref>] remove [<object>...]"), |
| 35 | N_("git notes [--ref <notes-ref>] prune [-n | -v]"), |
| 36 | N_("git notes [--ref <notes-ref>] get-ref"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 37 | NULL |
| 38 | }; |
| 39 | |
| 40 | static const char * const git_notes_list_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 41 | N_("git notes [list [<object>]]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 42 | NULL |
| 43 | }; |
| 44 | |
| 45 | static const char * const git_notes_add_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 46 | N_("git notes add [<options>] [<object>]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 47 | NULL |
| 48 | }; |
| 49 | |
| 50 | static const char * const git_notes_copy_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 51 | N_("git notes copy [<options>] <from-object> <to-object>"), |
| 52 | N_("git notes copy --stdin [<from-object> <to-object>]..."), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | static const char * const git_notes_append_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 57 | N_("git notes append [<options>] [<object>]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | static const char * const git_notes_edit_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 62 | N_("git notes edit [<object>]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 63 | NULL |
| 64 | }; |
| 65 | |
| 66 | static const char * const git_notes_show_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 67 | N_("git notes show [<object>]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 68 | NULL |
| 69 | }; |
| 70 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 71 | static const char * const git_notes_merge_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 72 | N_("git notes merge [<options>] <notes-ref>"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 73 | N_("git notes merge --commit [<options>]"), |
| 74 | N_("git notes merge --abort [<options>]"), |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 75 | NULL |
| 76 | }; |
| 77 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 78 | static const char * const git_notes_remove_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 79 | N_("git notes remove [<object>]"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 80 | NULL |
| 81 | }; |
| 82 | |
| 83 | static const char * const git_notes_prune_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 84 | N_("git notes prune [<options>]"), |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 85 | NULL |
| 86 | }; |
| 87 | |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 88 | static const char * const git_notes_get_ref_usage[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 89 | N_("git notes get-ref"), |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 90 | NULL |
| 91 | }; |
| 92 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 93 | static const char note_template[] = |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 94 | "\nWrite/edit the notes for the following object:\n"; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 95 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 96 | struct note_data { |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 97 | int given; |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 98 | int use_editor; |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 99 | char *edit_path; |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 100 | struct strbuf buf; |
| 101 | }; |
| 102 | |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 103 | static void free_note_data(struct note_data *d) |
| 104 | { |
| 105 | if (d->edit_path) { |
| 106 | unlink_or_warn(d->edit_path); |
| 107 | free(d->edit_path); |
| 108 | } |
| 109 | strbuf_release(&d->buf); |
| 110 | } |
| 111 | |
Johan Herland | e397421 | 2010-02-13 22:28:30 +0100 | [diff] [blame] | 112 | static int list_each_note(const unsigned char *object_sha1, |
| 113 | const unsigned char *note_sha1, char *note_path, |
| 114 | void *cb_data) |
| 115 | { |
| 116 | printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1)); |
| 117 | return 0; |
| 118 | } |
| 119 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 120 | static void copy_obj_to_fd(int fd, const unsigned char *sha1) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 121 | { |
| 122 | unsigned long size; |
| 123 | enum object_type type; |
| 124 | char *buf = read_sha1_file(sha1, &type, &size); |
| 125 | if (buf) { |
| 126 | if (size) |
| 127 | write_or_die(fd, buf, size); |
| 128 | free(buf); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void write_commented_object(int fd, const unsigned char *object) |
| 133 | { |
| 134 | const char *show_args[5] = |
| 135 | {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL}; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 136 | struct child_process show = CHILD_PROCESS_INIT; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 137 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 138 | struct strbuf cbuf = STRBUF_INIT; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 139 | |
| 140 | /* Invoke "git show --stat --no-notes $object" */ |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 141 | show.argv = show_args; |
| 142 | show.no_stdin = 1; |
| 143 | show.out = -1; |
| 144 | show.err = 0; |
| 145 | show.git_cmd = 1; |
| 146 | if (start_command(&show)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 147 | die(_("unable to start 'show' for object '%s'"), |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 148 | sha1_to_hex(object)); |
| 149 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 150 | if (strbuf_read(&buf, show.out, 0) < 0) |
| 151 | die_errno(_("could not read 'show' output")); |
| 152 | strbuf_add_commented_lines(&cbuf, buf.buf, buf.len); |
| 153 | write_or_die(fd, cbuf.buf, cbuf.len); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 154 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 155 | strbuf_release(&cbuf); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 156 | strbuf_release(&buf); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 157 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 158 | if (finish_command(&show)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 159 | die(_("failed to finish 'show' for object '%s'"), |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 160 | sha1_to_hex(object)); |
| 161 | } |
| 162 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 163 | static void prepare_note_data(const unsigned char *object, struct note_data *d, |
| 164 | const unsigned char *old_note) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 165 | { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 166 | if (d->use_editor || !d->given) { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 167 | int fd; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 168 | struct strbuf buf = STRBUF_INIT; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 169 | |
| 170 | /* write the template message before editing: */ |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 171 | d->edit_path = git_pathdup("NOTES_EDITMSG"); |
| 172 | fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 173 | if (fd < 0) |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 174 | die_errno(_("could not create file '%s'"), d->edit_path); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 175 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 176 | if (d->given) |
| 177 | write_or_die(fd, d->buf.buf, d->buf.len); |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 178 | else if (old_note) |
| 179 | copy_obj_to_fd(fd, old_note); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 180 | |
| 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); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 185 | |
| 186 | write_commented_object(fd, object); |
| 187 | |
| 188 | close(fd); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 189 | strbuf_release(&buf); |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 190 | strbuf_reset(&d->buf); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 191 | |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 192 | if (launch_editor(d->edit_path, &d->buf, NULL)) { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 193 | die(_("Please supply the note contents using either -m or -F option")); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 194 | } |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 195 | strbuf_stripspace(&d->buf, 1); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 196 | } |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 197 | } |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 198 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 199 | static void write_note_data(struct note_data *d, unsigned char *sha1) |
| 200 | { |
| 201 | if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) { |
| 202 | error(_("unable to write note object")); |
| 203 | if (d->edit_path) |
| 204 | error(_("The note contents have been left in %s"), |
| 205 | d->edit_path); |
| 206 | exit(128); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 207 | } |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 210 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
| 211 | { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 212 | struct note_data *d = opt->value; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 213 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 214 | strbuf_grow(&d->buf, strlen(arg) + 2); |
| 215 | if (d->buf.len) |
| 216 | strbuf_addch(&d->buf, '\n'); |
| 217 | strbuf_addstr(&d->buf, arg); |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 218 | strbuf_stripspace(&d->buf, 0); |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 219 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 220 | d->given = 1; |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int parse_file_arg(const struct option *opt, const char *arg, int unset) |
| 225 | { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 226 | struct note_data *d = opt->value; |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 227 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 228 | if (d->buf.len) |
| 229 | strbuf_addch(&d->buf, '\n'); |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 230 | if (!strcmp(arg, "-")) { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 231 | if (strbuf_read(&d->buf, 0, 1024) < 0) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 232 | die_errno(_("cannot read '%s'"), arg); |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 233 | } else if (strbuf_read_file(&d->buf, arg, 1024) < 0) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 234 | die_errno(_("could not open or read '%s'"), arg); |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 235 | strbuf_stripspace(&d->buf, 0); |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 236 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 237 | d->given = 1; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 241 | static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) |
| 242 | { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 243 | struct note_data *d = opt->value; |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 244 | char *buf; |
| 245 | unsigned char object[20]; |
| 246 | enum object_type type; |
| 247 | unsigned long len; |
| 248 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 249 | if (d->buf.len) |
| 250 | strbuf_addch(&d->buf, '\n'); |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 251 | |
| 252 | if (get_sha1(arg, object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 253 | die(_("Failed to resolve '%s' as a valid ref."), arg); |
Johan Herland | 511726e | 2014-11-09 13:30:47 +0100 | [diff] [blame] | 254 | if (!(buf = read_sha1_file(object, &type, &len))) { |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 255 | free(buf); |
Johan Herland | ce8daa1 | 2014-02-12 10:54:16 +0100 | [diff] [blame] | 256 | die(_("Failed to read object '%s'."), arg); |
| 257 | } |
| 258 | if (type != OBJ_BLOB) { |
| 259 | free(buf); |
| 260 | die(_("Cannot read note data from non-blob object '%s'."), arg); |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 261 | } |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 262 | strbuf_add(&d->buf, buf, len); |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 263 | free(buf); |
| 264 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 265 | d->given = 1; |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) |
| 270 | { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 271 | struct note_data *d = opt->value; |
| 272 | d->use_editor = 1; |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 273 | return parse_reuse_arg(opt, arg, unset); |
| 274 | } |
| 275 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 276 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 277 | { |
| 278 | struct strbuf buf = STRBUF_INIT; |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 279 | struct notes_rewrite_cfg *c = NULL; |
Ævar Arnfjörð Bjarmason | ef7a8e3 | 2010-06-14 23:40:05 +0000 | [diff] [blame] | 280 | struct notes_tree *t = NULL; |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 281 | int ret = 0; |
Johan Herland | 80a1466 | 2013-06-12 02:12:59 +0200 | [diff] [blame] | 282 | const char *msg = "Notes added by 'git notes copy'"; |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 283 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 284 | if (rewrite_cmd) { |
| 285 | c = init_copy_notes_for_rewrite(rewrite_cmd); |
| 286 | if (!c) |
| 287 | return 0; |
| 288 | } else { |
| 289 | init_notes(NULL, NULL, NULL, 0); |
| 290 | t = &default_notes_tree; |
| 291 | } |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 292 | |
| 293 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { |
| 294 | unsigned char from_obj[20], to_obj[20]; |
| 295 | struct strbuf **split; |
| 296 | int err; |
| 297 | |
| 298 | split = strbuf_split(&buf, ' '); |
| 299 | if (!split[0] || !split[1]) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 300 | die(_("Malformed input line: '%s'."), buf.buf); |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 301 | strbuf_rtrim(split[0]); |
| 302 | strbuf_rtrim(split[1]); |
| 303 | if (get_sha1(split[0]->buf, from_obj)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 304 | die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf); |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 305 | if (get_sha1(split[1]->buf, to_obj)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 306 | die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf); |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 307 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 308 | if (rewrite_cmd) |
| 309 | err = copy_note_for_rewrite(c, from_obj, to_obj); |
| 310 | else |
| 311 | err = copy_note(t, from_obj, to_obj, force, |
| 312 | combine_notes_overwrite); |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 313 | |
| 314 | if (err) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 315 | error(_("Failed to copy notes from '%s' to '%s'"), |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 316 | split[0]->buf, split[1]->buf); |
| 317 | ret = 1; |
| 318 | } |
| 319 | |
| 320 | strbuf_list_free(split); |
| 321 | } |
| 322 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 323 | if (!rewrite_cmd) { |
Johan Herland | 80a1466 | 2013-06-12 02:12:59 +0200 | [diff] [blame] | 324 | commit_notes(t, msg); |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 325 | free_notes(t); |
| 326 | } else { |
Johan Herland | 80a1466 | 2013-06-12 02:12:59 +0200 | [diff] [blame] | 327 | finish_copy_notes_for_rewrite(c, msg); |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 328 | } |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 329 | return ret; |
| 330 | } |
| 331 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 332 | static struct notes_tree *init_notes_check(const char *subcommand) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 333 | { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 334 | struct notes_tree *t; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 335 | init_notes(NULL, NULL, NULL, 0); |
| 336 | t = &default_notes_tree; |
Johan Herland | a0b4dfa | 2010-02-13 22:28:24 +0100 | [diff] [blame] | 337 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 338 | if (!starts_with(t->ref, "refs/notes/")) |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 339 | die("Refusing to %s notes in %s (outside of refs/notes/)", |
| 340 | subcommand, t->ref); |
| 341 | return t; |
| 342 | } |
| 343 | |
| 344 | static int list(int argc, const char **argv, const char *prefix) |
| 345 | { |
| 346 | struct notes_tree *t; |
| 347 | unsigned char object[20]; |
| 348 | const unsigned char *note; |
| 349 | int retval = -1; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 350 | struct option options[] = { |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 351 | OPT_END() |
| 352 | }; |
| 353 | |
| 354 | if (argc) |
| 355 | argc = parse_options(argc, argv, prefix, options, |
| 356 | git_notes_list_usage, 0); |
| 357 | |
| 358 | if (1 < argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 359 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 360 | usage_with_options(git_notes_list_usage, options); |
| 361 | } |
| 362 | |
| 363 | t = init_notes_check("list"); |
| 364 | if (argc) { |
| 365 | if (get_sha1(argv[0], object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 366 | die(_("Failed to resolve '%s' as a valid ref."), argv[0]); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 367 | note = get_note(t, object); |
| 368 | if (note) { |
| 369 | puts(sha1_to_hex(note)); |
| 370 | retval = 0; |
| 371 | } else |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 372 | retval = error(_("No note found for object %s."), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 373 | sha1_to_hex(object)); |
| 374 | } else |
| 375 | retval = for_each_note(t, 0, list_each_note, NULL); |
| 376 | |
| 377 | free_notes(t); |
| 378 | return retval; |
| 379 | } |
| 380 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame] | 381 | static int append_edit(int argc, const char **argv, const char *prefix); |
| 382 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 383 | static int add(int argc, const char **argv, const char *prefix) |
| 384 | { |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 385 | int force = 0, allow_empty = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 386 | const char *object_ref; |
| 387 | struct notes_tree *t; |
| 388 | unsigned char object[20], new_note[20]; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 389 | const unsigned char *note; |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 390 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 391 | struct option options[] = { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 392 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 393 | N_("note contents as a string"), PARSE_OPT_NONEG, |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 394 | parse_msg_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 395 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 396 | N_("note contents in a file"), PARSE_OPT_NONEG, |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 397 | parse_file_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 398 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 399 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 400 | parse_reedit_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 401 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 402 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 403 | parse_reuse_arg}, |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 404 | OPT_BOOL(0, "allow-empty", &allow_empty, |
| 405 | N_("allow storing empty note")), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 406 | OPT__FORCE(&force, N_("replace existing notes")), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 407 | OPT_END() |
| 408 | }; |
| 409 | |
| 410 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame] | 411 | PARSE_OPT_KEEP_ARGV0); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 412 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame] | 413 | if (2 < argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 414 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 415 | usage_with_options(git_notes_add_usage, options); |
| 416 | } |
| 417 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame] | 418 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 419 | |
| 420 | if (get_sha1(object_ref, object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 421 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 422 | |
| 423 | t = init_notes_check("add"); |
| 424 | note = get_note(t, object); |
| 425 | |
| 426 | if (note) { |
| 427 | if (!force) { |
Johan Herland | b0de56c | 2014-11-12 01:40:12 +0100 | [diff] [blame] | 428 | free_notes(t); |
| 429 | if (d.given) { |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 430 | free_note_data(&d); |
Johan Herland | b0de56c | 2014-11-12 01:40:12 +0100 | [diff] [blame] | 431 | return error(_("Cannot add notes. " |
| 432 | "Found existing notes for object %s. " |
| 433 | "Use '-f' to overwrite existing notes"), |
| 434 | sha1_to_hex(object)); |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame] | 435 | } |
Johan Herland | b0de56c | 2014-11-12 01:40:12 +0100 | [diff] [blame] | 436 | /* |
| 437 | * Redirect to "edit" subcommand. |
| 438 | * |
| 439 | * We only end up here if none of -m/-F/-c/-C or -f are |
| 440 | * given. The original args are therefore still in |
| 441 | * argv[0-1]. |
| 442 | */ |
| 443 | argv[0] = "edit"; |
| 444 | return append_edit(argc, argv, prefix); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 445 | } |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 446 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 447 | sha1_to_hex(object)); |
| 448 | } |
| 449 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 450 | prepare_note_data(object, &d, note); |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 451 | if (d.buf.len || allow_empty) { |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 452 | write_note_data(&d, new_note); |
| 453 | if (add_note(t, object, new_note, combine_notes_overwrite)) |
| 454 | die("BUG: combine_notes_overwrite failed"); |
| 455 | commit_notes(t, "Notes added by 'git notes add'"); |
| 456 | } else { |
| 457 | fprintf(stderr, _("Removing note for object %s\n"), |
| 458 | sha1_to_hex(object)); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 459 | remove_note(t, object); |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 460 | commit_notes(t, "Notes removed by 'git notes add'"); |
| 461 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 462 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 463 | free_note_data(&d); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 464 | free_notes(t); |
Johan Herland | b0de56c | 2014-11-12 01:40:12 +0100 | [diff] [blame] | 465 | return 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static int copy(int argc, const char **argv, const char *prefix) |
| 469 | { |
| 470 | int retval = 0, force = 0, from_stdin = 0; |
| 471 | const unsigned char *from_note, *note; |
| 472 | const char *object_ref; |
| 473 | unsigned char object[20], from_obj[20]; |
| 474 | struct notes_tree *t; |
| 475 | const char *rewrite_cmd = NULL; |
| 476 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 477 | OPT__FORCE(&force, N_("replace existing notes")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 478 | OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 479 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), |
| 480 | N_("load rewriting config for <command> (implies " |
| 481 | "--stdin)")), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 482 | OPT_END() |
| 483 | }; |
| 484 | |
| 485 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
| 486 | 0); |
| 487 | |
| 488 | if (from_stdin || rewrite_cmd) { |
| 489 | if (argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 490 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 491 | usage_with_options(git_notes_copy_usage, options); |
| 492 | } else { |
| 493 | return notes_copy_from_stdin(force, rewrite_cmd); |
| 494 | } |
| 495 | } |
| 496 | |
Jeff King | bbb1b8a | 2010-06-28 04:59:07 -0400 | [diff] [blame] | 497 | if (argc < 2) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 498 | error(_("too few parameters")); |
Jeff King | bbb1b8a | 2010-06-28 04:59:07 -0400 | [diff] [blame] | 499 | usage_with_options(git_notes_copy_usage, options); |
| 500 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 501 | if (2 < argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 502 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 503 | usage_with_options(git_notes_copy_usage, options); |
| 504 | } |
| 505 | |
| 506 | if (get_sha1(argv[0], from_obj)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 507 | die(_("Failed to resolve '%s' as a valid ref."), argv[0]); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 508 | |
| 509 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 510 | |
| 511 | if (get_sha1(object_ref, object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 512 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 513 | |
| 514 | t = init_notes_check("copy"); |
| 515 | note = get_note(t, object); |
| 516 | |
| 517 | if (note) { |
| 518 | if (!force) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 519 | retval = error(_("Cannot copy notes. Found existing " |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 520 | "notes for object %s. Use '-f' to " |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 521 | "overwrite existing notes"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 522 | sha1_to_hex(object)); |
| 523 | goto out; |
| 524 | } |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 525 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 526 | sha1_to_hex(object)); |
| 527 | } |
| 528 | |
| 529 | from_note = get_note(t, from_obj); |
| 530 | if (!from_note) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 531 | retval = error(_("Missing notes on source object %s. Cannot " |
| 532 | "copy."), sha1_to_hex(from_obj)); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 533 | goto out; |
| 534 | } |
| 535 | |
Johan Herland | 180619a | 2010-11-15 00:52:26 +0100 | [diff] [blame] | 536 | if (add_note(t, object, from_note, combine_notes_overwrite)) |
| 537 | die("BUG: combine_notes_overwrite failed"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 538 | commit_notes(t, "Notes added by 'git notes copy'"); |
| 539 | out: |
| 540 | free_notes(t); |
| 541 | return retval; |
| 542 | } |
| 543 | |
| 544 | static int append_edit(int argc, const char **argv, const char *prefix) |
| 545 | { |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 546 | int allow_empty = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 547 | const char *object_ref; |
| 548 | struct notes_tree *t; |
| 549 | unsigned char object[20], new_note[20]; |
| 550 | const unsigned char *note; |
| 551 | char logmsg[100]; |
| 552 | const char * const *usage; |
Johan Herland | 4282af0 | 2014-11-09 13:30:50 +0100 | [diff] [blame] | 553 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 554 | struct option options[] = { |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 555 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 556 | N_("note contents as a string"), PARSE_OPT_NONEG, |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 557 | parse_msg_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 558 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 559 | N_("note contents in a file"), PARSE_OPT_NONEG, |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 560 | parse_file_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 561 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 562 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 563 | parse_reedit_arg}, |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 564 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 565 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 566 | parse_reuse_arg}, |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 567 | OPT_BOOL(0, "allow-empty", &allow_empty, |
| 568 | N_("allow storing empty note")), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 569 | OPT_END() |
| 570 | }; |
| 571 | int edit = !strcmp(argv[0], "edit"); |
| 572 | |
| 573 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; |
| 574 | argc = parse_options(argc, argv, prefix, options, usage, |
| 575 | PARSE_OPT_KEEP_ARGV0); |
| 576 | |
| 577 | if (2 < argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 578 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 579 | usage_with_options(usage, options); |
| 580 | } |
| 581 | |
Johan Herland | bebf5c0 | 2014-11-09 13:30:49 +0100 | [diff] [blame] | 582 | if (d.given && edit) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 583 | fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 584 | "for the 'edit' subcommand.\n" |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 585 | "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 586 | |
| 587 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 588 | |
| 589 | if (get_sha1(object_ref, object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 590 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 591 | |
| 592 | t = init_notes_check(argv[0]); |
| 593 | note = get_note(t, object); |
| 594 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 595 | prepare_note_data(object, &d, edit ? note : NULL); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 596 | |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 597 | if (note && !edit) { |
| 598 | /* Append buf to previous note contents */ |
| 599 | unsigned long size; |
| 600 | enum object_type type; |
| 601 | char *prev_buf = read_sha1_file(note, &type, &size); |
| 602 | |
| 603 | strbuf_grow(&d.buf, size + 1); |
| 604 | if (d.buf.len && prev_buf && size) |
| 605 | strbuf_insert(&d.buf, 0, "\n", 1); |
| 606 | if (prev_buf && size) |
| 607 | strbuf_insert(&d.buf, 0, prev_buf, size); |
| 608 | free(prev_buf); |
| 609 | } |
| 610 | |
Johan Herland | d73a5b9 | 2014-11-12 01:40:14 +0100 | [diff] [blame] | 611 | if (d.buf.len || allow_empty) { |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 612 | write_note_data(&d, new_note); |
| 613 | if (add_note(t, object, new_note, combine_notes_overwrite)) |
| 614 | die("BUG: combine_notes_overwrite failed"); |
| 615 | snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'", |
| 616 | argv[0]); |
| 617 | } else { |
| 618 | fprintf(stderr, _("Removing note for object %s\n"), |
| 619 | sha1_to_hex(object)); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 620 | remove_note(t, object); |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 621 | snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'", |
| 622 | argv[0]); |
| 623 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 624 | commit_notes(t, logmsg); |
Johan Herland | 52694cd | 2014-11-12 01:40:13 +0100 | [diff] [blame] | 625 | |
| 626 | free_note_data(&d); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 627 | free_notes(t); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int show(int argc, const char **argv, const char *prefix) |
| 632 | { |
| 633 | const char *object_ref; |
| 634 | struct notes_tree *t; |
| 635 | unsigned char object[20]; |
| 636 | const unsigned char *note; |
| 637 | int retval; |
| 638 | struct option options[] = { |
| 639 | OPT_END() |
| 640 | }; |
| 641 | |
| 642 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, |
| 643 | 0); |
| 644 | |
| 645 | if (1 < argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 646 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 647 | usage_with_options(git_notes_show_usage, options); |
| 648 | } |
| 649 | |
| 650 | object_ref = argc ? argv[0] : "HEAD"; |
| 651 | |
| 652 | if (get_sha1(object_ref, object)) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 653 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 654 | |
| 655 | t = init_notes_check("show"); |
| 656 | note = get_note(t, object); |
| 657 | |
| 658 | if (!note) |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 659 | retval = error(_("No note found for object %s."), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 660 | sha1_to_hex(object)); |
| 661 | else { |
| 662 | const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; |
| 663 | retval = execv_git_cmd(show_args); |
| 664 | } |
| 665 | free_notes(t); |
| 666 | return retval; |
| 667 | } |
| 668 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 669 | static int merge_abort(struct notes_merge_options *o) |
| 670 | { |
| 671 | int ret = 0; |
| 672 | |
| 673 | /* |
| 674 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call |
| 675 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. |
| 676 | */ |
| 677 | |
| 678 | if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0)) |
| 679 | ret += error("Failed to delete ref NOTES_MERGE_PARTIAL"); |
| 680 | if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF)) |
| 681 | ret += error("Failed to delete ref NOTES_MERGE_REF"); |
| 682 | if (notes_merge_abort(o)) |
| 683 | ret += error("Failed to remove 'git notes merge' worktree"); |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static int merge_commit(struct notes_merge_options *o) |
| 688 | { |
| 689 | struct strbuf msg = STRBUF_INIT; |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 690 | unsigned char sha1[20], parent_sha1[20]; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 691 | struct notes_tree *t; |
| 692 | struct commit *partial; |
| 693 | struct pretty_print_context pretty_ctx; |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 694 | void *local_ref_to_free; |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 695 | int ret; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, |
| 699 | * and target notes ref from .git/NOTES_MERGE_REF. |
| 700 | */ |
| 701 | |
| 702 | if (get_sha1("NOTES_MERGE_PARTIAL", sha1)) |
| 703 | die("Failed to read ref NOTES_MERGE_PARTIAL"); |
| 704 | else if (!(partial = lookup_commit_reference(sha1))) |
| 705 | die("Could not find commit from NOTES_MERGE_PARTIAL."); |
| 706 | else if (parse_commit(partial)) |
| 707 | die("Could not parse commit from NOTES_MERGE_PARTIAL."); |
| 708 | |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 709 | if (partial->parents) |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame^] | 710 | hashcpy(parent_sha1, partial->parents->item->object.oid.hash); |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 711 | else |
| 712 | hashclr(parent_sha1); |
| 713 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 714 | t = xcalloc(1, sizeof(struct notes_tree)); |
| 715 | init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); |
| 716 | |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 717 | o->local_ref = local_ref_to_free = |
Ronnie Sahlberg | 7695d11 | 2014-07-15 12:59:36 -0700 | [diff] [blame] | 718 | resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 719 | if (!o->local_ref) |
| 720 | die("Failed to resolve NOTES_MERGE_REF"); |
| 721 | |
| 722 | if (notes_merge_commit(o, t, partial, sha1)) |
| 723 | die("Failed to finalize notes merge"); |
| 724 | |
| 725 | /* Reuse existing commit message in reflog message */ |
| 726 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); |
| 727 | format_commit_message(partial, "%s", &msg, &pretty_ctx); |
| 728 | strbuf_trim(&msg); |
| 729 | strbuf_insert(&msg, 0, "notes: ", 7); |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 730 | update_ref(msg.buf, o->local_ref, sha1, |
| 731 | is_null_sha1(parent_sha1) ? NULL : parent_sha1, |
Michael Haggerty | f412411 | 2014-04-07 15:47:56 +0200 | [diff] [blame] | 732 | 0, UPDATE_REFS_DIE_ON_ERR); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 733 | |
| 734 | free_notes(t); |
| 735 | strbuf_release(&msg); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 736 | ret = merge_abort(o); |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 737 | free(local_ref_to_free); |
Nguyễn Thái Ngọc Duy | d5a35c1 | 2011-11-13 17:22:15 +0700 | [diff] [blame] | 738 | return ret; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Jacob Keller | d2d68d9 | 2015-08-17 14:33:33 -0700 | [diff] [blame] | 741 | static int git_config_get_notes_strategy(const char *key, |
| 742 | enum notes_merge_strategy *strategy) |
| 743 | { |
| 744 | const char *value; |
| 745 | |
| 746 | if (git_config_get_string_const(key, &value)) |
| 747 | return 1; |
| 748 | if (parse_notes_merge_strategy(value, strategy)) |
| 749 | git_die_config(key, "unknown notes merge strategy %s", value); |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 754 | static int merge(int argc, const char **argv, const char *prefix) |
| 755 | { |
| 756 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; |
| 757 | unsigned char result_sha1[20]; |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 758 | struct notes_tree *t; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 759 | struct notes_merge_options o; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 760 | int do_merge = 0, do_commit = 0, do_abort = 0; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 761 | int verbosity = 0, result; |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 762 | const char *strategy = NULL; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 763 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 764 | OPT_GROUP(N_("General options")), |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 765 | OPT__VERBOSITY(&verbosity), |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 766 | OPT_GROUP(N_("Merge options")), |
| 767 | OPT_STRING('s', "strategy", &strategy, N_("strategy"), |
| 768 | N_("resolve notes conflicts using the given strategy " |
| 769 | "(manual/ours/theirs/union/cat_sort_uniq)")), |
| 770 | OPT_GROUP(N_("Committing unmerged notes")), |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 771 | { OPTION_SET_INT, 0, "commit", &do_commit, NULL, |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 772 | N_("finalize notes merge by committing unmerged notes"), |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 773 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 774 | OPT_GROUP(N_("Aborting notes merge resolution")), |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 775 | { OPTION_SET_INT, 0, "abort", &do_abort, NULL, |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 776 | N_("abort notes merge"), |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 777 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 778 | OPT_END() |
| 779 | }; |
| 780 | |
| 781 | argc = parse_options(argc, argv, prefix, options, |
| 782 | git_notes_merge_usage, 0); |
| 783 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 784 | if (strategy || do_commit + do_abort == 0) |
| 785 | do_merge = 1; |
| 786 | if (do_merge + do_commit + do_abort != 1) { |
| 787 | error("cannot mix --commit, --abort or -s/--strategy"); |
| 788 | usage_with_options(git_notes_merge_usage, options); |
| 789 | } |
| 790 | |
| 791 | if (do_merge && argc != 1) { |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 792 | error("Must specify a notes ref to merge"); |
| 793 | usage_with_options(git_notes_merge_usage, options); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 794 | } else if (!do_merge && argc) { |
| 795 | error("too many parameters"); |
| 796 | usage_with_options(git_notes_merge_usage, options); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | init_notes_merge_options(&o); |
| 800 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; |
| 801 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 802 | if (do_abort) |
| 803 | return merge_abort(&o); |
| 804 | if (do_commit) |
| 805 | return merge_commit(&o); |
| 806 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 807 | o.local_ref = default_notes_ref(); |
| 808 | strbuf_addstr(&remote_ref, argv[0]); |
| 809 | expand_notes_ref(&remote_ref); |
| 810 | o.remote_ref = remote_ref.buf; |
| 811 | |
Jacob Keller | d2d68d9 | 2015-08-17 14:33:33 -0700 | [diff] [blame] | 812 | t = init_notes_check("merge"); |
| 813 | |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 814 | if (strategy) { |
Jacob Keller | 93efcad | 2015-08-17 14:33:31 -0700 | [diff] [blame] | 815 | if (parse_notes_merge_strategy(strategy, &o.strategy)) { |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 816 | error("Unknown -s/--strategy: %s", strategy); |
| 817 | usage_with_options(git_notes_merge_usage, options); |
| 818 | } |
Jacob Keller | d2d68d9 | 2015-08-17 14:33:33 -0700 | [diff] [blame] | 819 | } else { |
Jacob Keller | 4f655e2 | 2015-08-17 14:33:34 -0700 | [diff] [blame] | 820 | struct strbuf merge_key = STRBUF_INIT; |
| 821 | const char *short_ref = NULL; |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 822 | |
Jacob Keller | 4f655e2 | 2015-08-17 14:33:34 -0700 | [diff] [blame] | 823 | if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref)) |
| 824 | die("BUG: local ref %s is outside of refs/notes/", |
| 825 | o.local_ref); |
| 826 | |
| 827 | strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref); |
| 828 | |
| 829 | if (git_config_get_notes_strategy(merge_key.buf, &o.strategy)) |
| 830 | git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy); |
| 831 | |
| 832 | strbuf_release(&merge_key); |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 833 | } |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 834 | |
| 835 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", |
| 836 | remote_ref.buf, default_notes_ref()); |
Johan Herland | 443259c | 2010-11-09 22:49:53 +0100 | [diff] [blame] | 837 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 838 | |
| 839 | result = notes_merge(&o, t, result_sha1); |
| 840 | |
| 841 | if (result >= 0) /* Merge resulted (trivially) in result_sha1 */ |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 842 | /* Update default notes ref with new commit */ |
| 843 | update_ref(msg.buf, default_notes_ref(), result_sha1, NULL, |
Michael Haggerty | f412411 | 2014-04-07 15:47:56 +0200 | [diff] [blame] | 844 | 0, UPDATE_REFS_DIE_ON_ERR); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 845 | else { /* Merge has unresolved conflicts */ |
David Turner | b02e859 | 2015-08-10 13:52:45 -0400 | [diff] [blame] | 846 | char *existing; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 847 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ |
| 848 | update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL, |
Michael Haggerty | f412411 | 2014-04-07 15:47:56 +0200 | [diff] [blame] | 849 | 0, UPDATE_REFS_DIE_ON_ERR); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 850 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
David Turner | b02e859 | 2015-08-10 13:52:45 -0400 | [diff] [blame] | 851 | existing = find_shared_symref("NOTES_MERGE_REF", default_notes_ref()); |
| 852 | if (existing) |
| 853 | die(_("A notes merge into %s is already in-progress at %s"), |
| 854 | default_notes_ref(), existing); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 855 | if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) |
| 856 | die("Failed to store link to current notes ref (%s)", |
| 857 | default_notes_ref()); |
| 858 | printf("Automatic notes merge failed. Fix conflicts in %s and " |
| 859 | "commit the result with 'git notes merge --commit', or " |
| 860 | "abort the merge with 'git notes merge --abort'.\n", |
Johan Herland | 809f38c | 2010-11-09 22:49:51 +0100 | [diff] [blame] | 861 | git_path(NOTES_MERGE_WORKTREE)); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 862 | } |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 863 | |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 864 | free_notes(t); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 865 | strbuf_release(&remote_ref); |
| 866 | strbuf_release(&msg); |
Johan Herland | 809f38c | 2010-11-09 22:49:51 +0100 | [diff] [blame] | 867 | return result < 0; /* return non-zero on conflicts */ |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 870 | #define IGNORE_MISSING 1 |
Junio C Hamano | 2d370d2 | 2011-05-18 16:02:58 -0700 | [diff] [blame] | 871 | |
| 872 | static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 873 | { |
| 874 | int status; |
| 875 | unsigned char sha1[20]; |
| 876 | if (get_sha1(name, sha1)) |
| 877 | return error(_("Failed to resolve '%s' as a valid ref."), name); |
| 878 | status = remove_note(t, sha1); |
| 879 | if (status) |
| 880 | fprintf(stderr, _("Object %s has no note\n"), name); |
| 881 | else |
| 882 | fprintf(stderr, _("Removing note for object %s\n"), name); |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 883 | return (flag & IGNORE_MISSING) ? 0 : status; |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 886 | static int remove_cmd(int argc, const char **argv, const char *prefix) |
| 887 | { |
Junio C Hamano | 2d370d2 | 2011-05-18 16:02:58 -0700 | [diff] [blame] | 888 | unsigned flag = 0; |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 889 | int from_stdin = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 890 | struct option options[] = { |
Junio C Hamano | 2d370d2 | 2011-05-18 16:02:58 -0700 | [diff] [blame] | 891 | OPT_BIT(0, "ignore-missing", &flag, |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 892 | N_("attempt to remove non-existent note is not an error"), |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 893 | IGNORE_MISSING), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 894 | OPT_BOOL(0, "stdin", &from_stdin, |
Nguyễn Thái Ngọc Duy | e6b895e | 2012-08-20 19:32:28 +0700 | [diff] [blame] | 895 | N_("read object names from the standard input")), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 896 | OPT_END() |
| 897 | }; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 898 | struct notes_tree *t; |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 899 | int retval = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 900 | |
| 901 | argc = parse_options(argc, argv, prefix, options, |
| 902 | git_notes_remove_usage, 0); |
| 903 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 904 | t = init_notes_check("remove"); |
| 905 | |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 906 | if (!argc && !from_stdin) { |
Junio C Hamano | 2d370d2 | 2011-05-18 16:02:58 -0700 | [diff] [blame] | 907 | retval = remove_one_note(t, "HEAD", flag); |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 908 | } else { |
| 909 | while (*argv) { |
Junio C Hamano | 2d370d2 | 2011-05-18 16:02:58 -0700 | [diff] [blame] | 910 | retval |= remove_one_note(t, *argv, flag); |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 911 | argv++; |
| 912 | } |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 913 | } |
Junio C Hamano | 4653801 | 2011-05-18 16:44:30 -0700 | [diff] [blame] | 914 | if (from_stdin) { |
| 915 | struct strbuf sb = STRBUF_INIT; |
| 916 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { |
| 917 | strbuf_rtrim(&sb); |
| 918 | retval |= remove_one_note(t, sb.buf, flag); |
| 919 | } |
| 920 | strbuf_release(&sb); |
| 921 | } |
Junio C Hamano | c3ab1a8 | 2011-05-18 15:44:37 -0700 | [diff] [blame] | 922 | if (!retval) |
| 923 | commit_notes(t, "Notes removed by 'git notes remove'"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 924 | free_notes(t); |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 925 | return retval; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static int prune(int argc, const char **argv, const char *prefix) |
| 929 | { |
| 930 | struct notes_tree *t; |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 931 | int show_only = 0, verbose = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 932 | struct option options[] = { |
René Scharfe | e21adb8 | 2010-11-08 18:58:51 +0100 | [diff] [blame] | 933 | OPT__DRY_RUN(&show_only, "do not remove, show only"), |
René Scharfe | fd03881 | 2010-11-08 18:56:39 +0100 | [diff] [blame] | 934 | OPT__VERBOSE(&verbose, "report pruned notes"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 935 | OPT_END() |
| 936 | }; |
| 937 | |
| 938 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, |
| 939 | 0); |
| 940 | |
| 941 | if (argc) { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 942 | error(_("too many parameters")); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 943 | usage_with_options(git_notes_prune_usage, options); |
| 944 | } |
| 945 | |
| 946 | t = init_notes_check("prune"); |
| 947 | |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 948 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
| 949 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); |
| 950 | if (!show_only) |
| 951 | commit_notes(t, "Notes removed by 'git notes prune'"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 952 | free_notes(t); |
| 953 | return 0; |
| 954 | } |
| 955 | |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 956 | static int get_ref(int argc, const char **argv, const char *prefix) |
| 957 | { |
| 958 | struct option options[] = { OPT_END() }; |
| 959 | argc = parse_options(argc, argv, prefix, options, |
| 960 | git_notes_get_ref_usage, 0); |
| 961 | |
| 962 | if (argc) { |
| 963 | error("too many parameters"); |
| 964 | usage_with_options(git_notes_get_ref_usage, options); |
| 965 | } |
| 966 | |
| 967 | puts(default_notes_ref()); |
| 968 | return 0; |
| 969 | } |
| 970 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 971 | int cmd_notes(int argc, const char **argv, const char *prefix) |
| 972 | { |
| 973 | int result; |
| 974 | const char *override_notes_ref = NULL; |
| 975 | struct option options[] = { |
Junio C Hamano | e703d71 | 2014-03-23 15:58:12 -0700 | [diff] [blame] | 976 | OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 977 | N_("use notes from <notes-ref>")), |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 978 | OPT_END() |
| 979 | }; |
| 980 | |
| 981 | git_config(git_default_config, NULL); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 982 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, |
| 983 | PARSE_OPT_STOP_AT_NON_OPTION); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 984 | |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 985 | if (override_notes_ref) { |
| 986 | struct strbuf sb = STRBUF_INIT; |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 987 | strbuf_addstr(&sb, override_notes_ref); |
Johan Herland | 8ef313e | 2010-11-09 22:49:45 +0100 | [diff] [blame] | 988 | expand_notes_ref(&sb); |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 989 | setenv("GIT_NOTES_REF", sb.buf, 1); |
| 990 | strbuf_release(&sb); |
| 991 | } |
| 992 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 993 | if (argc < 1 || !strcmp(argv[0], "list")) |
| 994 | result = list(argc, argv, prefix); |
| 995 | else if (!strcmp(argv[0], "add")) |
| 996 | result = add(argc, argv, prefix); |
| 997 | else if (!strcmp(argv[0], "copy")) |
| 998 | result = copy(argc, argv, prefix); |
| 999 | else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) |
| 1000 | result = append_edit(argc, argv, prefix); |
| 1001 | else if (!strcmp(argv[0], "show")) |
| 1002 | result = show(argc, argv, prefix); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 1003 | else if (!strcmp(argv[0], "merge")) |
| 1004 | result = merge(argc, argv, prefix); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1005 | else if (!strcmp(argv[0], "remove")) |
| 1006 | result = remove_cmd(argc, argv, prefix); |
| 1007 | else if (!strcmp(argv[0], "prune")) |
| 1008 | result = prune(argc, argv, prefix); |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 1009 | else if (!strcmp(argv[0], "get-ref")) |
| 1010 | result = get_ref(argc, argv, prefix); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1011 | else { |
Ævar Arnfjörð Bjarmason | caeba0e | 2011-02-22 23:42:26 +0000 | [diff] [blame] | 1012 | result = error(_("Unknown subcommand: %s"), argv[0]); |
Johan Herland | 92b3385 | 2010-02-13 22:28:25 +0100 | [diff] [blame] | 1013 | usage_with_options(git_notes_usage, options); |
| 1014 | } |
| 1015 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1016 | return result ? 1 : 0; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 1017 | } |