fast-import: checkpoint: dump branches/tags/marks even if object_count==0
[gitweb.git] / builtin / notes.c
index b97bcb07ef05ac8efec77e57c17d552f85b9ac68..77573cf1ea8cb4d998597e265263487e22fde592 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "notes.h"
 #include "blob.h"
 #include "string-list.h"
 #include "notes-merge.h"
 #include "notes-utils.h"
-#include "branch.h"
+#include "worktree.h"
 
 static const char * const git_notes_usage[] = {
-       N_("git notes [--ref <notes_ref>] [list [<object>]]"),
-       N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
-       N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
-       N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
-       N_("git notes [--ref <notes_ref>] edit [<object>]"),
-       N_("git notes [--ref <notes_ref>] show [<object>]"),
-       N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
+       N_("git notes [--ref <notes-ref>] [list [<object>]]"),
+       N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
+       N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
+       N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
+       N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
+       N_("git notes [--ref <notes-ref>] show [<object>]"),
+       N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
        N_("git notes merge --commit [-v | -q]"),
        N_("git notes merge --abort [-v | -q]"),
-       N_("git notes [--ref <notes_ref>] remove [<object>...]"),
-       N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
-       N_("git notes [--ref <notes_ref>] get-ref"),
+       N_("git notes [--ref <notes-ref>] remove [<object>...]"),
+       N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
+       N_("git notes [--ref <notes-ref>] get-ref"),
        NULL
 };
 
