Skip to content

Commit

Permalink
Convert "struct cache_entry *" to "const ..." wherever possible
Browse files Browse the repository at this point in the history
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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pclouds authored and gitster committed Jul 9, 2013
1 parent f8abaeb commit 9c5e6c8
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 85 deletions.
13 changes: 7 additions & 6 deletions builtin/apply.c
Expand Up @@ -2999,7 +2999,7 @@ static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsig
return 0;
}

static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
{
if (!ce)
return 0;
Expand Down Expand Up @@ -3117,7 +3117,7 @@ static struct patch *previous_patch(struct patch *patch, int *gone)
return previous;
}

static int verify_index_match(struct cache_entry *ce, struct stat *st)
static int verify_index_match(const struct cache_entry *ce, struct stat *st)
{
if (S_ISGITLINK(ce->ce_mode)) {
if (!S_ISDIR(st->st_mode))
Expand All @@ -3130,7 +3130,7 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
#define SUBMODULE_PATCH_WITHOUT_INDEX 1

static int load_patch_target(struct strbuf *buf,
struct cache_entry *ce,
const struct cache_entry *ce,
struct stat *st,
const char *name,
unsigned expected_mode)
Expand Down Expand Up @@ -3160,7 +3160,8 @@ static int load_patch_target(struct strbuf *buf,
* we read from the result of a previous diff.
*/
static int load_preimage(struct image *image,
struct patch *patch, struct stat *st, struct cache_entry *ce)
struct patch *patch, struct stat *st,
const struct cache_entry *ce)
{
struct strbuf buf = STRBUF_INIT;
size_t len;
Expand Down Expand Up @@ -3273,7 +3274,7 @@ static int load_current(struct image *image, struct patch *patch)
}

static int try_threeway(struct image *image, struct patch *patch,
struct stat *st, struct cache_entry *ce)
struct stat *st, const struct cache_entry *ce)
{
unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
struct strbuf buf = STRBUF_INIT;
Expand Down Expand Up @@ -3343,7 +3344,7 @@ static int try_threeway(struct image *image, struct patch *patch,
return 0;
}

static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
static int apply_data(struct patch *patch, struct stat *st, const struct cache_entry *ce)
{
struct image image;

Expand Down
8 changes: 4 additions & 4 deletions builtin/checkout.c
Expand Up @@ -97,15 +97,15 @@ static int read_tree_some(struct tree *tree, const char **pathspec)
return 0;
}

static int skip_same_name(struct cache_entry *ce, int pos)
static int skip_same_name(const struct cache_entry *ce, int pos)
{
while (++pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name))
; /* skip */
return pos;
}

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

static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
{
unsigned seen = 0;
const char *name = ce->name;
Expand Down Expand Up @@ -321,7 +321,7 @@ static int checkout_paths(const struct checkout_opts *opts,

/* Any unmerged paths? */
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];
if (ce->ce_flags & CE_MATCHED) {
if (!ce_stage(ce))
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/clean.c
Expand Up @@ -221,7 +221,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
struct dir_entry *ent = dir.entries[i];
int len, pos;
int matches = 0;
struct cache_entry *ce;
const struct cache_entry *ce;
struct stat st;

/*
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Expand Up @@ -205,7 +205,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
}

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

if (ce->ce_flags & CE_UPDATE)
Expand Down
2 changes: 1 addition & 1 deletion builtin/grep.c
Expand Up @@ -376,7 +376,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
read_cache();

for (nr = 0; nr < active_nr; nr++) {
struct cache_entry *ce = active_cache[nr];
const struct cache_entry *ce = active_cache[nr];
if (!S_ISREG(ce->ce_mode))
continue;
if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
Expand Down
12 changes: 6 additions & 6 deletions builtin/ls-files.c
Expand Up @@ -127,7 +127,7 @@ static void show_killed_files(struct dir_struct *dir)
}
}

static void show_ce_entry(const char *tag, struct cache_entry *ce)
static void show_ce_entry(const char *tag, const struct cache_entry *ce)
{
int len = max_prefix_len;

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

printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
Expand Down Expand Up @@ -203,7 +203,7 @@ static void show_ru_info(void)
}
}

static int ce_excluded(struct dir_struct *dir, struct cache_entry *ce)
static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce)
{
int dtype = ce_to_dtype(ce);
return is_excluded(dir, ce->name, &dtype);
Expand All @@ -223,7 +223,7 @@ static void show_files(struct dir_struct *dir)
}
if (show_cached || show_stage) {
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
const struct cache_entry *ce = active_cache[i];
if ((dir->flags & DIR_SHOW_IGNORED) &&
!ce_excluded(dir, ce))
continue;
Expand All @@ -237,7 +237,7 @@ static void show_files(struct dir_struct *dir)
}
if (show_deleted || show_modified) {
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
const struct cache_entry *ce = active_cache[i];
struct stat st;
int err;
if ((dir->flags & DIR_SHOW_IGNORED) &&
Expand Down Expand Up @@ -273,7 +273,7 @@ static void prune_cache(const char *prefix)
last = active_nr;
while (last > first) {
int next = (last + first) >> 1;
struct cache_entry *ce = active_cache[next];
const struct cache_entry *ce = active_cache[next];
if (!strncmp(ce->name, prefix, max_prefix_len)) {
first = next+1;
continue;
Expand Down
4 changes: 2 additions & 2 deletions builtin/merge-index.c
Expand Up @@ -16,7 +16,7 @@ static int merge_entry(int pos, const char *path)
die("git merge-index: %s not in the cache", path);
found = 0;
do {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];
int stage = ce_stage(ce);

if (strcmp(ce->name, path))
Expand Down Expand Up @@ -58,7 +58,7 @@ static void merge_all(void)
{
int i;
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
const struct cache_entry *ce = active_cache[i];
if (!ce_stage(ce))
continue;
i += merge_entry(i, ce->name)-1;
Expand Down
2 changes: 1 addition & 1 deletion builtin/merge.c
Expand Up @@ -889,7 +889,7 @@ static int suggest_conflicts(int renormalizing)
die_errno(_("Could not open '%s' for writing"), filename);
fprintf(fp, "\nConflicts:\n");
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];

if (ce_stage(ce)) {
fprintf(fp, "\t%s\n", ce->name);
Expand Down
6 changes: 3 additions & 3 deletions builtin/rm.c
Expand Up @@ -67,7 +67,7 @@ static int check_submodules_use_gitfiles(void)
for (i = 0; i < list.nr; i++) {
const char *name = list.entry[i].name;
int pos;
struct cache_entry *ce;
const struct cache_entry *ce;
struct stat st;

pos = cache_name_pos(name, strlen(name));
Expand Down Expand Up @@ -120,7 +120,7 @@ static int check_local_mod(unsigned char *head, int index_only)
for (i = 0; i < list.nr; i++) {
struct stat st;
int pos;
struct cache_entry *ce;
const struct cache_entry *ce;
const char *name = list.entry[i].name;
unsigned char sha1[20];
unsigned mode;
Expand Down Expand Up @@ -321,7 +321,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
seen = xcalloc(i, 1);

for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
const struct cache_entry *ce = active_cache[i];
if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
continue;
ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
Expand Down
14 changes: 7 additions & 7 deletions builtin/update-index.c
Expand Up @@ -83,7 +83,7 @@ static int process_lstat_error(const char *path, int err)
return error("lstat(\"%s\"): %s", path, strerror(errno));
}

static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
{
int option, size;
struct cache_entry *ce;
Expand Down Expand Up @@ -142,7 +142,7 @@ static int process_directory(const char *path, int len, struct stat *st)

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

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

if (strncmp(ce->name, path, len))
break;
Expand All @@ -183,7 +183,7 @@ static int process_path(const char *path)
{
int pos, len;
struct stat st;
struct cache_entry *ce;
const struct cache_entry *ce;

len = strlen(path);
if (has_symlink_leading_path(path, len))
Expand Down Expand Up @@ -448,7 +448,7 @@ static int unresolve_one(const char *path)
/* already merged */
pos = unmerge_cache_entry_at(pos);
if (pos < active_nr) {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];
if (ce_stage(ce) &&
ce_namelen(ce) == namelen &&
!memcmp(ce->name, path, namelen))
Expand All @@ -462,7 +462,7 @@ static int unresolve_one(const char *path)
*/
pos = -pos-1;
if (pos < active_nr) {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];
if (ce_namelen(ce) == namelen &&
!memcmp(ce->name, path, namelen)) {
fprintf(stderr,
Expand Down Expand Up @@ -558,7 +558,7 @@ static int do_reupdate(int ac, const char **av,
has_head = 0;
redo:
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
const struct cache_entry *ce = active_cache[pos];
struct cache_entry *old = NULL;
int save_nr;

Expand Down
19 changes: 10 additions & 9 deletions cache-tree.c
Expand Up @@ -149,7 +149,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
cache_tree_invalidate_path(down->cache_tree, slash + 1);
}

static int verify_cache(struct cache_entry **cache,
static int verify_cache(const struct cache_entry * const *cache,
int entries, int flags)
{
int i, funny;
Expand All @@ -158,7 +158,7 @@ static int verify_cache(struct cache_entry **cache,
/* Verify that the tree is merged */
funny = 0;
for (i = 0; i < entries; i++) {
struct cache_entry *ce = cache[i];
const struct cache_entry *ce = cache[i];
if (ce_stage(ce)) {
if (silent)
return -1;
Expand Down Expand Up @@ -234,7 +234,7 @@ int cache_tree_fully_valid(struct cache_tree *it)
}

static int update_one(struct cache_tree *it,
struct cache_entry **cache,
const struct cache_entry * const *cache,
int entries,
const char *base,
int baselen,
Expand Down Expand Up @@ -265,7 +265,7 @@ static int update_one(struct cache_tree *it,
*/
i = 0;
while (i < entries) {
struct cache_entry *ce = cache[i];
const struct cache_entry *ce = cache[i];
struct cache_tree_sub *sub;
const char *path, *slash;
int pathlen, sublen, subcnt, subskip;
Expand Down Expand Up @@ -312,7 +312,7 @@ static int update_one(struct cache_tree *it,

i = 0;
while (i < entries) {
struct cache_entry *ce = cache[i];
const struct cache_entry *ce = cache[i];
struct cache_tree_sub *sub;
const char *path, *slash;
int pathlen, entlen;
Expand Down Expand Up @@ -397,7 +397,7 @@ static int update_one(struct cache_tree *it,
}

int cache_tree_update(struct cache_tree *it,
struct cache_entry **cache,
const struct cache_entry * const *cache,
int entries,
int flags)
{
Expand Down Expand Up @@ -599,8 +599,8 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
was_valid = cache_tree_fully_valid(active_cache_tree);
if (!was_valid) {
if (cache_tree_update(active_cache_tree,
active_cache, active_nr,
flags) < 0)
(const struct cache_entry * const *)active_cache,
active_nr, flags) < 0)
return WRITE_TREE_UNMERGED_INDEX;
if (0 <= newfd) {
if (!write_cache(newfd, active_cache, active_nr) &&
Expand Down Expand Up @@ -701,5 +701,6 @@ int update_main_cache_tree(int flags)
if (!the_index.cache_tree)
the_index.cache_tree = cache_tree();
return cache_tree_update(the_index.cache_tree,
the_index.cache, the_index.cache_nr, flags);
(const struct cache_entry * const *)the_index.cache,
the_index.cache_nr, flags);
}
2 changes: 1 addition & 1 deletion cache-tree.h
Expand Up @@ -30,7 +30,7 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);

int cache_tree_fully_valid(struct cache_tree *);
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
int cache_tree_update(struct cache_tree *, const struct cache_entry * const *, int, int);

int update_main_cache_tree(int);

Expand Down
2 changes: 1 addition & 1 deletion cache.h
Expand Up @@ -476,7 +476,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
extern int add_file_to_index(struct index_state *, const char *path, int flags);
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
extern int index_name_is_other(const struct index_state *, const char *, int);
extern void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);

Expand Down
2 changes: 1 addition & 1 deletion diff.c
Expand Up @@ -2586,7 +2586,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
*/
static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
{
struct cache_entry *ce;
const struct cache_entry *ce;
struct stat st;
int pos, len;

Expand Down

0 comments on commit 9c5e6c8

Please sign in to comment.