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