Skip to contentSkip to conversation

for-each-ref: fix setup of option-parsing for --sort

PATCH In review Lars Hjemli 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 for-each-ref: fix setup of option-parsing for --sort

The option value for --sort is already a pointer to a pointer to struct ref_sort, so just use it.

---

Quoted 1 line · Johannes Schindelin

Could you add a test for that too, please?

Is this ok?

 builtin-for-each-ref.c  |    2 +-
 t/t6300-for-each-ref.sh |   22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletions(-)
builtin-for-each-ref.c +1 -1
index da8c794..e909e66 100644
@@ -847,7 +847,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_GROUP(""),
 		OPT_INTEGER( 0 , "count", &maxcount, "show only <n> matched refs"),
 		OPT_STRING(  0 , "format", &format, "format", "format to use for the output"),
-		OPT_CALLBACK(0 , "sort", &sort_tail, "key",
+		OPT_CALLBACK(0 , "sort", sort_tail, "key",
 		            "field name to sort on", &opt_parse_sort),
 		OPT_END(),
 	};
t/t6300-for-each-ref.sh +22 -0
index d0809eb..c722635 100755
@@ -148,4 +148,26 @@ test_expect_success 'Check format "rfc2822" date fields output' '
 	git diff expected actual
 '
 
+cat >expected <<\EOF
+refs/heads/master
+refs/tags/testtag
+EOF
+
+test_expect_success 'Verify ascending sort' '
+	git-for-each-ref --format="%(refname)" --sort=refname >actual &&
+	git diff expected actual
+'
+
+
+cat >expected <<\EOF
+refs/tags/testtag
+refs/heads/master
+EOF
+
+test_expect_success 'Verify descending sort' '
+	git-for-each-ref --format="%(refname)" --sort=-refname >actual &&
+	git diff expected actual
+'
+
+
 test_done
Signature · 1 line
1.5.3.5.623.g0a1d
#1 lore raw
Quoted 11 lines · Lars Hjemli

The option value for --sort is already a pointer to a pointer to struct ref_sort, so just use it.

---

Quoted 1 line · Johannes Schindelin

Could you add a test for that too, please?

Is this ok?

Testing "for that" would be kind of hard and semi pointless, isn't it?

If it's mismatch of the expected number of times a pointer is dereferenced between the caller and the callee, I'd imagine that it will read from and write to random place in memory and would lead to unpredictable behaviour. If you are lucky you would not get expected results but if you are unlucky who knows what would happen.

But the new test makes sure --sort takes intended effect, which is a good thing.

Thanks.

Quoted 50 lines
 builtin-for-each-ref.c  |    2 +-
 t/t6300-for-each-ref.sh |   22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletions(-)
builtin-for-each-ref.c +1 -1
index da8c794..e909e66 100644
@@ -847,7 +847,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_GROUP(""),
 		OPT_INTEGER( 0 , "count", &maxcount, "show only <n> matched refs"),
 		OPT_STRING(  0 , "format", &format, "format", "format to use for the output"),
-		OPT_CALLBACK(0 , "sort", &sort_tail, "key",
+		OPT_CALLBACK(0 , "sort", sort_tail, "key",
 		            "field name to sort on", &opt_parse_sort),
 		OPT_END(),
 	};
t/t6300-for-each-ref.sh +22 -0
index d0809eb..c722635 100755
@@ -148,4 +148,26 @@ test_expect_success 'Check format "rfc2822" date fields output' '
 	git diff expected actual
 '
 
+cat >expected <<\EOF
+refs/heads/master
+refs/tags/testtag
+EOF
+
+test_expect_success 'Verify ascending sort' '
+	git-for-each-ref --format="%(refname)" --sort=refname >actual &&
+	git diff expected actual
+'
+
+
+cat >expected <<\EOF
+refs/tags/testtag
+refs/heads/master
+EOF
+
+test_expect_success 'Verify descending sort' '
+	git-for-each-ref --format="%(refname)" --sort=-refname >actual &&
+	git diff expected actual
+'
+
+
 test_done
Signature · 1 line
1.5.3.5.623.g0a1d