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