| 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 | #define USE_THE_REPOSITORY_VARIABLE |
| 10 | #include "builtin.h" |
| 11 | #include "config.h" |
| 12 | #include "editor.h" |
| 13 | #include "environment.h" |
| 14 | #include "gettext.h" |
| 15 | #include "hex.h" |
| 16 | #include "notes.h" |
| 17 | #include "object-file.h" |
| 18 | #include "object-name.h" |
| 19 | #include "odb.h" |
| 20 | #include "path.h" |
| 21 | |
| 22 | #include "pretty.h" |
| 23 | #include "refs.h" |
| 24 | #include "exec-cmd.h" |
| 25 | #include "run-command.h" |
| 26 | #include "parse-options.h" |
| 27 | #include "string-list.h" |
| 28 | #include "notes-merge.h" |
| 29 | #include "notes-utils.h" |
| 30 | #include "worktree.h" |
| 31 | #include "write-or-die.h" |
| 32 | |
| 33 | static const char *separator = "\n"; |
| 34 | static const char * const git_notes_usage[] = { |
| 35 | N_("git notes [--ref <notes-ref>] [list [<object>]]"), |
| 36 | N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"), |
| 37 | N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"), |
| 38 | N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>] [-e]"), |
| 39 | N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"), |
| 40 | N_("git notes [--ref <notes-ref>] show [<object>]"), |
| 41 | N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"), |
| 42 | "git notes merge --commit [-v | -q]", |
| 43 | "git notes merge --abort [-v | -q]", |
| 44 | N_("git notes [--ref <notes-ref>] remove [<object>...]"), |
| 45 | N_("git notes [--ref <notes-ref>] prune [-n] [-v]"), |
| 46 | N_("git notes [--ref <notes-ref>] get-ref"), |
| 47 | NULL |
| 48 | }; |
| 49 | |
| 50 | static const char * const git_notes_list_usage[] = { |
| 51 | N_("git notes [list [<object>]]"), |
| 52 | NULL |
| 53 | }; |
| 54 | |
| 55 | static const char * const git_notes_add_usage[] = { |
| 56 | N_("git notes add [<options>] [<object>]"), |
| 57 | NULL |
| 58 | }; |
| 59 | |
| 60 | static const char * const git_notes_copy_usage[] = { |
| 61 | N_("git notes copy [<options>] <from-object> <to-object>"), |
| 62 | N_("git notes copy --stdin [<from-object> <to-object>]..."), |
| 63 | NULL |
| 64 | }; |
| 65 | |
| 66 | static const char * const git_notes_append_usage[] = { |
| 67 | N_("git notes append [<options>] [<object>]"), |
| 68 | NULL |
| 69 | }; |
| 70 | |
| 71 | static const char * const git_notes_edit_usage[] = { |
| 72 | N_("git notes edit [<object>]"), |
| 73 | NULL |
| 74 | }; |
| 75 | |
| 76 | static const char * const git_notes_show_usage[] = { |
| 77 | N_("git notes show [<object>]"), |
| 78 | NULL |
| 79 | }; |
| 80 | |
| 81 | static const char * const git_notes_merge_usage[] = { |
| 82 | N_("git notes merge [<options>] <notes-ref>"), |
| 83 | N_("git notes merge --commit [<options>]"), |
| 84 | N_("git notes merge --abort [<options>]"), |
| 85 | NULL |
| 86 | }; |
| 87 | |
| 88 | static const char * const git_notes_remove_usage[] = { |
| 89 | N_("git notes remove [<object>]"), |
| 90 | NULL |
| 91 | }; |
| 92 | |
| 93 | static const char * const git_notes_prune_usage[] = { |
| 94 | N_("git notes prune [<options>]"), |
| 95 | NULL |
| 96 | }; |
| 97 | |
| 98 | static const char * const git_notes_get_ref_usage[] = { |
| 99 | "git notes get-ref", |
| 100 | NULL |
| 101 | }; |
| 102 | |
| 103 | static const char note_template[] = |
| 104 | N_("Write/edit the notes for the following object:"); |
| 105 | |
| 106 | enum notes_stripspace { |
| 107 | UNSPECIFIED = -1, |
| 108 | NO_STRIPSPACE = 0, |
| 109 | STRIPSPACE = 1, |
| 110 | }; |
| 111 | |
| 112 | struct note_msg { |
| 113 | enum notes_stripspace stripspace; |
| 114 | struct strbuf buf; |
| 115 | }; |
| 116 | |
| 117 | struct note_data { |
| 118 | int use_editor; |
| 119 | int stripspace; |
| 120 | char *edit_path; |
| 121 | struct strbuf buf; |
| 122 | struct note_msg **messages; |
| 123 | size_t msg_nr; |
| 124 | size_t msg_alloc; |
| 125 | }; |
| 126 | |
| 127 | static void free_note_data(struct note_data *d) |
| 128 | { |
| 129 | if (d->edit_path) { |
| 130 | unlink_or_warn(d->edit_path); |
| 131 | free(d->edit_path); |
| 132 | } |
| 133 | strbuf_release(&d->buf); |
| 134 | |
| 135 | while (d->msg_nr--) { |
| 136 | strbuf_release(&d->messages[d->msg_nr]->buf); |
| 137 | free(d->messages[d->msg_nr]); |
| 138 | } |
| 139 | free(d->messages); |
| 140 | } |
| 141 | |
| 142 | static int list_each_note(const struct object_id *object_oid, |
| 143 | const struct object_id *note_oid, |
| 144 | char *note_path UNUSED, |
| 145 | void *cb_data UNUSED) |
| 146 | { |
| 147 | printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid)); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void copy_obj_to_fd(int fd, const struct object_id *oid) |
| 152 | { |
| 153 | size_t size; |
| 154 | enum object_type type; |
| 155 | char *buf = odb_read_object(the_repository->objects, oid, &type, &size); |
| 156 | if (buf) { |
| 157 | if (size) |
| 158 | write_or_die(fd, buf, size); |
| 159 | free(buf); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void write_commented_object(int fd, const struct object_id *object) |
| 164 | { |
| 165 | struct child_process show = CHILD_PROCESS_INIT; |
| 166 | struct strbuf buf = STRBUF_INIT; |
| 167 | struct strbuf cbuf = STRBUF_INIT; |
| 168 | |
| 169 | /* Invoke "git show --stat --no-notes $object" */ |
| 170 | strvec_pushl(&show.args, "show", "--stat", "--no-notes", |
| 171 | oid_to_hex(object), NULL); |
| 172 | show.no_stdin = 1; |
| 173 | show.out = -1; |
| 174 | show.err = 0; |
| 175 | show.git_cmd = 1; |
| 176 | if (start_command(&show)) |
| 177 | die(_("unable to start 'show' for object '%s'"), |
| 178 | oid_to_hex(object)); |
| 179 | |
| 180 | if (strbuf_read(&buf, show.out, 0) < 0) |
| 181 | die_errno(_("could not read 'show' output")); |
| 182 | strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str); |
| 183 | write_or_die(fd, cbuf.buf, cbuf.len); |
| 184 | |
| 185 | strbuf_release(&cbuf); |
| 186 | strbuf_release(&buf); |
| 187 | |
| 188 | if (finish_command(&show)) |
| 189 | die(_("failed to finish 'show' for object '%s'"), |
| 190 | oid_to_hex(object)); |
| 191 | } |
| 192 | |
| 193 | static void prepare_note_data(const struct object_id *object, struct note_data *d, |
| 194 | const struct object_id *old_note) |
| 195 | { |
| 196 | if (d->use_editor || !d->msg_nr) { |
| 197 | int fd; |
| 198 | struct strbuf buf = STRBUF_INIT; |
| 199 | |
| 200 | /* write the template message before editing: */ |
| 201 | d->edit_path = repo_git_path(the_repository, "NOTES_EDITMSG"); |
| 202 | fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 203 | |
| 204 | if (d->msg_nr) |
| 205 | write_or_die(fd, d->buf.buf, d->buf.len); |
| 206 | else if (old_note) |
| 207 | copy_obj_to_fd(fd, old_note); |
| 208 | |
| 209 | strbuf_addch(&buf, '\n'); |
| 210 | strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str); |
| 211 | strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)), |
| 212 | comment_line_str); |
| 213 | strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str); |
| 214 | write_or_die(fd, buf.buf, buf.len); |
| 215 | |
| 216 | write_commented_object(fd, object); |
| 217 | |
| 218 | close(fd); |
| 219 | strbuf_release(&buf); |
| 220 | strbuf_reset(&d->buf); |
| 221 | |
| 222 | if (launch_editor(d->edit_path, &d->buf, NULL)) { |
| 223 | die(_("please supply the note contents using either -m or -F option")); |
| 224 | } |
| 225 | if (d->stripspace) |
| 226 | strbuf_stripspace(&d->buf, comment_line_str); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static void write_note_data(struct note_data *d, struct object_id *oid) |
| 231 | { |
| 232 | if (odb_write_object(the_repository->objects, d->buf.buf, |
| 233 | d->buf.len, OBJ_BLOB, oid)) { |
| 234 | int status = die_message(_("unable to write note object")); |
| 235 | |
| 236 | if (d->edit_path) |
| 237 | die_message(_("the note contents have been left in %s"), |
| 238 | d->edit_path); |
| 239 | exit(status); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void append_separator(struct strbuf *message) |
| 244 | { |
| 245 | size_t sep_len = 0; |
| 246 | |
| 247 | if (!separator) |
| 248 | return; |
| 249 | else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n') |
| 250 | strbuf_addstr(message, separator); |
| 251 | else |
| 252 | strbuf_addf(message, "%s%s", separator, "\n"); |
| 253 | } |
| 254 | |
| 255 | static void concat_messages(struct note_data *d) |
| 256 | { |
| 257 | struct strbuf msg = STRBUF_INIT; |
| 258 | size_t i; |
| 259 | |
| 260 | for (i = 0; i < d->msg_nr ; i++) { |
| 261 | if (d->buf.len) |
| 262 | append_separator(&d->buf); |
| 263 | strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len); |
| 264 | strbuf_addbuf(&d->buf, &msg); |
| 265 | if ((d->stripspace == UNSPECIFIED && |
| 266 | d->messages[i]->stripspace == STRIPSPACE) || |
| 267 | d->stripspace == STRIPSPACE) |
| 268 | strbuf_stripspace(&d->buf, NULL); |
| 269 | strbuf_reset(&msg); |
| 270 | } |
| 271 | strbuf_release(&msg); |
| 272 | } |
| 273 | |
| 274 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
| 275 | { |
| 276 | struct note_data *d = opt->value; |
| 277 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
| 278 | |
| 279 | BUG_ON_OPT_NEG(unset); |
| 280 | |
| 281 | strbuf_init(&msg->buf, strlen(arg)); |
| 282 | strbuf_addstr(&msg->buf, arg); |
| 283 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); |
| 284 | d->messages[d->msg_nr - 1] = msg; |
| 285 | msg->stripspace = STRIPSPACE; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int parse_file_arg(const struct option *opt, const char *arg, int unset) |
| 290 | { |
| 291 | struct note_data *d = opt->value; |
| 292 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
| 293 | |
| 294 | BUG_ON_OPT_NEG(unset); |
| 295 | |
| 296 | strbuf_init(&msg->buf , 0); |
| 297 | if (!strcmp(arg, "-")) { |
| 298 | if (strbuf_read(&msg->buf, 0, 1024) < 0) |
| 299 | die_errno(_("cannot read '%s'"), arg); |
| 300 | } else if (strbuf_read_file(&msg->buf, arg, 1024) < 0) |
| 301 | die_errno(_("could not open or read '%s'"), arg); |
| 302 | |
| 303 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); |
| 304 | d->messages[d->msg_nr - 1] = msg; |
| 305 | msg->stripspace = STRIPSPACE; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) |
| 310 | { |
| 311 | struct note_data *d = opt->value; |
| 312 | struct note_msg *msg = xmalloc(sizeof(*msg)); |
| 313 | char *value; |
| 314 | struct object_id object; |
| 315 | enum object_type type; |
| 316 | size_t len; |
| 317 | |
| 318 | BUG_ON_OPT_NEG(unset); |
| 319 | |
| 320 | strbuf_init(&msg->buf, 0); |
| 321 | if (repo_get_oid(the_repository, arg, &object)) |
| 322 | die(_("failed to resolve '%s' as a valid ref."), arg); |
| 323 | if (!(value = odb_read_object(the_repository->objects, &object, &type, &len))) |
| 324 | die(_("failed to read object '%s'."), arg); |
| 325 | if (type != OBJ_BLOB) { |
| 326 | strbuf_release(&msg->buf); |
| 327 | free(value); |
| 328 | free(msg); |
| 329 | die(_("cannot read note data from non-blob object '%s'."), arg); |
| 330 | } |
| 331 | |
| 332 | strbuf_add(&msg->buf, value, len); |
| 333 | free(value); |
| 334 | |
| 335 | msg->buf.len = len; |
| 336 | ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc); |
| 337 | d->messages[d->msg_nr - 1] = msg; |
| 338 | msg->stripspace = NO_STRIPSPACE; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) |
| 343 | { |
| 344 | struct note_data *d = opt->value; |
| 345 | BUG_ON_OPT_NEG(unset); |
| 346 | d->use_editor = 1; |
| 347 | return parse_reuse_arg(opt, arg, unset); |
| 348 | } |
| 349 | |
| 350 | static int parse_separator_arg(const struct option *opt, const char *arg, |
| 351 | int unset) |
| 352 | { |
| 353 | if (unset) |
| 354 | *(const char **)opt->value = NULL; |
| 355 | else |
| 356 | *(const char **)opt->value = arg ? arg : "\n"; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
| 361 | { |
| 362 | struct strbuf buf = STRBUF_INIT; |
| 363 | struct notes_rewrite_cfg *c = NULL; |
| 364 | struct notes_tree *t = NULL; |
| 365 | int ret = 0; |
| 366 | const char *msg = "Notes added by 'git notes copy'"; |
| 367 | |
| 368 | if (rewrite_cmd) { |
| 369 | c = init_copy_notes_for_rewrite(rewrite_cmd); |
| 370 | if (!c) |
| 371 | return 0; |
| 372 | } else { |
| 373 | init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE); |
| 374 | t = &default_notes_tree; |
| 375 | } |
| 376 | |
| 377 | while (strbuf_getline_lf(&buf, stdin) != EOF) { |
| 378 | struct object_id from_obj, to_obj; |
| 379 | struct string_list split = STRING_LIST_INIT_NODUP; |
| 380 | int err; |
| 381 | |
| 382 | string_list_split_in_place_f(&split, buf.buf, " ", -1, |
| 383 | STRING_LIST_SPLIT_TRIM); |
| 384 | if (split.nr < 2) |
| 385 | die(_("malformed input line: '%s'."), buf.buf); |
| 386 | if (repo_get_oid(the_repository, split.items[0].string, &from_obj)) |
| 387 | die(_("failed to resolve '%s' as a valid ref."), |
| 388 | split.items[0].string); |
| 389 | if (repo_get_oid(the_repository, split.items[1].string, &to_obj)) |
| 390 | die(_("failed to resolve '%s' as a valid ref."), |
| 391 | split.items[1].string); |
| 392 | |
| 393 | if (rewrite_cmd) |
| 394 | err = copy_note_for_rewrite(c, &from_obj, &to_obj); |
| 395 | else |
| 396 | err = copy_note(t, &from_obj, &to_obj, force, |
| 397 | combine_notes_overwrite); |
| 398 | |
| 399 | if (err) { |
| 400 | error(_("failed to copy notes from '%s' to '%s'"), |
| 401 | split.items[0].string, split.items[1].string); |
| 402 | ret = 1; |
| 403 | } |
| 404 | |
| 405 | string_list_clear(&split, 0); |
| 406 | } |
| 407 | |
| 408 | if (!rewrite_cmd) { |
| 409 | commit_notes(the_repository, t, msg); |
| 410 | free_notes(t); |
| 411 | } else { |
| 412 | finish_copy_notes_for_rewrite(the_repository, c, msg); |
| 413 | } |
| 414 | strbuf_release(&buf); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static struct notes_tree *init_notes_check(const char *subcommand, |
| 419 | int flags) |
| 420 | { |
| 421 | struct notes_tree *t; |
| 422 | const char *ref; |
| 423 | init_notes(NULL, NULL, NULL, flags); |
| 424 | t = &default_notes_tree; |
| 425 | |
| 426 | ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref; |
| 427 | if (!starts_with(ref, "refs/notes/")) |
| 428 | /* |
| 429 | * TRANSLATORS: the first %s will be replaced by a git |
| 430 | * notes command: 'add', 'merge', 'remove', etc. |
| 431 | */ |
| 432 | die(_("refusing to %s notes in %s (outside of refs/notes/)"), |
| 433 | subcommand, ref); |
| 434 | return t; |
| 435 | } |
| 436 | |
| 437 | static int list(int argc, const char **argv, const char *prefix, |
| 438 | struct repository *repo UNUSED) |
| 439 | { |
| 440 | struct notes_tree *t; |
| 441 | struct object_id object; |
| 442 | const struct object_id *note; |
| 443 | int retval = -1; |
| 444 | struct option options[] = { |
| 445 | OPT_END() |
| 446 | }; |
| 447 | |
| 448 | if (argc) |
| 449 | argc = parse_options(argc, argv, prefix, options, |
| 450 | git_notes_list_usage, 0); |
| 451 | |
| 452 | if (1 < argc) { |
| 453 | error(_("too many arguments")); |
| 454 | usage_with_options(git_notes_list_usage, options); |
| 455 | } |
| 456 | |
| 457 | t = init_notes_check("list", 0); |
| 458 | if (argc) { |
| 459 | if (repo_get_oid(the_repository, argv[0], &object)) |
| 460 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
| 461 | note = get_note(t, &object); |
| 462 | if (note) { |
| 463 | puts(oid_to_hex(note)); |
| 464 | retval = 0; |
| 465 | } else |
| 466 | retval = error(_("no note found for object %s."), |
| 467 | oid_to_hex(&object)); |
| 468 | } else |
| 469 | retval = for_each_note(t, 0, list_each_note, NULL); |
| 470 | |
| 471 | free_notes(t); |
| 472 | return retval; |
| 473 | } |
| 474 | |
| 475 | static int append_edit(int argc, const char **argv, const char *prefix, |
| 476 | struct repository *repo UNUSED); |
| 477 | |
| 478 | static int add(int argc, const char **argv, const char *prefix, |
| 479 | struct repository *repo) |
| 480 | { |
| 481 | int force = 0, allow_empty = 0; |
| 482 | const char *object_ref; |
| 483 | struct notes_tree *t; |
| 484 | struct object_id object, new_note; |
| 485 | const struct object_id *note; |
| 486 | struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED }; |
| 487 | |
| 488 | struct option options[] = { |
| 489 | OPT_CALLBACK_F('m', "message", &d, N_("message"), |
| 490 | N_("note contents as a string"), PARSE_OPT_NONEG, |
| 491 | parse_msg_arg), |
| 492 | OPT_CALLBACK_F('F', "file", &d, N_("file"), |
| 493 | N_("note contents in a file"), PARSE_OPT_NONEG, |
| 494 | parse_file_arg), |
| 495 | OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"), |
| 496 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
| 497 | parse_reedit_arg), |
| 498 | OPT_BOOL('e', "edit", &d.use_editor, |
| 499 | N_("edit note message in editor")), |
| 500 | OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"), |
| 501 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
| 502 | parse_reuse_arg), |
| 503 | OPT_BOOL(0, "allow-empty", &allow_empty, |
| 504 | N_("allow storing empty note")), |
| 505 | OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE), |
| 506 | OPT_CALLBACK_F(0, "separator", &separator, |
| 507 | N_("<paragraph-break>"), |
| 508 | N_("insert <paragraph-break> between paragraphs"), |
| 509 | PARSE_OPT_OPTARG, parse_separator_arg), |
| 510 | OPT_BOOL(0, "stripspace", &d.stripspace, |
| 511 | N_("remove unnecessary whitespace")), |
| 512 | OPT_END() |
| 513 | }; |
| 514 | |
| 515 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
| 516 | PARSE_OPT_KEEP_ARGV0); |
| 517 | |
| 518 | if (2 < argc) { |
| 519 | error(_("too many arguments")); |
| 520 | usage_with_options(git_notes_add_usage, options); |
| 521 | } |
| 522 | |
| 523 | if (d.msg_nr) |
| 524 | concat_messages(&d); |
| 525 | |
| 526 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
| 527 | |
| 528 | if (repo_get_oid(the_repository, object_ref, &object)) |
| 529 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
| 530 | |
| 531 | t = init_notes_check("add", NOTES_INIT_WRITABLE); |
| 532 | note = get_note(t, &object); |
| 533 | |
| 534 | if (note) { |
| 535 | if (!force) { |
| 536 | free_notes(t); |
| 537 | if (d.msg_nr) { |
| 538 | free_note_data(&d); |
| 539 | return error(_("Cannot add notes. " |
| 540 | "Found existing notes for object %s. " |
| 541 | "Use '-f' to overwrite existing notes"), |
| 542 | oid_to_hex(&object)); |
| 543 | } |
| 544 | /* |
| 545 | * Redirect to "edit" subcommand. |
| 546 | * |
| 547 | * We only end up here if none of -m/-F/-c/-C or -f are |
| 548 | * given. The original args are therefore still in |
| 549 | * argv[0-1]. |
| 550 | */ |
| 551 | argv[0] = "edit"; |
| 552 | return append_edit(argc, argv, prefix, repo); |
| 553 | } |
| 554 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
| 555 | oid_to_hex(&object)); |
| 556 | } |
| 557 | |
| 558 | prepare_note_data(&object, &d, note); |
| 559 | if (d.buf.len || allow_empty) { |
| 560 | write_note_data(&d, &new_note); |
| 561 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
| 562 | BUG("combine_notes_overwrite failed"); |
| 563 | commit_notes(the_repository, t, |
| 564 | "Notes added by 'git notes add'"); |
| 565 | } else { |
| 566 | fprintf(stderr, _("Removing note for object %s\n"), |
| 567 | oid_to_hex(&object)); |
| 568 | remove_note(t, object.hash); |
| 569 | commit_notes(the_repository, t, |
| 570 | "Notes removed by 'git notes add'"); |
| 571 | } |
| 572 | |
| 573 | free_note_data(&d); |
| 574 | free_notes(t); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int copy(int argc, const char **argv, const char *prefix, |
| 579 | struct repository *repo UNUSED) |
| 580 | { |
| 581 | int retval = 0, force = 0, from_stdin = 0; |
| 582 | const struct object_id *from_note, *note; |
| 583 | const char *object_ref; |
| 584 | struct object_id object, from_obj; |
| 585 | struct notes_tree *t; |
| 586 | const char *rewrite_cmd = NULL; |
| 587 | struct option options[] = { |
| 588 | OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE), |
| 589 | OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")), |
| 590 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), |
| 591 | N_("load rewriting config for <command> (implies " |
| 592 | "--stdin)")), |
| 593 | OPT_END() |
| 594 | }; |
| 595 | |
| 596 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
| 597 | 0); |
| 598 | |
| 599 | if (from_stdin || rewrite_cmd) { |
| 600 | if (argc) { |
| 601 | error(_("too many arguments")); |
| 602 | usage_with_options(git_notes_copy_usage, options); |
| 603 | } else { |
| 604 | return notes_copy_from_stdin(force, rewrite_cmd); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (argc < 1) { |
| 609 | error(_("too few arguments")); |
| 610 | usage_with_options(git_notes_copy_usage, options); |
| 611 | } |
| 612 | if (2 < argc) { |
| 613 | error(_("too many arguments")); |
| 614 | usage_with_options(git_notes_copy_usage, options); |
| 615 | } |
| 616 | |
| 617 | if (repo_get_oid(the_repository, argv[0], &from_obj)) |
| 618 | die(_("failed to resolve '%s' as a valid ref."), argv[0]); |
| 619 | |
| 620 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 621 | |
| 622 | if (repo_get_oid(the_repository, object_ref, &object)) |
| 623 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
| 624 | |
| 625 | t = init_notes_check("copy", NOTES_INIT_WRITABLE); |
| 626 | note = get_note(t, &object); |
| 627 | |
| 628 | if (note) { |
| 629 | if (!force) { |
| 630 | retval = error(_("Cannot copy notes. Found existing " |
| 631 | "notes for object %s. Use '-f' to " |
| 632 | "overwrite existing notes"), |
| 633 | oid_to_hex(&object)); |
| 634 | goto out; |
| 635 | } |
| 636 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
| 637 | oid_to_hex(&object)); |
| 638 | } |
| 639 | |
| 640 | from_note = get_note(t, &from_obj); |
| 641 | if (!from_note) { |
| 642 | retval = error(_("missing notes on source object %s. Cannot " |
| 643 | "copy."), oid_to_hex(&from_obj)); |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | if (add_note(t, &object, from_note, combine_notes_overwrite)) |
| 648 | BUG("combine_notes_overwrite failed"); |
| 649 | commit_notes(the_repository, t, |
| 650 | "Notes added by 'git notes copy'"); |
| 651 | out: |
| 652 | free_notes(t); |
| 653 | return retval; |
| 654 | } |
| 655 | |
| 656 | static int append_edit(int argc, const char **argv, const char *prefix, |
| 657 | struct repository *repo UNUSED) |
| 658 | { |
| 659 | int allow_empty = 0; |
| 660 | const char *object_ref; |
| 661 | struct notes_tree *t; |
| 662 | struct object_id object, new_note; |
| 663 | const struct object_id *note; |
| 664 | char *logmsg; |
| 665 | const char * const *usage; |
| 666 | struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED }; |
| 667 | struct option options[] = { |
| 668 | OPT_CALLBACK_F('m', "message", &d, N_("message"), |
| 669 | N_("note contents as a string"), PARSE_OPT_NONEG, |
| 670 | parse_msg_arg), |
| 671 | OPT_CALLBACK_F('F', "file", &d, N_("file"), |
| 672 | N_("note contents in a file"), PARSE_OPT_NONEG, |
| 673 | parse_file_arg), |
| 674 | OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"), |
| 675 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
| 676 | parse_reedit_arg), |
| 677 | OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"), |
| 678 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
| 679 | parse_reuse_arg), |
| 680 | OPT_BOOL('e', "edit", &d.use_editor, |
| 681 | N_("edit note message in editor")), |
| 682 | OPT_BOOL(0, "allow-empty", &allow_empty, |
| 683 | N_("allow storing empty note")), |
| 684 | OPT_CALLBACK_F(0, "separator", &separator, |
| 685 | N_("<paragraph-break>"), |
| 686 | N_("insert <paragraph-break> between paragraphs"), |
| 687 | PARSE_OPT_OPTARG, parse_separator_arg), |
| 688 | OPT_BOOL(0, "stripspace", &d.stripspace, |
| 689 | N_("remove unnecessary whitespace")), |
| 690 | OPT_END() |
| 691 | }; |
| 692 | int edit = !strcmp(argv[0], "edit"); |
| 693 | |
| 694 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; |
| 695 | argc = parse_options(argc, argv, prefix, options, usage, |
| 696 | PARSE_OPT_KEEP_ARGV0); |
| 697 | |
| 698 | if (2 < argc) { |
| 699 | error(_("too many arguments")); |
| 700 | usage_with_options(usage, options); |
| 701 | } |
| 702 | |
| 703 | if (d.msg_nr) { |
| 704 | concat_messages(&d); |
| 705 | if (edit) |
| 706 | fprintf(stderr, _("The -m/-F/-c/-C options have been " |
| 707 | "deprecated for the 'edit' subcommand.\n" |
| 708 | "Please use 'git notes add -f -m/-F/-c/-C' " |
| 709 | "instead.\n")); |
| 710 | } |
| 711 | |
| 712 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 713 | |
| 714 | if (repo_get_oid(the_repository, object_ref, &object)) |
| 715 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
| 716 | |
| 717 | t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); |
| 718 | note = get_note(t, &object); |
| 719 | |
| 720 | prepare_note_data(&object, &d, edit && note ? note : NULL); |
| 721 | |
| 722 | if (note && !edit) { |
| 723 | /* Append buf to previous note contents */ |
| 724 | size_t size; |
| 725 | enum object_type type; |
| 726 | struct strbuf buf = STRBUF_INIT; |
| 727 | char *prev_buf = odb_read_object(the_repository->objects, note, &type, &size); |
| 728 | |
| 729 | if (!prev_buf) |
| 730 | die(_("unable to read %s"), oid_to_hex(note)); |
| 731 | if (size) |
| 732 | strbuf_add(&buf, prev_buf, size); |
| 733 | if (d.buf.len && size) |
| 734 | append_separator(&buf); |
| 735 | strbuf_insert(&d.buf, 0, buf.buf, buf.len); |
| 736 | |
| 737 | free(prev_buf); |
| 738 | strbuf_release(&buf); |
| 739 | } |
| 740 | |
| 741 | if (d.buf.len || allow_empty) { |
| 742 | write_note_data(&d, &new_note); |
| 743 | if (add_note(t, &object, &new_note, combine_notes_overwrite)) |
| 744 | BUG("combine_notes_overwrite failed"); |
| 745 | logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); |
| 746 | } else { |
| 747 | fprintf(stderr, _("Removing note for object %s\n"), |
| 748 | oid_to_hex(&object)); |
| 749 | remove_note(t, object.hash); |
| 750 | logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); |
| 751 | } |
| 752 | commit_notes(the_repository, t, logmsg); |
| 753 | |
| 754 | free(logmsg); |
| 755 | free_note_data(&d); |
| 756 | free_notes(t); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static int show(int argc, const char **argv, const char *prefix, |
| 761 | struct repository *repo UNUSED) |
| 762 | { |
| 763 | const char *object_ref; |
| 764 | struct notes_tree *t; |
| 765 | struct object_id object; |
| 766 | const struct object_id *note; |
| 767 | int retval; |
| 768 | struct option options[] = { |
| 769 | OPT_END() |
| 770 | }; |
| 771 | |
| 772 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, |
| 773 | 0); |
| 774 | |
| 775 | if (1 < argc) { |
| 776 | error(_("too many arguments")); |
| 777 | usage_with_options(git_notes_show_usage, options); |
| 778 | } |
| 779 | |
| 780 | object_ref = argc ? argv[0] : "HEAD"; |
| 781 | |
| 782 | if (repo_get_oid(the_repository, object_ref, &object)) |
| 783 | die(_("failed to resolve '%s' as a valid ref."), object_ref); |
| 784 | |
| 785 | t = init_notes_check("show", 0); |
| 786 | note = get_note(t, &object); |
| 787 | |
| 788 | if (!note) |
| 789 | retval = error(_("no note found for object %s."), |
| 790 | oid_to_hex(&object)); |
| 791 | else { |
| 792 | const char *show_args[3] = {"show", oid_to_hex(note), NULL}; |
| 793 | retval = execv_git_cmd(show_args); |
| 794 | } |
| 795 | free_notes(t); |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | static int merge_abort(struct notes_merge_options *o) |
| 800 | { |
| 801 | int ret = 0; |
| 802 | |
| 803 | /* |
| 804 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call |
| 805 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. |
| 806 | */ |
| 807 | |
| 808 | if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_PARTIAL", NULL, 0)) |
| 809 | ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL")); |
| 810 | if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF)) |
| 811 | ret += error(_("failed to delete ref NOTES_MERGE_REF")); |
| 812 | if (notes_merge_abort(o)) |
| 813 | ret += error(_("failed to remove 'git notes merge' worktree")); |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | static int merge_commit(struct notes_merge_options *o) |
| 818 | { |
| 819 | struct strbuf msg = STRBUF_INIT; |
| 820 | struct object_id oid, parent_oid; |
| 821 | struct notes_tree t = {0}; |
| 822 | struct commit *partial; |
| 823 | struct pretty_print_context pretty_ctx; |
| 824 | void *local_ref_to_free; |
| 825 | int ret; |
| 826 | |
| 827 | /* |
| 828 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, |
| 829 | * and target notes ref from .git/NOTES_MERGE_REF. |
| 830 | */ |
| 831 | |
| 832 | if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid)) |
| 833 | die(_("failed to read ref NOTES_MERGE_PARTIAL")); |
| 834 | else if (!(partial = lookup_commit_reference(the_repository, &oid))) |
| 835 | die(_("could not find commit from NOTES_MERGE_PARTIAL.")); |
| 836 | else if (repo_parse_commit(the_repository, partial)) |
| 837 | die(_("could not parse commit from NOTES_MERGE_PARTIAL.")); |
| 838 | |
| 839 | if (partial->parents) |
| 840 | oidcpy(&parent_oid, &partial->parents->item->object.oid); |
| 841 | else |
| 842 | oidclr(&parent_oid, the_repository->hash_algo); |
| 843 | |
| 844 | init_notes(&t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); |
| 845 | |
| 846 | o->local_ref = local_ref_to_free = |
| 847 | refs_resolve_refdup(get_main_ref_store(the_repository), |
| 848 | "NOTES_MERGE_REF", 0, &oid, NULL); |
| 849 | if (!o->local_ref) |
| 850 | die(_("failed to resolve NOTES_MERGE_REF")); |
| 851 | |
| 852 | if (notes_merge_commit(o, &t, partial, &oid)) |
| 853 | die(_("failed to finalize notes merge")); |
| 854 | |
| 855 | /* Reuse existing commit message in reflog message */ |
| 856 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); |
| 857 | repo_format_commit_message(the_repository, partial, "%s", &msg, |
| 858 | &pretty_ctx); |
| 859 | strbuf_trim(&msg); |
| 860 | strbuf_insertstr(&msg, 0, "notes: "); |
| 861 | refs_update_ref(get_main_ref_store(the_repository), msg.buf, |
| 862 | o->local_ref, &oid, |
| 863 | is_null_oid(&parent_oid) ? NULL : &parent_oid, |
| 864 | 0, UPDATE_REFS_DIE_ON_ERR); |
| 865 | |
| 866 | free_notes(&t); |
| 867 | strbuf_release(&msg); |
| 868 | ret = merge_abort(o); |
| 869 | free(local_ref_to_free); |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | static int git_config_get_notes_strategy(const char *key, |
| 874 | enum notes_merge_strategy *strategy) |
| 875 | { |
| 876 | char *value; |
| 877 | |
| 878 | if (repo_config_get_string(the_repository, key, &value)) |
| 879 | return 1; |
| 880 | if (parse_notes_merge_strategy(value, strategy)) |
| 881 | git_die_config(the_repository, key, _("unknown notes merge strategy %s"), value); |
| 882 | |
| 883 | free(value); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static int merge(int argc, const char **argv, const char *prefix, |
| 888 | struct repository *repo UNUSED) |
| 889 | { |
| 890 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; |
| 891 | struct object_id result_oid; |
| 892 | struct notes_tree *t; |
| 893 | struct notes_merge_options o; |
| 894 | int do_merge = 0, do_commit = 0, do_abort = 0; |
| 895 | int verbosity = 0, result; |
| 896 | const char *strategy = NULL; |
| 897 | struct option options[] = { |
| 898 | OPT_GROUP(N_("General options")), |
| 899 | OPT__VERBOSITY(&verbosity), |
| 900 | OPT_GROUP(N_("Merge options")), |
| 901 | OPT_STRING('s', "strategy", &strategy, N_("strategy"), |
| 902 | N_("resolve notes conflicts using the given strategy " |
| 903 | "(manual/ours/theirs/union/cat_sort_uniq)")), |
| 904 | OPT_GROUP(N_("Committing unmerged notes")), |
| 905 | OPT_SET_INT_F(0, "commit", &do_commit, |
| 906 | N_("finalize notes merge by committing unmerged notes"), |
| 907 | 1, PARSE_OPT_NONEG), |
| 908 | OPT_GROUP(N_("Aborting notes merge resolution")), |
| 909 | OPT_SET_INT_F(0, "abort", &do_abort, |
| 910 | N_("abort notes merge"), |
| 911 | 1, PARSE_OPT_NONEG), |
| 912 | OPT_END() |
| 913 | }; |
| 914 | char *notes_ref; |
| 915 | |
| 916 | argc = parse_options(argc, argv, prefix, options, |
| 917 | git_notes_merge_usage, 0); |
| 918 | |
| 919 | if (strategy || do_commit + do_abort == 0) |
| 920 | do_merge = 1; |
| 921 | if (do_merge + do_commit + do_abort != 1) { |
| 922 | error(_("cannot mix --commit, --abort or -s/--strategy")); |
| 923 | usage_with_options(git_notes_merge_usage, options); |
| 924 | } |
| 925 | |
| 926 | if (do_merge && argc != 1) { |
| 927 | error(_("must specify a notes ref to merge")); |
| 928 | usage_with_options(git_notes_merge_usage, options); |
| 929 | } else if (!do_merge && argc) { |
| 930 | error(_("too many arguments")); |
| 931 | usage_with_options(git_notes_merge_usage, options); |
| 932 | } |
| 933 | |
| 934 | init_notes_merge_options(the_repository, &o); |
| 935 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; |
| 936 | |
| 937 | if (do_abort) |
| 938 | return merge_abort(&o); |
| 939 | if (do_commit) |
| 940 | return merge_commit(&o); |
| 941 | |
| 942 | notes_ref = default_notes_ref(the_repository); |
| 943 | o.local_ref = notes_ref; |
| 944 | strbuf_addstr(&remote_ref, argv[0]); |
| 945 | expand_loose_notes_ref(&remote_ref); |
| 946 | o.remote_ref = remote_ref.buf; |
| 947 | |
| 948 | t = init_notes_check("merge", NOTES_INIT_WRITABLE); |
| 949 | |
| 950 | if (strategy) { |
| 951 | if (parse_notes_merge_strategy(strategy, &o.strategy)) { |
| 952 | error(_("unknown -s/--strategy: %s"), strategy); |
| 953 | usage_with_options(git_notes_merge_usage, options); |
| 954 | } |
| 955 | } else { |
| 956 | struct strbuf merge_key = STRBUF_INIT; |
| 957 | const char *short_ref = NULL; |
| 958 | |
| 959 | if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref)) |
| 960 | BUG("local ref %s is outside of refs/notes/", |
| 961 | o.local_ref); |
| 962 | |
| 963 | strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref); |
| 964 | |
| 965 | if (git_config_get_notes_strategy(merge_key.buf, &o.strategy)) |
| 966 | git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy); |
| 967 | |
| 968 | strbuf_release(&merge_key); |
| 969 | } |
| 970 | |
| 971 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", |
| 972 | remote_ref.buf, notes_ref); |
| 973 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
| 974 | |
| 975 | result = notes_merge(&o, t, &result_oid); |
| 976 | |
| 977 | if (result >= 0) /* Merge resulted (trivially) in result_oid */ |
| 978 | /* Update default notes ref with new commit */ |
| 979 | refs_update_ref(get_main_ref_store(the_repository), msg.buf, |
| 980 | notes_ref, &result_oid, NULL, 0, |
| 981 | UPDATE_REFS_DIE_ON_ERR); |
| 982 | else { /* Merge has unresolved conflicts */ |
| 983 | struct worktree **worktrees; |
| 984 | const struct worktree *wt; |
| 985 | char *path; |
| 986 | |
| 987 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ |
| 988 | refs_update_ref(get_main_ref_store(the_repository), msg.buf, |
| 989 | "NOTES_MERGE_PARTIAL", &result_oid, NULL, |
| 990 | 0, UPDATE_REFS_DIE_ON_ERR); |
| 991 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
| 992 | worktrees = get_worktrees(); |
| 993 | wt = find_shared_symref(worktrees, "NOTES_MERGE_REF", |
| 994 | notes_ref); |
| 995 | if (wt) |
| 996 | die(_("a notes merge into %s is already in-progress at %s"), |
| 997 | notes_ref, wt->path); |
| 998 | free_worktrees(worktrees); |
| 999 | if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL)) |
| 1000 | die(_("failed to store link to current notes ref (%s)"), |
| 1001 | notes_ref); |
| 1002 | |
| 1003 | path = repo_git_path(the_repository, NOTES_MERGE_WORKTREE); |
| 1004 | fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s " |
| 1005 | "and commit the result with 'git notes merge --commit', " |
| 1006 | "or abort the merge with 'git notes merge --abort'.\n"), |
| 1007 | path); |
| 1008 | free(path); |
| 1009 | } |
| 1010 | |
| 1011 | free_notes(t); |
| 1012 | free(notes_ref); |
| 1013 | strbuf_release(&remote_ref); |
| 1014 | strbuf_release(&msg); |
| 1015 | return result < 0; /* return non-zero on conflicts */ |
| 1016 | } |
| 1017 | |
| 1018 | #define IGNORE_MISSING 1 |
| 1019 | |
| 1020 | static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) |
| 1021 | { |
| 1022 | int status; |
| 1023 | struct object_id oid; |
| 1024 | if (repo_get_oid(the_repository, name, &oid)) |
| 1025 | return error(_("Failed to resolve '%s' as a valid ref."), name); |
| 1026 | status = remove_note(t, oid.hash); |
| 1027 | if (status) |
| 1028 | fprintf(stderr, _("Object %s has no note\n"), name); |
| 1029 | else |
| 1030 | fprintf(stderr, _("Removing note for object %s\n"), name); |
| 1031 | return (flag & IGNORE_MISSING) ? 0 : status; |
| 1032 | } |
| 1033 | |
| 1034 | static int remove_cmd(int argc, const char **argv, const char *prefix, |
| 1035 | struct repository *repo UNUSED) |
| 1036 | { |
| 1037 | unsigned flag = 0; |
| 1038 | int from_stdin = 0; |
| 1039 | struct option options[] = { |
| 1040 | OPT_BIT(0, "ignore-missing", &flag, |
| 1041 | N_("attempt to remove non-existent note is not an error"), |
| 1042 | IGNORE_MISSING), |
| 1043 | OPT_BOOL(0, "stdin", &from_stdin, |
| 1044 | N_("read object names from the standard input")), |
| 1045 | OPT_END() |
| 1046 | }; |
| 1047 | struct notes_tree *t; |
| 1048 | int retval = 0; |
| 1049 | |
| 1050 | argc = parse_options(argc, argv, prefix, options, |
| 1051 | git_notes_remove_usage, 0); |
| 1052 | |
| 1053 | t = init_notes_check("remove", NOTES_INIT_WRITABLE); |
| 1054 | |
| 1055 | if (!argc && !from_stdin) { |
| 1056 | retval = remove_one_note(t, "HEAD", flag); |
| 1057 | } else { |
| 1058 | while (*argv) { |
| 1059 | retval |= remove_one_note(t, *argv, flag); |
| 1060 | argv++; |
| 1061 | } |
| 1062 | } |
| 1063 | if (from_stdin) { |
| 1064 | struct strbuf sb = STRBUF_INIT; |
| 1065 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { |
| 1066 | strbuf_rtrim(&sb); |
| 1067 | retval |= remove_one_note(t, sb.buf, flag); |
| 1068 | } |
| 1069 | strbuf_release(&sb); |
| 1070 | } |
| 1071 | if (!retval) |
| 1072 | commit_notes(the_repository, t, |
| 1073 | "Notes removed by 'git notes remove'"); |
| 1074 | free_notes(t); |
| 1075 | return retval; |
| 1076 | } |
| 1077 | |
| 1078 | static int prune(int argc, const char **argv, const char *prefix, |
| 1079 | struct repository *repo UNUSED) |
| 1080 | { |
| 1081 | struct notes_tree *t; |
| 1082 | int show_only = 0, verbose = 0; |
| 1083 | struct option options[] = { |
| 1084 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
| 1085 | OPT__VERBOSE(&verbose, N_("report pruned notes")), |
| 1086 | OPT_END() |
| 1087 | }; |
| 1088 | |
| 1089 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, |
| 1090 | 0); |
| 1091 | |
| 1092 | if (argc) { |
| 1093 | error(_("too many arguments")); |
| 1094 | usage_with_options(git_notes_prune_usage, options); |
| 1095 | } |
| 1096 | |
| 1097 | t = init_notes_check("prune", NOTES_INIT_WRITABLE); |
| 1098 | |
| 1099 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
| 1100 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); |
| 1101 | if (!show_only) |
| 1102 | commit_notes(the_repository, t, |
| 1103 | "Notes removed by 'git notes prune'"); |
| 1104 | free_notes(t); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | static int get_ref(int argc, const char **argv, const char *prefix, |
| 1109 | struct repository *repo UNUSED) |
| 1110 | { |
| 1111 | struct option options[] = { OPT_END() }; |
| 1112 | char *notes_ref; |
| 1113 | argc = parse_options(argc, argv, prefix, options, |
| 1114 | git_notes_get_ref_usage, 0); |
| 1115 | |
| 1116 | if (argc) { |
| 1117 | error(_("too many arguments")); |
| 1118 | usage_with_options(git_notes_get_ref_usage, options); |
| 1119 | } |
| 1120 | |
| 1121 | notes_ref = default_notes_ref(the_repository); |
| 1122 | puts(notes_ref); |
| 1123 | free(notes_ref); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | int cmd_notes(int argc, |
| 1128 | const char **argv, |
| 1129 | const char *prefix, |
| 1130 | struct repository *repo) |
| 1131 | { |
| 1132 | const char *override_notes_ref = NULL; |
| 1133 | parse_opt_subcommand_fn *fn = NULL; |
| 1134 | struct option options[] = { |
| 1135 | OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), |
| 1136 | N_("use notes from <notes-ref>")), |
| 1137 | OPT_SUBCOMMAND("list", &fn, list), |
| 1138 | OPT_SUBCOMMAND("add", &fn, add), |
| 1139 | OPT_SUBCOMMAND("copy", &fn, copy), |
| 1140 | OPT_SUBCOMMAND("append", &fn, append_edit), |
| 1141 | OPT_SUBCOMMAND("edit", &fn, append_edit), |
| 1142 | OPT_SUBCOMMAND("show", &fn, show), |
| 1143 | OPT_SUBCOMMAND("merge", &fn, merge), |
| 1144 | OPT_SUBCOMMAND("remove", &fn, remove_cmd), |
| 1145 | OPT_SUBCOMMAND("prune", &fn, prune), |
| 1146 | OPT_SUBCOMMAND("get-ref", &fn, get_ref), |
| 1147 | OPT_END() |
| 1148 | }; |
| 1149 | |
| 1150 | repo_config(the_repository, git_default_config, NULL); |
| 1151 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, |
| 1152 | PARSE_OPT_SUBCOMMAND_OPTIONAL); |
| 1153 | if (!fn) { |
| 1154 | if (argc) { |
| 1155 | error(_("unknown subcommand: `%s'"), argv[0]); |
| 1156 | usage_with_options(git_notes_usage, options); |
| 1157 | } |
| 1158 | fn = list; |
| 1159 | } |
| 1160 | |
| 1161 | if (override_notes_ref) { |
| 1162 | struct strbuf sb = STRBUF_INIT; |
| 1163 | strbuf_addstr(&sb, override_notes_ref); |
| 1164 | expand_notes_ref(&sb); |
| 1165 | setenv("GIT_NOTES_REF", sb.buf, 1); |
| 1166 | strbuf_release(&sb); |
| 1167 | } |
| 1168 | |
| 1169 | return !!fn(argc, argv, prefix, repo); |
| 1170 | } |