blob: b52c174acc7a1f1c8e4c9a3d5a888323c9db0fad [file] [log] [blame]
Nguyễn Thái Ngọc Duy5fbe6002018-03-24 08:44:51 +01001#include "test-tool.h"
René Scharfe1ecb5ff2013-06-09 19:39:17 +02002#include "cache.h"
Johannes Schindelindc768522019-05-07 13:10:20 +02003#include "config.h"
Derrick Stoleee2df6c32021-03-30 13:10:50 +00004#include "blob.h"
5#include "commit.h"
6#include "tree.h"
Derrick Stolee2782db32021-03-30 13:10:51 +00007#include "sparse-index.h"
Derrick Stoleee2df6c32021-03-30 13:10:50 +00008
9static void print_cache_entry(struct cache_entry *ce)
10{
11 const char *type;
12 printf("%06o ", ce->ce_mode & 0177777);
13
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;
20
21 printf("%s %s\t%s\n",
22 type,
23 oid_to_hex(&ce->oid),
24 ce->name);
25}
26
27static void print_cache(struct index_state *istate)
28{
29 int i;
30 for (i = 0; i < istate->cache_nr; i++)
31 print_cache_entry(istate->cache[i]);
32}
René Scharfe1ecb5ff2013-06-09 19:39:17 +020033
Nguyễn Thái Ngọc Duy5fbe6002018-03-24 08:44:51 +010034int cmd__read_cache(int argc, const char **argv)
René Scharfe1ecb5ff2013-06-09 19:39:17 +020035{
Derrick Stoleee2df6c32021-03-30 13:10:50 +000036 struct repository *r = the_repository;
Jeff King7e6b96c2019-09-05 18:54:31 -040037 int i, cnt = 1;
Johannes Schindelindc768522019-05-07 13:10:20 +020038 const char *name = NULL;
Derrick Stolee2782db32021-03-30 13:10:51 +000039 int table = 0, expand = 0;
40
41 initialize_the_repository();
42 prepare_repo_settings(r);
43 r->settings.command_requires_full_index = 0;
Johannes Schindelindc768522019-05-07 13:10:20 +020044
Derrick Stoleee2df6c32021-03-30 13:10:50 +000045 for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
46 if (skip_prefix(*argv, "--print-and-refresh=", &name))
47 continue;
48 if (!strcmp(*argv, "--table"))
49 table = 1;
Derrick Stolee2782db32021-03-30 13:10:51 +000050 else if (!strcmp(*argv, "--expand"))
51 expand = 1;
Johannes Schindelindc768522019-05-07 13:10:20 +020052 }
53
Derrick Stoleee2df6c32021-03-30 13:10:50 +000054 if (argc == 1)
55 cnt = strtol(argv[0], NULL, 0);
René Scharfebccb22c2017-04-06 22:41:41 +020056 setup_git_directory();
Johannes Schindelindc768522019-05-07 13:10:20 +020057 git_config(git_default_config, NULL);
Derrick Stoleee2df6c32021-03-30 13:10:50 +000058
René Scharfe1ecb5ff2013-06-09 19:39:17 +020059 for (i = 0; i < cnt; i++) {
Derrick Stoleee2df6c32021-03-30 13:10:50 +000060 repo_read_index(r);
Derrick Stolee2782db32021-03-30 13:10:51 +000061
62 if (expand)
63 ensure_full_index(r->index);
64
Johannes Schindelindc768522019-05-07 13:10:20 +020065 if (name) {
66 int pos;
67
Derrick Stoleee2df6c32021-03-30 13:10:50 +000068 refresh_index(r->index, REFRESH_QUIET,
Johannes Schindelindc768522019-05-07 13:10:20 +020069 NULL, NULL, NULL);
Derrick Stoleee2df6c32021-03-30 13:10:50 +000070 pos = index_name_pos(r->index, name, strlen(name));
Johannes Schindelindc768522019-05-07 13:10:20 +020071 if (pos < 0)
72 die("%s not in index", name);
73 printf("%s is%s up to date\n", name,
Derrick Stoleee2df6c32021-03-30 13:10:50 +000074 ce_uptodate(r->index->cache[pos]) ? "" : " not");
Johannes Schindelindc768522019-05-07 13:10:20 +020075 write_file(name, "%d\n", i);
76 }
Derrick Stoleee2df6c32021-03-30 13:10:50 +000077 if (table)
78 print_cache(r->index);
79 discard_index(r->index);
René Scharfe1ecb5ff2013-06-09 19:39:17 +020080 }
81 return 0;
82}