Merge branch 'ab/config-based-hooks-base' into seen
[git] / t / helper / test-read-cache.c
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"
8 #include "parse-options.h"
9
10 static const char *read_cache_usage[] = {
11         "test-tool read-cache [<options>...]",
12         NULL
13 };
14
15 static void print_cache_entry(struct cache_entry *ce)
16 {
17         const char *type;
18         printf("%06o ", ce->ce_mode & 0177777);
19
20         if (S_ISSPARSEDIR(ce->ce_mode))
21                 type = tree_type;
22         else if (S_ISGITLINK(ce->ce_mode))
23                 type = commit_type;
24         else
25                 type = blob_type;
26
27         printf("%s %s\t%s\n",
28                type,
29                oid_to_hex(&ce->oid),
30                ce->name);
31 }
32
33 static void print_cache(struct index_state *istate)
34 {
35         int i;
36         for (i = 0; i < istate->cache_nr; i++)
37                 print_cache_entry(istate->cache[i]);
38 }
39
40 int cmd__read_cache(int argc, const char **argv)
41 {
42         struct repository *r = the_repository;
43         int table = 0, expand = 0;
44         struct option options[] = {
45                 OPT_BOOL(0, "table", &table,
46                          "print a dump of the cache"),
47                 OPT_BOOL(0, "expand", &expand,
48                          "call ensure_full_index()"),
49                 OPT_END()
50         };
51
52         argc = parse_options(argc, argv, "test-tools", options, read_cache_usage, 0);
53         if (argc > 0)
54                 usage_msg_opt("Too many arguments.", read_cache_usage, options);
55
56         initialize_the_repository();
57         prepare_repo_settings(r);
58         r->settings.command_requires_full_index = 0;
59
60         setup_git_directory();
61         git_config(git_default_config, NULL);
62         repo_read_index(r);
63
64         if (expand)
65                 ensure_full_index(r->index);
66
67         if (table)
68                 print_cache(r->index);
69         discard_index(r->index);
70
71         return 0;
72 }