Skip to content
This repository was archived by the owner on Jul 2, 2019. It is now read-only.

Commit 9c5e6c8

Browse files
pcloudsgitster
authored andcommitted
Convert "struct cache_entry *" to "const ..." wherever possible
I attempted to make index_state->cache[] a "const struct cache_entry **" to find out how existing entries in index are modified and where. The question I have is what do we do if we really need to keep track of on-disk changes in the index. The result is - diff-lib.c: setting CE_UPTODATE - name-hash.c: setting CE_HASHED - preload-index.c, read-cache.c, unpack-trees.c and builtin/update-index: obvious - entry.c: write_entry() may refresh the checked out entry via fill_stat_cache_info(). This causes "non-const struct cache_entry *" in builtin/apply.c, builtin/checkout-index.c and builtin/checkout.c - builtin/ls-files.c: --with-tree changes stagemask and may set CE_UPDATE Of these, write_entry() and its call sites are probably most interesting because it modifies on-disk info. But this is stat info and can be retrieved via refresh, at least for porcelain commands. Other just uses ce_flags for local purposes. So, keeping track of "dirty" entries is just a matter of setting a flag in index modification functions exposed by read-cache.c. Except unpack-trees, the rest of the code base does not do anything funny behind read-cache's back. The actual patch is less valueable than the summary above. But if anyone wants to re-identify the above sites. Applying this patch, then this: diff --git a/cache.h b/cache.h index 430d021..1692891 100644 --- a/cache.h +++ b/cache.h @@ -267,7 +267,7 @@ static inline unsigned int canon_mode(unsigned int mode) #define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1) struct index_state { - struct cache_entry **cache; + const struct cache_entry **cache; unsigned int version; unsigned int cache_nr, cache_alloc, cache_changed; struct string_list *resolve_undo; will help quickly identify them without bogus warnings. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f8abaeb commit 9c5e6c8

29 files changed

+93
-85
lines changed

builtin/apply.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsig
29992999
return 0;
30003000
}
30013001

3002-
static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
3002+
static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
30033003
{
30043004
if (!ce)
30053005
return 0;
@@ -3117,7 +3117,7 @@ static struct patch *previous_patch(struct patch *patch, int *gone)
31173117
return previous;
31183118
}
31193119

