test-ref-store: parse symbolic flag constants

This lets tests use REF_XXXX constants instead of hardcoded integers. The flag names should be separated by a ','. Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Dec 7, 2021 at 13:38 UTC cd2d40fb7f1f9e12b13937789bbf5573f2041cde
2 files changed +60 -9
t/helper/test-ref-store.c
+59 -7
@@ -5,6 +5,48 @@
5 #include "object-store.h"
6 #include "repository.h"
7
8 +struct flag_definition {
9 + const char *name;
10 + uint64_t mask;
11 +};
12 +
13 +#define FLAG_DEF(x) \
14 + { \
15 +#x, (x) \
16 + }
17 +
18 +static unsigned int parse_flags(const char *str, struct flag_definition *defs)
19 +{
20 + struct string_list masks = STRING_LIST_INIT_DUP;
21 + int i = 0;
22 + unsigned int result = 0;
23 +
24 + if (!strcmp(str, "0"))
25 + return 0;
26 +
27 + string_list_split(&masks, str, ',', 64);
28 + for (; i < masks.nr; i++) {
29 + const char *name = masks.items[i].string;
30 + struct flag_definition *def = defs;
31 + int found = 0;
32 + while (def->name) {
33 + if (!strcmp(def->name, name)) {
34 + result |= def->mask;
35 + found = 1;
36 + break;
37 + }
38 + def++;
39 + }
40 + if (!found)
41 + die("unknown flag \"%s\"", name);
42 + }
43 +
44 + string_list_clear(&masks, 0);
45 + return result;
46 +}
47 +
48 +static struct flag_definition empty_flags[] = { { NULL, 0 } };
49 +
50 static const char *notnull(const char *arg, const char *name)
51 {
52 if (!arg)
@@ -12,9 +54,10 @@ static const char *notnull(const char *arg, const char *name)
54 return arg;
55 }
56
15 -static unsigned int arg_flags(const char *arg, const char *name)
57 +static unsigned int arg_flags(const char *arg, const char *name,
58 + struct flag_definition *defs)
59 {
17 - return atoi(notnull(arg, name));
60 + return parse_flags(notnull(arg, name), defs);
61 }
62
63 static const char **get_store(const char **argv, struct ref_store **refs)
@@ -64,10 +107,13 @@ static const char **get_store(const char **argv, struct ref_store **refs)
107 return argv + 1;
108 }
109
110 +static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE),
111 + FLAG_DEF(PACK_REFS_ALL),
112 + { NULL, 0 } };
113
114 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
115 {
70 - unsigned int flags = arg_flags(*argv++, "flags");
116 + unsigned int flags = arg_flags(*argv++, "flags", pack_flags);
117
118 return refs_pack_refs(refs, flags);
119 }
@@ -81,9 +127,15 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
127 return refs_create_symref(refs, refname, target, logmsg);
128 }
129
130 +static struct flag_definition transaction_flags[] = {
131 + FLAG_DEF(REF_NO_DEREF),
132 + FLAG_DEF(REF_FORCE_CREATE_REFLOG),
133 + { NULL, 0 }
134 +};
135 +
136 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
137 {
86 - unsigned int flags = arg_flags(*argv++, "flags");
138 + unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
139 const char *msg = *argv++;
140 struct string_list refnames = STRING_LIST_INIT_NODUP;
141
@@ -120,7 +172,7 @@ static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
172 {
173 struct object_id oid = *null_oid();
174 const char *refname = notnull(*argv++, "refname");
123 - int resolve_flags = arg_flags(*argv++, "resolve-flags");
175 + int resolve_flags = arg_flags(*argv++, "resolve-flags", empty_flags);
176 int flags;
177 const char *ref;
178 int ignore_errno;
@@ -209,7 +261,7 @@ static int cmd_delete_ref(struct ref_store *refs, const char **argv)
261 const char *msg = notnull(*argv++, "msg");
262 const char *refname = notnull(*argv++, "refname");
263 const char *sha1_buf = notnull(*argv++, "old-sha1");
212 - unsigned int flags = arg_flags(*argv++, "flags");
264 + unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
265 struct object_id old_oid;
266
267 if (get_oid_hex(sha1_buf, &old_oid))
@@ -224,7 +276,7 @@ static int cmd_update_ref(struct ref_store *refs, const char **argv)
276 const char *refname = notnull(*argv++, "refname");
277 const char *new_sha1_buf = notnull(*argv++, "new-sha1");
278 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
227 - unsigned int flags = arg_flags(*argv++, "flags");
279 + unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
280 struct object_id old_oid;
281 struct object_id new_oid;
282
t/t1405-main-ref-store.sh
+1 -2
@@ -17,8 +17,7 @@ test_expect_success 'setup' '
17 test_expect_success REFFILES 'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
18 N=`find .git/refs -type f | wc -l` &&
19 test "$N" != 0 &&
20 - ALL_OR_PRUNE_FLAG=3 &&
21 - $RUN pack-refs ${ALL_OR_PRUNE_FLAG} &&
20 + $RUN pack-refs PACK_REFS_PRUNE,PACK_REFS_ALL &&
21 N=`find .git/refs -type f` &&
22 test -z "$N"
23 '