Skip to contentSkip to conversation

Allow default value for target of git-notes copy

PATCH In review Doan Tran Cong Danh 2 participants 8 messages Oct 15, 2019 – Oct 18, 2019 Documentation

lore
Share & tools
lore
#0 lore raw

cover Allow default value for target of git-notes copy

As an alternate for the incoming patch,
this snippet could be used to eliminate some dead code in builtin/notes.c Since `argc' will always be 2 in the removed condition.

Also:

---

 builtin/notes.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
builtin/notes.c +2 -5
index 02e97f55c5..641ae66f75 100644
@@ -488,7 +488,6 @@ static int copy(int argc, const char **argv, const char *prefix)
 {
 	int retval = 0, force = 0, from_stdin = 0;
 	const struct object_id *from_note, *note;
-	const char *object_ref;
 	struct object_id object, from_obj;
 	struct notes_tree *t;
 	const char *rewrite_cmd = NULL;
@@ -525,10 +524,8 @@ static int copy(int argc, const char **argv, const char *prefix)
 	if (get_oid(argv[0], &from_obj))
 		die(_("failed to resolve '%s' as a valid ref."), argv[0]);
 
-	object_ref = 1 < argc ? argv[1] : "HEAD";
-
-	if (get_oid(object_ref, &object))
-		die(_("failed to resolve '%s' as a valid ref."), object_ref);
+	if (get_oid(argv[1], &object))
+		die(_("failed to resolve '%s' as a valid ref."), argv[1]);
 
 	t = init_notes_check("copy", NOTES_INIT_WRITABLE);
 	note = get_note(t, &object);
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/1 notes: copy notes to HEAD by default

The target objects for copying notes was defaulted to HEAD from very early stage of git-notes.

However, that default was limited by commit bbb1b8a35a, ("notes: check number of parameters to "git notes copy"", 2010-06-28).

Lift that limitation by adjust the check for numbers of arguments.

---
 Documentation/git-notes.txt | 6 +++---
 builtin/notes.c             | 2 +-
 t/t3301-notes.sh            | 4 ++++
 3 files changed, 8 insertions(+), 4 deletions(-)
Documentation/git-notes.txt +3 -3
index f56a5a9197..ced2e8280e 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 [verse]
 'git notes' [list [<object>]]
 'git notes' add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
-'git notes' copy [-f] ( --stdin | <from-object> <to-object> )
+'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] )
 'git notes' append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
 'git notes' edit [--allow-empty] [<object>]
 'git notes' show [<object>]
@@ -68,8 +68,8 @@ add::
 	subcommand).
 
 copy::
-	Copy the notes for the first object onto the second object.
-	Abort if the second object already has notes, or if the first
+	Copy the notes for the first object onto the second object (defaults to
+	HEAD). Abort if the second object already has notes, or if the first
 	object 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>`
