git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Herland <johan@herland.net>
To: gitster@pobox.com
Cc: git@vger.kernel.org, johan@herland.net
Subject: [PATCHv13 25/30] builtin-notes: Add "append" subcommand for appending to note objects
Date: Sat, 13 Feb 2010 22:28:33 +0100	[thread overview]
Message-ID: <1266096518-2104-26-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1266096518-2104-1-git-send-email-johan@herland.net>

"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>
---
 Documentation/git-notes.txt |    5 +++++
 builtin-notes.c             |   35 ++++++++++++++++++++++++++---------
 t/t3301-notes.sh            |   36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index 94e12b5..35dd8fa 100644
--- a/Documentation/git-notes.txt
+++ b/Documentation/git-notes.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git notes' [list [<object>]]
 'git notes' add [-f] [-F <file> | -m <msg>] [<object>]
+'git notes' append [-F <file> | -m <msg>] [<object>]
 'git notes' edit [-F <file> | -m <msg>] [<object>]
 'git notes' show [<object>]
 'git notes' remove [<object>]
@@ -47,6 +48,10 @@ add::
 	object already has notes, abort. (use `-f` to overwrite an
 	existing note).
 
+append::
+	Append to the notes of an existing object (defaults to HEAD).
+	Creates a new notes object if needed.
+
 edit::
 	Edit the notes for a given object (defaults to HEAD).
 
diff --git a/builtin-notes.c b/builtin-notes.c
index 006edf6..c88df00 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
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index df458ca..290ed63 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -343,6 +343,42 @@ test_expect_success 'listing non-existing notes fails' '
 	test_cmp expect output
 '
 
+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"' '
+	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
+'
+
+test_expect_success 'appending empty string does not change existing note' '
+	git notes append -m "" &&
+	git notes show > output &&
+	test_cmp expect output
+'
+
+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
+'
+
+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 "" &&
+	test_must_fail git notes list HEAD
+'
+
 test_expect_success 'create other note on a different notes ref (setup)' '
 	: > a6 &&
 	git add a6 &&
-- 
1.7.0.rc1.141.gd3fd

  parent reply	other threads:[~2010-02-13 21:31 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-13 21:28 [PATCHv13 00/30] git notes Johan Herland
2010-02-13 21:28 ` [PATCHv13 01/30] Minor cosmetic fixes to notes.c Johan Herland
2010-02-13 21:28 ` [PATCHv13 02/30] Notes API: get_commit_notes() -> format_note() + remove the commit restriction Johan Herland
2010-02-13 21:28 ` [PATCHv13 03/30] Add tests for checking correct handling of $GIT_NOTES_REF and core.notesRef Johan Herland
2010-02-13 21:28 ` [PATCHv13 04/30] Notes API: init_notes(): Initialize the notes tree from the given notes ref Johan Herland
2010-02-13 21:28 ` [PATCHv13 05/30] Notes API: add_note(): Add note objects to the internal notes tree structure Johan Herland
2010-02-13 21:28 ` [PATCHv13 06/30] Notes API: remove_note(): Remove note objects from the " Johan Herland
2010-02-13 21:28 ` [PATCHv13 07/30] Notes API: get_note(): Return the note annotating the given object Johan Herland
2010-02-13 21:28 ` [PATCHv13 08/30] Notes API: for_each_note(): Traverse the entire notes tree with a callback Johan Herland
2010-02-13 21:28 ` [PATCHv13 09/30] Notes API: write_notes_tree(): Store the notes tree in the database Johan Herland
2010-02-13 21:28 ` [PATCHv13 10/30] Notes API: Allow multiple concurrent notes trees with new struct notes_tree Johan Herland
2010-02-13 21:28 ` [PATCHv13 11/30] Refactor notes concatenation into a flexible interface for combining notes Johan Herland
2010-02-13 21:28 ` [PATCHv13 12/30] Builtin-ify git-notes Johan Herland
2010-02-13 21:28 ` [PATCHv13 13/30] t3301: Verify successful annotation of non-commits Johan Herland
2010-02-13 21:28 ` [PATCHv13 14/30] t3305: Verify that adding many notes with git-notes triggers increased fanout Johan Herland
2010-02-13 21:28 ` [PATCHv13 15/30] Teach notes code to properly preserve non-notes in the notes tree Johan Herland
2010-02-13 21:28 ` [PATCHv13 16/30] Teach builtin-notes to remove empty notes Johan Herland
2010-02-13 21:28 ` [PATCHv13 17/30] builtin-notes: Add "remove" subcommand for removing existing notes Johan Herland
2010-02-13 21:28 ` [PATCHv13 18/30] t3305: Verify that removing notes triggers automatic fanout consolidation Johan Herland
2010-02-13 21:28 ` [PATCHv13 19/30] Notes API: prune_notes(): Prune notes that belong to non-existing objects Johan Herland
2010-02-13 21:28 ` [PATCHv13 20/30] builtin-notes: Add "prune" subcommand for removing notes for missing objects Johan Herland
2010-02-13 21:28 ` [PATCHv13 21/30] Documentation: Generalize git-notes docs to 'objects' instead of 'commits' Johan Herland
2010-02-13 21:28 ` [PATCHv13 22/30] builtin-notes: Add "list" subcommand for listing note objects Johan Herland
2010-02-13 21:28 ` [PATCHv13 23/30] builtin-notes: Add --message/--file aliases for -m/-F options Johan Herland
2010-02-13 21:28 ` [PATCHv13 24/30] builtin-notes: Add "add" subcommand for adding notes to objects Johan Herland
2010-02-13 21:28 ` Johan Herland [this message]
2010-02-13 21:28 ` [PATCHv13 26/30] builtin-notes: Deprecate the -m/-F options for "git notes edit" Johan Herland
2010-02-13 21:28 ` [PATCHv13 27/30] builtin-notes: Refactor handling of -F option to allow combining -m and -F Johan Herland
2010-02-13 21:28 ` [PATCHv13 28/30] builtin-notes: Add -c/-C options for reusing notes Johan Herland
2010-02-15 10:34   ` Stephen Boyd
2010-02-16  1:47     ` Johan Herland
2010-02-25  0:48       ` [PATCH] builtin-notes: Minor (mostly parse_options-related) fixes Johan Herland
2010-02-25  3:07         ` Stephen Boyd
2010-02-13 21:28 ` [PATCHv13 29/30] builtin-notes: Misc. refactoring of argc and exit value handling Johan Herland
2010-02-13 21:28 ` [PATCHv13 30/30] builtin-notes: Add "copy" subcommand for copying notes between objects Johan Herland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1266096518-2104-26-git-send-email-johan@herland.net \
    --to=johan@herland.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).