t/helper: fix segfault in "oid-array" command without repository
The "oid-array" test helper can supposedly work without a Git repository, but will in fact crash because `the_repository->hash_algo` is not initialized. This is because `oid_pos()`, which is used by `oid_array_lookup()`, depends on `the_hash_algo->rawsz`. Ideally, we'd adapt `oid_pos()` to not depend on `the_hash_algo` anymore. That is a bigger untertaking though, so instead we fall back to SHA1 when there is no repository. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jun 14, 2024 at 08:51 UTC
8e9a1d0dc2d543c05cb0c11a598fb7675d5deea8
2 files changed
+22
t/helper/test-oid-array.c
+4
@@ -1,3 +1,5 @@
1
+#define USE_THE_REPOSITORY_VARIABLE
2
+
3
#include "test-tool.h"
4
#include "hex.h"
5
#include "oid-array.h"
@@ -17,6 +19,8 @@ int cmd__oid_array(int argc UNUSED, const char **argv UNUSED)
19
int nongit_ok;
20
21
setup_git_directory_gently(&nongit_ok);
22
+ if (nongit_ok)
23
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
24
25
while (strbuf_getline(&line, stdin) != EOF) {
26
const char *arg;
t/t0064-oid-array.sh
+18
@@ -15,6 +15,24 @@ echoid () {
15
done
16
}
17
18
+test_expect_success 'without repository' '
19
+ cat >expect <<-EOF &&
20
+ 4444444444444444444444444444444444444444
21
+ 5555555555555555555555555555555555555555
22
+ 8888888888888888888888888888888888888888
23
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
24
+ EOF
25
+ cat >input <<-EOF &&
26
+ append 4444444444444444444444444444444444444444
27
+ append 5555555555555555555555555555555555555555
28
+ append 8888888888888888888888888888888888888888
29
+ append aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
30
+ for_each_unique
31
+ EOF
32
+ nongit test-tool oid-array <input >actual &&
33
+ test_cmp expect actual
34
+'
35
+
36
test_expect_success 'ordered enumeration' '
37
echoid "" 44 55 88 aa >expect &&
38
{