diff options
author | Johan Herland <johan@herland.net> | 2010-02-13 22:28:33 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2010-02-13 19:36:16 -0800 |
commit | 2347fae50b2f75c6c0b362bd9ef24249419ed2b1 (patch) | |
tree | 13975d09a3a5bbbe4b95d21ff8e0a54afe83bcdc /builtin-notes.c | |
parent | 7aa4754e552eff22d70d496dea73a9c7639d66d3 (diff) | |
download | git-2347fae50b2f75c6c0b362bd9ef24249419ed2b1.tar.gz git-2347fae50b2f75c6c0b362bd9ef24249419ed2b1.tar.xz |
builtin-notes: Add "append" subcommand for appending to note objects
"git notes append" is equivalent to "git notes edit" except that instead
of editing existing notes contents, you can only append to it. This is
useful for quickly adding annotations like e.g.:
git notes append -m "Acked-by: A U Thor <author@example.com>"
"git notes append" takes the same -m/-F options as "git notes add".
If there is no existing note to append to, "git notes append" is identical
to "git notes add" (i.e. it adds a new note).
The patch includes tests verifying correct behaviour of the new subcommand.
Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-notes.c')
-rw-r--r-- | builtin-notes.c | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/builtin-notes.c b/builtin-notes.c index 006edf6b1..c88df00b3 100644 --- a/builtin-notes.c +++ b/builtin-notes.c @@ -20,6 +20,7 @@ static const char * const git_notes_usage[] = { "git notes [list [<object>]]", "git notes add [-f] [-m <msg> | -F <file>] [<object>]", + "git notes append [-m <msg> | -F <file>] [<object>]", "git notes edit [-m <msg> | -F <file>] [<object>]", "git notes show [<object>]", "git notes remove [<object>]", @@ -94,7 +95,7 @@ static void write_commented_object(int fd, const unsigned char *object) static void create_note(const unsigned char *object, struct strbuf *buf, - int skip_editor, + int skip_editor, int append_only, const unsigned char *prev, unsigned char *result) { @@ -109,7 +110,7 @@ static void create_note(const unsigned char *object, if (fd < 0) die_errno("could not create file '%s'", path); - if (prev) + if (prev && !append_only) write_note_data(fd, prev); write_or_die(fd, note_template, strlen(note_template)); @@ -125,6 +126,20 @@ static void create_note(const unsigned char *object, stripspace(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(buf, size + 1); + if (buf->len && prev_buf && size) + strbuf_insert(buf, 0, "\n", 1); + if (prev_buf && size) + strbuf_insert(buf, 0, prev_buf, size); + free(prev_buf); + } + if (!buf->len) { fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object)); @@ -212,8 +227,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix) const char *object_ref; char logmsg[100]; - int list = 0, add = 0, edit = 0, show = 0, remove = 0, prune = 0, - force = 0; + int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0, + prune = 0, force = 0; int given_object; const char *msgfile = NULL; struct msg_arg msg = { 0, STRBUF_INIT }; @@ -234,6 +249,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix) list = 1; else if (argc && !strcmp(argv[0], "add")) add = 1; + else if (argc && !strcmp(argv[0], "append")) + append = 1; else if (argc && !strcmp(argv[0], "edit")) edit = 1; else if (argc && !strcmp(argv[0], "show")) @@ -245,10 +262,10 @@ int cmd_notes(int argc, const char **argv, const char *prefix) else if (!argc) list = 1; /* Default to 'list' if no other subcommand given */ - if (list + add + edit + show + remove + prune != 1) + if (list + add + append + edit + show + remove + prune != 1) usage_with_options(git_notes_usage, options); - if ((msg.given || msgfile) && !(add || edit)) { + if ((msg.given || msgfile) && !(add || append || edit)) { error("cannot use -m/-F options with %s subcommand.", argv[0]); usage_with_options(git_notes_usage, options); } @@ -304,7 +321,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix) return execv_git_cmd(show_args); } - /* add/edit/remove/prune command */ + /* add/append/edit/remove/prune command */ if (add && note) { if (force) @@ -334,8 +351,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix) hashclr(new_note); prune_notes(t); } else { - create_note(object, &buf, msg.given || msgfile || remove, note, - new_note); + create_note(object, &buf, msg.given || msgfile || remove, + append, note, new_note); if (is_null_sha1(new_note)) remove_note(t, object); else |