From e73bbd96c6e9ce11a101dac03402d0f718a1bd23 Mon Sep 17 00:00:00 2001 From: Johan Herland Date: Sat, 13 Feb 2010 22:28:38 +0100 Subject: builtin-notes: Add "copy" subcommand for copying notes between objects 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 " is in practice equivalent to "git notes add -C $(git notes list ) ", 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 -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 Signed-off-by: Junio C Hamano --- builtin-notes.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'builtin-notes.c') diff --git a/builtin-notes.c b/builtin-notes.c index bbf98a9f6f..123ecad830 100644 --- a/builtin-notes.c +++ b/builtin-notes.c @@ -20,6 +20,7 @@ static const char * const git_notes_usage[] = { "git notes [list []]", "git notes add [-f] [-m | -F | (-c | -C) ] []", + "git notes copy [-f] ", "git notes append [-m | -F | (-c | -C) ] []", "git notes edit []", "git notes show []", @@ -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); -- cgit v1.2.3