test-read-cache.c: prepare_repo_settings after config init
[git.git] / t / helper / test-read-cache.c
blob0d9f08931a15f10a546ea2a9840f733cfb9955e0
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "blob.h"
5 #include "commit.h"
6 #include "tree.h"
7 #include "sparse-index.h"
9 static void print_cache_entry(struct cache_entry *ce)
11 const char *type;
12 printf("%06o ", ce->ce_mode & 0177777);
14 if (S_ISSPARSEDIR(ce->ce_mode))
15 type = tree_type;
16 else if (S_ISGITLINK(ce->ce_mode))
17 type = commit_type;
18 else
19 type = blob_type;
21 printf("%s %s\t%s\n",
22 type,
23 oid_to_hex(&ce->oid),
24 ce->name);
27 static void print_cache(struct index_state *istate)
29 int i;
30 for (i = 0; i < istate->cache_nr; i++)
31 print_cache_entry(istate->cache[i]);
34 int cmd__read_cache(int argc, const char **argv)
36 struct repository *r = the_repository;
37 int i, cnt = 1;
38 const char *name = NULL;
39 int table = 0, expand = 0;
41 initialize_the_repository();
43 for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
44 if (skip_prefix(*argv, "--print-and-refresh=", &name))
45 continue;
46 if (!strcmp(*argv, "--table"))
47 table = 1;
48 else if (!strcmp(*argv, "--expand"))
49 expand = 1;
52 if (argc == 1)
53 cnt = strtol(argv[0], NULL, 0);
54 setup_git_directory();
55 git_config(git_default_config, NULL);
57 prepare_repo_settings(r);
58 r->settings.command_requires_full_index = 0;
60 for (i = 0; i < cnt; i++) {
61 repo_read_index(r);
63 if (expand)
64 ensure_full_index(r->index);
66 if (name) {
67 int pos;
69 refresh_index(r->index, REFRESH_QUIET,
70 NULL, NULL, NULL);
71 pos = index_name_pos(r->index, name, strlen(name));
72 if (pos < 0)
73 die("%s not in index", name);
74 printf("%s is%s up to date\n", name,
75 ce_uptodate(r->index->cache[pos]) ? "" : " not");
76 write_file(name, "%d\n", i);
78 if (table)
79 print_cache(r->index);
80 discard_index(r->index);
82 return 0;