threads / patch / 31287

patch, 2 partst6300: test sort with multiple keys

Subject: [PATCH 1/2] t6300: test sort with multiple keys

## tl;dr

8 messages between Aug 19, 2012 and Aug 21, 2012. Diffs are folded; open one to read it.

replies: 7people: 2as markdown or json

Kacper Kornet· Aug 19, 2012, 21:15 UTC · lore

Documentation of git-for-each-ref says that --sort=<key> option can be used multiple times, in which case the last key becomes the primary key. However this functionality was never checked in test suite and is currently broken. This commit adds appropriate test in preparation for fix.

Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
---
 t/t6300-for-each-ref.sh | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
Show changes to t/t6300-for-each-ref.sh +32 −1
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 1721784..3d59bfc 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -242,7 +242,32 @@ test_expect_success 'Verify descending sort' '
 	test_cmp expected actual
 '
 
+test_expect_success 'Create branches to test sort with multiple keys' '
+	git checkout -b Branch1 &&
+	echo foo >> one &&
+	git commit -a -m "Branch1 commit" &&
+	git checkout -b Branch2 &&
+	echo foo >> one &&
+	git commit -a -m "Branch2 commit"
+'
+
+test_atom refs/heads/Branch1 objectname 32fca05e9f638021a123a84226acf17756acc18b
+test_atom refs/heads/Branch2 objectname 194a5b89ac661a114566ba4374bc06c2797539f3
+
 cat >expected <<\EOF
+67a36f10722846e891fbada1ba48ed035de75581 commit	refs/heads/master
+194a5b89ac661a114566ba4374bc06c2797539f3 commit	refs/heads/Branch2
+32fca05e9f638021a123a84226acf17756acc18b commit	refs/heads/Branch1
+EOF
+
+test_expect_failure 'Verify sort with multiple keys' '
+	git for-each-ref --sort=objectname --sort=committerdate refs/heads > actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<\EOF
+'refs/heads/Branch1'
+'refs/heads/Branch2'
 'refs/heads/master'
 'refs/remotes/origin/master'
 'refs/tags/testtag'
@@ -264,6 +289,8 @@ test_expect_success 'Quoting style: python' '
 '
 
 cat >expected <<\EOF
+"refs/heads/Branch1"
+"refs/heads/Branch2"
 "refs/heads/master"
 "refs/remotes/origin/master"
 "refs/tags/testtag"
@@ -285,6 +312,8 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 done
 
 cat >expected <<\EOF
+Branch1
+Branch2
 master
 testtag
 EOF
@@ -296,6 +325,8 @@ test_expect_success 'Check short refname format' '
 '
 
 cat >expected <<EOF
+
+
 origin/master
 EOF
 
@@ -309,7 +340,7 @@ cat >expected <<EOF
 EOF
 
 test_expect_success 'Check short objectname format' '
-	git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
+	git for-each-ref --format="%(objectname:short)" refs/heads/master >actual &&
 	test_cmp expected actual
 '
 
-- 
1.7.12.rc3
Kacper Kornet· Aug 19, 2012, 21:15 UTC · re: Kacper Kornet · lore

[PATCH 2/2] for-each-ref: Fix sort with multiple keys

The linked list describing sort options was not correctly set up in opt_parse_sort. In the result, contrary to the documentation. only the last of multiple --sort options to git-for-each-ref was taken into account. This commit fixes it.

Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
---
 builtin/for-each-ref.c  | 4 +++-
 t/t6300-for-each-ref.sh | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
Show changes to 2 files +4 −2

builtin/for-each-ref.c, t/t6300-for-each-ref.sh

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index b01d76a..0c5294e 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -962,7 +962,9 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 	if (!arg) /* should --no-sort void the list ? */
 		return -1;
 
