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