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