@matheusbsbs / matheus-bagliano / commits / e73bbd96c6
Fork 0

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 <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>

Johan Herland committed Feb 13, 2010 at 22:28 UTC e73bbd96c6e9ce11a101dac03402d0f718a1bd23
3 files changed +102 -8
Documentation/git-notes.txt
+8
@@ -10,6 +10,7 @@ SYNOPSIS
10 [verse]
11 'git notes' [list [<object>]]
12 'git notes' add [-f] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
13 +'git notes' copy [-f] <from-object> <to-object>
14 'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15 'git notes' edit [<object>]
16 'git notes' show [<object>]
@@ -48,6 +49,13 @@ add::
49 object already has notes, abort. (use `-f` to overwrite an
50 existing note).
51
52 +copy::
53 + Copy the notes for the first object onto the second object.
54 + Abort if the second object already has notes, or if the first
55 + objects has none. (use -f to overwrite existing notes to the
56 + second object). This subcommand is equivalent to:
57 + `git notes add [-f] -C $(git notes list <from-object>) <to-object>`
58 +
59 append::
60 Append to the notes of an existing object (defaults to HEAD).
61 Creates a new notes object if needed.
builtin-notes.c
+31 -8
@@ -20,6 +20,7 @@
20 static const char * const git_notes_usage[] = {
21 "git notes [list [<object>]]",
22 "git notes add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
23 + "git notes copy [-f] <from-object> <to-object>",
24 "git notes append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
25 "git notes edit [<object>]",
26 "git notes show [<object>]",
@@ -280,13 +281,13 @@ int commit_notes(struct notes_tree *t, const char *msg)
281 int cmd_notes(int argc, const char **argv, const char *prefix)
282 {
283 struct notes_tree *t;
283 - unsigned char object[20], new_note[20];
284 + unsigned char object[20], from_obj[20], new_note[20];
285 const unsigned char *note;
286 const char *object_ref;
287 char logmsg[100];
288
288 - int list = 0, add = 0, append = 0, edit = 0, show = 0, remove = 0,
289 - prune = 0, force = 0;
289 + int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
290 + remove = 0, prune = 0, force = 0;
291 int given_object = 0, i = 1, retval = 0;
292 struct msg_arg msg = { 0, 0, STRBUF_INIT };
293 struct option options[] = {
@@ -311,6 +312,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
312 list = 1;
313 else if (argc && !strcmp(argv[0], "add"))
314 add = 1;
315 + else if (argc && !strcmp(argv[0], "copy"))
316 + copy = 1;
317 else if (argc && !strcmp(argv[0], "append"))
318 append = 1;
319 else if (argc && !strcmp(argv[0], "edit"))
@@ -326,7 +329,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
329 i = 0;
330 }
331
329 - if (list + add + append + edit + show + remove + prune != 1)
332 + if (list + add + copy + append + edit + show + remove + prune != 1)
333 usage_with_options(git_notes_usage, options);
334
335 if (msg.given && !(add || append || edit)) {
@@ -341,11 +344,22 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
344 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n");
345 }
346
344 - if (force && !add) {
347 + if (force && !(add || copy)) {
348 error("cannot use -f option with %s subcommand.", argv[0]);
349 usage_with_options(git_notes_usage, options);
350 }
351
352 + if (copy) {
353 + const char *from_ref;
354 + if (argc < 3) {
355 + error("too few parameters");
356 + usage_with_options(git_notes_usage, options);
357 + }
358 + from_ref = argv[i++];
359 + if (get_sha1(from_ref, from_obj))
360 + die("Failed to resolve '%s' as a valid ref.", from_ref);
361 + }
362 +
363 given_object = argc > i;
364 object_ref = given_object ? argv[i++] : "HEAD";
365
@@ -394,11 +408,11 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
408
409 /* add/append/edit/remove/prune command */
410
397 - if (add && note) {
411 + if ((add || copy) && note) {
412 if (!force) {
399 - error("Cannot add notes. Found existing notes for object"
413 + error("Cannot %s notes. Found existing notes for object"
414 " %s. Use '-f' to overwrite existing notes",
401 - sha1_to_hex(object));
415 + argv[0], sha1_to_hex(object));
416 retval = 1;
417 goto end;
418 }
@@ -416,6 +430,15 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
430 hashclr(new_note);
431 prune_notes(t);
432 goto commit;
433 + } else if (copy) {
434 + const unsigned char *from_note = get_note(t, from_obj);
435 + if (!from_note) {
436 + error("Missing notes on source object %s. Cannot copy.",
437 + sha1_to_hex(from_obj));
438 + retval = 1;
439 + goto end;
440 + }
441 + hashcpy(new_note, from_note);
442 } else
443 create_note(object, &msg, append, note, new_note);
444
t/t3301-notes.sh
+63
@@ -581,4 +581,67 @@ test_expect_success 'append to note from other note with "git notes append -c"'
581 test_cmp expect actual
582 '
583
584 +cat > expect << EOF
585 +commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
586 +Author: A U Thor <author@example.com>
587 +Date: Thu Apr 7 15:23:13 2005 -0700
588 +
589 + 11th
590 +
591 +Notes:
592 + other note
593 +$whitespace
594 + yet another note
595 +EOF
596 +
597 +test_expect_success 'copy note with "git notes copy"' '
598 + : > a11 &&
599 + git add a11 &&
600 + test_tick &&
601 + git commit -m 11th &&
602 + git notes copy HEAD^ HEAD &&
603 + git log -1 > actual &&
604 + test_cmp expect actual &&
605 + test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
606 +'
607 +
608 +test_expect_success 'prevent overwrite with "git notes copy"' '
609 + test_must_fail git notes copy HEAD~2 HEAD &&
610 + git log -1 > actual &&
611 + test_cmp expect actual &&
612 + test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
613 +'
614 +
615 +cat > expect << EOF
616 +commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
617 +Author: A U Thor <author@example.com>
618 +Date: Thu Apr 7 15:23:13 2005 -0700
619 +
620 + 11th
621 +
622 +Notes:
623 + yet another note
624 +$whitespace
625 + yet another note
626 +EOF
627 +
628 +test_expect_success 'allow overwrite with "git notes copy -f"' '
629 + git notes copy -f HEAD~2 HEAD &&
630 + git log -1 > actual &&
631 + test_cmp expect actual &&
632 + test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
633 +'
634 +
635 +test_expect_success 'cannot copy note from object without notes' '
636 + : > a12 &&
637 + git add a12 &&
638 + test_tick &&
639 + git commit -m 12th &&
640 + : > a13 &&
641 + git add a13 &&
642 + test_tick &&
643 + git commit -m 13th &&
644 + test_must_fail git notes copy HEAD^ HEAD
645 +'
646 +
647 test_done