builtin/notes.c +1 -1
index 02e97f55c5..95456f3165 100644
@@ -513,7 +513,7 @@ static int copy(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (argc < 2) {
+	if (argc < 1) {
 		error(_("too few parameters"));
 		usage_with_options(git_notes_copy_usage, options);
 	}
t/t3301-notes.sh +4 -0
index d3fa298c6a..a8f9a0f36c 100755
@@ -908,6 +908,10 @@ 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)" &&
+	git notes copy -f HEAD~2 &&
+	git log -1 >actual &&
+	test_cmp expect actual &&
 	test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
 '
 
Signature · 1 line
2.23.0
#2 lore raw

Re: [PATCH 1/1] notes: copy notes to HEAD by default

Quoted 5 lines · Doan Tran Cong Danh

The target objects for copying notes was defaulted to HEAD from very early stage of git-notes.

However, that default was limited by commit bbb1b8a35a, ("notes: check number of parameters to "git notes copy"", 2010-06-28).

Sorry, I don't quite get the above. The said commit made sure 'git notes copy' gets the right number of arguments, saying """Otherwise we may segfault with too few parameters.""" I take that as a sign that before that commit it was not defaulting to HEAD but attempting to access the missing argv[2] (or whatever the index the <to-object> should be at) and dereferencing a NULL?

    ... goes and digs ...

I think v1.6.6.1-458-g74884b524e is the commit that made the command line parsing into the current shape, i.e. one parse_options() call in each of the subcommand that gets dispatched, and you are right that with that version a single argument given on the command line is taken as the <from-object> and <to-object> defaults to HEAD.

So... what happend between that vesrion and v1.7.1-200-gbbb1b8a35a?

... goes and looks at bbb1b8a35a again ...

Ah, I think there is an off-by-one. When not from-stdin and not using rewrite-cmd, before that patch, we did not even check if from-obj exists, so in that sense, the commit had a right idea that it must check for "too few parameters", but it shouldn't have insisted that we have at least two. It is OK to have just one, i.e. only the from-obj, for our purpose.

Review

Quoted 8 lines

copy::

(quoted diff) +2 -2
-	Copy the notes for the first object onto the second object.
-	Abort if the second object already has notes, or if the first
+	Copy the notes for the first object onto the second object (defaults to
+	HEAD). Abort if the second object already has notes, or if the first
 	object 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>`

This is OK.

builtin/notes.c static int copy(int argc, const char **argv, const char *prefix) · line 519

Quoted 13 lines
builtin/notes.c +1 -1
index 02e97f55c5..95456f3165 100644
@@ -513,7 +513,7 @@ static int copy(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (argc < 2) {
+	if (argc < 1) {
 		error(_("too few parameters"));
 		usage_with_options(git_notes_copy_usage, options);
 	}

So is this.

Quoted 5 lines
t/t3301-notes.sh +0 -0
index d3fa298c6a..a8f9a0f36c 100755

t/t3301-notes.sh test_expect_success 'allow overwrite with "git notes copy -f"' ' · line 916

Quoted 9 lines
t/t3301-notes.sh +4 -0
@@ -908,6 +908,10 @@ 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)" &&
+	git notes copy -f HEAD~2 &&
+	git log -1 >actual &&
+	test_cmp expect actual &&
 	test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
 '

This I am not sure is a good test to add to, especially as a fix to bbb1b8a, which added this test:

t/t3301-notes.sh +6 -0
index 64f32ad94d..2d67a40fc1 100755
@@ -1044,4 +1044,10 @@ test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
 	git log -1 > output &&
 	test_cmp expect output
 '
+
+test_expect_success 'git notes copy diagnoses too many or too few parameters' '
+	test_must_fail git notes copy &&
+	test_must_fail git notes copy one two three
+'
+
 test_done

The lack of testing that "git notes copy <from-obj>" succeeding is why the off-by-one bug was not noticed, so I think that test (which still exists to this day) is the right place to add a test to protect this fix.

As to the log message, here is how I would explain/justify the change, if I were writing it.

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

The builtin/notes.::copy() function is prepared to handle only one argument given from the command line; in such a case, to-obj defaults to HEAD.

When 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 making sure we did not get too few parameters, it incorrectly insisted that we get two arguments, instead of just one. This disabled the defaulting to-obj to HEAD. Correct it.

Thanks.

author
#3 lore raw

Re: [PATCH 1/1] notes: copy notes to HEAD by default

Quoted 25 lines · Junio C Hamano
Quoted 5 lines · Doan Tran Cong Danh

The target objects for copying notes was defaulted to HEAD from very early stage of git-notes.

However, that default was limited by commit bbb1b8a35a, ("notes: check number of parameters to "git notes copy"", 2010-06-28).

Sorry, I don't quite get the above. The said commit made sure 'git notes copy' gets the right number of arguments, saying """Otherwise we may segfault with too few parameters.""" I take that as a sign that before that commit it was not defaulting to HEAD but attempting to access the missing argv[2] (or whatever the index the <to-object> should be at) and dereferencing a NULL?

    ... goes and digs ...

I think v1.6.6.1-458-g74884b524e is the commit that made the command line parsing into the current shape, i.e. one parse_options() call in each of the subcommand that gets dispatched, and you are right that with that version a single argument given on the command line is taken as the <from-object> and <to-object> defaults to HEAD.

So... what happend between that vesrion and v1.7.1-200-gbbb1b8a35a?

Quoted 8 lines

... goes and looks at bbb1b8a35a again ...

Ah, I think there is an off-by-one. When not from-stdin and not using rewrite-cmd, before that patch, we did not even check if from-obj exists, so in that sense, the commit had a right idea that it must check for "too few parameters", but it shouldn't have insisted that we have at least two. It is OK to have just one, i.e. only the from-obj, for our purpose.

Yes, this is my intention.

Quoted 8 lines
Quoted 8 lines
t/t3301-notes.sh +0 -0
index d3fa298c6a..a8f9a0f36c 100755
@@ -908,6 +908,10 @@ 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 &&
Quoted 8 lines

t/t3301-notes.sh test_expect_success 'allow overwrite with "git notes copy -f"' ' · line 916

Quoted 6 lines
t/t3301-notes.sh +4 -0
+	test "$(git notes list HEAD)" = "$(git notes list HEAD~2)" &&
+	git notes copy -f HEAD~2 &&
+	git log -1 >actual &&
+	test_cmp expect actual &&
 	test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
 '

This I am not sure is a good test to add to, especially as a fix to

I was writing this patch just before my bed time, just to get some comments on the directions, e.g:
- loosen the argc requirements; or
- do the code cleanup

Quoted 18 lines

bbb1b8a, which added this test:

t/t3301-notes.sh +6 -0
index 64f32ad94d..2d67a40fc1 100755
@@ -1044,4 +1044,10 @@ test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
 	git log -1 > output &&
 	test_cmp expect output
 '
+
+test_expect_success 'git notes copy diagnoses too many or too few parameters' '
+	test_must_fail git notes copy &&
+	test_must_fail git notes copy one two three
+'
+
 test_done
Quoted 4 lines

The lack of testing that "git notes copy <from-obj>" succeeding is why the off-by-one bug was not noticed, so I think that test (which still exists to this day) is the right place to add a test to protect this fix.

I don't think this is a good place to add this test either, since the test description specificaly said it diagnoses too many or too few parameters.

Anyway, the test git notes copy one two three still fails if we accidentally allow 3 arguments since one two three isn't valid ref. I'm gonna add a statement to assert the diagnose message.

Since I don't want to update the commit id (e.g. 10th, 11th, etc..) of other test cases, I think it'd be better to modify the current test:

- for the test case without '-f' flag, remove HEAD if it's the target, and add a test-case to copy to somewhere else. Well, all of our current test cases only test with HEAD as target-object.
- for the test case with '-f' flag, I think I'll keep my current approach.

Quoted 3 lines

As to the log message, here is how I would explain/justify the change, if I were writing it.

I'll update the commit message.

Signature · 1 line
Danh
#4 lore raw

PATCH 1/2 t3301: test diagnose messages for too few/many paramters

In reply to Doan Tran Cong Danh

From commit bbb1b8a35a, ("notes: check number of parameters to "git notes copy"", 2010-06-28), we have a test for too many or too few of parameters provided to `git notes copy'.

However, the test is only ensure the command will fail but it doesn't really check if it fails because of number of parameters.

If we accidentally lifted the check inside our code base, the test may still failed because the provided parameter is not a valid ref.

Correct it.

---
 t/t3301-notes.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
t/t3301-notes.sh +4 -2
index d3fa298c6a..d7767e4438 100755
@@ -1167,8 +1167,10 @@ test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
 '
 
 test_expect_success 'git notes copy diagnoses too many or too few parameters' '
-	test_must_fail git notes copy &&
-	test_must_fail git notes copy one two three
+	test_must_fail git notes copy 2>error &&
+	test_i18ngrep "too few parameters" error &&
+	test_must_fail git notes copy one two three 2>error &&
+	test_i18ngrep "too many parameters" error
 '
 
 test_expect_success 'git notes get-ref expands refs/heads/master to refs/notes/refs/heads/master' '
Signature · 1 line
2.23.0
#5 lore raw

PATCH 2/2 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;
in case of one argument is given, to-obj defaults to HEAD.

When 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 making sure we did not get too few parameters, it incorrectly insisted that we need two arguments, instead of either one or two.
This disabled the defaulting to-obj to HEAD.
Correct it.

---
 Documentation/git-notes.txt |  6 +++---
 builtin/notes.c             |  2 +-
 t/t3301-notes.sh            | 40 +++++++++++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 6 deletions(-)
Documentation/git-notes.txt +3 -3
index f56a5a9197..ced2e8280e 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 [verse]
 'git notes' [list [<object>]]
 'git notes' add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
-'git notes' copy [-f] ( --stdin | <from-object> <to-object> )
+'git notes' copy [-f] ( --stdin | <from-object> [<to-object>] )
 'git notes' append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
 'git notes' edit [--allow-empty] [<object>]
 'git notes' show [<object>]
@@ -68,8 +68,8 @@ add::
 	subcommand).
 
 copy::
