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