Re: [PATCH] Convert "struct cache_entry *" to "const ..." wherever possible

2013-07-09 Thread Junio C Hamano
Nguyễn Thái Ngọc Duy   writes:

> 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.

Nicely done and a very interesting result.  I quickly eyeballed the
output of

$ git grep -e 'struct cache_entry' --and --not -e 'const struct cache_entry'

and the result matches what I would expect to see.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] Convert "struct cache_entry *" to "const ..." wherever possible

2013-07-09 Thread Nguyễn Thái Ngọc Duy
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 
---
 builtin/apply.c| 13 +++--
 builtin/checkout.c |  8 
 builtin/clean.c|  2 +-
 builtin/commit.c   |  2 +-
 builtin/grep.c |  2 +-
 builtin/ls-files.c | 12 ++--
 builtin/merge-index.c  |  4 ++--
 builtin/merge.c|  2 +-
 builtin/rm.c   |  6 +++---
 builtin/update-index.c | 14 +++---
 cache-tree.c   | 19 ++-
 cache-tree.h   |  2 +-
 cache.h|  2 +-
 diff.c |  2 +-
 dir.c  |  6 +++---
 entry.c| 12 +++-
 merge-recursive.c  |  7 ---
 pathspec.c |  4 ++--
 read-cache.c   |  2 +-
 rerere.c   | 12 ++--
 resolve-undo.c |  6 +++---
 revision.c |  2 +-
 sequencer.c|  7 ---
 sha1_name.c|  4 ++--
 submodule.c|  2 +-
 test-dump-cache-tree.c |  4 +++-
 tree.c |  2 +-
 unpack-trees.c | 14 +++---
 wt-status.c|  4 ++--
 29 files changed, 93 insertions(+), 85 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 0e9b631..023bb3a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -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;
@@ -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))
@@ -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)
@@ -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;
@@ -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, con