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

notes: fix minimum number of parameters to "copy" subcommand

The builtin/notes.c::copy() function is prepared to handle either one or two arguments given from the command line; when one argument is given, to-obj defaults to HEAD. bbb1b8a3 ("notes: check number of parameters to "git notes copy"", 2010-06-28) tried to make sure "git notes copy" (with *no* other argument) does not dereference NULL by checking the number of parameters, but it incorrectly insisted that we need two arguments, instead of either one or two. This disabled the defaulting to-obj to HEAD. Correct it. Signed-off-by: Doan Tran Cong Danh <congdanhqx@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Doan Tran Cong Danh committed Oct 16, 2019 at 12:18 UTC d58deb9c4e151d4d8380cd14223391ce0d58f588
3 files changed +42 -6
Documentation/git-notes.txt
+3 -3
@@ -10,7 +10,7 @@ SYNOPSIS
10 [verse]
11 'git notes' [list [<object>]]
12 'git notes' add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
13 -'git notes' copy [-f] ( --stdin | <from-object> <to-object> )
13 +'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] )
14 'git notes' append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
15 'git notes' edit [--allow-empty] [<object>]
16 'git notes' show [<object>]
@@ -68,8 +68,8 @@ add::
68 subcommand).
69
70 copy::
71 - Copy the notes for the first object onto the second object.
72 - Abort if the second object already has notes, or if the first
71 + Copy the notes for the first object onto the second object (defaults to
72 + HEAD). Abort if the second object already has notes, or if the first
73 object has none (use -f to overwrite existing notes to the
74 second object). This subcommand is equivalent to:
75 `git notes add [-f] -C $(git notes list <from-object>) <to-object>`
builtin/notes.c
+1 -1
@@ -513,7 +513,7 @@ static int copy(int argc, const char **argv, const char *prefix)
513 }
514 }
515
516 - if (argc < 2) {
516 + if (argc < 1) {
517 error(_("too few parameters"));
518 usage_with_options(git_notes_copy_usage, options);
519 }
t/t3301-notes.sh
+38 -2
@@ -864,6 +864,24 @@ test_expect_success 'append to note from other note with "git notes append -c"'
864 '
865
866 test_expect_success 'copy note with "git notes copy"' '
867 + commit=$(git rev-parse 4th) &&
868 + cat >expect <<-EOF &&
869 + commit $commit
870 + Author: A U Thor <author@example.com>
871 + Date: Thu Apr 7 15:16:13 2005 -0700
872 +
873 + ${indent}4th
874 +
875 + Notes:
876 + ${indent}This is a blob object
877 + EOF
878 + git notes copy 8th 4th &&
879 + git log 3rd..4th >actual &&
880 + test_cmp expect actual &&
881 + test "$(git note list 4th)" = "$(git note list 8th)"
882 +'
883 +
884 +test_expect_success 'copy note with "git notes copy" with default' '
885 test_commit 11th &&
886 commit=$(git rev-parse HEAD) &&
887 cat >expect <<-EOF &&
@@ -878,7 +896,7 @@ test_expect_success 'copy note with "git notes copy"' '
896 ${indent}
897 ${indent}yet another note
898 EOF
881 - git notes copy HEAD^ HEAD &&
899 + git notes copy HEAD^ &&
900 git log -1 >actual &&
901 test_cmp expect actual &&
902 test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
@@ -892,6 +910,24 @@ test_expect_success 'prevent overwrite with "git notes copy"' '
910 '
911
912 test_expect_success 'allow overwrite with "git notes copy -f"' '
913 + commit=$(git rev-parse HEAD) &&
914 + cat >expect <<-EOF &&
915 + commit $commit
916 + Author: A U Thor <author@example.com>
917 + Date: Thu Apr 7 15:23:13 2005 -0700
918 +
919 + ${indent}11th
920 +
921 + Notes:
922 + ${indent}This is a blob object
923 + EOF
924 + git notes copy -f HEAD~3 HEAD &&
925 + git log -1 >actual &&
926 + test_cmp expect actual &&
927 + test "$(git notes list HEAD)" = "$(git notes list HEAD~3)"
928 +'
929 +
930 +test_expect_success 'allow overwrite with "git notes copy -f" with default' '
931 commit=$(git rev-parse HEAD) &&
932 cat >expect <<-EOF &&
933 commit $commit
@@ -905,7 +941,7 @@ test_expect_success 'allow overwrite with "git notes copy -f"' '
941 ${indent}
942 ${indent}yet another note
943 EOF
908 - git notes copy -f HEAD~2 HEAD &&
944 + git notes copy -f HEAD~2 &&
945 git log -1 >actual &&
946 test_cmp expect actual &&
947 test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"