builtin-notes.con commit builtin-notes: Add -c/-C options for reusing notes (0691cff)
   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
  20static const char * const git_notes_usage[] = {
  21        "git notes [list [<object>]]",
  22        "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  23        "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
  24        "git notes edit [<object>]",
  25        "git notes show [<object>]",
  26        "git notes remove [<object>]",
  27        "git notes prune",
  28        NULL
  29};
  30
  31static const char note_template[] =
  32        "\n"
  33        "#\n"
  34        "# Write/edit the notes for the following object:\n"
  35        "#\n";
  36
  37struct msg_arg {
  38        int given;
  39        int use_editor;
  40        struct strbuf buf;
  41};
  42
  43static int list_each_note(const unsigned char *object_sha1,
  44                const unsigned char *note_sha1, char *note_path,
  45                void *cb_data)
  46{
  47        printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
  48        return 0;
  49}
  50
  51static void write_note_data(int fd, const unsigned char *sha1)
  52{
  53        unsigned long size;
  54        enum object_type type;
  55        char *buf = read_sha1_file(sha1, &type, &size);
  56        if (buf) {
  57                if (size)
  58                        write_or_die(fd, buf, size);
  59                free(buf);
  60        }
  61}
  62
  63static void write_commented_object(int fd, const unsigned char *object)
  64{
  65        const char *show_args[5] =
  66                {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
  67        struct child_process show;
  68        struct strbuf buf = STRBUF_INIT;
  69        FILE *show_out;
  70
  71        /* Invoke "git show --stat --no-notes $object" */
  72        memset(&show, 0, sizeof(show));
  73        show.argv = show_args;
  74        show.no_stdin = 1;
  75        show.out = -1;
  76        show.err = 0;
  77        show.git_cmd = 1;
  78        if (start_command(&show))
  79                die("unable to start 'show' for object '%s'",
  80                    sha1_to_hex(object));
  81
  82        /* Open the output as FILE* so strbuf_getline() can be used. */
  83        show_out = xfdopen(show.out, "r");
  84        if (show_out == NULL)
  85                die_errno("can't fdopen 'show' output fd");
  86
  87        /* Prepend "# " to each output line and write result to 'fd' */
  88        while (strbuf_getline(&buf, show_out, '\n') != EOF) {
  89                write_or_die(fd, "# ", 2);
  90                write_or_die(fd, buf.buf, buf.len);
  91                write_or_die(fd, "\n", 1);
  92        }
  93        strbuf_release(&buf);
  94        if (fclose(show_out))
  95                die_errno("failed to close pipe to 'show' for object '%s'",
  96                          sha1_to_hex(object));
  97        if (finish_command(&show))
  98                die("failed to finish 'show' for object '%s'",
  99                    sha1_to_hex(object));
 100}
 101
 102static void create_note(const unsigned char *object, struct msg_arg *msg,
 103                        int append_only, const unsigned char *prev,
 104                        unsigned char *result)
 105{
 106        char *path = NULL;
 107
 108        if (msg->use_editor || !msg->given) {
 109                int fd;
 110
 111                /* write the template message before editing: */
 112                path = git_pathdup("NOTES_EDITMSG");
 113                fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 114                if (fd < 0)
 115                        die_errno("could not create file '%s'", path);
 116
 117                if (msg->given)
 118                        write_or_die(fd, msg->buf.buf, msg->buf.len);
 119                else if (prev && !append_only)
 120                        write_note_data(fd, prev);
 121                write_or_die(fd, note_template, strlen(note_template));
 122
 123                write_commented_object(fd, object);
 124
 125                close(fd);
 126                strbuf_reset(&(msg->buf));
 127
 128                if (launch_editor(path, &(msg->buf), NULL)) {
 129                        die("Please supply the note contents using either -m" \
 130                            " or -F option");
 131                }
 132                stripspace(&(msg->buf), 1);
 133        }
 134
 135        if (prev && append_only) {
 136                /* Append buf to previous note contents */
 137                unsigned long size;
 138                enum object_type type;
 139                char *prev_buf = read_sha1_file(prev, &type, &size);
 140
 141                strbuf_grow(&(msg->buf), size + 1);
 142                if (msg->buf.len && prev_buf && size)
 143                        strbuf_insert(&(msg->buf), 0, "\n", 1);
 144                if (prev_buf && size)
 145                        strbuf_insert(&(msg->buf), 0, prev_buf, size);
 146                free(prev_buf);
 147        }
 148
 149        if (!msg->buf.len) {
 150                fprintf(stderr, "Removing note for object %s\n",
 151                        sha1_to_hex(object));
 152                hashclr(result);
 153        } else {
 154                if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
 155                        error("unable to write note object");
 156                        if (path)
 157                                error("The note contents has been left in %s",
 158                                      path);
 159                        exit(128);
 160                }
 161        }
 162
 163        if (path) {
 164                unlink_or_warn(path);
 165                free(path);
 166        }
 167}
 168
 169static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 170{
 171        struct msg_arg *msg = opt->value;
 172
 173        if (!arg)
 174                return -1;
 175
 176        strbuf_grow(&(msg->buf), strlen(arg) + 2);
 177        if (msg->buf.len)
 178                strbuf_addstr(&(msg->buf), "\n");
 179        strbuf_addstr(&(msg->buf), arg);
 180        stripspace(&(msg->buf), 0);
 181
 182        msg->given = 1;
 183        return 0;
 184}
 185
 186static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 187{
 188        struct msg_arg *msg = opt->value;
 189
 190        if (!arg)
 191                return -1;
 192
 193        if (msg->buf.len)
 194                strbuf_addstr(&(msg->buf), "\n");
 195        if (!strcmp(arg, "-")) {
 196                if (strbuf_read(&(msg->buf), 0, 1024) < 0)
 197                        die_errno("cannot read '%s'", arg);
 198        } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
 199                die_errno("could not open or read '%s'", arg);
 200        stripspace(&(msg->buf), 0);
 201
 202        msg->given = 1;
 203        return 0;
 204}
 205
 206static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
 207{
 208        struct msg_arg *msg = opt->value;
 209        char *buf;
 210        unsigned char object[20];
 211        enum object_type type;
 212        unsigned long len;
 213
 214        if (!arg)
 215                return -1;
 216
 217        if (msg->buf.len)
 218                strbuf_addstr(&(msg->buf), "\n");
 219
 220        if (get_sha1(arg, object))
 221                die("Failed to resolve '%s' as a valid ref.", arg);
 222        if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
 223                free(buf);
 224                die("Failed to read object '%s'.", arg);;
 225        }
 226        strbuf_add(&(msg->buf), buf, len);
 227        free(buf);
 228
 229        msg->given = 1;
 230        return 0;
 231}
 232
 233static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
 234{
 235        struct msg_arg *msg = opt->value;
 236        msg->use_editor = 1;
 237        return parse_reuse_arg(opt, arg, unset);
 238}
 239
 240int commit_notes(struct notes_tree *t, const char *msg)
 241{
 242        struct commit_list *parent;
 243        unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
 244        struct strbuf buf = STRBUF_INIT;
 245
 246        if (!t)
 247                t = &default_notes_tree;
 248        if (!t->initialized || !t->ref || !*t->ref)
 249                die("Cannot commit uninitialized/unreferenced notes tree");
 250
 251        /* Prepare commit message and reflog message */
 252        strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
 253        strbuf_addstr(&buf, msg);
 254        if (buf.buf[buf.len - 1] != '\n')
 255                strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
 256
 257        /* Convert notes tree to tree object */
 258        if (write_notes_tree(t, tree_sha1))
 259                die("Failed to write current notes tree to database");
 260
 261        /* Create new commit for the tree object */
 262        if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
 263                parent = xmalloc(sizeof(*parent));
 264                parent->item = lookup_commit(prev_commit);
 265                parent->next = NULL;
 266        } else {
 267                hashclr(prev_commit);
 268                parent = NULL;
 269        }
 270        if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
 271                die("Failed to commit notes tree to database");
 272
 273        /* Update notes ref with new commit */
 274        update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
 275
 276        strbuf_release(&buf);
 277        return 0;
 278}
 279
 280int cmd_notes(int argc, const char **argv, const char *prefix)
 281{
 282        struct notes_tree *t;
 283        unsigned char object[20], new_note[20];
 284        const unsigned char *note;
 285        const char *object_ref;
 286        char logmsg[100];
 287
 288        int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
 289            prune = 0, force = 0;
 290        int given_object;
 291        struct msg_arg msg = { 0, 0, STRBUF_INIT };
 292        struct option options[] = {
 293                OPT_GROUP("Notes options"),
 294                OPT_CALLBACK('m', "message", &msg, "MSG",
 295                             "note contents as a string", parse_msg_arg),
 296                OPT_CALLBACK('F', "file", &msg, "FILE",
 297                             "note contents in a file", parse_file_arg),
 298                OPT_CALLBACK('c', "reedit-message", &msg, "OBJECT",
 299                           "reuse and edit specified note object", parse_reedit_arg),
 300                OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
 301                           "reuse specified note object", parse_reuse_arg),
 302                OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
 303                OPT_END()
 304        };
 305
 306        git_config(git_default_config, NULL);
 307
 308        argc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);
 309
 310        if (argc && !strcmp(argv[0], "list"))
 311                list = 1;
 312        else if (argc && !strcmp(argv[0], "add"))
 313                add = 1;
 314        else if (argc && !strcmp(argv[0], "append"))
 315                append = 1;
 316        else if (argc && !strcmp(argv[0], "edit"))
 317                edit = 1;
 318        else if (argc && !strcmp(argv[0], "show"))
 319                show = 1;
 320        else if (argc && !strcmp(argv[0], "remove"))
 321                remove = 1;
 322        else if (argc && !strcmp(argv[0], "prune"))
 323                prune = 1;
 324        else if (!argc)
 325                list = 1; /* Default to 'list' if no other subcommand given */
 326
 327        if (list + add + append + edit + show + remove + prune != 1)
 328                usage_with_options(git_notes_usage, options);
 329
 330        if (msg.given && !(add || append || edit)) {
 331                error("cannot use -m/-F/-c/-C options with %s subcommand.",
 332                      argv[0]);
 333                usage_with_options(git_notes_usage, options);
 334        }
 335
 336        if (msg.given && edit) {
 337                fprintf(stderr, "The -m/-F/-c/-C options have been deprecated "
 338                        "for the 'edit' subcommand.\n"
 339                        "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
 340        }
 341
 342        if (force && !add) {
 343                error("cannot use -f option with %s subcommand.", argv[0]);
 344                usage_with_options(git_notes_usage, options);
 345        }
 346
 347        given_object = argc == 2;
 348        object_ref = given_object ? argv[1] : "HEAD";
 349        if (argc > 2 || (prune && argc > 1)) {
 350                error("too many parameters");
 351                usage_with_options(git_notes_usage, options);
 352        }
 353
 354        if (get_sha1(object_ref, object))
 355                die("Failed to resolve '%s' as a valid ref.", object_ref);
 356
 357        init_notes(NULL, NULL, NULL, 0);
 358        t = &default_notes_tree;
 359
 360        if (prefixcmp(t->ref, "refs/notes/"))
 361                die("Refusing to %s notes in %s (outside of refs/notes/)",
 362                    argv[0], t->ref);
 363
 364        note = get_note(t, object);
 365
 366        /* list command */
 367
 368        if (list) {
 369                if (given_object) {
 370                        if (note) {
 371                                puts(sha1_to_hex(note));
 372                                return 0;
 373                        }
 374                } else
 375                        return for_each_note(t, 0, list_each_note, NULL);
 376        }
 377
 378        /* show command */
 379
 380        if ((list || show) && !note) {
 381                error("No note found for object %s.", sha1_to_hex(object));
 382                return 1;
 383        } else if (show) {
 384                const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
 385                return execv_git_cmd(show_args);
 386        }
 387
 388        /* add/append/edit/remove/prune command */
 389
 390        if (add && note) {
 391                if (force)
 392                        fprintf(stderr, "Overwriting existing notes for object %s\n",
 393                                sha1_to_hex(object));
 394                else {
 395                        error("Cannot add notes. Found existing notes for object %s. "
 396                              "Use '-f' to overwrite existing notes",
 397                              sha1_to_hex(object));
 398                        return 1;
 399                }
 400        }
 401
 402        if (remove) {
 403                msg.given = 1;
 404                msg.use_editor = 0;
 405                strbuf_reset(&(msg.buf));
 406        }
 407
 408        if (prune) {
 409                hashclr(new_note);
 410                prune_notes(t);
 411        } else {
 412                create_note(object, &msg, append, note, new_note);
 413                if (is_null_sha1(new_note))
 414                        remove_note(t, object);
 415                else
 416                        add_note(t, object, new_note, combine_notes_overwrite);
 417        }
 418        snprintf(logmsg, sizeof(logmsg), "Note %s by 'git notes %s'",
 419                 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
 420        commit_notes(t, logmsg);
 421
 422        free_notes(t);
 423        strbuf_release(&(msg.buf));
 424        return 0;
 425}