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 30/30] builtin-notes: Add "copy" subcommand for copying notes between objects
Date: Sat, 13 Feb 2010 22:28:38 +0100	[thread overview]
Message-ID: <1266096518-2104-31-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1266096518-2104-1-git-send-email-johan@herland.net>

This is useful for keeping notes to objects that are being rewritten by e.g.
'git commit --amend', 'git rebase', or 'git cherry-pick'.

"git notes copy <from> <to>" is in practice equivalent to
"git notes add -C $(git notes list <from>) <to>", although it is somewhat
more convenient for regular users.

"git notes copy" takes the same -f option as "git add", to overwrite existing
notes at the target (instead of aborting with an error message).

If the <from>-object has no notes, "git notes copy" will abort with an error
message.

The patch includes tests verifying correct behaviour of the new subcommand.

Signed-off-by: Johan Herland <johan@herland.net>
---
 Documentation/git-notes.txt |    8 +++++
 builtin-notes.c             |   39 +++++++++++++++++++++-----
 t/t3301-notes.sh            |   63 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 8 deletions(-)

diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index 15de4b3..14f73b9 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> | (-c | -C) <object>] [<object>]
+'git notes' copy [-f] <from-object> <to-object>
 'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
 'git notes' edit [<object>]
 'git notes' show [<object>]
@@ -48,6 +49,13 @@ add::
 	object already has notes, abort. (use `-f` to overwrite an
 	existing note).
 
+copy::
+	Copy the notes for the first object onto the second object.
+	Abort if the second object already has notes, or if the first
+	objects has none. (use -f to overwrite existing notes to the
+	second object). This subcommand is equivalent to:
+	`git notes add [-f] -C $(git notes list <from-object>) <to-object>`
+
 append::
 	Append to the notes of an existing object (defaults to HEAD).
 	Creates a new notes object if needed.
diff --git a/builtin-notes.c b/builtin-notes.c
index bbf98a9..123ecad 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> | (-c | -C) <object>] [<object>]",
+	"git notes copy [-f] <from-object> <to-object>",
 	"git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
 	"git notes edit [<object>]",
 	"git notes show [<object>]",
@@ -280,13 +281,13 @@ int commit_notes(struct notes_tree *t, const char *msg)
 int cmd_notes(int argc, const char **argv, const char *prefix)
 {
 	struct notes_tree *t;
-	unsigned char object[20], new_note[20];
+	unsigned char object[20], from_obj[20], new_note[20];
 	const unsigned char *note;
 	const char *object_ref;
 	char logmsg[100];
 
-	int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
-	    prune = 0, force = 0;
+	int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
+	    remove = 0, prune = 0, force = 0;
 	int given_object = 0, i = 1, retval = 0;
 	struct msg_arg msg = { 0, 0, STRBUF_INIT };
 	struct option options[] = {
@@ -311,6 +312,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], "copy"))
+		copy = 1;
 	else if (argc && !strcmp(argv[0], "append"))
 		append = 1;
 	else if (argc && !strcmp(argv[0], "edit"))
@@ -326,7 +329,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 		i = 0;
 	}
 
-	if (list + add + append + edit + show + remove + prune != 1)
+	if (list + add + copy + append + edit + show + remove + prune != 1)
 		usage_with_options(git_notes_usage, options);
 
 	if (msg.given && !(add || append || edit)) {
@@ -341,11 +344,22 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 			"Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
 	}
 
-	if (force && !add) {
+	if (force && !(add || copy)) {
 		error("cannot use -f option with %s subcommand.", argv[0]);
 		usage_with_options(git_notes_usage, options);
 	}
 
+	if (copy) {
+		const char *from_ref;
+		if (argc < 3) {
+			error("too few parameters");
+			usage_with_options(git_notes_usage, options);
+		}
+		from_ref = argv[i++];
+		if (get_sha1(from_ref, from_obj))
+			die("Failed to resolve '%s' as a valid ref.", from_ref);
+	}
+
 	given_object = argc > i;
 	object_ref = given_object ? argv[i++] : "HEAD";
 
@@ -394,11 +408,11 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 
 	/* add/append/edit/remove/prune command */
 
-	if (add && note) {
+	if ((add || copy) && note) {
 		if (!force) {
-			error("Cannot add notes. Found existing notes for object"
+			error("Cannot %s notes. Found existing notes for object"
 			      " %s. Use '-f' to overwrite existing notes",
-			      sha1_to_hex(object));
+			      argv[0], sha1_to_hex(object));
 			retval = 1;
 			goto end;
 		}
@@ -416,6 +430,15 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 		hashclr(new_note);
 		prune_notes(t);
 		goto commit;
+	} else if (copy) {
+		const unsigned char *from_note = get_note(t, from_obj);
+		if (!from_note) {
+			error("Missing notes on source object %s. Cannot copy.",
+			      sha1_to_hex(from_obj));
+			retval = 1;
+			goto end;
+		}
+		hashcpy(new_note, from_note);
 	} else
 		create_note(object, &msg, append, note, new_note);
 
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 6447e5f..90178f9 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -581,4 +581,67 @@ test_expect_success 'append to note from other note with "git notes append -c"'
 	test_cmp expect actual
 '
 
+cat > expect << EOF
+commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:23:13 2005 -0700
+
+    11th
+
+Notes:
+    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 &&
+	git notes copy HEAD^ HEAD &&
+	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 &&
+	test_cmp expect actual &&
+	test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
+'
+
+cat > expect << EOF
+commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:23:13 2005 -0700
+
+    11th
+
+Notes:
+    yet another note
+$whitespace
+    yet another note
+EOF
+
+test_expect_success 'allow overwrite with "git notes copy -f"' '
+	git notes copy -f HEAD~2 HEAD &&
+	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_must_fail git notes copy HEAD^ HEAD
+'
+
 test_done
-- 
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 ` [PATCHv13 25/30] builtin-notes: Add "append" subcommand for appending to note objects Johan Herland
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 ` Johan Herland [this message]

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-31-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).