summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 5848769)
raw | patch | inline | side by side (parent: 5848769)
author | Johan Herland <johan@herland.net> | |
Sat, 13 Feb 2010 21:28:38 +0000 (22:28 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 14 Feb 2010 03:36:17 +0000 (19:36 -0800) |
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
'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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-notes.txt | patch | blob | history | |
builtin-notes.c | patch | blob | history | |
t/t3301-notes.sh | patch | blob | history |
index 15de4b344a093354df1332c6e037e8f29508c8d5..14f73b988e556ecce1bea8a1dacf7284966832ae 100644 (file)
[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>]
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 bbf98a9f6fdc44674dd5c2fd7d88c8156a05f3df..123ecad830007bbf6df3e69442ce04abace7ad8e 100644 (file)
--- a/builtin-notes.c
+++ b/builtin-notes.c
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>]",
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[] = {
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"))
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)) {
"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";
/* 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;
}
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 6447e5f54e45414089677d6bcff9738538e47e15..90178f96d29597a886da1f64f95176116294554a 100755 (executable)
--- 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