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