builtin / notes.con commit git-svn: strip off leading slashes on --trunk argument (b4b3360)
   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"
  19#include "string-list.h"
  20
  21static const char * const git_notes_usage[] = {
  22        "git notes [--ref <notes_ref>] [list [<object>]]",
  23        "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  24        "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
  25        "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  26        "git notes [--ref <notes_ref>] edit [<object>]",
  27        "git notes [--ref <notes_ref>] show [<object>]",
  28        "git notes [--ref <notes_ref>] remove [<object>]",
  29        "git notes [--ref <notes_ref>] prune [-n | -v]",
  30        NULL
  31};
  32
  33static const char * const git_notes_list_usage[] = {
  34        "git notes [list [<object>]]",
  35        NULL
  36};
  37
  38static const char * const git_notes_add_usage[] = {
  39        "git notes add [<options>] [<object>]",
  40        NULL
  41};
  42
  43static const char * const git_notes_copy_usage[] = {
  44        "git notes copy [<options>] <from-object> <to-object>",
  45        "git notes copy --stdin [<from-object> <to-object>]...",
  46        NULL
  47};
  48
  49static const char * const git_notes_append_usage[] = {
  50        "git notes append [<options>] [<object>]",
  51        NULL
  52};
  53
  54static const char * const git_notes_edit_usage[] = {
  55        "git notes edit [<object>]",
  56        NULL
  57};
  58
  59static const char * const git_notes_show_usage[] = {
  60        "git notes show [<object>]",
  61        NULL
  62};
  63
  64static const char * const git_notes_remove_usage[] = {
  65        "git notes remove [<object>]",
  66        NULL
  67};
  68
  69static const char * const git_notes_prune_usage[] = {
  70        "git notes prune [<options>]",
  71        NULL
  72};
  73
  74static const char note_template[] =
  75        "\n"
  76        "#\n"
  77        "# Write/edit the notes for the following object:\n"
  78        "#\n";
  79
  80struct msg_arg {
  81        int given;
  82        int use_editor;
  83        struct strbuf buf;
  84};
  85
  86static int list_each_note(const unsigned char *object_sha1,
  87                const unsigned char *note_sha1, char *note_path,
  88                void *cb_data)
  89{
  90        printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
  91        return 0;
  92}
  93
  94static void write_note_data(int fd, const unsigned char *sha1)
  95{
  96        unsigned long size;
  97        enum object_type type;
  98        char *buf = read_sha1_file(sha1, &type, &size);
  99        if (buf) {
 100                if (size)
 101                        write_or_die(fd, buf, size);
 102                free(buf);
 103        }
 104}
 105
 106static void write_commented_object(int fd, const unsigned char *object)
 107{
 108        const char *show_args[5] =
 109                {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
 110        struct child_process show;
 111        struct strbuf buf = STRBUF_INIT;
 112        FILE *show_out;
 113
 114        /* Invoke "git show --stat --no-notes $object" */
 115        memset(&show, 0, sizeof(show));
 116        show.argv = show_args;
 117        show.no_stdin = 1;
 118        show.out = -1;
 119        show.err = 0;
 120        show.git_cmd = 1;
 121        if (start_command(&show))
 122                die("unable to start 'show' for object '%s'",
 123                    sha1_to_hex(object));
 124
 125        /* Open the output as FILE* so strbuf_getline() can be used. */
 126        show_out = xfdopen(show.out, "r");
 127        if (show_out == NULL)
 128                die_errno("can't fdopen 'show' output fd");
 129
 130        /* Prepend "# " to each output line and write result to 'fd' */
 131        while (strbuf_getline(&buf, show_out, '\n') != EOF) {
 132                write_or_die(fd, "# ", 2);
 133                write_or_die(fd, buf.buf, buf.len);
 134                write_or_die(fd, "\n", 1);
 135        }
 136        strbuf_release(&buf);
 137        if (fclose(show_out))
 138                die_errno("failed to close pipe to 'show' for object '%s'",
 139                          sha1_to_hex(object));
 140        if (finish_command(&show))
 141                die("failed to finish 'show' for object '%s'",
 142                    sha1_to_hex(object));
 143}
 144
 145static void create_note(const unsigned char *object, struct msg_arg *msg,
 146                        int append_only, const unsigned char *prev,
 147                        unsigned char *result)
 148{
 149        char *path = NULL;
 150
 151        if (msg->use_editor || !msg->given) {
 152                int fd;
 153
 154                /* write the template message before editing: */
 155                path = git_pathdup("NOTES_EDITMSG");
 156                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 157                if (fd < 0)
 158                        die_errno("could not create file '%s'", path);
 159
 160                if (msg->given)
 161                        write_or_die(fd, msg->buf.buf, msg->buf.len);
 162                else if (prev && !append_only)
 163                        write_note_data(fd, prev);
 164                write_or_die(fd, note_template, strlen(note_template));
 165
 166                write_commented_object(fd, object);
 167
 168                close(fd);
 169                strbuf_reset(&(msg->buf));
 170
 171                if (launch_editor(path, &(msg->buf), NULL)) {
 172                        die("Please supply the note contents using either -m" \
 173                            " or -F option");
 174                }
 175                stripspace(&(msg->buf), 1);
 176        }
 177
 178        if (prev && append_only) {
 179                /* Append buf to previous note contents */
 180                unsigned long size;
 181                enum object_type type;
 182                char *prev_buf = read_sha1_file(prev, &type, &size);
 183
 184                strbuf_grow(&(msg->buf), size + 1);
 185                if (msg->buf.len && prev_buf && size)
 186                        strbuf_insert(&(msg->buf), 0, "\n", 1);
 187                if (prev_buf && size)
 188                        strbuf_insert(&(msg->buf), 0, prev_buf, size);
 189                free(prev_buf);
 190        }
 191
 192        if (!msg->buf.len) {
 193                fprintf(stderr, "Removing note for object %s\n",
 194                        sha1_to_hex(object));
 195                hashclr(result);
 196        } else {
 197                if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
 198                        error("unable to write note object");
 199                        if (path)
 200                                error("The note contents has been left in %s",
 201                                      path);
 202                        exit(128);
 203                }
 204        }
 205
 206        if (path) {
 207                unlink_or_warn(path);
 208                free(path);
 209        }
 210}
 211
 212static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 213{
 214        struct msg_arg *msg = opt->value;
 215
 216        strbuf_grow(&(msg->buf), strlen(arg) + 2);
 217        if (msg->buf.len)
 218                strbuf_addch(&(msg->buf), '\n');
 219        strbuf_addstr(&(msg->buf), arg);
 220        stripspace(&(msg->buf), 0);
 221
 222        msg->given = 1;
 223        return 0;
 224}
 225
 226static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 227{
 228        struct msg_arg *msg = opt->value;
 229
 230        if (msg->buf.len)
 231                strbuf_addch(&(msg->buf), '\n');
 232        if (!strcmp(arg, "-")) {
 233                if (strbuf_read(&(msg->buf), 0, 1024) < 0)
 234                        die_errno("cannot read '%s'", arg);
 235        } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
 236                die_errno("could not open or read '%s'", arg);
 237        stripspace(&(msg->buf), 0);
 238
 239        msg->given = 1;
 240        return 0;
 241}
 242
 243static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
 244{
 245        struct msg_arg *msg = opt->value;
 246        char *buf;
 247        unsigned char object[20];
 248        enum object_type type;
 249        unsigned long len;
 250
 251        if (msg->buf.len)
 252                strbuf_addch(&(msg->buf), '\n');
 253
 254        if (get_sha1(arg, object))
 255                die("Failed to resolve '%s' as a valid ref.", arg);
 256        if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
 257                free(buf);
 258                die("Failed to read object '%s'.", arg);;
 259        }
 260        strbuf_add(&(msg->buf), buf, len);
 261        free(buf);
 262
 263        msg->given = 1;
 264        return 0;
 265}
 266
 267static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
 268{
 269        struct msg_arg *msg = opt->value;
 270        msg->use_editor = 1;
 271        return parse_reuse_arg(opt, arg, unset);
 272}
 273
 274int commit_notes(struct notes_tree *t, const char *msg)
 275{
 276        struct commit_list *parent;
 277        unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
 278        struct strbuf buf = STRBUF_INIT;
 279
 280        if (!t)
 281                t = &default_notes_tree;
 282        if (!t->initialized || !t->ref || !*t->ref)
 283                die("Cannot commit uninitialized/unreferenced notes tree");
 284        if (!t->dirty)
 285                return 0; /* don't have to commit an unchanged tree */
 286
 287        /* Prepare commit message and reflog message */
 288        strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
 289        strbuf_addstr(&buf, msg);
 290        if (buf.buf[buf.len - 1] != '\n')
 291                strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
 292
 293        /* Convert notes tree to tree object */
 294        if (write_notes_tree(t, tree_sha1))
 295                die("Failed to write current notes tree to database");
 296
 297        /* Create new commit for the tree object */
 298        if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
 299                parent = xmalloc(sizeof(*parent));
 300                parent->item = lookup_commit(prev_commit);
 301                parent->next = NULL;
 302        } else {
 303                hashclr(prev_commit);
 304                parent = NULL;
 305        }
 306        if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
 307                die("Failed to commit notes tree to database");
 308
 309        /* Update notes ref with new commit */
 310        update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
 311
 312        strbuf_release(&buf);
 313        return 0;
 314}
 315
 316combine_notes_fn *parse_combine_notes_fn(const char *v)
 317{
 318        if (!strcasecmp(v, "overwrite"))
 319                return combine_notes_overwrite;
 320        else if (!strcasecmp(v, "ignore"))
 321                return combine_notes_ignore;
 322        else if (!strcasecmp(v, "concatenate"))
 323                return combine_notes_concatenate;
 324        else
 325                return NULL;
 326}
 327
 328static int notes_rewrite_config(const char *k, const char *v, void *cb)
 329{
 330        struct notes_rewrite_cfg *c = cb;
 331        if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
 332                c->enabled = git_config_bool(k, v);
 333                return 0;
 334        } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
 335                if (!v)
 336                        config_error_nonbool(k);
 337                c->combine = parse_combine_notes_fn(v);
 338                if (!c->combine) {
 339                        error("Bad notes.rewriteMode value: '%s'", v);
 340                        return 1;
 341                }
 342                return 0;
 343        } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
 344                /* note that a refs/ prefix is implied in the
 345                 * underlying for_each_glob_ref */
 346                if (!prefixcmp(v, "refs/notes/"))
 347                        string_list_add_refs_by_glob(c->refs, v);
 348                else
 349                        warning("Refusing to rewrite notes in %s"
 350                                " (outside of refs/notes/)", v);
 351                return 0;
 352        }
 353
 354        return 0;
 355}
 356
 357
 358struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
 359{
 360        struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
 361        const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
 362        const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
 363        c->cmd = cmd;
 364        c->enabled = 1;
 365        c->combine = combine_notes_concatenate;
 366        c->refs = xcalloc(1, sizeof(struct string_list));
 367        c->refs->strdup_strings = 1;
 368        c->refs_from_env = 0;
 369        c->mode_from_env = 0;
 370        if (rewrite_mode_env) {
 371                c->mode_from_env = 1;
 372                c->combine = parse_combine_notes_fn(rewrite_mode_env);
 373                if (!c->combine)
 374                        error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
 375                              " value: '%s'", rewrite_mode_env);
 376        }
 377        if (rewrite_refs_env) {
 378                c->refs_from_env = 1;
 379                string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
 380        }
 381        git_config(notes_rewrite_config, c);
 382        if (!c->enabled || !c->refs->nr) {
 383                string_list_clear(c->refs, 0);
 384                free(c->refs);
 385                free(c);
 386                return NULL;
 387        }
 388        c->trees = load_notes_trees(c->refs);
 389        string_list_clear(c->refs, 0);
 390        free(c->refs);
 391        return c;
 392}
 393
 394int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
 395                          const unsigned char *from_obj, const unsigned char *to_obj)
 396{
 397        int ret = 0;
 398        int i;
 399        for (i = 0; c->trees[i]; i++)
 400                ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
 401        return ret;
 402}
 403
 404void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
 405{
 406        int i;
 407        for (i = 0; c->trees[i]; i++) {
 408                commit_notes(c->trees[i], "Notes added by 'git notes copy'");
 409                free_notes(c->trees[i]);
 410        }
 411        free(c->trees);
 412        free(c);
 413}
 414
 415int notes_copy_from_stdin(int force, const char *rewrite_cmd)
 416{
 417        struct strbuf buf = STRBUF_INIT;
 418        struct notes_rewrite_cfg *c = NULL;
 419        struct notes_tree *t;
 420        int ret = 0;
 421
 422        if (rewrite_cmd) {
 423                c = init_copy_notes_for_rewrite(rewrite_cmd);
 424                if (!c)
 425                        return 0;
 426        } else {
 427                init_notes(NULL, NULL, NULL, 0);
 428                t = &default_notes_tree;
 429        }
 430
 431        while (strbuf_getline(&buf, stdin, '\n') != EOF) {
 432                unsigned char from_obj[20], to_obj[20];
 433                struct strbuf **split;
 434                int err;
 435
 436                split = strbuf_split(&buf, ' ');
 437                if (!split[0] || !split[1])
 438                        die("Malformed input line: '%s'.", buf.buf);
 439                strbuf_rtrim(split[0]);
 440                strbuf_rtrim(split[1]);
 441                if (get_sha1(split[0]->buf, from_obj))
 442                        die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
 443                if (get_sha1(split[1]->buf, to_obj))
 444                        die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
 445
 446                if (rewrite_cmd)
 447                        err = copy_note_for_rewrite(c, from_obj, to_obj);
 448                else
 449                        err = copy_note(t, from_obj, to_obj, force,
 450                                        combine_notes_overwrite);
 451
 452                if (err) {
 453                        error("Failed to copy notes from '%s' to '%s'",
 454                              split[0]->buf, split[1]->buf);
 455                        ret = 1;
 456                }
 457
 458                strbuf_list_free(split);
 459        }
 460
 461        if (!rewrite_cmd) {
 462                commit_notes(t, "Notes added by 'git notes copy'");
 463                free_notes(t);
 464        } else {
 465                finish_copy_notes_for_rewrite(c);
 466        }
 467        return ret;
 468}
 469
 470static struct notes_tree *init_notes_check(const char *subcommand)
 471{
 472        struct notes_tree *t;
 473        init_notes(NULL, NULL, NULL, 0);
 474        t = &default_notes_tree;
 475
 476        if (prefixcmp(t->ref, "refs/notes/"))
 477                die("Refusing to %s notes in %s (outside of refs/notes/)",
 478                    subcommand, t->ref);
 479        return t;
 480}
 481
 482static int list(int argc, const char **argv, const char *prefix)
 483{
 484        struct notes_tree *t;
 485        unsigned char object[20];
 486        const unsigned char *note;
 487        int retval = -1;
 488        struct option options[] = {
 489                OPT_END()
 490        };
 491
 492        if (argc)
 493                argc = parse_options(argc, argv, prefix, options,
 494                                     git_notes_list_usage, 0);
 495
 496        if (1 < argc) {
 497                error("too many parameters");
 498                usage_with_options(git_notes_list_usage, options);
 499        }
 500
 501        t = init_notes_check("list");
 502        if (argc) {
 503                if (get_sha1(argv[0], object))
 504                        die("Failed to resolve '%s' as a valid ref.", argv[0]);
 505                note = get_note(t, object);
 506                if (note) {
 507                        puts(sha1_to_hex(note));
 508                        retval = 0;
 509                } else
 510                        retval = error("No note found for object %s.",
 511                                       sha1_to_hex(object));
 512        } else
 513                retval = for_each_note(t, 0, list_each_note, NULL);
 514
 515        free_notes(t);
 516        return retval;
 517}
 518
 519static int add(int argc, const char **argv, const char *prefix)
 520{
 521        int retval = 0, force = 0;
 522        const char *object_ref;
 523        struct notes_tree *t;
 524        unsigned char object[20], new_note[20];
 525        char logmsg[100];
 526        const unsigned char *note;
 527        struct msg_arg msg = { 0, 0, STRBUF_INIT };
 528        struct option options[] = {
 529                { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
 530                        "note contents as a string", PARSE_OPT_NONEG,
 531                        parse_msg_arg},
 532                { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
 533                        "note contents in a file", PARSE_OPT_NONEG,
 534                        parse_file_arg},
 535                { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
 536                        "reuse and edit specified note object", PARSE_OPT_NONEG,
 537                        parse_reedit_arg},
 538                { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
 539                        "reuse specified note object", PARSE_OPT_NONEG,
 540                        parse_reuse_arg},
 541                OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
 542                OPT_END()
 543        };
 544
 545        argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
 546                             0);
 547
 548        if (1 < argc) {
 549                error("too many parameters");
 550                usage_with_options(git_notes_add_usage, options);
 551        }
 552
 553        object_ref = argc ? argv[0] : "HEAD";
 554
 555        if (get_sha1(object_ref, object))
 556                die("Failed to resolve '%s' as a valid ref.", object_ref);
 557
 558        t = init_notes_check("add");
 559        note = get_note(t, object);
 560
 561        if (note) {
 562                if (!force) {
 563                        retval = error("Cannot add notes. Found existing notes "
 564                                       "for object %s. Use '-f' to overwrite "
 565                                       "existing notes", sha1_to_hex(object));
 566                        goto out;
 567                }
 568                fprintf(stderr, "Overwriting existing notes for object %s\n",
 569                        sha1_to_hex(object));
 570        }
 571
 572        create_note(object, &msg, 0, note, new_note);
 573
 574        if (is_null_sha1(new_note))
 575                remove_note(t, object);
 576        else
 577                add_note(t, object, new_note, combine_notes_overwrite);
 578
 579        snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
 580                 is_null_sha1(new_note) ? "removed" : "added", "add");
 581        commit_notes(t, logmsg);
 582out:
 583        free_notes(t);
 584        strbuf_release(&(msg.buf));
 585        return retval;
 586}
 587
 588static int copy(int argc, const char **argv, const char *prefix)
 589{
 590        int retval = 0, force = 0, from_stdin = 0;
 591        const unsigned char *from_note, *note;
 592        const char *object_ref;
 593        unsigned char object[20], from_obj[20];
 594        struct notes_tree *t;
 595        const char *rewrite_cmd = NULL;
 596        struct option options[] = {
 597                OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
 598                OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
 599                OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
 600                           "load rewriting config for <command> (implies "
 601                           "--stdin)"),
 602                OPT_END()
 603        };
 604
 605        argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
 606                             0);
 607
 608        if (from_stdin || rewrite_cmd) {
 609                if (argc) {
 610                        error("too many parameters");
 611                        usage_with_options(git_notes_copy_usage, options);
 612                } else {
 613                        return notes_copy_from_stdin(force, rewrite_cmd);
 614                }
 615        }
 616
 617        if (2 < argc) {
 618                error("too many parameters");
 619                usage_with_options(git_notes_copy_usage, options);
 620        }
 621
 622        if (get_sha1(argv[0], from_obj))
 623                die("Failed to resolve '%s' as a valid ref.", argv[0]);
 624
 625        object_ref = 1 < argc ? argv[1] : "HEAD";
 626
 627        if (get_sha1(object_ref, object))
 628                die("Failed to resolve '%s' as a valid ref.", object_ref);
 629
 630        t = init_notes_check("copy");
 631        note = get_note(t, object);
 632
 633        if (note) {
 634                if (!force) {
 635                        retval = error("Cannot copy notes. Found existing "
 636                                       "notes for object %s. Use '-f' to "
 637                                       "overwrite existing notes",
 638                                       sha1_to_hex(object));
 639                        goto out;
 640                }
 641                fprintf(stderr, "Overwriting existing notes for object %s\n",
 642                        sha1_to_hex(object));
 643        }
 644
 645        from_note = get_note(t, from_obj);
 646        if (!from_note) {
 647                retval = error("Missing notes on source object %s. Cannot "
 648                               "copy.", sha1_to_hex(from_obj));
 649                goto out;
 650        }
 651
 652        add_note(t, object, from_note, combine_notes_overwrite);
 653        commit_notes(t, "Notes added by 'git notes copy'");
 654out:
 655        free_notes(t);
 656        return retval;
 657}
 658
 659static int append_edit(int argc, const char **argv, const char *prefix)
 660{
 661        const char *object_ref;
 662        struct notes_tree *t;
 663        unsigned char object[20], new_note[20];
 664        const unsigned char *note;
 665        char logmsg[100];
 666        const char * const *usage;
 667        struct msg_arg msg = { 0, 0, STRBUF_INIT };
 668        struct option options[] = {
 669                { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
 670                        "note contents as a string", PARSE_OPT_NONEG,
 671                        parse_msg_arg},
 672                { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
 673                        "note contents in a file", PARSE_OPT_NONEG,
 674                        parse_file_arg},
 675                { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
 676                        "reuse and edit specified note object", PARSE_OPT_NONEG,
 677                        parse_reedit_arg},
 678                { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
 679                        "reuse specified note object", PARSE_OPT_NONEG,
 680                        parse_reuse_arg},
 681                OPT_END()
 682        };
 683        int edit = !strcmp(argv[0], "edit");
 684
 685        usage = edit ? git_notes_edit_usage : git_notes_append_usage;
 686        argc = parse_options(argc, argv, prefix, options, usage,
 687                             PARSE_OPT_KEEP_ARGV0);
 688
 689        if (2 < argc) {
 690                error("too many parameters");
 691                usage_with_options(usage, options);
 692        }
 693
 694        if (msg.given && edit)
 695                fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
 696                        "for the 'edit' subcommand.\n"
 697                        "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
 698
 699        object_ref = 1 < argc ? argv[1] : "HEAD";
 700
 701        if (get_sha1(object_ref, object))
 702                die("Failed to resolve '%s' as a valid ref.", object_ref);
 703
 704        t = init_notes_check(argv[0]);
 705        note = get_note(t, object);
 706
 707        create_note(object, &msg, !edit, note, new_note);
 708
 709        if (is_null_sha1(new_note))
 710                remove_note(t, object);
 711        else
 712                add_note(t, object, new_note, combine_notes_overwrite);
 713
 714        snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
 715                 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
 716        commit_notes(t, logmsg);
 717        free_notes(t);
 718        strbuf_release(&(msg.buf));
 719        return 0;
 720}
 721
 722static int show(int argc, const char **argv, const char *prefix)
 723{
 724        const char *object_ref;
 725        struct notes_tree *t;
 726        unsigned char object[20];
 727        const unsigned char *note;
 728        int retval;
 729        struct option options[] = {
 730                OPT_END()
 731        };
 732
 733        argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
 734                             0);
 735
 736        if (1 < argc) {
 737                error("too many parameters");
 738                usage_with_options(git_notes_show_usage, options);
 739        }
 740
 741        object_ref = argc ? argv[0] : "HEAD";
 742
 743        if (get_sha1(object_ref, object))
 744                die("Failed to resolve '%s' as a valid ref.", object_ref);
 745
 746        t = init_notes_check("show");
 747        note = get_note(t, object);
 748
 749        if (!note)
 750                retval = error("No note found for object %s.",
 751                               sha1_to_hex(object));
 752        else {
 753                const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
 754                retval = execv_git_cmd(show_args);
 755        }
 756        free_notes(t);
 757        return retval;
 758}
 759
 760static int remove_cmd(int argc, const char **argv, const char *prefix)
 761{
 762        struct option options[] = {
 763                OPT_END()
 764        };
 765        const char *object_ref;
 766        struct notes_tree *t;
 767        unsigned char object[20];
 768
 769        argc = parse_options(argc, argv, prefix, options,
 770                             git_notes_remove_usage, 0);
 771
 772        if (1 < argc) {
 773                error("too many parameters");
 774                usage_with_options(git_notes_remove_usage, options);
 775        }
 776
 777        object_ref = argc ? argv[0] : "HEAD";
 778
 779        if (get_sha1(object_ref, object))
 780                die("Failed to resolve '%s' as a valid ref.", object_ref);
 781
 782        t = init_notes_check("remove");
 783
 784        fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object));
 785        remove_note(t, object);
 786
 787        commit_notes(t, "Notes removed by 'git notes remove'");
 788        free_notes(t);
 789        return 0;
 790}
 791
 792static int prune(int argc, const char **argv, const char *prefix)
 793{
 794        struct notes_tree *t;
 795        int show_only = 0, verbose = 0;
 796        struct option options[] = {
 797                OPT_BOOLEAN('n', NULL, &show_only, "do not remove, show only"),
 798                OPT_BOOLEAN('v', NULL, &verbose, "report pruned notes"),
 799                OPT_END()
 800        };
 801
 802        argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
 803                             0);
 804
 805        if (argc) {
 806                error("too many parameters");
 807                usage_with_options(git_notes_prune_usage, options);
 808        }
 809
 810        t = init_notes_check("prune");
 811
 812        prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
 813                (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
 814        if (!show_only)
 815                commit_notes(t, "Notes removed by 'git notes prune'");
 816        free_notes(t);
 817        return 0;
 818}
 819
 820int cmd_notes(int argc, const char **argv, const char *prefix)
 821{
 822        int result;
 823        const char *override_notes_ref = NULL;
 824        struct option options[] = {
 825                OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
 826                           "use notes from <notes_ref>"),
 827                OPT_END()
 828        };
 829
 830        git_config(git_default_config, NULL);
 831        argc = parse_options(argc, argv, prefix, options, git_notes_usage,
 832                             PARSE_OPT_STOP_AT_NON_OPTION);
 833
 834        if (override_notes_ref) {
 835                struct strbuf sb = STRBUF_INIT;
 836                if (!prefixcmp(override_notes_ref, "refs/notes/"))
 837                        /* we're happy */;
 838                else if (!prefixcmp(override_notes_ref, "notes/"))
 839                        strbuf_addstr(&sb, "refs/");
 840                else
 841                        strbuf_addstr(&sb, "refs/notes/");
 842                strbuf_addstr(&sb, override_notes_ref);
 843                setenv("GIT_NOTES_REF", sb.buf, 1);
 844                strbuf_release(&sb);
 845        }
 846
 847        if (argc < 1 || !strcmp(argv[0], "list"))
 848                result = list(argc, argv, prefix);
 849        else if (!strcmp(argv[0], "add"))
 850                result = add(argc, argv, prefix);
 851        else if (!strcmp(argv[0], "copy"))
 852                result = copy(argc, argv, prefix);
 853        else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
 854                result = append_edit(argc, argv, prefix);
 855        else if (!strcmp(argv[0], "show"))
 856                result = show(argc, argv, prefix);
 857        else if (!strcmp(argv[0], "remove"))
 858                result = remove_cmd(argc, argv, prefix);
 859        else if (!strcmp(argv[0], "prune"))
 860                result = prune(argc, argv, prefix);
 861        else {
 862                result = error("Unknown subcommand: %s", argv[0]);
 863                usage_with_options(git_notes_usage, options);
 864        }
 865
 866        return result ? 1 : 0;
 867}