Skip to content

Commit cd2d40f

Browse files
hanwengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 93db6ee commit cd2d40f

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

t/helper/test-ref-store.c

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,59 @@
55
#include "object-store.h"
66
#include "repository.h"
77

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+
850
static const char *notnull(const char *arg, const char *name)
951
{
1052
if (!arg)
1153
die("%s required", name);
1254
return arg;
1355
}
1456

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)
1659
{
17-
return atoi(notnull(arg, name));
60+
return parse_flags(notnull(arg, name), defs);
1861
}
1962

2063
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)
64107
return argv + 1;
65108
}
66109

110+
static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE),
111+
FLAG_DEF(PACK_REFS_ALL),
112+
{ NULL, 0 } };
67113

68114
static int cmd_pack_refs(struct ref_store *refs, const char **argv)
69115
{
70-
unsigned int flags = arg_flags(*argv++, "flags");
116+
unsigned int flags = arg_flags(*argv++, "flags", pack_flags);
71117

72118
return refs_pack_refs(refs, flags);
73119
}
@@ -81,9 +127,15 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
81127
return refs_create_symref(refs, refname, target, logmsg);
82128
}
83129

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+
84136
static int cmd_delete_refs(struct ref_store *refs, const char **argv)
85137
{
86-
unsigned int flags = arg_flags(*argv++, "flags");
138+
unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
87139
const char *msg = *argv++;
88140
struct string_list refnames = STRING_LIST_INIT_NODUP;
89141

@@ -120,7 +172,7 @@ static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
120172
{
121173
struct object_id oid = *null_oid();
122174
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);
124176
int flags;
125177
const char *ref;
126178
int ignore_errno;
@@ -209,7 +261,7 @@ static int cmd_delete_ref(struct ref_store *refs, const char **argv)
209261
const char *msg = notnull(*argv++, "msg");
210262
const char *refname = notnull(*argv++, "refname");
211263
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);
213265
struct object_id old_oid;
214266

215267
if (get_oid_hex(sha1_buf, &old_oid))
@@ -224,7 +276,7 @@ static int cmd_update_ref(struct ref_store *refs, const char **argv)
224276
const char *refname = notnull(*argv++, "refname");
225277
const char *new_sha1_buf = notnull(*argv++, "new-sha1");
226278
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);
228280
struct object_id old_oid;
229281
struct object_id new_oid;
230282

t/t1405-main-ref-store.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ test_expect_success 'setup' '
1717
test_expect_success REFFILES 'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
1818
N=`find .git/refs -type f | wc -l` &&
1919
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 &&
2221
N=`find .git/refs -type f` &&
2322
test -z "$N"
2423
'

0 commit comments

Comments
 (0)