-	*sort_tail = s = xcalloc(1, sizeof(*s));
+	s = xcalloc(1, sizeof(*s));
+	s->next = *sort_tail;
+	*sort_tail = s;
 
 	if (*arg == '-') {
 		s->reverse = 1;
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 3d59bfc..4c5d8ba 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -260,7 +260,7 @@ cat >expected <<\EOF
 32fca05e9f638021a123a84226acf17756acc18b commit	refs/heads/Branch1
 EOF
 
-test_expect_failure 'Verify sort with multiple keys' '
+test_expect_success 'Verify sort with multiple keys' '
 	git for-each-ref --sort=objectname --sort=committerdate refs/heads > actual &&
 	test_cmp expected actual
 '
-- 
1.7.12.rc3
Junio C Hamano· Aug 20, 2012, 00:41 UTC · re: Kacper Kornet · lore

Re: [PATCH 2/2] for-each-ref: Fix sort with multiple keys

Kacper Kornet <draenog@pld-linux.org> writes:
Show 23 quoted lines
> The linked list describing sort options was not correctly set up in
> opt_parse_sort. In the result, contrary to the documentation. only the
> last of multiple --sort options to git-for-each-ref was taken into
> account. This commit fixes it.
>
> Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
> ---
>  builtin/for-each-ref.c  | 4 +++-
>  t/t6300-for-each-ref.sh | 2 +-
>  2 files changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index b01d76a..0c5294e 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -962,7 +962,9 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
>  	if (!arg) /* should --no-sort void the list ? */
>  		return -1;
>  
> -	*sort_tail = s = xcalloc(1, sizeof(*s));
> +	s = xcalloc(1, sizeof(*s));
> +	s->next = *sort_tail;
> +	*sort_tail = s;
This fix looks correct.  
Junio C Hamano· Aug 20, 2012, 00:38 UTC · re: Kacper Kornet · lore

Re: [PATCH 1/2] t6300: test sort with multiple keys

Kacper Kornet <draenog@pld-linux.org> writes:
Show 7 quoted lines
> Documentation of git-for-each-ref says that --sort=<key> option can be
> used multiple times, in which case the last key becomes the primary key.
> However this functionality was never checked in test suite and is
> currently broken. This commit adds appropriate test in preparation for fix.
>
> Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
> ---
Thanks.
Show 11 quoted lines
> +test_expect_success 'Create branches to test sort with multiple keys' '
> +	git checkout -b Branch1 &&
> +	echo foo >> one &&
> +	git commit -a -m "Branch1 commit" &&
> +	git checkout -b Branch2 &&
> +	echo foo >> one &&
> +	git commit -a -m "Branch2 commit"
> +'
> +
> +test_atom refs/heads/Branch1 objectname 32fca05e9f638021a123a84226acf17756acc18b
> +test_atom refs/heads/Branch2 objectname 194a5b89ac661a114566ba4374bc06c2797539f3

Do these need to be "Branch[12]", not "branch[12]" for the code to exhibit the bug? If not, please don't be creative in names like these. On case corrupting filesystems you may write Branch1 and they may come back as branch1, but that is not what we are testing here.

Also, style: redirection sticks to the target file, e.g.
	echo foo >>one &&
Show 43 quoted lines
> +
>  cat >expected <<\EOF
> +67a36f10722846e891fbada1ba48ed035de75581 commit	refs/heads/master
> +194a5b89ac661a114566ba4374bc06c2797539f3 commit	refs/heads/Branch2
> +32fca05e9f638021a123a84226acf17756acc18b commit	refs/heads/Branch1
> +EOF
> +
> +test_expect_failure 'Verify sort with multiple keys' '
> +	git for-each-ref --sort=objectname --sort=committerdate refs/heads > actual &&
> +	test_cmp expected actual
> +'
> +
> +cat >expected <<\EOF
> +'refs/heads/Branch1'
> +'refs/heads/Branch2'
>  'refs/heads/master'
>  'refs/remotes/origin/master'
>  'refs/tags/testtag'
> @@ -264,6 +289,8 @@ test_expect_success 'Quoting style: python' '
>  '
>  
>  cat >expected <<\EOF
> +"refs/heads/Branch1"
> +"refs/heads/Branch2"
>  "refs/heads/master"
>  "refs/remotes/origin/master"
>  "refs/tags/testtag"
> @@ -285,6 +312,8 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
>  done
>  
>  cat >expected <<\EOF
> +Branch1
> +Branch2
>  master
>  testtag
>  EOF
> @@ -296,6 +325,8 @@ test_expect_success 'Check short refname format' '
>  '
>  
>  cat >expected <<EOF
> +
> +
>  origin/master
What are these blank line outputs?
Show 10 quoted lines
>  EOF
>  
> @@ -309,7 +340,7 @@ cat >expected <<EOF
>  EOF
>  
>  test_expect_success 'Check short objectname format' '
> -	git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
> +	git for-each-ref --format="%(objectname:short)" refs/heads/master >actual &&
>  	test_cmp expected actual
>  '

All in all, I have to wonder if you can limit the updates to other unrelated tests if you added a new test near the end. Also doesn't the existing test already create enough refs to let you sort with multiple keys and demonstrate the breakage already, without adding new refs and objects?

Kacper Kornet· Aug 20, 2012, 05:24 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] t6300: test sort with multiple keys

