diff options
author | Taylor Blau <me@ttaylorr.com> | 2022-11-18 20:04:57 -0500 |
---|---|---|
committer | Taylor Blau <me@ttaylorr.com> | 2022-11-18 20:04:57 -0500 |
commit | 6582bac2ac61ad889b18d4ec4dbc6c65e99b1105 (patch) | |
tree | 16e7f6f7d8245bdcdacf10833ddf592fe2c3346f | |
parent | cd9aeb40a73b32ab898f1d32b08599522e6abb4a (diff) | |
parent | a97c1490b51b0ac52a7caca149f957c111bac6f3 (diff) | |
download | git-6582bac2ac61ad889b18d4ec4dbc6c65e99b1105.tar.gz |
Merge branch 'tl/notes--blankline' into jch
'git notes append' was taught '--[no-]blank-line' to conditionally
add a LF between a new and existing note.
* tl/notes--blankline:
notes.c: introduce "--no-blank-line" option
notes.c: provide tips when target and append note are both empty
notes.c: drop unreachable code in 'append_edit()'
notes.c: cleanup for "designated init" and "char ptr init"
notes.c: cleanup 'strbuf_grow' call in 'append_edit'
-rw-r--r-- | Documentation/git-notes.txt | 10 | ||||
-rw-r--r-- | builtin/notes.c | 20 | ||||
-rwxr-xr-x | t/t3301-notes.sh | 18 |
3 files changed, 34 insertions, 14 deletions
diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt index efbc10f0f5..50b198c2b2 100644 --- a/Documentation/git-notes.txt +++ b/Documentation/git-notes.txt @@ -11,7 +11,7 @@ SYNOPSIS 'git notes' [list [<object>]] 'git notes' add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>] 'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] ) -'git notes' append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>] +'git notes' append [--allow-empty] [--no-blank-line] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>] 'git notes' edit [--allow-empty] [<object>] 'git notes' show [<object>] 'git notes' merge [-v | -q] [-s <strategy> ] <notes-ref> @@ -86,7 +86,9 @@ the command can read the input given to the `post-rewrite` hook.) append:: Append to the notes of an existing object (defaults to HEAD). - Creates a new notes object if needed. + Creates a new notes object if needed. If the note of the given + object and the note to be appended are not empty, a blank line + will be inserted between them. edit:: Edit the notes for a given object (defaults to HEAD). @@ -159,6 +161,10 @@ OPTIONS Allow an empty note object to be stored. The default behavior is to automatically remove empty notes. +--no-blank-line:: + Do not insert a blank line before the inserted notes (insert + a blank line is the default). + --ref <ref>:: Manipulate the notes tree in <ref>. This overrides `GIT_NOTES_REF` and the "core.notesRef" configuration. The ref diff --git a/builtin/notes.c b/builtin/notes.c index 80d9dfd25c..902418df3f 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -562,13 +562,14 @@ out: static int append_edit(int argc, const char **argv, const char *prefix) { int allow_empty = 0; + int blankline = 1; const char *object_ref; struct notes_tree *t; struct object_id object, new_note; const struct object_id *note; - char *logmsg; + char *logmsg = NULL; const char * const *usage; - struct note_data d = { 0, 0, NULL, STRBUF_INIT }; + struct note_data d = { .buf = STRBUF_INIT }; struct option options[] = { OPT_CALLBACK_F('m', "message", &d, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, @@ -584,6 +585,8 @@ static int append_edit(int argc, const char **argv, const char *prefix) parse_reuse_arg), OPT_BOOL(0, "allow-empty", &allow_empty, N_("allow storing empty note")), + OPT_BOOL(0, "blank-line", &blankline, + N_("insert paragraph break before appending to an existing note")), OPT_END() }; int edit = !strcmp(argv[0], "edit"); @@ -618,8 +621,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) enum object_type type; char *prev_buf = read_object_file(note, &type, &size); - strbuf_grow(&d.buf, size + 1); - if (d.buf.len && prev_buf && size) + if (blankline && d.buf.len && prev_buf && size) strbuf_insertstr(&d.buf, 0, "\n"); if (prev_buf && size) strbuf_insert(&d.buf, 0, prev_buf, size); @@ -631,13 +633,11 @@ static int append_edit(int argc, const char **argv, const char *prefix) if (add_note(t, &object, &new_note, combine_notes_overwrite)) BUG("combine_notes_overwrite failed"); logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); - } else { - fprintf(stderr, _("Removing note for object %s\n"), + commit_notes(the_repository, t, logmsg); + } else if (!d.buf.len && !note) + fprintf(stderr, + _("Both original and appended notes are empty in %s, do nothing\n"), oid_to_hex(&object)); - remove_note(t, object.hash); - logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); - } - commit_notes(the_repository, t, logmsg); free(logmsg); free_note_data(&d); diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index 3288aaec7d..dedad93a2f 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -521,12 +521,25 @@ test_expect_success 'listing non-existing notes fails' ' test_must_be_empty actual ' +test_expect_success 'append to existing note without a beginning blank line' ' + test_when_finished git notes remove HEAD && + cat >expect <<-\EOF && + Initial set of notes + Appended notes + EOF + git notes add -m "Initial set of notes" && + git notes append --no-blank-line -m "Appended notes" && + git notes show >actual && + test_cmp expect actual +' + 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 >actual && @@ -552,6 +565,7 @@ test_expect_success 'appending empty string does not change existing note' ' ' test_expect_success 'git notes append == add when there is no existing note' ' + test_when_finished git notes remove HEAD && git notes remove HEAD && test_must_fail git notes list HEAD && git notes append -m "Initial set of notes${LF}${LF}More notes appended with git notes append" && @@ -560,9 +574,9 @@ test_expect_success 'git notes append == add when there is no existing note' ' ' test_expect_success 'appending empty string to non-existing note does not create note' ' - git notes remove HEAD && test_must_fail git notes list HEAD && - git notes append -m "" && + git notes append -m "" >output 2>&1 && + grep "Both original and appended notes are empty" output && test_must_fail git notes list HEAD ' |