Skip to content

Commit e2df6c3

Browse files
derrickstoleegitster
authored andcommitted
test-read-cache: print cache entries with --table
This table is helpful for discovering data in the index to ensure it is being written correctly, especially as we build and test the sparse-index. This table includes an output format similar to 'git ls-tree', but should not be compared to that directly. The biggest reasons are that 'git ls-tree' includes a tree entry for every subdirectory, even those that would not appear as a sparse directory in a sparse-index. Further, 'git ls-tree' does not use a trailing directory separator for its tree rows. This does not print the stat() information for the blobs. That will be added in a future change with another option. The tests that are added in the next few changes care only about the object types and IDs. However, this future need for full index information justifies the need for this test helper over extending a user-facing feature, such as 'git ls-files'. To make the option parsing slightly more robust, wrap the string comparisons in a loop adapted from test-dir-iterator.c. Care must be taken with the final check for the 'cnt' variable. We continue the expectation that the numerical value is the final argument. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ecfc47c commit e2df6c3

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

t/helper/test-read-cache.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
11
#include "test-tool.h"
22
#include "cache.h"
33
#include "config.h"
4+
#include "blob.h"
5+
#include "commit.h"
6+
#include "tree.h"
7+
8+
static void print_cache_entry(struct cache_entry *ce)
9+
{
10+
const char *type;
11+
printf("%06o ", ce->ce_mode & 0177777);
12+
13+
if (S_ISSPARSEDIR(ce->ce_mode))
14+
type = tree_type;
15+
else if (S_ISGITLINK(ce->ce_mode))
16+
type = commit_type;
17+
else
18+
type = blob_type;
19+
20+
printf("%s %s\t%s\n",
21+
type,
22+
oid_to_hex(&ce->oid),
23+
ce->name);
24+
}
25+
26+
static void print_cache(struct index_state *istate)
27+
{
28+
int i;
29+
for (i = 0; i < istate->cache_nr; i++)
30+
print_cache_entry(istate->cache[i]);
31+
}
432

533
int cmd__read_cache(int argc, const char **argv)
634
{
35+
struct repository *r = the_repository;
736
int i, cnt = 1;
837
const char *name = NULL;
38+
int table = 0;
939

10-
if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) {
11-
argc--;
12-
argv++;
40+
for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
41+
if (skip_prefix(*argv, "--print-and-refresh=", &name))
42+
continue;
43+
if (!strcmp(*argv, "--table"))
44+
table = 1;
1345
}
1446

15-
if (argc == 2)
16-
cnt = strtol(argv[1], NULL, 0);
47+
if (argc == 1)
48+
cnt = strtol(argv[0], NULL, 0);
1749
setup_git_directory();
1850
git_config(git_default_config, NULL);
51+
1952
for (i = 0; i < cnt; i++) {
20-
read_cache();
53+
repo_read_index(r);
2154
if (name) {
2255
int pos;
2356

24-
refresh_index(&the_index, REFRESH_QUIET,
57+
refresh_index(r->index, REFRESH_QUIET,
2558
NULL, NULL, NULL);
26-
pos = index_name_pos(&the_index, name, strlen(name));
59+
pos = index_name_pos(r->index, name, strlen(name));
2760
if (pos < 0)
2861
die("%s not in index", name);
2962
printf("%s is%s up to date\n", name,
30-
ce_uptodate(the_index.cache[pos]) ? "" : " not");
63+
ce_uptodate(r->index->cache[pos]) ? "" : " not");
3164
write_file(name, "%d\n", i);
3265
}
33-
discard_cache();
66+
if (table)
67+
print_cache(r->index);
68+
discard_index(r->index);
3469
}
3570
return 0;
3671
}

0 commit comments

Comments
 (0)