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>
This commit is contained in:
Han-Wen Nienhuys 2021-12-07 13:38:14 +00:00 committed by Junio C Hamano
parent 93db6eef04
commit cd2d40fb7f
2 changed files with 60 additions and 9 deletions

View File

@ -5,6 +5,48 @@
#include "object-store.h"
#include "repository.h"
struct flag_definition {
const char *name;
uint64_t mask;
};
#define FLAG_DEF(x) \
{ \
#x, (x) \
}
static unsigned int parse_flags(const char *str, struct flag_definition *defs)
{
struct string_list masks = STRING_LIST_INIT_DUP;
int i = 0;
unsigned int result = 0;
if (!strcmp(str, "0"))
return 0;
string_list_split(&masks, str, ',', 64);
for (; i < masks.nr; i++) {
const char *name = masks.items[i].string;
struct flag_definition *def = defs;
int found = 0;
while (def->name) {
if (!strcmp(def->name, name)) {
result |= def->mask;
found = 1;
break;
}
def++;
}
if (!found)
die("unknown flag \"%s\"", name);
}
string_list_clear(&masks, 0);
return result;
}
static struct flag_definition empty_flags[] = { { NULL, 0 } };
static const char *notnull(const char *arg, const char *name)
{
if (!arg)
@ -12,9 +54,10 @@ static const char *notnull(const char *arg, const char *name)
return arg;
}
static unsigned int arg_flags(const char *arg, const char *name)
static unsigned int arg_flags(const char *arg, const char *name,
struct flag_definition *defs)
{
return atoi(notnull(arg, name));
return parse_flags(notnull(arg, name), defs);
}
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)
return argv + 1;
}
static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE),
FLAG_DEF(PACK_REFS_ALL),
{ NULL, 0 } };
static int cmd_pack_refs(struct ref_store *refs, const char **argv)
{
unsigned int flags = arg_flags(*argv++, "flags");
unsigned int flags = arg_flags(*argv++, "flags", pack_flags);
return refs_pack_refs(refs, flags);
}
@ -81,9 +127,15 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
return refs_create_symref(refs, refname, target, logmsg);
}
static struct flag_definition transaction_flags[] = {
FLAG_DEF(REF_NO_DEREF),
FLAG_DEF(REF_FORCE_CREATE_REFLOG),
{ NULL, 0 }
};
static int cmd_delete_refs(struct ref_store *refs, const char **argv)
{
unsigned int flags = arg_flags(*argv++, "flags");
unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
const char *msg = *argv++;
struct string_list refnames = STRING_LIST_INIT_NODUP;
@ -120,7 +172,7 @@ static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
{
struct object_id oid = *null_oid();
const char *refname = notnull(*argv++, "refname");
int resolve_flags = arg_flags(*argv++, "resolve-flags");
int resolve_flags = arg_flags(*argv++, "resolve-flags", empty_flags);
int flags;
const char *ref;
int ignore_errno;
@ -209,7 +261,7 @@ static int cmd_delete_ref(struct ref_store *refs, const char **argv)
const char *msg = notnull(*argv++, "msg");
const char *refname = notnull(*argv++, "refname");
const char *sha1_buf = notnull(*argv++, "old-sha1");
unsigned int flags = arg_flags(*argv++, "flags");
unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
struct object_id old_oid;
if (get_oid_hex(sha1_buf, &old_oid))
@ -224,7 +276,7 @@ static int cmd_update_ref(struct ref_store *refs, const char **argv)
const char *refname = notnull(*argv++, "refname");
const char *new_sha1_buf = notnull(*argv++, "new-sha1");
const char *old_sha1_buf = notnull(*argv++, "old-sha1");
unsigned int flags = arg_flags(*argv++, "flags");
unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
struct object_id old_oid;
struct object_id new_oid;

View File

@ -17,8 +17,7 @@ test_expect_success 'setup' '
test_expect_success REFFILES 'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
N=`find .git/refs -type f | wc -l` &&
test "$N" != 0 &&
ALL_OR_PRUNE_FLAG=3 &&
$RUN pack-refs ${ALL_OR_PRUNE_FLAG} &&
$RUN pack-refs PACK_REFS_PRUNE,PACK_REFS_ALL &&
N=`find .git/refs -type f` &&
test -z "$N"
'