From 511726e4b191ad76e7b4875d8f2361b05d5cfb39 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sun, 9 Nov 2014 13:30:47 +0100 Subject: [PATCH 1/9] builtin/notes: fix premature failure when trying to add the empty blob This fixes a small buglet when trying to explicitly add the empty blob as a note object using the -c or -C option to git notes add/append. Instead of failing with a nonsensical error message indicating that the empty blob does not exist, we should rather behave as if an empty notes message was given (e.g. using -m "" or -F /dev/null). The next patch contains a test that verifies the fixed behavior. Found-by: Eric Sunshine Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- builtin/notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/notes.c b/builtin/notes.c index 68b6cd8cc1..9ee6816888 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -266,7 +266,7 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) 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 (!(buf = read_sha1_file(object, &type, &len))) { free(buf); die(_("Failed to read object '%s'."), arg); } From d0923b6d4c01e3c5b86d56b1741292eda7c181fe Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sun, 9 Nov 2014 13:30:48 +0100 Subject: [PATCH 2/9] t3301: verify that 'git notes' removes empty notes by default Add test cases documenting the current behavior when trying to add/append/edit empty notes. This is in preparation for adding --allow-empty; to allow empty notes to be stored. Improved-by: Eric Sunshine Improved-by: Junio C Hamano Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- t/t3301-notes.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index cfd67ff3df..f74b3fa335 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -1239,4 +1239,31 @@ test_expect_success 'git notes get-ref (--ref)' ' test "$(GIT_NOTES_REF=refs/notes/bar git notes --ref=baz get-ref)" = "refs/notes/baz" ' +test_expect_success 'setup testing of empty notes' ' + test_unconfig core.notesRef && + test_commit 16th && + empty_blob=$(git hash-object -w /dev/null) +' + +while read cmd +do + test_expect_success "'git notes $cmd' removes empty note" " + test_might_fail git notes remove HEAD && + MSG= git notes $cmd && + test_must_fail git notes list HEAD + " +done <<\EOF +add +add -F /dev/null +add -m "" +add -c "$empty_blob" +add -C "$empty_blob" +append +append -F /dev/null +append -m "" +append -c "$empty_blob" +append -C "$empty_blob" +edit +EOF + test_done From bebf5c04767b3c67dadfa8b65e6f0ceb258f6b83 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sun, 9 Nov 2014 13:30:49 +0100 Subject: [PATCH 3/9] builtin/notes: improve naming In preparation for some needed refactoring, rename struct msg_arg to struct note_data, and rename its instances from "msg" to "d" (also removing some unnecessary parentheses). The 'msg_arg' name was inherited from tag.c, but is not really a good name for the contents of a note. Also rename write_note_data() to copy_obj_to_fd(), which more aptly describes what it actually does: Copying the contents of a git object (given by its SHA1) into a given file descriptor. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- builtin/notes.c | 109 ++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 9ee6816888..3cf22cb30b 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -92,7 +92,7 @@ static const char * const git_notes_get_ref_usage[] = { static const char note_template[] = "\nWrite/edit the notes for the following object:\n"; -struct msg_arg { +struct note_data { int given; int use_editor; struct strbuf buf; @@ -106,7 +106,7 @@ static int list_each_note(const unsigned char *object_sha1, 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; @@ -149,13 +149,13 @@ static void write_commented_object(int fd, const unsigned char *object) sha1_to_hex(object)); } -static void create_note(const unsigned char *object, struct msg_arg *msg, +static void create_note(const unsigned char *object, struct note_data *d, int append_only, const unsigned char *prev, unsigned char *result) { char *path = NULL; - if (msg->use_editor || !msg->given) { + if (d->use_editor || !d->given) { int fd; struct strbuf buf = STRBUF_INIT; @@ -165,10 +165,10 @@ static void create_note(const unsigned char *object, struct msg_arg *msg, if (fd < 0) die_errno(_("could not create file '%s'"), path); - if (msg->given) - write_or_die(fd, msg->buf.buf, msg->buf.len); + if (d->given) + write_or_die(fd, d->buf.buf, d->buf.len); else if (prev && !append_only) - write_note_data(fd, prev); + copy_obj_to_fd(fd, prev); strbuf_addch(&buf, '\n'); strbuf_add_commented_lines(&buf, note_template, strlen(note_template)); @@ -179,13 +179,12 @@ 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")); + if (launch_editor(path, &d->buf, NULL)) { + die(_("Please supply the note contents using either -m or -F option")); } - stripspace(&(msg->buf), 1); + stripspace(&d->buf, 1); } if (prev && append_only) { @@ -194,20 +193,20 @@ static void create_note(const unsigned char *object, struct msg_arg *msg, 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); + 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(&(msg->buf), 0, prev_buf, size); + strbuf_insert(&d->buf, 0, prev_buf, size); free(prev_buf); } - if (!msg->buf.len) { + if (!d->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)) { + if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) { error(_("unable to write note object")); if (path) error(_("The note contents have been left in %s"), @@ -224,45 +223,45 @@ static void create_note(const unsigned char *object, struct msg_arg *msg, 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); + 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); + 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]; 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); @@ -274,17 +273,17 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) free(buf); 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); } @@ -403,18 +402,18 @@ static int add(int argc, const char **argv, const char *prefix) unsigned char object[20], new_note[20]; char logmsg[100]; const unsigned char *note; - struct msg_arg msg = { 0, 0, STRBUF_INIT }; + struct note_data d = { 0, 0, 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__FORCE(&force, N_("replace existing notes")), @@ -439,7 +438,7 @@ static int add(int argc, const char **argv, const char *prefix) if (note) { if (!force) { - if (!msg.given) { + if (!d.given) { /* * Redirect to "edit" subcommand. * @@ -460,7 +459,7 @@ static int add(int argc, const char **argv, const char *prefix) sha1_to_hex(object)); } - create_note(object, &msg, 0, note, new_note); + create_note(object, &d, 0, note, new_note); if (is_null_sha1(new_note)) remove_note(t, object); @@ -472,7 +471,7 @@ static int add(int argc, const char **argv, const char *prefix) commit_notes(t, logmsg); out: free_notes(t); - strbuf_release(&(msg.buf)); + strbuf_release(&d.buf); return retval; } @@ -560,18 +559,18 @@ static int append_edit(int argc, const char **argv, const char *prefix) const unsigned char *note; char logmsg[100]; const char * const *usage; - struct msg_arg msg = { 0, 0, STRBUF_INIT }; + struct note_data d = { 0, 0, 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_END() @@ -587,7 +586,7 @@ 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")); @@ -600,7 +599,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) t = init_notes_check(argv[0]); note = get_note(t, object); - create_note(object, &msg, !edit, note, new_note); + create_note(object, &d, !edit, note, new_note); if (is_null_sha1(new_note)) remove_note(t, object); @@ -611,7 +610,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) is_null_sha1(new_note) ? "removed" : "added", argv[0]); commit_notes(t, logmsg); free_notes(t); - strbuf_release(&(msg.buf)); + strbuf_release(&d.buf); return 0; } From 4282af0fc91e7a2667f8cba7282a5b70347f6e6f Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sun, 9 Nov 2014 13:30:50 +0100 Subject: [PATCH 4/9] builtin/notes: refactor note file path into struct note_data Move the 'path' variable from create_note() and into the note_data struct. Unify cleanup of note_data objects with a free_note_data() function. This might not make too much sense on its own, but it makes the future refactoring of create_note() considerably cleaner. Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- builtin/notes.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 3cf22cb30b..101747243a 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -95,9 +95,19 @@ static const char note_template[] = struct note_data { int given; int use_editor; + char *edit_path; struct strbuf buf; }; +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 unsigned char *object_sha1, const unsigned char *note_sha1, char *note_path, void *cb_data) @@ -153,17 +163,15 @@ static void create_note(const unsigned char *object, struct note_data *d, int append_only, const unsigned char *prev, unsigned char *result) { - char *path = NULL; - 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 (d->given) write_or_die(fd, d->buf.buf, d->buf.len); @@ -181,7 +189,7 @@ static void create_note(const unsigned char *object, struct note_data *d, strbuf_release(&buf); strbuf_reset(&d->buf); - if (launch_editor(path, &d->buf, NULL)) { + if (launch_editor(d->edit_path, &d->buf, NULL)) { die(_("Please supply the note contents using either -m or -F option")); } stripspace(&d->buf, 1); @@ -208,17 +216,12 @@ static void create_note(const unsigned char *object, struct note_data *d, } else { if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) { error(_("unable to write note object")); - if (path) + if (d->edit_path) error(_("The note contents have been left in %s"), - path); + d->edit_path); exit(128); } } - - if (path) { - unlink_or_warn(path); - free(path); - } } static int parse_msg_arg(const struct option *opt, const char *arg, int unset) @@ -402,7 +405,7 @@ static int add(int argc, const char **argv, const char *prefix) unsigned char object[20], new_note[20]; char logmsg[100]; const unsigned char *note; - struct note_data d = { 0, 0, STRBUF_INIT }; + struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, @@ -447,6 +450,7 @@ static int add(int argc, const char **argv, const char *prefix) * therefore still in argv[0-1]. */ argv[0] = "edit"; + free_note_data(&d); free_notes(t); return append_edit(argc, argv, prefix); } @@ -460,6 +464,7 @@ static int add(int argc, const char **argv, const char *prefix) } create_note(object, &d, 0, note, new_note); + free_note_data(&d); if (is_null_sha1(new_note)) remove_note(t, object); @@ -471,7 +476,6 @@ static int add(int argc, const char **argv, const char *prefix) commit_notes(t, logmsg); out: free_notes(t); - strbuf_release(&d.buf); return retval; } @@ -559,7 +563,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) const unsigned char *note; char logmsg[100]; const char * const *usage; - struct note_data d = { 0, 0, STRBUF_INIT }; + struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, @@ -600,6 +604,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) note = get_note(t, object); create_note(object, &d, !edit, note, new_note); + free_note_data(&d); if (is_null_sha1(new_note)) remove_note(t, object); @@ -610,7 +615,6 @@ static int append_edit(int argc, const char **argv, const char *prefix) is_null_sha1(new_note) ? "removed" : "added", argv[0]); commit_notes(t, logmsg); free_notes(t); - strbuf_release(&d.buf); return 0; } From b0de56c6a5bb41da71babafefc9207b0fc211d66 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 12 Nov 2014 01:40:12 +0100 Subject: [PATCH 5/9] builtin/notes: simplify early exit code in add() Remove the need for 'retval' and the unnecessary goto. Also reorganize to only call free_note_data() is actually needed. Improved-by: Junio C Hamano Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- builtin/notes.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index 101747243a..acdedbdf5a 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -399,7 +399,7 @@ 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; const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; @@ -441,23 +441,23 @@ static int add(int argc, const char **argv, const char *prefix) if (note) { if (!force) { - if (!d.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); + if (d.given) { free_note_data(&d); - free_notes(t); - return append_edit(argc, argv, prefix); + return error(_("Cannot add notes. " + "Found existing notes for object %s. " + "Use '-f' to overwrite existing notes"), + sha1_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)); @@ -474,9 +474,8 @@ static int add(int argc, const char **argv, const char *prefix) snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", is_null_sha1(new_note) ? "removed" : "added", "add"); commit_notes(t, logmsg); -out: free_notes(t); - return retval; + return 0; } static int copy(int argc, const char **argv, const char *prefix) From 52694cdabbf68f19c8289416e7bb3bbef41d8d27 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 12 Nov 2014 01:40:13 +0100 Subject: [PATCH 6/9] builtin/notes: split create_note() to clarify add vs. remove logic create_note() has a non-trivial interface, and comprises three loosely related parts: 1. launching the editor with the note contents, if needed 2. appending to an existing note, if append_only was given 3. adding or removing the resulting note, based on whether it's non-empty Split it along those lines to make the logic clearer: The first part goes into a new function - prepare_note_data(), with a simpler interface. The second part is moved into append_edit(), which is the only user of this code. Finally, the add vs. remove decision is moved into the callers (add() and append_edit()), keeping the logic for writing the actual note object in a separate function: write_note_data(). Suggested-by: Junio C Hamano Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- builtin/notes.c | 103 +++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index acdedbdf5a..ed32d634fc 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -159,9 +159,8 @@ static void write_commented_object(int fd, const unsigned char *object) sha1_to_hex(object)); } -static void create_note(const unsigned char *object, struct note_data *d, - int append_only, const unsigned char *prev, - unsigned char *result) +static void prepare_note_data(const unsigned char *object, struct note_data *d, + const unsigned char *old_note) { if (d->use_editor || !d->given) { int fd; @@ -175,8 +174,8 @@ static void create_note(const unsigned char *object, struct note_data *d, if (d->given) write_or_die(fd, d->buf.buf, d->buf.len); - else if (prev && !append_only) - copy_obj_to_fd(fd, prev); + 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)); @@ -194,33 +193,16 @@ static void create_note(const unsigned char *object, struct note_data *d, } stripspace(&d->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(&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); - } - - if (!d->buf.len) { - fprintf(stderr, _("Removing note for object %s\n"), - sha1_to_hex(object)); - hashclr(result); - } else { - if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) { - 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 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); } } @@ -403,7 +385,6 @@ static int add(int argc, const char **argv, const char *prefix) const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; - char logmsg[100]; const unsigned char *note; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { @@ -463,17 +444,20 @@ static int add(int argc, const char **argv, const char *prefix) sha1_to_hex(object)); } - create_note(object, &d, 0, note, new_note); - free_note_data(&d); - - if (is_null_sha1(new_note)) + prepare_note_data(object, &d, note); + if (d.buf.len) { + write_note_data(&d, new_note); + 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"), + sha1_to_hex(object)); remove_note(t, object); - else if (add_note(t, object, new_note, combine_notes_overwrite)) - die("BUG: combine_notes_overwrite failed"); + 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); + free_note_data(&d); free_notes(t); return 0; } @@ -602,17 +586,38 @@ static int append_edit(int argc, const char **argv, const char *prefix) t = init_notes_check(argv[0]); note = get_note(t, object); - create_note(object, &d, !edit, note, new_note); - free_note_data(&d); + prepare_note_data(object, &d, edit ? note : NULL); - if (is_null_sha1(new_note)) + if (note && !edit) { + /* Append buf to previous note contents */ + unsigned long size; + enum object_type type; + char *prev_buf = read_sha1_file(note, &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); + } + + if (d.buf.len) { + write_note_data(&d, new_note); + if (add_note(t, object, new_note, combine_notes_overwrite)) + die("BUG: combine_notes_overwrite failed"); + snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'", + argv[0]); + } else { + fprintf(stderr, _("Removing note for object %s\n"), + sha1_to_hex(object)); remove_note(t, object); - else if (add_note(t, object, new_note, combine_notes_overwrite)) - die("BUG: combine_notes_overwrite failed"); - - snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", - is_null_sha1(new_note) ? "removed" : "added", argv[0]); + snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'", + argv[0]); + } commit_notes(t, logmsg); + + free_note_data(&d); free_notes(t); return 0; } From d73a5b933db1e722faa54d6709e89e2450d7c1e3 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 12 Nov 2014 01:40:14 +0100 Subject: [PATCH 7/9] builtin/notes: add --allow-empty, to allow storing empty notes Although the "git notes" man page advertises that we support binary-safe notes addition (using the -C option), we currently do not support adding the empty note (i.e. using the empty blob to annotate an object). Instead, an empty note is always treated as an intent to remove the note altogether. Introduce the --allow-empty option to the add/append/edit subcommands, to explicitly allow an empty note to be stored into the notes tree. Also update the documentation, and add test cases for the new option. Reported-by: James H. Fisher Improved-by: Kyle J. McKay Improved-by: Junio C Hamano Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- Documentation/git-notes.txt | 12 ++++++++---- builtin/notes.c | 17 +++++++++++------ t/t3301-notes.sh | 10 +++++++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt index 310f0a5e8c..851518d531 100644 --- a/Documentation/git-notes.txt +++ b/Documentation/git-notes.txt @@ -9,10 +9,10 @@ SYNOPSIS -------- [verse] 'git notes' [list []] -'git notes' add [-f] [-F | -m | (-c | -C) ] [] +'git notes' add [-f] [--allow-empty] [-F | -m | (-c | -C) ] [] 'git notes' copy [-f] ( --stdin | ) -'git notes' append [-F | -m | (-c | -C) ] [] -'git notes' edit [] +'git notes' append [--allow-empty] [-F | -m | (-c | -C) ] [] +'git notes' edit [--allow-empty] [] 'git notes' show [] 'git notes' merge [-v | -q] [-s ] 'git notes' merge --commit [-v | -q] @@ -155,6 +155,10 @@ OPTIONS Like '-C', but with '-c' the editor is invoked, so that the user can further edit the note message. +--allow-empty:: + Allow an empty note object to be stored. The default behavior is + to automatically remove empty notes. + --ref :: Manipulate the notes tree in . This overrides 'GIT_NOTES_REF' and the "core.notesRef" configuration. The ref @@ -287,7 +291,7 @@ arbitrary files using 'git hash-object': ------------ $ cc *.c $ blob=$(git hash-object -w a.out) -$ git notes --ref=built add -C "$blob" HEAD +$ git notes --ref=built add --allow-empty -C "$blob" HEAD ------------ (You cannot simply use `git notes --ref=built add -F a.out HEAD` diff --git a/builtin/notes.c b/builtin/notes.c index ed32d634fc..a9f37d0456 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -22,10 +22,10 @@ static const char * const git_notes_usage[] = { N_("git notes [--ref ] [list []]"), - N_("git notes [--ref ] add [-f] [-m | -F | (-c | -C) ] []"), + N_("git notes [--ref ] add [-f] [--allow-empty] [-m | -F | (-c | -C) ] []"), N_("git notes [--ref ] copy [-f] "), - N_("git notes [--ref ] append [-m | -F | (-c | -C) ] []"), - N_("git notes [--ref ] edit []"), + N_("git notes [--ref ] append [--allow-empty] [-m | -F | (-c | -C) ] []"), + N_("git notes [--ref ] edit [--allow-empty] []"), N_("git notes [--ref ] show []"), N_("git notes [--ref ] merge [-v | -q] [-s ] "), N_("git notes merge --commit [-v | -q]"), @@ -381,7 +381,7 @@ static int append_edit(int argc, const char **argv, const char *prefix); static int add(int argc, const char **argv, const char *prefix) { - int force = 0; + int force = 0, allow_empty = 0; const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; @@ -400,6 +400,8 @@ static int add(int argc, const char **argv, const char *prefix) { 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() }; @@ -445,7 +447,7 @@ static int add(int argc, const char **argv, const char *prefix) } prepare_note_data(object, &d, note); - if (d.buf.len) { + if (d.buf.len || allow_empty) { write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); @@ -540,6 +542,7 @@ out: 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]; @@ -560,6 +563,8 @@ static int append_edit(int argc, const char **argv, const char *prefix) { 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"); @@ -602,7 +607,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) free(prev_buf); } - if (d.buf.len) { + if (d.buf.len || allow_empty) { write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index f74b3fa335..70ec5c395f 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -1242,7 +1242,8 @@ test_expect_success 'git notes get-ref (--ref)' ' test_expect_success 'setup testing of empty notes' ' test_unconfig core.notesRef && test_commit 16th && - empty_blob=$(git hash-object -w /dev/null) + empty_blob=$(git hash-object -w /dev/null) && + echo "$empty_blob" >expect_empty ' while read cmd @@ -1252,6 +1253,13 @@ do MSG= git notes $cmd && test_must_fail git notes list HEAD " + + test_expect_success "'git notes $cmd --allow-empty' stores empty note" " + test_might_fail git notes remove HEAD && + MSG= git notes $cmd --allow-empty && + git notes list HEAD >actual && + test_cmp expect_empty actual + " done <<\EOF add add -F /dev/null From 8a4acd699536c518c9245714d6175aa39d13eb9d Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 12 Nov 2014 01:40:15 +0100 Subject: [PATCH 8/9] notes: empty notes should be shown by 'git log' If the user has gone through the trouble of explicitly adding an empty note, then "git log" should not silently skip it (as if it didn't exist). Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- notes.c | 3 +-- t/t3301-notes.sh | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/notes.c b/notes.c index 5fe691dbcd..62bc6e1818 100644 --- a/notes.c +++ b/notes.c @@ -1218,8 +1218,7 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1, if (!sha1) return; - if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen || - type != OBJ_BLOB) { + if (!(msg = read_sha1_file(sha1, &type, &msglen)) || type != OBJ_BLOB) { free(msg); return; } diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index 70ec5c395f..416ed9e9dc 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -1274,4 +1274,16 @@ append -C "$empty_blob" edit EOF +test_expect_success 'empty notes are displayed by git log' ' + test_commit 17th && + git log -1 >expect && + cat >>expect <<\EOF && + +Notes: +EOF + git notes add -C "$empty_blob" --allow-empty && + git log -1 >actual && + test_cmp expect actual +' + test_done From 908a3203632a02568df230c0fccf9a2cd8da24e6 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Wed, 12 Nov 2014 01:40:16 +0100 Subject: [PATCH 9/9] t3301: modernize style Make this test script appear somewhat less old-fashioned: - Use test helper functions: - write_script - test_commit - test_write_lines - test_line_count - test_config - test_unconfig - test_path_is_missing - Remove whitespace between redirection operators and their targets. - Move preparation of "expect" files into tests. - Rename "output" files to "actual". - More consistent quoting, especially around commands that might expand to nothing. - More visibility of important whitespace with ${indent}. - Combine pairs of tests that unnecessarily split setup and verification. Improved-by: Eric Sunshine Improved-by: Junio C Hamano Improved-by: Michael Blume Improved-by: Jeff King Signed-off-by: Johan Herland Signed-off-by: Junio C Hamano --- t/t3301-notes.sh | 1352 +++++++++++++++++++++------------------------- 1 file changed, 627 insertions(+), 725 deletions(-) diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index 416ed9e9dc..245406ab97 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -7,28 +7,22 @@ test_description='Test commit notes' . ./test-lib.sh -cat > fake_editor.sh << \EOF -#!/bin/sh -echo "$MSG" > "$1" -echo "$MSG" >& 2 +write_script fake_editor <<\EOF +echo "$MSG" >"$1" +echo "$MSG" >&2 EOF -chmod a+x fake_editor.sh -GIT_EDITOR=./fake_editor.sh +GIT_EDITOR=./fake_editor export GIT_EDITOR +indent=" " + test_expect_success 'cannot annotate non-existing HEAD' ' test_must_fail env MSG=3 git notes add ' -test_expect_success setup ' - : > a1 && - git add a1 && - test_tick && - git commit -m 1st && - : > a2 && - git add a2 && - test_tick && - git commit -m 2nd +test_expect_success 'setup' ' + test_commit 1st && + test_commit 2nd ' test_expect_success 'need valid notes ref' ' @@ -50,206 +44,186 @@ test_expect_success 'handle empty notes gracefully' ' ' test_expect_success 'show non-existent notes entry with %N' ' - for l in A B - do - echo "$l" - done >expect && - git show -s --format='A%n%NB' >output && - test_cmp expect output + test_write_lines A B >expect && + git show -s --format="A%n%NB" >actual && + test_cmp expect actual ' test_expect_success 'create notes' ' - git config core.notesRef refs/notes/commits && MSG=b4 git notes add && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b4 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b4" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' test_expect_success 'show notes entry with %N' ' - for l in A b4 B - do - echo "$l" - done >expect && - git show -s --format='A%n%NB' >output && - test_cmp expect output + test_write_lines A b4 B >expect && + git show -s --format="A%n%NB" >actual && + test_cmp expect actual ' -cat >expect <output && - test_cmp expect output + cat <<-EOF >expect && + a1d8fa6 refs/notes/commits@{0}: notes: Notes added by '\''git notes add'\'' + EOF + git reflog show refs/notes/commits >actual && + test_cmp expect actual ' test_expect_success 'edit existing notes' ' MSG=b3 git notes edit && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b3 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b3" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' test_expect_success 'cannot "git notes add -m" where notes already exists' ' test_must_fail git notes add -m "b2" && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b3 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b3" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' test_expect_success 'can overwrite existing note with "git notes add -f -m"' ' git notes add -f -m "b1" && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b1 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b1" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' test_expect_success 'add w/no options on existing note morphs into edit' ' MSG=b2 git notes add && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b2 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b2" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' test_expect_success 'can overwrite existing note with "git notes add -f"' ' MSG=b1 git notes add -f && - test ! -f .git/NOTES_EDITMSG && - test 1 = $(git ls-tree refs/notes/commits | wc -l) && - test b1 = $(git notes show) && + test_path_is_missing .git/NOTES_EDITMSG && + git ls-tree -r refs/notes/commits >actual && + test_line_count = 1 actual && + test "b1" = "$(git notes show)" && git show HEAD^ && test_must_fail git notes show HEAD^ ' -cat > expect << EOF -commit 268048bfb8a1fb38e703baceb8ab235421bf80c5 -Author: A U Thor -Date: Thu Apr 7 15:14:13 2005 -0700 - - 2nd - -Notes: - b1 -EOF - test_expect_success 'show notes' ' + cat >expect <<-EOF && + commit 7a4ca6ee52a974a66cbaa78e33214535dff1d691 + Author: A U Thor + Date: Thu Apr 7 15:14:13 2005 -0700 + + ${indent}2nd + + Notes: + ${indent}b1 + EOF ! (git cat-file commit HEAD | grep b1) && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' -test_expect_success 'create multi-line notes (setup)' ' - : > a3 && - git add a3 && - test_tick && - git commit -m 3rd && - MSG="b3 -c3c3c3c3 -d3d3d3" git notes add -' - -cat > expect-multiline << EOF -commit 1584215f1d29c65e99c6c6848626553fdd07fd75 -Author: A U Thor -Date: Thu Apr 7 15:15:13 2005 -0700 - - 3rd - -Notes: - b3 - c3c3c3c3 - d3d3d3 -EOF - -printf "\n" >> expect-multiline -cat expect >> expect-multiline - test_expect_success 'show multi-line notes' ' - git log -2 > output && - test_cmp expect-multiline output + test_commit 3rd && + MSG="b3${LF}c3c3c3c3${LF}d3d3d3" git notes add && + cat >expect-multiline <<-EOF && + commit d07d62e5208f22eb5695e7eb47667dc8b9860290 + Author: A U Thor + Date: Thu Apr 7 15:15:13 2005 -0700 + + ${indent}3rd + + Notes: + ${indent}b3 + ${indent}c3c3c3c3 + ${indent}d3d3d3 + + EOF + cat expect >>expect-multiline && + git log -2 >actual && + test_cmp expect-multiline actual ' -test_expect_success 'create -F notes (setup)' ' - : > a4 && - git add a4 && - test_tick && - git commit -m 4th && - echo "xyzzy" > note5 && - git notes add -F note5 -' - -cat > expect-F << EOF -commit 15023535574ded8b1a89052b32673f84cf9582b8 -Author: A U Thor -Date: Thu Apr 7 15:16:13 2005 -0700 - - 4th - -Notes: - xyzzy -EOF - -printf "\n" >> expect-F -cat expect-multiline >> expect-F test_expect_success 'show -F notes' ' - git log -3 > output && - test_cmp expect-F output + test_commit 4th && + echo "xyzzy" >note5 && + git notes add -F note5 && + cat >expect-F <<-EOF && + commit 0f7aa3ec6325aeb88b910453bb3eb37c49d75c11 + Author: A U Thor + Date: Thu Apr 7 15:16:13 2005 -0700 + + ${indent}4th + + Notes: + ${indent}xyzzy + + EOF + cat expect-multiline >>expect-F && + git log -3 >actual && + test_cmp expect-F actual ' test_expect_success 'Re-adding -F notes without -f fails' ' - echo "zyxxy" > note5 && + echo "zyxxy" >note5 && test_must_fail git notes add -F note5 && - git log -3 > output && - test_cmp expect-F output + git log -3 >actual && + test_cmp expect-F actual ' -cat >expect << EOF -commit 15023535574ded8b1a89052b32673f84cf9582b8 -tree e070e3af51011e47b183c33adf9736736a525709 -parent 1584215f1d29c65e99c6c6848626553fdd07fd75 -author A U Thor 1112912173 -0700 -committer C O Mitter 1112912173 -0700 - - 4th -EOF test_expect_success 'git log --pretty=raw does not show notes' ' - git log -1 --pretty=raw >output && - test_cmp expect output + cat >expect <<-EOF && + commit 0f7aa3ec6325aeb88b910453bb3eb37c49d75c11 + tree 05ac65288c4c4b3b709a020ae94b2ece2f2201ae + parent d07d62e5208f22eb5695e7eb47667dc8b9860290 + author A U Thor 1112912173 -0700 + committer C O Mitter 1112912173 -0700 + + ${indent}4th + EOF + git log -1 --pretty=raw >actual && + test_cmp expect actual ' -cat >>expect <output && - test_cmp expect output + cat >>expect <<-EOF && + + Notes: + ${indent}xyzzy + EOF + git log -1 --pretty=raw --show-notes >actual && + test_cmp expect actual ' test_expect_success 'git log --no-notes' ' - git log -1 --no-notes >output && - ! grep xyzzy output + git log -1 --no-notes >actual && + ! grep xyzzy actual ' test_expect_success 'git format-patch does not show notes' ' - git format-patch -1 --stdout >output && - ! grep xyzzy output + git format-patch -1 --stdout >actual && + ! grep xyzzy actual ' test_expect_success 'git format-patch --show-notes does show notes' ' - git format-patch --show-notes -1 --stdout >output && - grep xyzzy output + git format-patch --show-notes -1 --stdout >actual && + grep xyzzy actual ' for pretty in \ @@ -261,8 +235,8 @@ do ?*) p="$pretty" not=" not" negate="!" ;; esac test_expect_success "git show $pretty does$not show notes" ' - git show $p >output && - eval "$negate grep xyzzy output" + git show $p >actual && + eval "$negate grep xyzzy actual" ' done @@ -271,161 +245,131 @@ test_expect_success 'setup alternate notes ref' ' ' test_expect_success 'git log --notes shows default notes' ' - git log -1 --notes >output && - grep xyzzy output && - ! grep alternate output + git log -1 --notes >actual && + grep xyzzy actual && + ! grep alternate actual ' test_expect_success 'git log --notes=X shows only X' ' - git log -1 --notes=alternate >output && - ! grep xyzzy output && - grep alternate output + git log -1 --notes=alternate >actual && + ! grep xyzzy actual && + grep alternate actual ' test_expect_success 'git log --notes --notes=X shows both' ' - git log -1 --notes --notes=alternate >output && - grep xyzzy output && - grep alternate output + git log -1 --notes --notes=alternate >actual && + grep xyzzy actual && + grep alternate actual ' test_expect_success 'git log --no-notes resets default state' ' git log -1 --notes --notes=alternate \ --no-notes --notes=alternate \ - >output && - ! grep xyzzy output && - grep alternate output + >actual && + ! grep xyzzy actual && + grep alternate actual ' test_expect_success 'git log --no-notes resets ref list' ' git log -1 --notes --notes=alternate \ --no-notes --notes \ - >output && - grep xyzzy output && - ! grep alternate output + >actual && + grep xyzzy actual && + ! grep alternate actual ' -test_expect_success 'create -m notes (setup)' ' - : > a5 && - git add a5 && - test_tick && - git commit -m 5th && - git notes add -m spam -m "foo -bar -baz" -' - -whitespace=" " -cat > expect-m << EOF -commit bd1753200303d0a0344be813e504253b3d98e74d -Author: A U Thor -Date: Thu Apr 7 15:17:13 2005 -0700 - - 5th - -Notes: - spam -$whitespace - foo - bar - baz -EOF - -printf "\n" >> expect-m -cat expect-F >> expect-m - test_expect_success 'show -m notes' ' - git log -4 > output && - test_cmp expect-m output + test_commit 5th && + git notes add -m spam -m "foo${LF}bar${LF}baz" && + cat >expect-m <<-EOF && + commit 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + Author: A U Thor + Date: Thu Apr 7 15:17:13 2005 -0700 + + ${indent}5th + + Notes: + ${indent}spam + ${indent} + ${indent}foo + ${indent}bar + ${indent}baz + + EOF + cat expect-F >>expect-m && + git log -4 >actual && + test_cmp expect-m actual ' -test_expect_success 'remove note with add -f -F /dev/null (setup)' ' - git notes add -f -F /dev/null -' +test_expect_success 'remove note with add -f -F /dev/null' ' + git notes add -f -F /dev/null && + cat >expect-rm-F <<-EOF && + commit 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + Author: A U Thor + Date: Thu Apr 7 15:17:13 2005 -0700 -cat > expect-rm-F << EOF -commit bd1753200303d0a0344be813e504253b3d98e74d -Author: A U Thor -Date: Thu Apr 7 15:17:13 2005 -0700 + ${indent}5th - 5th -EOF - -printf "\n" >> expect-rm-F -cat expect-F >> expect-rm-F - -test_expect_success 'verify note removal with -F /dev/null' ' - git log -4 > output && - test_cmp expect-rm-F output && + EOF + cat expect-F >>expect-rm-F && + git log -4 >actual && + test_cmp expect-rm-F actual && test_must_fail git notes show ' -test_expect_success 'do not create empty note with -m "" (setup)' ' - git notes add -m "" -' - -test_expect_success 'verify non-creation of note with -m ""' ' - git log -4 > output && - test_cmp expect-rm-F output && +test_expect_success 'do not create empty note with -m ""' ' + git notes add -m "" && + git log -4 >actual && + test_cmp expect-rm-F actual && test_must_fail git notes show ' -cat > expect-combine_m_and_F << EOF -foo - -xyzzy - -bar - -zyxxy - -baz -EOF - test_expect_success 'create note with combination of -m and -F' ' - echo "xyzzy" > note_a && - echo "zyxxy" > note_b && + cat >expect-combine_m_and_F <<-EOF && + foo + + xyzzy + + bar + + zyxxy + + baz + EOF + echo "xyzzy" >note_a && + echo "zyxxy" >note_b && git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" && - git notes show > output && - test_cmp expect-combine_m_and_F output + git notes show >actual && + test_cmp expect-combine_m_and_F actual ' -test_expect_success 'remove note with "git notes remove" (setup)' ' +test_expect_success 'remove note with "git notes remove"' ' git notes remove HEAD^ && - git notes remove -' + git notes remove && + cat >expect-rm-remove <<-EOF && + commit 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + Author: A U Thor + Date: Thu Apr 7 15:17:13 2005 -0700 -cat > expect-rm-remove << EOF -commit bd1753200303d0a0344be813e504253b3d98e74d -Author: A U Thor -Date: Thu Apr 7 15:17:13 2005 -0700 + ${indent}5th - 5th + commit 0f7aa3ec6325aeb88b910453bb3eb37c49d75c11 + Author: A U Thor + Date: Thu Apr 7 15:16:13 2005 -0700 -commit 15023535574ded8b1a89052b32673f84cf9582b8 -Author: A U Thor -Date: Thu Apr 7 15:16:13 2005 -0700 + ${indent}4th - 4th -EOF - -printf "\n" >> expect-rm-remove -cat expect-multiline >> expect-rm-remove - -test_expect_success 'verify note removal with "git notes remove"' ' - git log -4 > output && - test_cmp expect-rm-remove output && + EOF + cat expect-multiline >>expect-rm-remove && + git log -4 >actual && + test_cmp expect-rm-remove actual && test_must_fail git notes show HEAD^ ' -cat > expect << EOF -c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75 -c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5 -EOF - test_expect_success 'removing non-existing note should not create new commit' ' - git rev-parse --verify refs/notes/commits > before_commit && + git rev-parse --verify refs/notes/commits >before_commit && test_must_fail git notes remove HEAD^ && - git rev-parse --verify refs/notes/commits > after_commit && + git rev-parse --verify refs/notes/commits >after_commit && test_cmp before_commit after_commit ' @@ -505,70 +449,68 @@ test_expect_success 'removing with --stdin --ignore-missing' ' ' test_expect_success 'list notes with "git notes list"' ' - git notes list > output && - test_cmp expect output + cat >expect <<-EOF && + c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 7a4ca6ee52a974a66cbaa78e33214535dff1d691 + c18dc024e14f08d18d14eea0d747ff692d66d6a3 d07d62e5208f22eb5695e7eb47667dc8b9860290 + EOF + git notes list >actual && + test_cmp expect actual ' test_expect_success 'list notes with "git notes"' ' - git notes > output && - test_cmp expect output + git notes >actual && + test_cmp expect actual ' -cat > expect << EOF -c18dc024e14f08d18d14eea0d747ff692d66d6a3 -EOF - test_expect_success 'list specific note with "git notes list "' ' - git notes list HEAD^^ > output && - test_cmp expect output + cat >expect <<-EOF && + c18dc024e14f08d18d14eea0d747ff692d66d6a3 + EOF + git notes list HEAD^^ >actual && + test_cmp expect actual ' -cat > expect << EOF -EOF - test_expect_success 'listing non-existing notes fails' ' - test_must_fail git notes list HEAD > output && - test_cmp expect output + cat >expect <<-EOF && + EOF + test_must_fail git notes list HEAD >actual && + test_cmp expect actual ' -cat > expect << EOF -Initial set of notes - -More notes appended with git notes append -EOF - test_expect_success 'append to existing note with "git notes append"' ' + cat >expect <<-EOF && + Initial set of notes + + More notes appended with git notes append + EOF git notes add -m "Initial set of notes" && git notes append -m "More notes appended with git notes append" && - git notes show > output && - test_cmp expect output + git notes show >actual && + test_cmp expect actual ' -cat > expect_list << EOF -c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75 -c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5 -4b6ad22357cc8a1296720574b8d2fbc22fab0671 bd1753200303d0a0344be813e504253b3d98e74d -EOF - test_expect_success '"git notes list" does not expand to "git notes list HEAD"' ' - git notes list > output && - test_cmp expect_list output + cat >expect_list <<-EOF && + c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 7a4ca6ee52a974a66cbaa78e33214535dff1d691 + 4b6ad22357cc8a1296720574b8d2fbc22fab0671 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + c18dc024e14f08d18d14eea0d747ff692d66d6a3 d07d62e5208f22eb5695e7eb47667dc8b9860290 + EOF + git notes list >actual && + test_cmp expect_list actual ' test_expect_success 'appending empty string does not change existing note' ' git notes append -m "" && - git notes show > output && - test_cmp expect output + git notes show >actual && + test_cmp expect actual ' test_expect_success 'git notes append == add when there is no existing note' ' git notes remove HEAD && test_must_fail git notes list HEAD && - git notes append -m "Initial set of notes - -More notes appended with git notes append" && - git notes show > output && - test_cmp expect output + git notes append -m "Initial set of notes${LF}${LF}More notes appended with git notes append" && + git notes show >actual && + test_cmp expect actual ' test_expect_success 'appending empty string to non-existing note does not create note' ' @@ -579,229 +521,208 @@ test_expect_success 'appending empty string to non-existing note does not create ' test_expect_success 'create other note on a different notes ref (setup)' ' - : > a6 && - git add a6 && - test_tick && - git commit -m 6th && - GIT_NOTES_REF="refs/notes/other" git notes add -m "other note" + test_commit 6th && + GIT_NOTES_REF="refs/notes/other" git notes add -m "other note" && + cat >expect-not-other <<-EOF && + commit 2c125331118caba0ff8238b7f4958ac6e93fe39c + Author: A U Thor + Date: Thu Apr 7 15:18:13 2005 -0700 + + ${indent}6th + EOF + cp expect-not-other expect-other && + cat >>expect-other <<-EOF + + Notes (other): + ${indent}other note + EOF ' -cat > expect-other << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th - -Notes (other): - other note -EOF - -cat > expect-not-other << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th -EOF - test_expect_success 'Do not show note on other ref by default' ' - git log -1 > output && - test_cmp expect-not-other output + git log -1 >actual && + test_cmp expect-not-other actual ' test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' ' - GIT_NOTES_REF="refs/notes/other" git log -1 > output && - test_cmp expect-other output + GIT_NOTES_REF="refs/notes/other" git log -1 >actual && + test_cmp expect-other actual ' test_expect_success 'Do show note when ref is given in core.notesRef config' ' - git config core.notesRef "refs/notes/other" && - git log -1 > output && - test_cmp expect-other output + test_config core.notesRef "refs/notes/other" && + git log -1 >actual && + test_cmp expect-other actual ' test_expect_success 'Do not show note when core.notesRef is overridden' ' - GIT_NOTES_REF="refs/notes/wrong" git log -1 > output && - test_cmp expect-not-other output + test_config core.notesRef "refs/notes/other" && + GIT_NOTES_REF="refs/notes/wrong" git log -1 >actual && + test_cmp expect-not-other actual ' -cat > expect-both << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th - -Notes: - order test - -Notes (other): - other note - -commit bd1753200303d0a0344be813e504253b3d98e74d -Author: A U Thor -Date: Thu Apr 7 15:17:13 2005 -0700 - - 5th - -Notes: - replacement for deleted note -EOF - test_expect_success 'Show all notes when notes.displayRef=refs/notes/*' ' + cat >expect-both <<-EOF && + commit 2c125331118caba0ff8238b7f4958ac6e93fe39c + Author: A U Thor + Date: Thu Apr 7 15:18:13 2005 -0700 + + ${indent}6th + + Notes: + ${indent}order test + + Notes (other): + ${indent}other note + + commit 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + Author: A U Thor + Date: Thu Apr 7 15:17:13 2005 -0700 + + ${indent}5th + + Notes: + ${indent}replacement for deleted note + EOF GIT_NOTES_REF=refs/notes/commits git notes add \ -m"replacement for deleted note" HEAD^ && GIT_NOTES_REF=refs/notes/commits git notes add -m"order test" && - git config --unset core.notesRef && - git config notes.displayRef "refs/notes/*" && - git log -2 > output && - test_cmp expect-both output + test_unconfig core.notesRef && + test_config notes.displayRef "refs/notes/*" && + git log -2 >actual && + test_cmp expect-both actual ' test_expect_success 'core.notesRef is implicitly in notes.displayRef' ' - git config core.notesRef refs/notes/commits && - git config notes.displayRef refs/notes/other && - git log -2 > output && - test_cmp expect-both output + test_config core.notesRef refs/notes/commits && + test_config notes.displayRef refs/notes/other && + git log -2 >actual && + test_cmp expect-both actual ' test_expect_success 'notes.displayRef can be given more than once' ' - git config --unset core.notesRef && - git config notes.displayRef refs/notes/commits && + test_unconfig core.notesRef && + test_config notes.displayRef refs/notes/commits && git config --add notes.displayRef refs/notes/other && - git log -2 > output && - test_cmp expect-both output + git log -2 >actual && + test_cmp expect-both actual ' -cat > expect-both-reversed << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th - -Notes (other): - other note - -Notes: - order test -EOF - test_expect_success 'notes.displayRef respects order' ' - git config core.notesRef refs/notes/other && - git config --unset-all notes.displayRef && - git config notes.displayRef refs/notes/commits && - git log -1 > output && - test_cmp expect-both-reversed output + cat >expect-both-reversed <<-EOF && + commit 2c125331118caba0ff8238b7f4958ac6e93fe39c + Author: A U Thor + Date: Thu Apr 7 15:18:13 2005 -0700 + + ${indent}6th + + Notes (other): + ${indent}other note + + Notes: + ${indent}order test + EOF + test_config core.notesRef refs/notes/other && + test_config notes.displayRef refs/notes/commits && + git log -1 >actual && + test_cmp expect-both-reversed actual ' test_expect_success 'GIT_NOTES_DISPLAY_REF works' ' - git config --unset-all core.notesRef && - git config --unset-all notes.displayRef && GIT_NOTES_DISPLAY_REF=refs/notes/commits:refs/notes/other \ - git log -2 > output && - test_cmp expect-both output + git log -2 >actual && + test_cmp expect-both actual ' -cat > expect-none << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th - -commit bd1753200303d0a0344be813e504253b3d98e74d -Author: A U Thor -Date: Thu Apr 7 15:17:13 2005 -0700 - - 5th -EOF - test_expect_success 'GIT_NOTES_DISPLAY_REF overrides config' ' - git config notes.displayRef "refs/notes/*" && - GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log -2 > output && - test_cmp expect-none output + cat >expect-none <<-EOF && + commit 2c125331118caba0ff8238b7f4958ac6e93fe39c + Author: A U Thor + Date: Thu Apr 7 15:18:13 2005 -0700 + + ${indent}6th + + commit 7f9ad8836c775acb134c0a055fc55fb4cd1ba361 + Author: A U Thor + Date: Thu Apr 7 15:17:13 2005 -0700 + + ${indent}5th + EOF + test_config notes.displayRef "refs/notes/*" && + GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log -2 >actual && + test_cmp expect-none actual ' test_expect_success '--show-notes=* adds to GIT_NOTES_DISPLAY_REF' ' - GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log --show-notes=* -2 > output && - test_cmp expect-both output + GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log --show-notes=* -2 >actual && + test_cmp expect-both actual ' -cat > expect-commits << EOF -commit 387a89921c73d7ed72cd94d179c1c7048ca47756 -Author: A U Thor -Date: Thu Apr 7 15:18:13 2005 -0700 - - 6th - -Notes: - order test -EOF - test_expect_success '--no-standard-notes' ' - git log --no-standard-notes --show-notes=commits -1 > output && - test_cmp expect-commits output + cat >expect-commits < + Date: Thu Apr 7 15:18:13 2005 -0700 + + ${indent}6th + + Notes: + ${indent}order test + EOF + git log --no-standard-notes --show-notes=commits -1 >actual && + test_cmp expect-commits actual ' test_expect_success '--standard-notes' ' + test_config notes.displayRef "refs/notes/*" && git log --no-standard-notes --show-notes=commits \ - --standard-notes -2 > output && - test_cmp expect-both output + --standard-notes -2 >actual && + test_cmp expect-both actual ' test_expect_success '--show-notes=ref accumulates' ' git log --show-notes=other --show-notes=commits \ - --no-standard-notes -1 > output && - test_cmp expect-both-reversed output + --no-standard-notes -1 >actual && + test_cmp expect-both-reversed actual ' test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' ' - git config core.notesRef refs/notes/other && - echo "Note on a tree" > expect && + test_config core.notesRef refs/notes/other && + echo "Note on a tree" >expect && git notes add -m "Note on a tree" HEAD: && - git notes show HEAD: > actual && + git notes show HEAD: >actual && test_cmp expect actual && - echo "Note on a blob" > expect && + echo "Note on a blob" >expect && filename=$(git ls-tree --name-only HEAD | head -n1) && git notes add -m "Note on a blob" HEAD:$filename && - git notes show HEAD:$filename > actual && + git notes show HEAD:$filename >actual && test_cmp expect actual && - echo "Note on a tag" > expect && + echo "Note on a tag" >expect && git tag -a -m "This is an annotated tag" foobar HEAD^ && git notes add -m "Note on a tag" foobar && - git notes show foobar > actual && + git notes show foobar >actual && test_cmp expect actual ' -cat > expect << EOF -commit 2ede89468182a62d0bde2583c736089bcf7d7e92 -Author: A U Thor -Date: Thu Apr 7 15:19:13 2005 -0700 - - 7th - -Notes (other): - other note -EOF - test_expect_success 'create note from other note with "git notes add -C"' ' - : > a7 && - git add a7 && - test_tick && - git commit -m 7th && + cat >expect <<-EOF && + commit fb01e0ca8c33b6cc0c6451dde747f97df567cb5c + Author: A U Thor + Date: Thu Apr 7 15:19:13 2005 -0700 + + ${indent}7th + + Notes: + ${indent}order test + EOF + test_commit 7th && git notes add -C $(git notes list HEAD^) && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual && test "$(git notes list HEAD)" = "$(git notes list HEAD^)" ' test_expect_success 'create note from non-existing note with "git notes add -C" fails' ' - : > a8 && - git add a8 && - test_tick && - git commit -m 8th && + test_commit 8th && test_must_fail git notes add -C deadbeef && test_must_fail git notes list HEAD ' @@ -814,405 +735,386 @@ test_expect_success 'create note from non-blob with "git notes add -C" fails' ' test_must_fail git notes list HEAD ' -cat > expect << EOF -commit 80d796defacd5db327b7a4e50099663902fbdc5c -Author: A U Thor -Date: Thu Apr 7 15:20:13 2005 -0700 - - 8th - -Notes (other): - This is a blob object -EOF - test_expect_success 'create note from blob with "git notes add -C" reuses blob id' ' + cat >expect <<-EOF && + commit 9a4c31c7f722b5d517e92c64e932dd751e1413bf + Author: A U Thor + Date: Thu Apr 7 15:20:13 2005 -0700 + + ${indent}8th + + Notes: + ${indent}This is a blob object + EOF blob=$(echo "This is a blob object" | git hash-object -w --stdin) && git notes add -C $blob && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual && test "$(git notes list HEAD)" = "$blob" ' -cat > expect << EOF -commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b -Author: A U Thor -Date: Thu Apr 7 15:21:13 2005 -0700 - - 9th - -Notes (other): - yet another note -EOF - test_expect_success 'create note from other note with "git notes add -c"' ' - : > a9 && - git add a9 && - test_tick && - git commit -m 9th && + cat >expect <<-EOF && + commit 2e0db4bc649e174d667a1cde19e725cf897a5bd2 + Author: A U Thor + Date: Thu Apr 7 15:21:13 2005 -0700 + + ${indent}9th + + Notes: + ${indent}yet another note + EOF + test_commit 9th && MSG="yet another note" git notes add -c $(git notes list HEAD^^) && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual ' test_expect_success 'create note from non-existing note with "git notes add -c" fails' ' - : > a10 && - git add a10 && - test_tick && - git commit -m 10th && + test_commit 10th && test_must_fail env MSG="yet another note" git notes add -c deadbeef && test_must_fail git notes list HEAD ' -cat > expect << EOF -commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b -Author: A U Thor -Date: Thu Apr 7 15:21:13 2005 -0700 - - 9th - -Notes (other): - yet another note -$whitespace - yet another note -EOF - test_expect_success 'append to note from other note with "git notes append -C"' ' + cat >expect <<-EOF && + commit 2e0db4bc649e174d667a1cde19e725cf897a5bd2 + Author: A U Thor + Date: Thu Apr 7 15:21:13 2005 -0700 + + ${indent}9th + + Notes: + ${indent}yet another note + ${indent} + ${indent}yet another note + EOF git notes append -C $(git notes list HEAD^) HEAD^ && - git log -1 HEAD^ > actual && + git log -1 HEAD^ >actual && test_cmp expect actual ' -cat > expect << EOF -commit ffed603236bfa3891c49644257a83598afe8ae5a -Author: A U Thor -Date: Thu Apr 7 15:22:13 2005 -0700 - - 10th - -Notes (other): - other note -EOF - test_expect_success 'create note from other note with "git notes append -c"' ' + cat >expect <<-EOF && + commit 7c3b87ab368f81e11b1ea87b2ab99a71ccd25406 + Author: A U Thor + Date: Thu Apr 7 15:22:13 2005 -0700 + + ${indent}10th + + Notes: + ${indent}other note + EOF MSG="other note" git notes append -c $(git notes list HEAD^) && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual ' -cat > expect << EOF -commit ffed603236bfa3891c49644257a83598afe8ae5a -Author: A U Thor -Date: Thu Apr 7 15:22:13 2005 -0700 - - 10th - -Notes (other): - other note -$whitespace - yet another note -EOF - test_expect_success 'append to note from other note with "git notes append -c"' ' + cat >expect <<-EOF && + commit 7c3b87ab368f81e11b1ea87b2ab99a71ccd25406 + Author: A U Thor + Date: Thu Apr 7 15:22:13 2005 -0700 + + ${indent}10th + + Notes: + ${indent}other note + ${indent} + ${indent}yet another note + EOF MSG="yet another note" git notes append -c $(git notes list HEAD) && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual ' -cat > expect << EOF -commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be -Author: A U Thor -Date: Thu Apr 7 15:23:13 2005 -0700 - - 11th - -Notes (other): - other note -$whitespace - yet another note -EOF - test_expect_success 'copy note with "git notes copy"' ' - : > a11 && - git add a11 && - test_tick && - git commit -m 11th && + cat >expect <<-EOF && + commit a446fff8777efdc6eb8f4b7c8a5ff699484df0d5 + Author: A U Thor + Date: Thu Apr 7 15:23:13 2005 -0700 + + ${indent}11th + + Notes: + ${indent}other note + ${indent} + ${indent}yet another note + EOF + test_commit 11th && git notes copy HEAD^ HEAD && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual && test "$(git notes list HEAD)" = "$(git notes list HEAD^)" ' test_expect_success 'prevent overwrite with "git notes copy"' ' test_must_fail git notes copy HEAD~2 HEAD && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual && test "$(git notes list HEAD)" = "$(git notes list HEAD^)" ' -cat > expect << EOF -commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be -Author: A U Thor -Date: Thu Apr 7 15:23:13 2005 -0700 - - 11th - -Notes (other): - yet another note -$whitespace - yet another note -EOF - test_expect_success 'allow overwrite with "git notes copy -f"' ' + cat >expect <<-EOF && + commit a446fff8777efdc6eb8f4b7c8a5ff699484df0d5 + Author: A U Thor + Date: Thu Apr 7 15:23:13 2005 -0700 + + ${indent}11th + + Notes: + ${indent}yet another note + ${indent} + ${indent}yet another note + EOF git notes copy -f HEAD~2 HEAD && - git log -1 > actual && + git log -1 >actual && test_cmp expect actual && test "$(git notes list HEAD)" = "$(git notes list HEAD~2)" ' test_expect_success 'cannot copy note from object without notes' ' - : > a12 && - git add a12 && - test_tick && - git commit -m 12th && - : > a13 && - git add a13 && - test_tick && - git commit -m 13th && + test_commit 12th && + test_commit 13th && test_must_fail git notes copy HEAD^ HEAD ' -cat > expect << EOF -commit e5d4fb5698d564ab8c73551538ecaf2b0c666185 -Author: A U Thor -Date: Thu Apr 7 15:25:13 2005 -0700 - - 13th - -Notes (other): - yet another note -$whitespace - yet another note - -commit 7038787dfe22a14c3867ce816dbba39845359719 -Author: A U Thor -Date: Thu Apr 7 15:24:13 2005 -0700 - - 12th - -Notes (other): - other note -$whitespace - yet another note -EOF - test_expect_success 'git notes copy --stdin' ' + cat >expect <<-EOF && + commit e871aa61182b1d95d0a6fb75445d891722863b6b + Author: A U Thor + Date: Thu Apr 7 15:25:13 2005 -0700 + + ${indent}13th + + Notes: + ${indent}yet another note + ${indent} + ${indent}yet another note + + commit 65e263ded02ae4e8839bc151095113737579dc12 + Author: A U Thor + Date: Thu Apr 7 15:24:13 2005 -0700 + + ${indent}12th + + Notes: + ${indent}other note + ${indent} + ${indent}yet another note + EOF (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \ echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) | git notes copy --stdin && - git log -2 > output && - test_cmp expect output && + git log -2 >actual && + test_cmp expect actual && test "$(git notes list HEAD)" = "$(git notes list HEAD~2)" && test "$(git notes list HEAD^)" = "$(git notes list HEAD~3)" ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d -Author: A U Thor -Date: Thu Apr 7 15:26:13 2005 -0700 - - 14th -EOF - test_expect_success 'git notes copy --for-rewrite (unconfigured)' ' + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + commit 07c85d77059393ed0154b8c96906547a59dfcddd + Author: A U Thor + Date: Thu Apr 7 15:26:13 2005 -0700 + + ${indent}14th + EOF test_commit 14th && test_commit 15th && (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \ echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) | git notes copy --for-rewrite=foo && - git log -2 > output && - test_cmp expect output + git log -2 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - yet another note -$whitespace - yet another note - -commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d -Author: A U Thor -Date: Thu Apr 7 15:26:13 2005 -0700 - - 14th - -Notes (other): - other note -$whitespace - yet another note -EOF - test_expect_success 'git notes copy --for-rewrite (enabled)' ' - git config notes.rewriteMode overwrite && - git config notes.rewriteRef "refs/notes/*" && + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}yet another note + ${indent} + ${indent}yet another note + + commit 07c85d77059393ed0154b8c96906547a59dfcddd + Author: A U Thor + Date: Thu Apr 7 15:26:13 2005 -0700 + + ${indent}14th + + Notes: + ${indent}other note + ${indent} + ${indent}yet another note + EOF + test_config notes.rewriteMode overwrite && + test_config notes.rewriteRef "refs/notes/*" && (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \ echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) | git notes copy --for-rewrite=foo && - git log -2 > output && - test_cmp expect output + git log -2 >actual && + test_cmp expect actual ' test_expect_success 'git notes copy --for-rewrite (disabled)' ' - git config notes.rewrite.bar false && + test_config notes.rewrite.bar false && echo $(git rev-parse HEAD~3) $(git rev-parse HEAD) | git notes copy --for-rewrite=bar && - git log -2 > output && - test_cmp expect output + git log -2 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - a fresh note -EOF - test_expect_success 'git notes copy --for-rewrite (overwrite)' ' + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}a fresh note + EOF git notes add -f -m"a fresh note" HEAD^ && + test_config notes.rewriteMode overwrite && + test_config notes.rewriteRef "refs/notes/*" && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' test_expect_success 'git notes copy --for-rewrite (ignore)' ' - git config notes.rewriteMode ignore && + test_config notes.rewriteMode ignore && + test_config notes.rewriteRef "refs/notes/*" && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - a fresh note -$whitespace - another fresh note -EOF - test_expect_success 'git notes copy --for-rewrite (append)' ' + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}a fresh note + ${indent} + ${indent}another fresh note + EOF git notes add -f -m"another fresh note" HEAD^ && - git config notes.rewriteMode concatenate && + test_config notes.rewriteMode concatenate && + test_config notes.rewriteRef "refs/notes/*" && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - a fresh note -$whitespace - another fresh note -$whitespace - append 1 -$whitespace - append 2 -EOF - test_expect_success 'git notes copy --for-rewrite (append two to one)' ' + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}a fresh note + ${indent} + ${indent}another fresh note + ${indent} + ${indent}append 1 + ${indent} + ${indent}append 2 + EOF git notes add -f -m"append 1" HEAD^ && git notes add -f -m"append 2" HEAD^^ && + test_config notes.rewriteMode concatenate && + test_config notes.rewriteRef "refs/notes/*" && (echo $(git rev-parse HEAD^) $(git rev-parse HEAD); echo $(git rev-parse HEAD^^) $(git rev-parse HEAD)) | git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' test_expect_success 'git notes copy --for-rewrite (append empty)' ' git notes remove HEAD^ && + test_config notes.rewriteMode concatenate && + test_config notes.rewriteRef "refs/notes/*" && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - replacement note 1 -EOF - test_expect_success 'GIT_NOTES_REWRITE_MODE works' ' + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}replacement note 1 + EOF + test_config notes.rewriteMode concatenate && + test_config notes.rewriteRef "refs/notes/*" && git notes add -f -m"replacement note 1" HEAD^ && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | GIT_NOTES_REWRITE_MODE=overwrite git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' -cat > expect << EOF -commit 37a0d4cba38afef96ba54a3ea567e6dac575700b -Author: A U Thor -Date: Thu Apr 7 15:27:13 2005 -0700 - - 15th - -Notes (other): - replacement note 2 -EOF - test_expect_success 'GIT_NOTES_REWRITE_REF works' ' - git config notes.rewriteMode overwrite && + cat >expect <<-EOF && + commit 4acf42e847e7fffbbf89ee365c20ac7caf40de89 + Author: A U Thor + Date: Thu Apr 7 15:27:13 2005 -0700 + + ${indent}15th + + Notes: + ${indent}replacement note 2 + EOF git notes add -f -m"replacement note 2" HEAD^ && - git config --unset-all notes.rewriteRef && + test_config notes.rewriteMode overwrite && + test_unconfig notes.rewriteRef && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | GIT_NOTES_REWRITE_REF=refs/notes/commits:refs/notes/other \ git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' ' - git config notes.rewriteRef refs/notes/other && git notes add -f -m"replacement note 3" HEAD^ && + test_config notes.rewriteMode overwrite && + test_config notes.rewriteRef refs/notes/other && echo $(git rev-parse HEAD^) $(git rev-parse HEAD) | GIT_NOTES_REWRITE_REF= git notes copy --for-rewrite=foo && - git log -1 > output && - test_cmp expect output + git log -1 >actual && + test_cmp expect actual ' test_expect_success 'git notes copy diagnoses too many or too few parameters' ' @@ -1221,13 +1123,13 @@ test_expect_success 'git notes copy diagnoses too many or too few parameters' ' ' test_expect_success 'git notes get-ref (no overrides)' ' - git config --unset core.notesRef && + test_unconfig core.notesRef && sane_unset GIT_NOTES_REF && test "$(git notes get-ref)" = "refs/notes/commits" ' test_expect_success 'git notes get-ref (core.notesRef)' ' - git config core.notesRef refs/notes/foo && + test_config core.notesRef refs/notes/foo && test "$(git notes get-ref)" = "refs/notes/foo" ' @@ -1277,10 +1179,10 @@ EOF test_expect_success 'empty notes are displayed by git log' ' test_commit 17th && git log -1 >expect && - cat >>expect <<\EOF && + cat >>expect <<-EOF && -Notes: -EOF + Notes: + EOF git notes add -C "$empty_blob" --allow-empty && git log -1 >actual && test_cmp expect actual