builtin / notes.con commit Merge branch 'tr/notes-display' (a86ed83)
   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 [list [<object>]]",
  23        "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  24        "git notes copy [-f] <from-object> <to-object>",
  25        "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  26        "git notes edit [<object>]",
  27        "git notes show [<object>]",
  28        "git notes remove [<object>]",
  29        "git notes prune",
  30        NULL
  31};
  32
  33static const char note_template[] =
  34        "\n"
  35        "#\n"
  36        "# Write/edit the notes for the following object:\n"
  37        "#\n";
  38
  39struct msg_arg {
  40        int given;
  41        int use_editor;
  42        struct strbuf buf;
  43};
  44
  45static int list_each_note(const unsigned char *object_sha1,
  46                const unsigned char *note_sha1, char *note_path,
  47                void *cb_data)
  48{
  49        printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
  50        return 0;
  51}
  52
  53static void write_note_data(int fd, const unsigned char *sha1)
  54{
  55        unsigned long size;
  56        enum object_type type;
  57        char *buf = read_sha1_file(sha1, &type, &size);
  58        if (buf) {
  59                if (size)
  60                        write_or_die(fd, buf, size);
  61                free(buf);
  62        }
  63}
  64
  65static void write_commented_object(int fd, const unsigned char *object)
  66{
  67        const char *show_args[5] =
  68                {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
  69        struct child_process show;
  70        struct strbuf buf = STRBUF_INIT;
  71        FILE *show_out;
  72
  73        /* Invoke "git show --stat --no-notes $object" */
  74        memset(&show, 0, sizeof(show));
  75        show.argv = show_args;
  76        show.no_stdin = 1;
  77        show.out = -1;
  78        show.err = 0;
  79        show.git_cmd = 1;
  80        if (start_command(&show))
  81                die("unable to start 'show' for object '%s'",
  82                    sha1_to_hex(object));
  83
  84        /* Open the output as FILE* so strbuf_getline() can be used. */
  85        show_out = xfdopen(show.out, "r");
  86        if (show_out == NULL)
  87                die_errno("can't fdopen 'show' output fd");
  88
  89        /* Prepend "# " to each output line and write result to 'fd' */
  90        while (strbuf_getline(&buf, show_out, '\n') != EOF) {
  91                write_or_die(fd, "# ", 2);
  92                write_or_die(fd, buf.buf, buf.len);
  93                write_or_die(fd, "\n", 1);
  94        }
  95        strbuf_release(&buf);
  96        if (fclose(show_out))
  97                die_errno("failed to close pipe to 'show' for object '%s'",
  98                          sha1_to_hex(object));
  99        if (finish_command(&show))
 100                die("failed to finish 'show' for object '%s'",
 101                    sha1_to_hex(object));
 102}
 103
 104static void create_note(const unsigned char *object, struct msg_arg *msg,
 105                        int append_only, const unsigned char *prev,
 106                        unsigned char *result)
 107{
 108        char *path = NULL;
 109
 110        if (msg->use_editor || !msg->given) {
 111                int fd;
 112
 113                /* write the template message before editing: */
 114                path = git_pathdup("NOTES_EDITMSG");
 115                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 116                if (fd < 0)
 117                        die_errno("could not create file '%s'", path);
 118
 119                if (msg->given)
 120                        write_or_die(fd, msg->buf.buf, msg->buf.len);
 121                else if (prev && !append_only)
 122                        write_note_data(fd, prev);
 123                write_or_die(fd, note_template, strlen(note_template));
 124
 125                write_commented_object(fd, object);
 126
 127                close(fd);
 128                strbuf_reset(&(msg->buf));
 129
 130                if (launch_editor(path, &(msg->buf), NULL)) {
 131                        die("Please supply the note contents using either -m" \
 132                            " or -F option");
 133                }
 134                stripspace(&(msg->buf), 1);
 135        }
 136
 137        if (prev && append_only) {
 138                /* Append buf to previous note contents */
 139                unsigned long size;
 140                enum object_type type;
 141                char *prev_buf = read_sha1_file(prev, &type, &size);
 142
 143                strbuf_grow(&(msg->buf), size + 1);
 144                if (msg->buf.len && prev_buf && size)
 145                        strbuf_insert(&(msg->buf), 0, "\n", 1);
 146                if (prev_buf && size)
 147                        strbuf_insert(&(msg->buf), 0, prev_buf, size);
 148                free(prev_buf);
 149        }
 150
 151        if (!msg->buf.len) {
 152                fprintf(stderr, "Removing note for object %s\n",
 153                        sha1_to_hex(object));
 154                hashclr(result);
 155        } else {
 156                if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
 157                        error("unable to write note object");
 158                        if (path)
 159                                error("The note contents has been left in %s",
 160                                      path);
 161                        exit(128);
 162                }
 163        }
 164
 165        if (path) {
 166                unlink_or_warn(path);
 167                free(path);
 168        }
 169}
 170
 171static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 172{
 173        struct msg_arg *msg = opt->value;
 174
 175        strbuf_grow(&(msg->buf), strlen(arg) + 2);
 176        if (msg->buf.len)
 177                strbuf_addch(&(msg->buf), '\n');
 178        strbuf_addstr(&(msg->buf), arg);
 179        stripspace(&(msg->buf), 0);
 180
 181        msg->given = 1;
 182        return 0;
 183}
 184
 185static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 186{
 187        struct msg_arg *msg = opt->value;
 188
 189        if (msg->buf.len)
 190                strbuf_addch(&(msg->buf), '\n');
 191        if (!strcmp(arg, "-")) {
 192                if (strbuf_read(&(msg->buf), 0, 1024) < 0)
 193                        die_errno("cannot read '%s'", arg);
 194        } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
 195                die_errno("could not open or read '%s'", arg);
 196        stripspace(&(msg->buf), 0);
 197
 198        msg->given = 1;
 199        return 0;
 200}
 201
 202static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
 203{
 204        struct msg_arg *msg = opt->value;
 205        char *buf;
 206        unsigned char object[20];
 207        enum object_type type;
 208        unsigned long len;
 209
 210        if (msg->buf.len)
 211                strbuf_addch(&(msg->buf), '\n');
 212
 213        if (get_sha1(arg, object))
 214                die("Failed to resolve '%s' as a valid ref.", arg);
 215        if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
 216                free(buf);
 217                die("Failed to read object '%s'.", arg);;
 218        }
 219        strbuf_add(&(msg->buf), buf, len);
 220        free(buf);
 221
 222        msg->given = 1;
 223        return 0;
 224}
 225
 226static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
 227{
 228        struct msg_arg *msg = opt->value;
 229        msg->use_editor = 1;
 230        return parse_reuse_arg(opt, arg, unset);
 231}
 232
 233int commit_notes(struct notes_tree *t, const char *msg)
 234{
 235        struct commit_list *parent;
 236        unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
 237        struct strbuf buf = STRBUF_INIT;
 238
 239        if (!t)
 240                t = &default_notes_tree;
 241        if (!t->initialized || !t->ref || !*t->ref)
 242                die("Cannot commit uninitialized/unreferenced notes tree");
 243        if (!t->dirty)
 244                return 0; /* don't have to commit an unchanged tree */
 245
 246        /* Prepare commit message and reflog message */
 247        strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
 248        strbuf_addstr(&buf, msg);
 249        if (buf.buf[buf.len - 1] != '\n')
 250                strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
 251
 252        /* Convert notes tree to tree object */
 253        if (write_notes_tree(t, tree_sha1))
 254                die("Failed to write current notes tree to database");
 255
 256        /* Create new commit for the tree object */
 257        if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
 258                parent = xmalloc(sizeof(*parent));
 259                parent->item = lookup_commit(prev_commit);
 260                parent->next = NULL;
 261        } else {
 262                hashclr(prev_commit);
 263                parent = NULL;
 264        }
 265        if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
 266                die("Failed to commit notes tree to database");
 267
 268        /* Update notes ref with new commit */
 269        update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
 270
 271        strbuf_release(&buf);
 272        return 0;
 273}
 274
 275
 276combine_notes_fn *parse_combine_notes_fn(const char *v)
 277{
 278        if (!strcasecmp(v, "overwrite"))
 279                return combine_notes_overwrite;
 280        else if (!strcasecmp(v, "ignore"))
 281                return combine_notes_ignore;
 282        else if (!strcasecmp(v, "concatenate"))
 283                return combine_notes_concatenate;
 284        else
 285                return NULL;
 286}
 287
 288static int notes_rewrite_config(const char *k, const char *v, void *cb)
 289{
 290        struct notes_rewrite_cfg *c = cb;
 291        if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
 292                c->enabled = git_config_bool(k, v);
 293                return 0;
 294        } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
 295                if (!v)
 296                        config_error_nonbool(k);
 297                c->combine = parse_combine_notes_fn(v);
 298                if (!c->combine) {
 299                        error("Bad notes.rewriteMode value: '%s'", v);
 300                        return 1;
 301                }
 302                return 0;
 303        } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
 304                /* note that a refs/ prefix is implied in the
 305                 * underlying for_each_glob_ref */
 306                if (!prefixcmp(v, "refs/notes/"))
 307                        string_list_add_refs_by_glob(c->refs, v);
 308                else
 309                        warning("Refusing to rewrite notes in %s"
 310                                " (outside of refs/notes/)", v);
 311                return 0;
 312        }
 313
 314        return 0;
 315}
 316
 317
 318struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
 319{
 320        struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
 321        const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
 322        const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
 323        c->cmd = cmd;
 324        c->enabled = 1;
 325        c->combine = combine_notes_concatenate;
 326        c->refs = xcalloc(1, sizeof(struct string_list));
 327        c->refs->strdup_strings = 1;
 328        c->refs_from_env = 0;
 329        c->mode_from_env = 0;
 330        if (rewrite_mode_env) {
 331                c->mode_from_env = 1;
 332                c->combine = parse_combine_notes_fn(rewrite_mode_env);
 333                if (!c->combine)
 334                        error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT
 335                              " value: '%s'", rewrite_mode_env);
 336        }
 337        if (rewrite_refs_env) {
 338                c->refs_from_env = 1;
 339                string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
 340        }
 341        git_config(notes_rewrite_config, c);
 342        if (!c->enabled || !c->refs->nr) {
 343                string_list_clear(c->refs, 0);
 344                free(c->refs);
 345                free(c);
 346                return NULL;
 347        }
 348        c->trees = load_notes_trees(c->refs);
 349        string_list_clear(c->refs, 0);
 350        free(c->refs);
 351        return c;
 352}
 353
 354int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
 355                          const unsigned char *from_obj, const unsigned char *to_obj)
 356{
 357        int ret = 0;
 358        int i;
 359        for (i = 0; c->trees[i]; i++)
 360                ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
 361        return ret;
 362}
 363
 364void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
 365{
 366        int i;
 367        for (i = 0; c->trees[i]; i++) {
 368                commit_notes(c->trees[i], "Notes added by 'git notes copy'");
 369                free_notes(c->trees[i]);
 370        }
 371        free(c->trees);
 372        free(c);
 373}
 374
 375int notes_copy_from_stdin(int force, const char *rewrite_cmd)
 376{
 377        struct strbuf buf = STRBUF_INIT;
 378        struct notes_rewrite_cfg *c = NULL;
 379        struct notes_tree *t;
 380        int ret = 0;
 381
 382        if (rewrite_cmd) {
 383                c = init_copy_notes_for_rewrite(rewrite_cmd);
 384                if (!c)
 385                        return 0;
 386        } else {
 387                init_notes(NULL, NULL, NULL, 0);
 388                t = &default_notes_tree;
 389        }
 390
 391        while (strbuf_getline(&buf, stdin, '\n') != EOF) {
 392                unsigned char from_obj[20], to_obj[20];
 393                struct strbuf **split;
 394                int err;
 395
 396                split = strbuf_split(&buf, ' ');
 397                if (!split[0] || !split[1])
 398                        die("Malformed input line: '%s'.", buf.buf);
 399                strbuf_rtrim(split[0]);
 400                strbuf_rtrim(split[1]);
 401                if (get_sha1(split[0]->buf, from_obj))
 402                        die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
 403                if (get_sha1(split[1]->buf, to_obj))
 404                        die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
 405
 406                if (rewrite_cmd)
 407                        err = copy_note_for_rewrite(c, from_obj, to_obj);
 408                else
 409                        err = copy_note(t, from_obj, to_obj, force,
 410                                        combine_notes_overwrite);
 411
 412                if (err) {
 413                        error("Failed to copy notes from '%s' to '%s'",
 414                              split[0]->buf, split[1]->buf);
 415                        ret = 1;
 416                }
 417
 418                strbuf_list_free(split);
 419        }
 420
 421        if (!rewrite_cmd) {
 422                commit_notes(t, "Notes added by 'git notes copy'");
 423                free_notes(t);
 424        } else {
 425                finish_copy_notes_for_rewrite(c);
 426        }
 427        return ret;
 428}
 429
 430int cmd_notes(int argc, const char **argv, const char *prefix)
 431{
 432        struct notes_tree *t;
 433        unsigned char object[20], from_obj[20], new_note[20];
 434        const unsigned char *note;
 435        const char *object_ref;
 436        char logmsg[100];
 437
 438        int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
 439            remove = 0, prune = 0, force = 0, from_stdin = 0;
 440        int given_object = 0, i = 1, retval = 0;
 441        struct msg_arg msg = { 0, 0, STRBUF_INIT };
 442        const char *rewrite_cmd = NULL;
 443        const char *override_notes_ref = NULL;
 444        struct option options[] = {
 445                OPT_GROUP("Notes contents options"),
 446                { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
 447                        "note contents as a string", PARSE_OPT_NONEG,
 448                        parse_msg_arg},
 449                { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
 450                        "note contents in a file", PARSE_OPT_NONEG,
 451                        parse_file_arg},
 452                { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
 453                        "reuse and edit specified note object", PARSE_OPT_NONEG,
 454                        parse_reedit_arg},
 455                { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
 456                        "reuse specified note object", PARSE_OPT_NONEG,
 457                        parse_reuse_arg},
 458                OPT_GROUP("Other options"),
 459                OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
 460                OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
 461                OPT_STRING(0, "ref", &override_notes_ref, "notes_ref",
 462                           "use notes from <notes_ref>"),
 463                OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command",
 464                           "load rewriting config for <command> (implies --stdin)"),
 465                OPT_END()
 466        };
 467
 468        git_config(git_default_config, NULL);
 469
 470        argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
 471
 472        if (override_notes_ref) {
 473                struct strbuf sb = STRBUF_INIT;
 474                if (!prefixcmp(override_notes_ref, "refs/notes/"))
 475                        /* we're happy */;
 476                else if (!prefixcmp(override_notes_ref, "notes/"))
 477                        strbuf_addstr(&sb, "refs/");
 478                else
 479                        strbuf_addstr(&sb, "refs/notes/");
 480                strbuf_addstr(&sb, override_notes_ref);
 481                setenv("GIT_NOTES_REF", sb.buf, 1);
 482                strbuf_release(&sb);
 483        }
 484
 485        if (argc && !strcmp(argv[0], "list"))
 486                list = 1;
 487        else if (argc && !strcmp(argv[0], "add"))
 488                add = 1;
 489        else if (argc && !strcmp(argv[0], "copy"))
 490                copy = 1;
 491        else if (argc && !strcmp(argv[0], "append"))
 492                append = 1;
 493        else if (argc && !strcmp(argv[0], "edit"))
 494                edit = 1;
 495        else if (argc && !strcmp(argv[0], "show"))
 496                show = 1;
 497        else if (argc && !strcmp(argv[0], "remove"))
 498                remove = 1;
 499        else if (argc && !strcmp(argv[0], "prune"))
 500                prune = 1;
 501        else if (!argc) {
 502                list = 1; /* Default to 'list' if no other subcommand given */
 503                i = 0;
 504        }
 505
 506        if (list + add + copy + append + edit + show + remove + prune != 1)
 507                usage_with_options(git_notes_usage, options);
 508
 509        if (msg.given && !(add || append || edit)) {
 510                error("cannot use -m/-F/-c/-C options with %s subcommand.",
 511                      argv[0]);
 512                usage_with_options(git_notes_usage, options);
 513        }
 514
 515        if (msg.given && edit) {
 516                fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
 517                        "for the 'edit' subcommand.\n"
 518                        "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
 519        }
 520
 521        if (force && !(add || copy)) {
 522                error("cannot use -f option with %s subcommand.", argv[0]);
 523                usage_with_options(git_notes_usage, options);
 524        }
 525
 526        if (!copy && rewrite_cmd) {
 527                error("cannot use --for-rewrite with %s subcommand.", argv[0]);
 528                usage_with_options(git_notes_usage, options);
 529        }
 530        if (!copy && from_stdin) {
 531                error("cannot use --stdin with %s subcommand.", argv[0]);
 532                usage_with_options(git_notes_usage, options);
 533        }
 534
 535        if (copy) {
 536                const char *from_ref;
 537                if (from_stdin || rewrite_cmd) {
 538                        if (argc > 1) {
 539                                error("too many parameters");
 540                                usage_with_options(git_notes_usage, options);
 541                        } else {
 542                                return notes_copy_from_stdin(force, rewrite_cmd);
 543                        }
 544                }
 545                if (argc < 3) {
 546                        error("too few parameters");
 547                        usage_with_options(git_notes_usage, options);
 548                }
 549                from_ref = argv[i++];
 550                if (get_sha1(from_ref, from_obj))
 551                        die("Failed to resolve '%s' as a valid ref.", from_ref);
 552        }
 553
 554        given_object = argc > i;
 555        object_ref = given_object ? argv[i++] : "HEAD";
 556
 557        if (argc > i || (prune && given_object)) {
 558                error("too many parameters");
 559                usage_with_options(git_notes_usage, options);
 560        }
 561
 562        if (get_sha1(object_ref, object))
 563                die("Failed to resolve '%s' as a valid ref.", object_ref);
 564
 565        init_notes(NULL, NULL, NULL, 0);
 566        t = &default_notes_tree;
 567
 568        if (prefixcmp(t->ref, "refs/notes/"))
 569                die("Refusing to %s notes in %s (outside of refs/notes/)",
 570                    argv[0], t->ref);
 571
 572        note = get_note(t, object);
 573
 574        /* list command */
 575
 576        if (list) {
 577                if (given_object) {
 578                        if (note) {
 579                                puts(sha1_to_hex(note));
 580                                goto end;
 581                        }
 582                } else {
 583                        retval = for_each_note(t, 0, list_each_note, NULL);
 584                        goto end;
 585                }
 586        }
 587
 588        /* show command */
 589
 590        if ((list || show) && !note) {
 591                error("No note found for object %s.", sha1_to_hex(object));
 592                retval = 1;
 593                goto end;
 594        } else if (show) {
 595                const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
 596                retval = execv_git_cmd(show_args);
 597                goto end;
 598        }
 599
 600        /* add/append/edit/remove/prune command */
 601
 602        if ((add || copy) && note) {
 603                if (!force) {
 604                        error("Cannot %s notes. Found existing notes for object"
 605                              " %s. Use '-f' to overwrite existing notes",
 606                              argv[0], sha1_to_hex(object));
 607                        retval = 1;
 608                        goto end;
 609                }
 610                fprintf(stderr, "Overwriting existing notes for object %s\n",
 611                        sha1_to_hex(object));
 612        }
 613
 614        if (remove) {
 615                msg.given = 1;
 616                msg.use_editor = 0;
 617                strbuf_reset(&(msg.buf));
 618        }
 619
 620        if (prune) {
 621                hashclr(new_note);
 622                prune_notes(t);
 623                goto commit;
 624        } else if (copy) {
 625                const unsigned char *from_note = get_note(t, from_obj);
 626                if (!from_note) {
 627                        error("Missing notes on source object %s. Cannot copy.",
 628                              sha1_to_hex(from_obj));
 629                        retval = 1;
 630                        goto end;
 631                }
 632                hashcpy(new_note, from_note);
 633        } else
 634                create_note(object, &msg, append, note, new_note);
 635
 636        if (is_null_sha1(new_note))
 637                remove_note(t, object);
 638        else
 639                add_note(t, object, new_note, combine_notes_overwrite);
 640
 641commit:
 642        snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
 643                 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
 644        commit_notes(t, logmsg);
 645
 646end:
 647        free_notes(t);
 648        strbuf_release(&(msg.buf));
 649        return retval;
 650}