@@ -69,7 +70,7 @@ static const char * const git_notes_show_usage[] = {
 };
 
 static const char * const git_notes_merge_usage[] = {
-       N_("git notes merge [<options>] <notes_ref>"),
+       N_("git notes merge [<options>] <notes-ref>"),
        N_("git notes merge --commit [<options>]"),
        N_("git notes merge --abort [<options>]"),
        NULL
@@ -91,23 +92,33 @@ static const char * const git_notes_get_ref_usage[] = {
 };
 
 static const char note_template[] =
-       "\nWrite/edit the notes for the following object:\n";
+       N_("Write/edit the notes for the following object:");
 
-struct msg_arg {
+struct note_data {
        int given;
        int use_editor;
+       char *edit_path;
        struct strbuf buf;
 };
 
-static int list_each_note(const unsigned char *object_sha1,
-               const unsigned char *note_sha1, char *note_path,
+static void free_note_data(struct note_data *d)
+{
+       if (d->edit_path) {
+               unlink_or_warn(d->edit_path);
+               free(d->edit_path);
+       }
+       strbuf_release(&d->buf);
+}
+
+static int list_each_note(const struct object_id *object_oid,
+               const struct object_id *note_oid, char *note_path,
                void *cb_data)
 {
-       printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
+       printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
        return 0;
 }
 
-static void write_note_data(int fd, const unsigned char *sha1)
+static void copy_obj_to_fd(int fd, const unsigned char *sha1)
 {
        unsigned long size;
        enum object_type type;
@@ -119,10 +130,10 @@ static void write_note_data(int fd, const unsigned char *sha1)
        }
 }
 
-static void write_commented_object(int fd, const unsigned char *object)
+static void write_commented_object(int fd, const struct object_id *object)
 {
        const char *show_args[5] =
-               {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
+               {"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
        struct child_process show = CHILD_PROCESS_INIT;
        struct strbuf buf = STRBUF_INIT;
        struct strbuf cbuf = STRBUF_INIT;
@@ -135,7 +146,7 @@ static void write_commented_object(int fd, const unsigned char *object)
        show.git_cmd = 1;
        if (start_command(&show))
                die(_("unable to start 'show' for object '%s'"),
-                   sha1_to_hex(object));
+                   oid_to_hex(object));
 
        if (strbuf_read(&buf, show.out, 0) < 0)
                die_errno(_("could not read 'show' output"));
@@ -147,32 +158,30 @@ static void write_commented_object(int fd, const unsigned char *object)
 
        if (finish_command(&show))
                die(_("failed to finish 'show' for object '%s'"),
-                   sha1_to_hex(object));
+                   oid_to_hex(object));
 }
 
-static void create_note(const unsigned char *object, struct msg_arg *msg,
-                       int append_only, const unsigned char *prev,
-                       unsigned char *result)
+static void prepare_note_data(const struct object_id *object, struct note_data *d,
+               const unsigned char *old_note)
 {
-       char *path = NULL;
-
-       if (msg->use_editor || !msg->given) {
+       if (d->use_editor || !d->given) {
                int fd;
                struct strbuf buf = STRBUF_INIT;
 
                /* write the template message before editing: */
-               path = git_pathdup("NOTES_EDITMSG");
-               fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
+               d->edit_path = git_pathdup("NOTES_EDITMSG");
+               fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
                if (fd < 0)
-                       die_errno(_("could not create file '%s'"), path);
+                       die_errno(_("could not create file '%s'"), d->edit_path);
 
-               if (msg->given)
-                       write_or_die(fd, msg->buf.buf, msg->buf.len);
-               else if (prev && !append_only)
-                       write_note_data(fd, prev);
+               if (d->given)
+                       write_or_die(fd, d->buf.buf, d->buf.len);
+               else if (old_note)
+                       copy_obj_to_fd(fd, old_note);
 
                strbuf_addch(&buf, '\n');
-               strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
+               strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
+               strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
                strbuf_addch(&buf, '\n');
                write_or_die(fd, buf.buf, buf.len);
 
@@ -180,112 +189,89 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
 
                close(fd);
                strbuf_release(&buf);
-               strbuf_reset(&(msg->buf));
+               strbuf_reset(&d->buf);
 
-               if (launch_editor(path, &(msg->buf), NULL)) {
-                       die(_("Please supply the note contents using either -m" \
-                           " or -F option"));
-               }
-               stripspace(&(msg->buf), 1);
-       }
-
-       if (prev && append_only) {
-               /* Append buf to previous note contents */
-               unsigned long size;
-               enum object_type type;
-               char *prev_buf = read_sha1_file(prev, &type, &size);
-
-               strbuf_grow(&(msg->buf), size + 1);
-               if (msg->buf.len && prev_buf && size)
-                       strbuf_insert(&(msg->buf), 0, "\n", 1);
-               if (prev_buf && size)
-                       strbuf_insert(&(msg->buf), 0, prev_buf, size);
-               free(prev_buf);
-       }
-
-       if (!msg->buf.len) {
-               fprintf(stderr, _("Removing note for object %s\n"),
-                       sha1_to_hex(object));
-               hashclr(result);
-       } else {
-               if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
-                       error(_("unable to write note object"));
-                       if (path)
-                               error(_("The note contents have been left in %s"),
-                                     path);
-                       exit(128);
+               if (launch_editor(d->edit_path, &d->buf, NULL)) {
+                       die(_("please supply the note contents using either -m or -F option"));
                }
+               strbuf_stripspace(&d->buf, 1);
        }
+}
 
-       if (path) {
-               unlink_or_warn(path);
-               free(path);
+static void write_note_data(struct note_data *d, unsigned char *sha1)
+{
+       if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
+               error(_("unable to write note object"));
+               if (d->edit_path)
+                       error(_("the note contents have been left in %s"),
+                               d->edit_path);
+               exit(128);
        }
 }
 
 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 {
-       struct msg_arg *msg = opt->value;
+       struct note_data *d = opt->value;
 
-       strbuf_grow(&(msg->buf), strlen(arg) + 2);
-       if (msg->buf.len)
-               strbuf_addch(&(msg->buf), '\n');
-       strbuf_addstr(&(msg->buf), arg);
-       stripspace(&(msg->buf), 0);
+       strbuf_grow(&d->buf, strlen(arg) + 2);
+       if (d->buf.len)
+               strbuf_addch(&d->buf, '\n');
+       strbuf_addstr(&d->buf, arg);
+       strbuf_stripspace(&d->buf, 0);
 
-       msg->given = 1;
+       d->given = 1;
        return 0;
 }
 
 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 {
-       struct msg_arg *msg = opt->value;
+       struct note_data *d = opt->value;
 
-       if (msg->buf.len)
-               strbuf_addch(&(msg->buf), '\n');
+       if (d->buf.len)
+               strbuf_addch(&d->buf, '\n');
        if (!strcmp(arg, "-")) {
-               if (strbuf_read(&(msg->buf), 0, 1024) < 0)
+               if (strbuf_read(&d->buf, 0, 1024) < 0)
                        die_errno(_("cannot read '%s'"), arg);
-       } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
+       } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
                die_errno(_("could not open or read '%s'"), arg);
-       stripspace(&(msg->buf), 0);
+       strbuf_stripspace(&d->buf, 0);
 
-       msg->given = 1;
+       d->given = 1;
        return 0;
 }
 
 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
 {
-       struct msg_arg *msg = opt->value;
+       struct note_data *d = opt->value;
        char *buf;
-       unsigned char object[20];
+       struct object_id object;
        enum object_type type;
        unsigned long len;
 
-       if (msg->buf.len)
-               strbuf_addch(&(msg->buf), '\n');
+       if (d->buf.len)
+               strbuf_addch(&d->buf, '\n');
 
-       if (get_sha1(arg, object))
-               die(_("Failed to resolve '%s' as a valid ref."), arg);
-       if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
+       if (get_oid(arg, &object))
+               die(_("failed to resolve '%s' as a valid ref."), arg);
+       if (!(buf = read_sha1_file(object.hash, &type, &len))) {
                free(buf);
-               die(_("Failed to read object '%s'."), arg);
+               die(_("failed to read object '%s'."), arg);
        }
        if (type != OBJ_BLOB) {
                free(buf);
-               die(_("Cannot read note data from non-blob object '%s'."), arg);
+               die(_("cannot read note data from non-blob object '%s'."), arg);
        }
-       strbuf_add(&(msg->buf), buf, len);
+       strbuf_add(&d->buf, buf, len);
        free(buf);
 
-       msg->given = 1;
+       d->given = 1;
        return 0;
 }
 
 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
 {
-       struct msg_arg *msg = opt->value;
-       msg->use_editor = 1;
+       struct note_data *d = opt->value;
+       d->use_editor = 1;
        return parse_reuse_arg(opt, arg, unset);
 }
 
@@ -302,33 +288,33 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
                if (!c)
                        return 0;
        } else {
-               init_notes(NULL, NULL, NULL, 0);
+               init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
                t = &default_notes_tree;
        }
 
-       while (strbuf_getline(&buf, stdin, '\n') != EOF) {
-               unsigned char from_obj[20], to_obj[20];
+       while (strbuf_getline_lf(&buf, stdin) != EOF) {
+               struct object_id from_obj, to_obj;
                struct strbuf **split;
                int err;
 
                split = strbuf_split(&buf, ' ');
                if (!split[0] || !split[1])
-                       die(_("Malformed input line: '%s'."), buf.buf);
+                       die(_("malformed input line: '%s'."), buf.buf);
                strbuf_rtrim(split[0]);
                strbuf_rtrim(split[1]);
-               if (get_sha1(split[0]->buf, from_obj))
-                       die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
-               if (get_sha1(split[1]->buf, to_obj))
-                       die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
+               if (get_oid(split[0]->buf, &from_obj))
+                       die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
+               if (get_oid(split[1]->buf, &to_obj))
+                       die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
 
                if (rewrite_cmd)
-                       err = copy_note_for_rewrite(c, from_obj, to_obj);
+                       err = copy_note_for_rewrite(c, &from_obj, &to_obj);
                else
-                       err = copy_note(t, from_obj, to_obj, force,
+                       err = copy_note(t, &from_obj, &to_obj, force,
                                        combine_notes_overwrite);
 
                if (err) {
-                       error(_("Failed to copy notes from '%s' to '%s'"),
+                       error(_("failed to copy notes from '%s' to '%s'"),
                              split[0]->buf, split[1]->buf);
                        ret = 1;
                }
@@ -345,23 +331,30 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
        return ret;
 }
 
-static struct notes_tree *init_notes_check(const char *subcommand)
+static struct notes_tree *init_notes_check(const char *subcommand,
+                                          int flags)
 {
        struct notes_tree *t;
-       init_notes(NULL, NULL, NULL, 0);
+       const char *ref;
+       init_notes(NULL, NULL, NULL, flags);
        t = &default_notes_tree;
 
-       if (!starts_with(t->ref, "refs/notes/"))
-               die("Refusing to %s notes in %s (outside of refs/notes/)",
-                   subcommand, t->ref);
+       ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
+       if (!starts_with(ref, "refs/notes/"))
+               /*
+                * TRANSLATORS: the first %s will be replaced by a git
+                * notes command: 'add', 'merge', 'remove', etc.
+                */
+               die(_("refusing to %s notes in %s (outside of refs/notes/)"),
+                   subcommand, ref);
        return t;
 }
 
 static int list(int argc, const char **argv, const char *prefix)
 {
        struct notes_tree *t;
-       unsigned char object[20];
-       const unsigned char *note;
+       struct object_id object;
+       const struct object_id *note;
        int retval = -1;
        struct option options[] = {
                OPT_END()
@@ -376,17 +369,17 @@ static int list(int argc, const char **argv, const char *prefix)
                usage_with_options(git_notes_list_usage, options);
        }
 
-       t = init_notes_check("list");
+       t = init_notes_check("list", 0);
        if (argc) {
-               if (get_sha1(argv[0], object))
-                       die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
-               note = get_note(t, object);
+               if (get_oid(argv[0], &object))
+                       die(_("failed to resolve '%s' as a valid ref."), argv[0]);
+               note = get_note(t, &object);
                if (note) {
-                       puts(sha1_to_hex(note));
+                       puts(oid_to_hex(note));
                        retval = 0;
                } else
-                       retval = error(_("No note found for object %s."),
-                                      sha1_to_hex(object));
+                       retval = error(_("no note found for object %s."),
+                                      oid_to_hex(&object));
        } else
                retval = for_each_note(t, 0, list_each_note, NULL);
 
@@ -398,26 +391,27 @@ static int append_edit(int argc, const char **argv, const char *prefix);
 
 static int add(int argc, const char **argv, const char *prefix)
 {
-       int retval = 0, force = 0;
+       int force = 0, allow_empty = 0;
        const char *object_ref;
        struct notes_tree *t;
-       unsigned char object[20], new_note[20];
-       char logmsg[100];
-       const unsigned char *note;
-       struct msg_arg msg = { 0, 0, STRBUF_INIT };
+       struct object_id object, new_note;
+       const struct object_id *note;
+       struct note_data d = { 0, 0, NULL, STRBUF_INIT };
        struct option options[] = {
-               { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
+               { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
                        N_("note contents as a string"), PARSE_OPT_NONEG,
                        parse_msg_arg},
-               { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
+               { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
                        N_("note contents in a file"), PARSE_OPT_NONEG,
                        parse_file_arg},
-               { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
+               { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
                        N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
                        parse_reedit_arg},
-               { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
+               { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
                        N_("reuse specified note object"), PARSE_OPT_NONEG,
                        parse_reuse_arg},
+               OPT_BOOL(0, "allow-empty", &allow_empty,
+                       N_("allow storing empty note")),
                OPT__FORCE(&force, N_("replace existing notes")),
                OPT_END()
        };
@@ -432,57 +426,60 @@ static int add(int argc, const char **argv, const char *prefix)
 
        object_ref = argc > 1 ? argv[1] : "HEAD";
 
-       if (get_sha1(object_ref, object))
-               die(_("Failed to resolve '%s' as a valid ref."), object_ref);
+       if (get_oid(object_ref, &object))
+               die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
-       t = init_notes_check("add");
-       note = get_note(t, object);
+       t = init_notes_check("add", NOTES_INIT_WRITABLE);
+       note = get_note(t, &object);
 
        if (note) {
                if (!force) {
-                       if (!msg.given) {
-                               /*
-                                * Redirect to "edit" subcommand.
-                                *
-                                * We only end up here if none of -m/-F/-c/-C
-                                * or -f are given. The original args are
-                                * therefore still in argv[0-1].
-                                */
-                               argv[0] = "edit";
-                               free_notes(t);
-                               return append_edit(argc, argv, prefix);
+                       free_notes(t);
+                       if (d.given) {
+                               free_note_data(&d);
+                               return error(_("Cannot add notes. "
+                                       "Found existing notes for object %s. "
+                                       "Use '-f' to overwrite existing notes"),
+                                       oid_to_hex(&object));
                        }
-                       retval = error(_("Cannot add notes. Found existing notes "
-                                      "for object %s. Use '-f' to overwrite "
-                                      "existing notes"), sha1_to_hex(object));
-                       goto out;
+                       /*
+                        * Redirect to "edit" subcommand.
+                        *
+                        * We only end up here if none of -m/-F/-c/-C or -f are
+                        * given. The original args are therefore still in
+                        * argv[0-1].
+                        */
+                       argv[0] = "edit";
+                       return append_edit(argc, argv, prefix);
                }
                fprintf(stderr, _("Overwriting existing notes for object %s\n"),
-                       sha1_to_hex(object));
+                       oid_to_hex(&object));
        }
 
-       create_note(object, &msg, 0, note, new_note);
-
-       if (is_null_sha1(new_note))
-               remove_note(t, object);
-       else if (add_note(t, object, new_note, combine_notes_overwrite))
-               die("BUG: combine_notes_overwrite failed");
+       prepare_note_data(&object, &d, note->hash);
+       if (d.buf.len || allow_empty) {
+               write_note_data(&d, new_note.hash);
+               if (add_note(t, &object, &new_note, combine_notes_overwrite))
+                       die("BUG: combine_notes_overwrite failed");
+               commit_notes(t, "Notes added by 'git notes add'");
+       } else {
+               fprintf(stderr, _("Removing note for object %s\n"),
+                       oid_to_hex(&object));
+               remove_note(t, object.hash);
+               commit_notes(t, "Notes removed by 'git notes add'");
+       }
 
-       snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
-                is_null_sha1(new_note) ? "removed" : "added", "add");
-       commit_notes(t, logmsg);
-out:
+       free_note_data(&d);
        free_notes(t);
-       strbuf_release(&(msg.buf));
-       return retval;
+       return 0;
 }
 
 static int copy(int argc, const char **argv, const char *prefix)
 {
        int retval = 0, force = 0, from_stdin = 0;
-       const unsigned char *from_note, *note;
+       const struct object_id *from_note, *note;
        const char *object_ref;
-       unsigned char object[20], from_obj[20];
+       struct object_id object, from_obj;
        struct notes_tree *t;
        const char *rewrite_cmd = NULL;
        struct option options[] = {
@@ -515,37 +512,37 @@ static int copy(int argc, const char **argv, const char *prefix)
                usage_with_options(git_notes_copy_usage, options);
        }
 
-       if (get_sha1(argv[0], from_obj))
-               die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
+       if (get_oid(argv[0], &from_obj))
+               die(_("failed to resolve '%s' as a valid ref."), argv[0]);
 
        object_ref = 1 < argc ? argv[1] : "HEAD";
 
-       if (get_sha1(object_ref, object))
-               die(_("Failed to resolve '%s' as a valid ref."), object_ref);
+       if (get_oid(object_ref, &object))
+               die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
-       t = init_notes_check("copy");
-       note = get_note(t, object);
+       t = init_notes_check("copy", NOTES_INIT_WRITABLE);
+       note = get_note(t, &object);
 
        if (note) {
                if (!force) {
                        retval = error(_("Cannot copy notes. Found existing "
                                       "notes for object %s. Use '-f' to "
                                       "overwrite existing notes"),
-                                      sha1_to_hex(object));
+                                      oid_to_hex(&object));
                        goto out;
                }
                fprintf(stderr, _("Overwriting existing notes for object %s\n"),
-                       sha1_to_hex(object));
+                       oid_to_hex(&object));
        }
 
-       from_note = get_note(t, from_obj);
+       from_note = get_note(t, &from_obj);
        if (!from_note) {
-               retval = error(_("Missing notes on source object %s. Cannot "
-                              "copy."), sha1_to_hex(from_obj));
+               retval = error(_("missing notes on source object %s. Cannot "
+                              "copy."), oid_to_hex(&from_obj));
                goto out;
        }
 
-       if (add_note(t, object, from_note, combine_notes_overwrite))
+       if (add_note(t, &object, from_note, combine_notes_overwrite))
                die("BUG: combine_notes_overwrite failed");
        commit_notes(t, "Notes added by 'git notes copy'");
 out:
@@ -555,26 +552,29 @@ static int copy(int argc, const char **argv, const char *prefix)
 
 static int append_edit(int argc, const char **argv, const char *prefix)
 {
+       int allow_empty = 0;
        const char *object_ref;
        struct notes_tree *t;
-       unsigned char object[20], new_note[20];
-       const unsigned char *note;
-       char logmsg[100];
+       struct object_id object, new_note;
+       const struct object_id *note;
+       char *logmsg;
        const char * const *usage;
-       struct msg_arg msg = { 0, 0, STRBUF_INIT };
+       struct note_data d = { 0, 0, NULL, STRBUF_INIT };
        struct option options[] = {
-               { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
+               { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
                        N_("note contents as a string"), PARSE_OPT_NONEG,
                        parse_msg_arg},
-               { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
+               { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
                        N_("note contents in a file"), PARSE_OPT_NONEG,
                        parse_file_arg},
-               { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
+               { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
                        N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
                        parse_reedit_arg},
-               { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
+               { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
                        N_("reuse specified note object"), PARSE_OPT_NONEG,
                        parse_reuse_arg},
+               OPT_BOOL(0, "allow-empty", &allow_empty,
+                       N_("allow storing empty note")),
                OPT_END()
        };
        int edit = !strcmp(argv[0], "edit");
@@ -588,31 +588,51 @@ static int append_edit(int argc, const char **argv, const char *prefix)
                usage_with_options(usage, options);
        }
 
-       if (msg.given && edit)
+       if (d.given && edit)
                fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
                        "for the 'edit' subcommand.\n"
                        "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
 
        object_ref = 1 < argc ? argv[1] : "HEAD";
 
-       if (get_sha1(object_ref, object))
-               die(_("Failed to resolve '%s' as a valid ref."), object_ref);
+       if (get_oid(object_ref, &object))
+               die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
-       t = init_notes_check(argv[0]);
-       note = get_note(t, object);
+       t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
+       note = get_note(t, &object);
 
-       create_note(object, &msg, !edit, note, new_note);
+       prepare_note_data(&object, &d, edit && note ? note->hash : NULL);
 
-       if (is_null_sha1(new_note))
-               remove_note(t, object);
-       else if (add_note(t, object, new_note, combine_notes_overwrite))
-               die("BUG: combine_notes_overwrite failed");
+       if (note && !edit) {
+               /* Append buf to previous note contents */
+               unsigned long size;
+               enum object_type type;
+               char *prev_buf = read_sha1_file(note->hash, &type, &size);
+
+               strbuf_grow(&d.buf, size + 1);
+               if (d.buf.len && prev_buf && size)
+                       strbuf_insert(&d.buf, 0, "\n", 1);
+               if (prev_buf && size)
+                       strbuf_insert(&d.buf, 0, prev_buf, size);
+               free(prev_buf);
+       }
 
-       snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
-                is_null_sha1(new_note) ? "removed" : "added", argv[0]);
+       if (d.buf.len || allow_empty) {
+               write_note_data(&d, new_note.hash);
+               if (add_note(t, &object, &new_note, combine_notes_overwrite))
+                       die("BUG: combine_notes_overwrite failed");
+               logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
+       } else {
+               fprintf(stderr, _("Removing note for object %s\n"),
+                       oid_to_hex(&object));
+               remove_note(t, object.hash);
+               logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
+       }
        commit_notes(t, logmsg);
+
+       free(logmsg);
+       free_note_data(&d);
        free_notes(t);
-       strbuf_release(&(msg.buf));
        return 0;
 }
 
@@ -620,8 +640,8 @@ static int show(int argc, const char **argv, const char *prefix)
 {
        const char *object_ref;
        struct notes_tree *t;
-       unsigned char object[20];
-       const unsigned char *note;
+       struct object_id object;
+       const struct object_id *note;
        int retval;
        struct option options[] = {
                OPT_END()
@@ -637,17 +657,17 @@ static int show(int argc, const char **argv, const char *prefix)
 
        object_ref = argc ? argv[0] : "HEAD";
 
-       if (get_sha1(object_ref, object))
-               die(_("Failed to resolve '%s' as a valid ref."), object_ref);
+       if (get_oid(object_ref, &object))
+               die(_("failed to resolve '%s' as a valid ref."), object_ref);
 
-       t = init_notes_check("show");
-       note = get_note(t, object);
+       t = init_notes_check("show", 0);
+       note = get_note(t, &object);
 
        if (!note)
-               retval = error(_("No note found for object %s."),
-                              sha1_to_hex(object));
+               retval = error(_("no note found for object %s."),
+                              oid_to_hex(&object));
        else {
-               const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
+               const char *show_args[3] = {"show", oid_to_hex(note), NULL};
                retval = execv_git_cmd(show_args);
        }
        free_notes(t);
@@ -663,19 +683,19 @@ static int merge_abort(struct notes_merge_options *o)
         * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
         */
 
-       if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
-               ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
-       if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
-               ret += error("Failed to delete ref NOTES_MERGE_REF");
+       if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
+               ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
+       if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
+               ret += error(_("failed to delete ref NOTES_MERGE_REF"));
        if (notes_merge_abort(o))
-               ret += error("Failed to remove 'git notes merge' worktree");
+               ret += error(_("failed to remove 'git notes merge' worktree"));
        return ret;
 }
 
 static int merge_commit(struct notes_merge_options *o)
 {
        struct strbuf msg = STRBUF_INIT;
-       unsigned char sha1[20], parent_sha1[20];
+       struct object_id oid, parent_oid;
        struct notes_tree *t;
        struct commit *partial;
        struct pretty_print_context pretty_ctx;
@@ -687,36 +707,36 @@ static int merge_commit(struct notes_merge_options *o)
         * and target notes ref from .git/NOTES_MERGE_REF.
         */
 
-       if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
-               die("Failed to read ref NOTES_MERGE_PARTIAL");
-       else if (!(partial = lookup_commit_reference(sha1)))
-               die("Could not find commit from NOTES_MERGE_PARTIAL.");
+       if (get_oid("NOTES_MERGE_PARTIAL", &oid))
+               die(_("failed to read ref NOTES_MERGE_PARTIAL"));
+       else if (!(partial = lookup_commit_reference(&oid)))
+               die(_("could not find commit from NOTES_MERGE_PARTIAL."));
        else if (parse_commit(partial))
-               die("Could not parse commit from NOTES_MERGE_PARTIAL.");
+               die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
 
        if (partial->parents)
-               hashcpy(parent_sha1, partial->parents->item->object.sha1);
+               oidcpy(&parent_oid, &partial->parents->item->object.oid);
        else
-               hashclr(parent_sha1);
+               oidclr(&parent_oid);
 
        t = xcalloc(1, sizeof(struct notes_tree));
        init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
 
        o->local_ref = local_ref_to_free =
-               resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
+               resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
        if (!o->local_ref)
-               die("Failed to resolve NOTES_MERGE_REF");
+               die(_("failed to resolve NOTES_MERGE_REF"));
 
-       if (notes_merge_commit(o, t, partial, sha1))
-               die("Failed to finalize notes merge");
+       if (notes_merge_commit(o, t, partial, &oid))
+               die(_("failed to finalize notes merge"));
 
        /* Reuse existing commit message in reflog message */
        memset(&pretty_ctx, 0, sizeof(pretty_ctx));
        format_commit_message(partial, "%s", &msg, &pretty_ctx);
        strbuf_trim(&msg);
        strbuf_insert(&msg, 0, "notes: ", 7);
-       update_ref(msg.buf, o->local_ref, sha1,
-                  is_null_sha1(parent_sha1) ? NULL : parent_sha1,
+       update_ref(msg.buf, o->local_ref, oid.hash,
+                  is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
                   0, UPDATE_REFS_DIE_ON_ERR);
 
        free_notes(t);
@@ -726,10 +746,24 @@ static int merge_commit(struct notes_merge_options *o)
        return ret;
 }
 
+static int git_config_get_notes_strategy(const char *key,
+                                        enum notes_merge_strategy *strategy)
+{
+       char *value;
+
+       if (git_config_get_string(key, &value))
+               return 1;
+       if (parse_notes_merge_strategy(value, strategy))
+               git_die_config(key, _("unknown notes merge strategy %s"), value);
+
+       free(value);
+       return 0;
+}
+
 static int merge(int argc, const char **argv, const char *prefix)
 {
        struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
-       unsigned char result_sha1[20];
+       struct object_id result_oid;
        struct notes_tree *t;
        struct notes_merge_options o;
        int do_merge = 0, do_commit = 0, do_abort = 0;
@@ -759,15 +793,15 @@ static int merge(int argc, const char **argv, const char *prefix)
        if (strategy || do_commit + do_abort == 0)
                do_merge = 1;
        if (do_merge + do_commit + do_abort != 1) {
-               error("cannot mix --commit, --abort or -s/--strategy");
+               error(_("cannot mix --commit, --abort or -s/--strategy"));
                usage_with_options(git_notes_merge_usage, options);
        }
 
        if (do_merge && argc != 1) {
-               error("Must specify a notes ref to merge");
+               error(_("must specify a notes ref to merge"));
                usage_with_options(git_notes_merge_usage, options);
        } else if (!do_merge && argc) {
-               error("too many parameters");
+               error(_("too many parameters"));
                usage_with_options(git_notes_merge_usage, options);
        }
 
@@ -781,54 +815,58 @@ static int merge(int argc, const char **argv, const char *prefix)
 
        o.local_ref = default_notes_ref();
        strbuf_addstr(&remote_ref, argv[0]);
-       expand_notes_ref(&remote_ref);
+       expand_loose_notes_ref(&remote_ref);
        o.remote_ref = remote_ref.buf;
 
+       t = init_notes_check("merge", NOTES_INIT_WRITABLE);
+
        if (strategy) {
-               if (!strcmp(strategy, "manual"))
-                       o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
-               else if (!strcmp(strategy, "ours"))
-                       o.strategy = NOTES_MERGE_RESOLVE_OURS;
-               else if (!strcmp(strategy, "theirs"))
-                       o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
-               else if (!strcmp(strategy, "union"))
-                       o.strategy = NOTES_MERGE_RESOLVE_UNION;
-               else if (!strcmp(strategy, "cat_sort_uniq"))
-                       o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
-               else {
-                       error("Unknown -s/--strategy: %s", strategy);
+               if (parse_notes_merge_strategy(strategy, &o.strategy)) {
+                       error(_("unknown -s/--strategy: %s"), strategy);
                        usage_with_options(git_notes_merge_usage, options);
                }
-       }
+       } else {
+               struct strbuf merge_key = STRBUF_INIT;
+               const char *short_ref = NULL;
+
+               if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
+                       die("BUG: local ref %s is outside of refs/notes/",
+                           o.local_ref);
+
+               strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
 
-       t = init_notes_check("merge");
+               if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
+                       git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
+
+               strbuf_release(&merge_key);
+       }
 
        strbuf_addf(&msg, "notes: Merged notes from %s into %s",
                    remote_ref.buf, default_notes_ref());
        strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
 
-       result = notes_merge(&o, t, result_sha1);
+       result = notes_merge(&o, t, &result_oid);
 
-       if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
+       if (result >= 0) /* Merge resulted (trivially) in result_oid */
                /* Update default notes ref with new commit */
-               update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
+               update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL,
                           0, UPDATE_REFS_DIE_ON_ERR);
        else { /* Merge has unresolved conflicts */
-               char *existing;
+               const struct worktree *wt;
                /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
-               update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
+               update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL,
                           0, UPDATE_REFS_DIE_ON_ERR);
                /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
-               existing = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
-               if (existing)
-                       die(_("A notes merge into %s is already in-progress at %s"),
-                           default_notes_ref(), existing);
+               wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
+               if (wt)
+                       die(_("a notes merge into %s is already in-progress at %s"),
+                           default_notes_ref(), wt->path);
                if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
-                       die("Failed to store link to current notes ref (%s)",
+                       die(_("failed to store link to current notes ref (%s)"),
                            default_notes_ref());
-               printf("Automatic notes merge failed. Fix conflicts in %s and "
-                      "commit the result with 'git notes merge --commit', or "
-                      "abort the merge with 'git notes merge --abort'.\n",
+               printf(_("Automatic notes merge failed. Fix conflicts in %s and "
+                        "commit the result with 'git notes merge --commit', or "
+                        "abort the merge with 'git notes merge --abort'.\n"),
                       git_path(NOTES_MERGE_WORKTREE));
        }
 
@@ -843,10 +881,10 @@ static int merge(int argc, const char **argv, const char *prefix)
 static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
 {
        int status;
-       unsigned char sha1[20];
-       if (get_sha1(name, sha1))
+       struct object_id oid;
+       if (get_oid(name, &oid))
                return error(_("Failed to resolve '%s' as a valid ref."), name);
-       status = remove_note(t, sha1);
+       status = remove_note(t, oid.hash);
        if (status)
                fprintf(stderr, _("Object %s has no note\n"), name);
        else
@@ -872,7 +910,7 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, options,
                             git_notes_remove_usage, 0);
 
-       t = init_notes_check("remove");
+       t = init_notes_check("remove", NOTES_INIT_WRITABLE);
 
        if (!argc && !from_stdin) {
                retval = remove_one_note(t, "HEAD", flag);
@@ -901,8 +939,8 @@ static int prune(int argc, const char **argv, const char *prefix)
        struct notes_tree *t;
        int show_only = 0, verbose = 0;
        struct option options[] = {
-               OPT__DRY_RUN(&show_only, "do not remove, show only"),
-               OPT__VERBOSE(&verbose, "report pruned notes"),
+               OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
+               OPT__VERBOSE(&verbose, N_("report pruned notes")),
                OPT_END()
        };
 
@@ -914,7 +952,7 @@ static int prune(int argc, const char **argv, const char *prefix)
                usage_with_options(git_notes_prune_usage, options);
        }
 
-       t = init_notes_check("prune");
+       t = init_notes_check("prune", NOTES_INIT_WRITABLE);
 
        prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
                (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
@@ -931,7 +969,7 @@ static int get_ref(int argc, const char **argv, const char *prefix)
                             git_notes_get_ref_usage, 0);
 
        if (argc) {
-               error("too many parameters");
+               error(_("too many parameters"));
                usage_with_options(git_notes_get_ref_usage, options);
        }
 
@@ -945,7 +983,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
        const char *override_notes_ref = NULL;
        struct option options[] = {
                OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
-                          N_("use notes from <notes_ref>")),
+                          N_("use notes from <notes-ref>")),
                OPT_END()
        };
 
@@ -980,7 +1018,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
        else if (!strcmp(argv[0], "get-ref"))
                result = get_ref(argc, argv, prefix);
        else {
-               result = error(_("Unknown subcommand: %s"), argv[0]);
+               result = error(_("unknown subcommand: %s"), argv[0]);
                usage_with_options(git_notes_usage, options);
        }