-	Copy the notes for the first object onto the second object.
-	Abort if the second object already has notes, or if the first
+	Copy the notes for the first object onto the second object (defaults to
+	HEAD). Abort if the second object already has notes, or if the first
 	object 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>`
builtin/notes.c +1 -1
index 02e97f55c5..95456f3165 100644
@@ -513,7 +513,7 @@ static int copy(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (argc < 2) {
+	if (argc < 1) {
 		error(_("too few parameters"));
 		usage_with_options(git_notes_copy_usage, options);
 	}
t/t3301-notes.sh +38 -2
index d7767e4438..d66a5f6faa 100755
@@ -864,6 +864,24 @@ test_expect_success 'append to note from other note with "git notes append -c"'
 '
 
 test_expect_success 'copy note with "git notes copy"' '
+	commit=$(git rev-parse 4th) &&
+	cat >expect <<-EOF &&
+		commit $commit
+		Author: A U Thor <[email protected]>
+		Date:   Thu Apr 7 15:16:13 2005 -0700
+
+		${indent}4th
+
+		Notes:
+		${indent}This is a blob object
+	EOF
+	git notes copy 8th 4th &&
+	git log 3rd..4th >actual &&
+	test_cmp expect actual &&
+	test "$(git note list 4th)" = "$(git note list 8th)"
+'
+
+test_expect_success 'copy note with "git notes copy" with default' '
 	test_commit 11th &&
 	commit=$(git rev-parse HEAD) &&
 	cat >expect <<-EOF &&
@@ -878,7 +896,7 @@ test_expect_success 'copy note with "git notes copy"' '
 		${indent}
 		${indent}yet another note
 	EOF
-	git notes copy HEAD^ HEAD &&
+	git notes copy HEAD^ &&
 	git log -1 >actual &&
 	test_cmp expect actual &&
 	test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
@@ -892,6 +910,24 @@ test_expect_success 'prevent overwrite with "git notes copy"' '
 '
 
 test_expect_success 'allow overwrite with "git notes copy -f"' '
+	commit=$(git rev-parse HEAD) &&
+	cat >expect <<-EOF &&
+		commit $commit
+		Author: A U Thor <[email protected]>
+		Date:   Thu Apr 7 15:23:13 2005 -0700
+
+		${indent}11th
+
+		Notes:
+		${indent}This is a blob object
+	EOF
+	git notes copy -f HEAD~3 HEAD &&
+	git log -1 >actual &&
+	test_cmp expect actual &&
+	test "$(git notes list HEAD)" = "$(git notes list HEAD~3)"
+'
+
+test_expect_success 'allow overwrite with "git notes copy -f" with default' '
 	commit=$(git rev-parse HEAD) &&
 	cat >expect <<-EOF &&
 		commit $commit
@@ -905,7 +941,7 @@ test_expect_success 'allow overwrite with "git notes copy -f"' '
 		${indent}
 		${indent}yet another note
 	EOF
-	git notes copy -f HEAD~2 HEAD &&
+	git notes copy -f HEAD~2 &&
 	git log -1 >actual &&
 	test_cmp expect actual &&
 	test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
Signature · 1 line
2.23.0
#6 lore raw

Re: [PATCH 2/2] notes: fix minimum number of parameters to "copy" subcommand

Quoted 4 lines · Doan Tran Cong Danh
 Documentation/git-notes.txt |  6 +++---
 builtin/notes.c             |  2 +-
 t/t3301-notes.sh            | 40 +++++++++++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 6 deletions(-)

Thanks, makes sense.

#7 lore raw

Re: [PATCH 1/2] t3301: test diagnose messages for too few/many paramters

In reply to Doan Tran Cong Danh
Quoted 2 lines · Doan Tran Cong Danh

If we accidentally lifted the check inside our code base, the test may still failed because the provided parameter is not a valid ref.

Makes sense. Another option would be to use a valid ref to make sure there are no other possible reason for the command to fail, which would make the test robust against us accidentally switching the order of the check to see if they are refs first and then complain about too many or too few arguments ;-) But I think what we have here is fine as-is (I'll copy-edit the proposed log message for grammos, though).

Thanks.

Quoted 25 lines

Correct it.

---
 t/t3301-notes.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
t/t3301-notes.sh +4 -2
index d3fa298c6a..d7767e4438 100755
@@ -1167,8 +1167,10 @@ test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
 '
 
 test_expect_success 'git notes copy diagnoses too many or too few parameters' '
-	test_must_fail git notes copy &&
-	test_must_fail git notes copy one two three
+	test_must_fail git notes copy 2>error &&
+	test_i18ngrep "too few parameters" error &&
+	test_must_fail git notes copy one two three 2>error &&
+	test_i18ngrep "too many parameters" error
 '
 
 test_expect_success 'git notes get-ref expands refs/heads/master to refs/notes/refs/heads/master' '