On Sun, Aug 19, 2012 at 05:38:29PM -0700, Junio C Hamano wrote:
> Kacper Kornet <draenog@pld-linux.org> writes:
> > Documentation of git-for-each-ref says that --sort=<key> option can be
> > used multiple times, in which case the last key becomes the primary key.
> > However this functionality was never checked in test suite and is
> > currently broken. This commit adds appropriate test in preparation for fix.
> > Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
> > ---
> Thanks.
Show 11 quoted lines
> > +test_expect_success 'Create branches to test sort with multiple keys' '
> > +	git checkout -b Branch1 &&
> > +	echo foo >> one &&
> > +	git commit -a -m "Branch1 commit" &&
> > +	git checkout -b Branch2 &&
> > +	echo foo >> one &&
> > +	git commit -a -m "Branch2 commit"
> > +'
> > +
> > +test_atom refs/heads/Branch1 objectname 32fca05e9f638021a123a84226acf17756acc18b
> > +test_atom refs/heads/Branch2 objectname 194a5b89ac661a114566ba4374bc06c2797539f3
Show 5 quoted lines
> Do these need to be "Branch[12]", not "branch[12]" for the code to
> exhibit the bug?  If not, please don't be creative in names like
> these.  On case corrupting filesystems you may write Branch1 and
> they may come back as branch1, but that is not what we are testing
> here.

Branches names can be lowercased. Only the commit messages should be preserved as they produce the test depends on the lexicographical order of created SHA1s.

> > @@ -296,6 +325,8 @@ test_expect_success 'Check short refname format' '
> >  '
> >  cat >expected <<EOF
> > +
> > +
> >  origin/master
> What are these blank line outputs?
The upstreams of Branch1 and Branch2.
> >  EOF
> > @@ -309,7 +340,7 @@ cat >expected <<EOF
> >  EOF
Show 5 quoted lines
> >  test_expect_success 'Check short objectname format' '
> > -	git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
> > +	git for-each-ref --format="%(objectname:short)" refs/heads/master >actual &&
> >  	test_cmp expected actual
> >  '
Show 5 quoted lines
> All in all, I have to wonder if you can limit the updates to other
> unrelated tests if you added a new test near the end.  Also doesn't
> the existing test already create enough refs to let you sort with
> multiple keys and demonstrate the breakage already, without adding new
> refs and objects?

My intention was to group all tests to sort in one place. But if the preferred place for a new one is at the end, then it is possible to find the adequate refs among existing ones.

-- 
  Kacper Kornet
Kacper Kornet· Aug 21, 2012, 07:46 UTC · re: Junio C Hamano · lore

[PATCHv2 1/2] t6300: test sort with multiple keys

Documentation of git-for-each-ref says that --sort=<key> option can be used multiple times, in which case the last key becomes the primary key. However this functionality was never checked in test suite and is currently broken. This commit adds appropriate test in preparation for fix.

Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
---
 t/t6300-for-each-ref.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
