git/t/helper/test-dump-untracked-cache.c
Ævar Arnfjörð Bjarmason c6b4888b3f environment.c: remove test-specific "ignore_untracked..." variable
Instead of the global ignore_untracked_cache_config variable added in
dae6c322fa (test-dump-untracked-cache: don't modify the untracked
cache, 2016-01-27) we can make use of the new facility to set config
via environment variables added in d8d77153ea (config: allow
specifying config entries via envvar pairs, 2021-01-12).

It's arguably a bit hacky to use setenv() and getenv() to pass
messages between the same program, but since the test helpers are not
the main intended audience of repo-settings.c I think it's better than
hardcoding the test-only special-case in prepare_repo_settings().

This uses the xsetenv() wrapper added in the preceding commit, if we
don't set these in the environment we'll fail in
t7063-status-untracked-cache.sh, but let's fail earlier anyway if that
were to happen.

This breaks any parent process that's potentially using the
GIT_CONFIG_* and GIT_CONFIG_PARAMETERS mechanism to pass one-shot
config setting down to a git subprocess, but in this case we don't
care about the general case of such potential parents. This process
neither spawns other "git" processes, nor is it interested in other
configuration. We might want to pick up other test modes here, but
those will be passed via GIT_TEST_* environment variables.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-09-22 13:15:00 -07:00

69 lines
1.8 KiB
C

#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "test-tool.h"
#include "cache.h"
#include "dir.h"
static int compare_untracked(const void *a_, const void *b_)
{
const char *const *a = a_;
const char *const *b = b_;
return strcmp(*a, *b);
}
static int compare_dir(const void *a_, const void *b_)
{
const struct untracked_cache_dir *const *a = a_;
const struct untracked_cache_dir *const *b = b_;
return strcmp((*a)->name, (*b)->name);
}
static void dump(struct untracked_cache_dir *ucd, struct strbuf *base)
{
int i, len;
QSORT(ucd->untracked, ucd->untracked_nr, compare_untracked);
QSORT(ucd->dirs, ucd->dirs_nr, compare_dir);
len = base->len;
strbuf_addf(base, "%s/", ucd->name);
printf("%s %s", base->buf,
oid_to_hex(&ucd->exclude_oid));
if (ucd->recurse)
fputs(" recurse", stdout);
if (ucd->check_only)
fputs(" check_only", stdout);
if (ucd->valid)
fputs(" valid", stdout);
printf("\n");
for (i = 0; i < ucd->untracked_nr; i++)
printf("%s\n", ucd->untracked[i]);
for (i = 0; i < ucd->dirs_nr; i++)
dump(ucd->dirs[i], base);
strbuf_setlen(base, len);
}
int cmd__dump_untracked_cache(int ac, const char **av)
{
struct untracked_cache *uc;
struct strbuf base = STRBUF_INIT;
/* Set core.untrackedCache=keep before setup_git_directory() */
xsetenv("GIT_CONFIG_COUNT", "1", 1);
xsetenv("GIT_CONFIG_KEY_0", "core.untrackedCache", 1);
xsetenv("GIT_CONFIG_VALUE_0", "keep", 1);
setup_git_directory();
if (read_cache() < 0)
die("unable to read index file");
uc = the_index.untracked;
if (!uc) {
printf("no untracked cache\n");
return 0;
}
printf("info/exclude %s\n", oid_to_hex(&uc->ss_info_exclude.oid));
printf("core.excludesfile %s\n", oid_to_hex(&uc->ss_excludes_file.oid));
printf("exclude_per_dir %s\n", uc->exclude_per_dir);
printf("flags %08x\n", uc->dir_flags);
if (uc->root)
dump(uc->root, &base);
return 0;
}