3120-
static int verify_index_match(struct cache_entry *ce, struct stat *st)
3120+
static int verify_index_match(const struct cache_entry *ce, struct stat *st)
31213121
{
31223122
if (S_ISGITLINK(ce->ce_mode)) {
31233123
if (!S_ISDIR(st->st_mode))
@@ -3130,7 +3130,7 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
31303130
#define SUBMODULE_PATCH_WITHOUT_INDEX 1
31313131

31323132
static int load_patch_target(struct strbuf *buf,
3133-
struct cache_entry *ce,
3133+
const struct cache_entry *ce,
31343134
struct stat *st,
31353135
const char *name,
31363136
unsigned expected_mode)
@@ -3160,7 +3160,8 @@ static int load_patch_target(struct strbuf *buf,
31603160
* we read from the result of a previous diff.
31613161
*/
31623162
static int load_preimage(struct image *image,
3163-
struct patch *patch, struct stat *st, struct cache_entry *ce)
3163+
struct patch *patch, struct stat *st,
3164+
const struct cache_entry *ce)
31643165
{
31653166
struct strbuf buf = STRBUF_INIT;
31663167
size_t len;
@@ -3273,7 +3274,7 @@ static int load_current(struct image *image, struct patch *patch)
32733274
}
32743275

32753276
static int try_threeway(struct image *image, struct patch *patch,
3276-
struct stat *st, struct cache_entry *ce)
3277+
struct stat *st, const struct cache_entry *ce)
32773278
{
32783279
unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
32793280
struct strbuf buf = STRBUF_INIT;
@@ -3343,7 +3344,7 @@ static int try_threeway(struct image *image, struct patch *patch,
33433344
return 0;
33443345
}
33453346

3346-
static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
3347+
static int apply_data(struct patch *patch, struct stat *st, const struct cache_entry *ce)
33473348
{
33483349
struct image image;
33493350

builtin/checkout.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ static int read_tree_some(struct tree *tree, const char **pathspec)
9797
return 0;
9898
}
9999

100-
static int skip_same_name(struct cache_entry *ce, int pos)
100+
static int skip_same_name(const struct cache_entry *ce, int pos)
101101
{
102102
while (++pos < active_nr &&
103103
!strcmp(active_cache[pos]->name, ce->name))
104104
; /* skip */
105105
return pos;
106106
}
107107

108-
static int check_stage(int stage, struct cache_entry *ce, int pos)
108+
static int check_stage(int stage, const struct cache_entry *ce, int pos)
109109
{
110110
while (pos < active_nr &&
111111
!strcmp(active_cache[pos]->name, ce->name)) {
@@ -119,7 +119,7 @@ static int check_stage(int stage, struct cache_entry *ce, int pos)
119119
return error(_("path '%s' does not have their version"), ce->name);
120120
}
121121

122-
static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
122+
static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
123123
{
124124
unsigned seen = 0;
125125
const char *name = ce->name;
@@ -321,7 +321,7 @@ static int checkout_paths(const struct checkout_opts *opts,
321321

322322
/* Any unmerged paths? */
323323
for (pos = 0; pos < active_nr; pos++) {
324-
struct cache_entry *ce = active_cache[pos];
324+
const struct cache_entry *ce = active_cache[pos];
325325
if (ce->ce_flags & CE_MATCHED) {
326326
if (!ce_stage(ce))
327327
continue;

builtin/clean.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
221221
struct dir_entry *ent = dir.entries[i];
222222
int len, pos;
223223
int matches = 0;
224-
struct cache_entry *ce;
224+
const struct cache_entry *ce;
225225
struct stat st;
226226

227227
/*

builtin/commit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
205205
}
206206

207207
for (i = 0; i < active_nr; i++) {
208-
struct cache_entry *ce = active_cache[i];
208+
const struct cache_entry *ce = active_cache[i];
209209
struct string_list_item *item;
210210

211211
if (ce->ce_flags & CE_UPDATE)

builtin/grep.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
376376
read_cache();
377377

378378
for (nr = 0; nr < active_nr; nr++) {
379-
struct cache_entry *ce = active_cache[nr];
379+
const struct cache_entry *ce = active_cache[nr];
380380
if (!S_ISREG(ce->ce_mode))
381381
continue;
382382
if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))

builtin/ls-files.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static void show_killed_files(struct dir_struct *dir)
127127
}
128128
}
129129

130-
static void show_ce_entry(const char *tag, struct cache_entry *ce)
130+
static void show_ce_entry(const char *tag, const struct cache_entry *ce)
131131
{
132132
int len = max_prefix_len;
133133

@@ -165,7 +165,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
165165
}
166166
write_name(ce->name, ce_namelen(ce));
167167
if (debug_mode) {
168-
struct stat_data *sd = &ce->ce_stat_data;
168+
const struct stat_data *sd = &ce->ce_stat_data;
169169

170170
printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
171171
printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
@@ -203,7 +203,7 @@ static void show_ru_info(void)
203203
}
204204
}
205205

206-
static int ce_excluded(struct dir_struct *dir, struct cache_entry *ce)
206+
static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce)
207207
{
208208
int dtype = ce_to_dtype(ce);
209209
return is_excluded(dir, ce->name, &dtype);
@@ -223,7 +223,7 @@ static void show_files(struct dir_struct *dir)
223223
}
224224
if (show_cached || show_stage) {
225225
for (i = 0; i < active_nr; i++) {
226-
struct cache_entry *ce = active_cache[i];
226+
const struct cache_entry *ce = active_cache[i];
227227
if ((dir->flags & DIR_SHOW_IGNORED) &&
228228
!ce_excluded(dir, ce))
229229
continue;
@@ -237,7 +237,7 @@ static void show_files(struct dir_struct *dir)
237237
}
238238
if (show_deleted || show_modified) {
239239
for (i = 0; i < active_nr; i++) {
240-
struct cache_entry *ce = active_cache[i];
240+
const struct cache_entry *ce = active_cache[i];
241241
struct stat st;
242242
int err;
243243
if ((dir->flags & DIR_SHOW_IGNORED) &&
@@ -273,7 +273,7 @@ static void prune_cache(const char *prefix)
273273
last = active_nr;
274274
while (last > first) {
275275
int next = (last + first) >> 1;
276-
struct cache_entry *ce = active_cache[next];
276+
const struct cache_entry *ce = active_cache[next];
277277
if (!strncmp(ce->name, prefix, max_prefix_len)) {
278278
first = next+1;
279279
continue;

builtin/merge-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int merge_entry(int pos, const char *path)
1616
die("git merge-index: %s not in the cache", path);
1717
found = 0;
1818
do {
19-
struct cache_entry *ce = active_cache[pos];
19+
const struct cache_entry *ce = active_cache[pos];
2020
int stage = ce_stage(ce);
2121

2222
if (strcmp(ce->name, path))
@@ -58,7 +58,7 @@ static void merge_all(void)
5858
{
5959
int i;
6060
for (i = 0; i < active_nr; i++) {
61-
struct cache_entry *ce = active_cache[i];
61+
const struct cache_entry *ce = active_cache[i];
6262
if (!ce_stage(ce))
6363
continue;
6464
i += merge_entry(i, ce->name)-1;

builtin/merge.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ static int suggest_conflicts(int renormalizing)
889889
die_errno(_("Could not open '%s' for writing"), filename);
890890
fprintf(fp, "\nConflicts:\n");
891891
for (pos = 0; pos < active_nr; pos++) {
892-
struct cache_entry *ce = active_cache[pos];
892+
const struct cache_entry *ce = active_cache[pos];
893893

894894
if (ce_stage(ce)) {
895895
fprintf(fp, "\t%s\n", ce->name);

builtin/rm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int check_submodules_use_gitfiles(void)
6767
for (i = 0; i < list.nr; i++) {
6868
const char *name = list.entry[i].name;
6969
int pos;
70-
struct cache_entry *ce;
70+
const struct cache_entry *ce;
7171
struct stat st;
7272

7373
pos = cache_name_pos(name, strlen(name));
@@ -120,7 +120,7 @@ static int check_local_mod(unsigned char *head, int index_only)
120120
for (i = 0; i < list.nr; i++) {
121121
struct stat st;
122122
int pos;
123-
struct cache_entry *ce;
123+
const struct cache_entry *ce;
124124
const char *name = list.entry[i].name;
125125
unsigned char sha1[20];
126126
unsigned mode;
@@ -321,7 +321,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
321321
seen = xcalloc(i, 1);
322322

323323
for (i = 0; i < active_nr; i++) {
324-
struct cache_entry *ce = active_cache[i];
324+
const struct cache_entry *ce = active_cache[i];
325325
if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
326326
continue;
327327
ALLOC_GROW(list.entry, list.nr + 1, list.alloc);

builtin/update-index.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int process_lstat_error(const char *path, int err)
8383
return error("lstat(\"%s\"): %s", path, strerror(errno));
8484
}
8585

86-
static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
86+
static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
8787
{
8888
int option, size;
8989
struct cache_entry *ce;
@@ -142,7 +142,7 @@ static int process_directory(const char *path, int len, struct stat *st)
142142

143143
/* Exact match: file or existing gitlink */
144144
if (pos >= 0) {
145-
struct cache_entry *ce = active_cache[pos];
145+
const struct cache_entry *ce = active_cache[pos];
146146
if (S_ISGITLINK(ce->ce_mode)) {
147147

148148
/* Do nothing to the index if there is no HEAD! */
@@ -158,7 +158,7 @@ static int process_directory(const char *path, int len, struct stat *st)
158158
/* Inexact match: is there perhaps a subdirectory match? */
159159
pos = -pos-1;
160160
while (pos < active_nr) {
161-
struct cache_entry *ce = active_cache[pos++];
161+
const struct cache_entry *ce = active_cache[pos++];
162162

163163
if (strncmp(ce->name, path, len))
164164
break;
@@ -183,7 +183,7 @@ static int process_path(const char *path)
183183
{
184184
int pos, len;
185185
struct stat st;
186-
struct cache_entry *ce;
186+
const struct cache_entry *ce;
187187

188188
len = strlen(path);
189189
if (has_symlink_leading_path(path, len))
@@ -448,7 +448,7 @@ static int unresolve_one(const char *path)
448448
/* already merged */
449449
pos = unmerge_cache_entry_at(pos);
450450
if (pos < active_nr) {
451-
struct cache_entry *ce = active_cache[pos];
451+
const struct cache_entry *ce = active_cache[pos];
452452
if (ce_stage(ce) &&
453453
ce_namelen(ce) == namelen &&
454454
!memcmp(ce->name, path, namelen))
@@ -462,7 +462,7 @@ static int unresolve_one(const char *path)
462462
*/
463463
pos = -pos-1;
464464
if (pos < active_nr) {
465-
struct cache_entry *ce = active_cache[pos];
465+
const struct cache_entry *ce = active_cache[pos];
466466
if (ce_namelen(ce) == namelen &&
467467
!memcmp(ce->name, path, namelen)) {
468468
fprintf(stderr,
@@ -558,7 +558,7 @@ static int do_reupdate(int ac, const char **av,
558558
has_head = 0;
559559
redo:
560560
for (pos = 0; pos < active_nr; pos++) {
561-
struct cache_entry *ce = active_cache[pos];
561+
const struct cache_entry *ce = active_cache[pos];
562562
struct cache_entry *old = NULL;
563563
int save_nr;
564564

cache-tree.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
149149
cache_tree_invalidate_path(down->cache_tree, slash + 1);
150150
}
151151

152-
static int verify_cache(struct cache_entry **cache,
152+
static int verify_cache(const struct cache_entry * const *cache,
153153
int entries, int flags)
154154
{
155155
int i, funny;
@@ -158,7 +158,7 @@ static int verify_cache(struct cache_entry **cache,
158158
/* Verify that the tree is merged */
159159
funny = 0;
160160
for (i = 0; i < entries; i++) {
161-
struct cache_entry *ce = cache[i];
161+
const struct cache_entry *ce = cache[i];
162162
if (ce_stage(ce)) {
163163
if (silent)
164164
return -1;
@@ -234,7 +234,7 @@ int cache_tree_fully_valid(struct cache_tree *it)
234234
}
235235

236236
static int update_one(struct cache_tree *it,
237-
struct cache_entry **cache,
237+
const struct cache_entry * const *cache,
238238
int entries,
239239
const char *base,
240240
int baselen,
@@ -265,7 +265,7 @@ static int update_one(struct cache_tree *it,
265265
*/
266266
i = 0;
267267
while (i < entries) {
268-
struct cache_entry *ce = cache[i];
268+
const struct cache_entry *ce = cache[i];
269269
struct cache_tree_sub *sub;
270270
const char *path, *slash;
271271
int pathlen, sublen, subcnt, subskip;
@@ -312,7 +312,7 @@ static int update_one(struct cache_tree *it,
312312

313313
i = 0;
314314
while (i < entries) {
315-
struct cache_entry *ce = cache[i];
315+
const struct cache_entry *ce = cache[i];
316316
struct cache_tree_sub *sub;
317317
const char *path, *slash;
318318
int pathlen, entlen;
@@ -397,7 +397,7 @@ static int update_one(struct cache_tree *it,
397397
}
398398

399399
int cache_tree_update(struct cache_tree *it,
400-
struct cache_entry **cache,
400+
const struct cache_entry * const *cache,
401401
int entries,
402402
int flags)
403403
{
@@ -599,8 +599,8 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
599599
was_valid = cache_tree_fully_valid(active_cache_tree);
600600
if (!was_valid) {
601601
if (cache_tree_update(active_cache_tree,
602-
active_cache, active_nr,
603-
flags) < 0)
602+
(const struct cache_entry * const *)active_cache,
603+
active_nr, flags) < 0)
604604
return WRITE_TREE_UNMERGED_INDEX;
605605
if (0 <= newfd) {
606606
if (!write_cache(newfd, active_cache, active_nr) &&
@@ -701,5 +701,6 @@ int update_main_cache_tree(int flags)
701701
if (!the_index.cache_tree)
702702
the_index.cache_tree = cache_tree();
703703
return cache_tree_update(the_index.cache_tree,
704-
the_index.cache, the_index.cache_nr, flags);
704+
(const struct cache_entry * const *)the_index.cache,
705+
the_index.cache_nr, flags);
705706
}

cache-tree.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);
3030
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3131

3232
int cache_tree_fully_valid(struct cache_tree *);
33-
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
33+
int cache_tree_update(struct cache_tree *, const struct cache_entry * const *, int, int);
3434

3535
int update_main_cache_tree(int);
3636

cache.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
476476
extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
477477
extern int add_file_to_index(struct index_state *, const char *path, int flags);
478478
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
479-
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
479+
extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
480480
extern int index_name_is_other(const struct index_state *, const char *, int);
481481
extern void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
482482

diff.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
25862586
*/
25872587
static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
25882588
{
2589-
struct cache_entry *ce;
2589+
const struct cache_entry *ce;
25902590
struct stat st;
25912591
int pos, len;
25922592

0 commit comments

Comments
 (0)