Show changes to t/t6300-for-each-ref.sh +10 −0
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 1721784..a0d82d4 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -456,4 +456,14 @@ test_atom refs/tags/signed-long contents "subject line
 body contents
 $sig"
 
+cat >expected <<\EOF
+408fe76d02a785a006c2e9c669b7be5589ede96d <committer@example.com> refs/tags/master
+90b5ebede4899eda64893bc2a4c8f1d6fb6dfc40 <committer@example.com> refs/tags/bogo
+EOF
+
+test_expect_failure 'Verify sort with multiple keys' '
+	git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
+		refs/tags/bogo refs/tags/master > actual &&
+	test_cmp expected actual
+'
 test_done
-- 
1.7.12.rc3


-- 
  Kacper Kornet
Kacper Kornet· Aug 21, 2012, 07:47 UTC · re: Kacper Kornet · lore

[PATCHv2 2/2] for-each-ref: Fix sort with multiple keys

The linked list describing sort options was not correctly set up in opt_parse_sort. In the result, contrary to the documentation, only the last of multiple --sort options to git-for-each-ref was taken into account. This commit fixes it.

Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
---
 builtin/for-each-ref.c  | 4 +++-
 t/t6300-for-each-ref.sh | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
Show changes to 2 files +4 −2

builtin/for-each-ref.c, t/t6300-for-each-ref.sh

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index b01d76a..0c5294e 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -962,7 +962,9 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 	if (!arg) /* should --no-sort void the list ? */
 		return -1;
 
-	*sort_tail = s = xcalloc(1, sizeof(*s));
+	s = xcalloc(1, sizeof(*s));
+	s->next = *sort_tail;
+	*sort_tail = s;
 
 	if (*arg == '-') {
 		s->reverse = 1;
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index a0d82d4..752f5cb 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -461,7 +461,7 @@ cat >expected <<\EOF
 90b5ebede4899eda64893bc2a4c8f1d6fb6dfc40 <committer@example.com> refs/tags/bogo
 EOF
 
-test_expect_failure 'Verify sort with multiple keys' '
+test_expect_success 'Verify sort with multiple keys' '
 	git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 		refs/tags/bogo refs/tags/master > actual &&
 	test_cmp expected actual
-- 
1.7.12.rc3
Junio C Hamano· Aug 21, 2012, 21:33 UTC · re: Kacper Kornet · lore

Re: [PATCHv2 1/2] t6300: test sort with multiple keys

Kacper Kornet <draenog@pld-linux.org> writes:
Show 9 quoted lines
> Documentation of git-for-each-ref says that --sort=<key> option can be
> used multiple times, in which case the last key becomes the primary key.
> However this functionality was never checked in test suite and is
> currently broken. This commit adds appropriate test in preparation for fix.
>
> Signed-off-by: Kacper Kornet <draenog@pld-linux.org>
> ---
>  t/t6300-for-each-ref.sh | 10 ++++++++++
>  1 file changed, 10 insertions(+)

Much nicer and concise. It would have been even better if it didn't have to depend on exact object names, but the existing tests already depend on them, so it is not making things worse.

Thanks.  Will queue.
Show 21 quoted lines
> diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
> index 1721784..a0d82d4 100755
> --- a/t/t6300-for-each-ref.sh
> +++ b/t/t6300-for-each-ref.sh
> @@ -456,4 +456,14 @@ test_atom refs/tags/signed-long contents "subject line
>  body contents
>  $sig"
>  
> +cat >expected <<\EOF
> +408fe76d02a785a006c2e9c669b7be5589ede96d <committer@example.com> refs/tags/master
> +90b5ebede4899eda64893bc2a4c8f1d6fb6dfc40 <committer@example.com> refs/tags/bogo
> +EOF
> +
> +test_expect_failure 'Verify sort with multiple keys' '
> +	git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
> +		refs/tags/bogo refs/tags/master > actual &&
> +	test_cmp expected actual
> +'
>  test_done
> -- 
> 1.7.12.rc3

← back to recent threads