From: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>

The extension contains a bitmap, one bit for each entry in the
index. If the n-th bit is zero, the n-th entry is considered
unchanged, we can ce_mark_uptodate() it without refreshing. If the bit
is non-zero and we found out the corresponding file is clean after
refresh, we can clear the bit.

The 'skipping refresh' bit is not in this patch yet as we would need
watchman. More details in later patches.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 cache.h      |  4 ++++
 read-cache.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/cache.h b/cache.h
index cc1f6f5..8f7b4b1 100644
--- a/cache.h
+++ b/cache.h
@@ -182,6 +182,8 @@ struct cache_entry {
 #define CE_VALID     (0x8000)
 #define CE_STAGESHIFT 12
 
+#define CE_WATCHMAN_DIRTY  (0x0001)
+
 /*
  * Range 0xFFFF0FFF in ce_flags is divided into
  * two parts: in-memory flags and on-disk ones.
@@ -320,6 +322,7 @@ static inline unsigned int canon_mode(unsigned int mode)
 #define CACHE_TREE_CHANGED     (1 << 5)
 #define SPLIT_INDEX_ORDERED    (1 << 6)
 #define UNTRACKED_CHANGED      (1 << 7)
+#define WATCHMAN_CHANGED       (1 << 8)
 
 struct split_index;
 struct untracked_cache;
@@ -344,6 +347,7 @@ struct index_state {
        struct untracked_cache *untracked;
        void *mmap;
        size_t mmap_size;
+       char *last_update;
 };
 
 extern struct index_state the_index;
diff --git a/read-cache.c b/read-cache.c
index 16fbdf6..85ef15b 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -19,6 +19,7 @@
 #include "split-index.h"
 #include "utf8.h"
 #include "shm.h"
+#include "ewah/ewok.h"
 
 static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
                                               unsigned int options);
@@ -41,11 +42,13 @@ static struct cache_entry *refresh_cache_entry(struct 
cache_entry *ce,
 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
 #define CACHE_EXT_LINK 0x6c696e6b        /* "link" */
 #define CACHE_EXT_UNTRACKED 0x554E5452   /* "UNTR" */
+#define CACHE_EXT_WATCHMAN 0x57414D41    /* "WAMA" */
 
 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
                 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
-                SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED)
+                SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | \
+                WATCHMAN_CHANGED)
 
 struct index_state the_index;
 static const char *alternate_index_output;
@@ -1224,8 +1227,13 @@ int refresh_index(struct index_state *istate, unsigned 
int flags,
                        continue;
 
                new = refresh_cache_ent(istate, ce, options, &cache_errno, 
&changed);
-               if (new == ce)
+               if (new == ce) {
+                       if (ce->ce_flags & CE_WATCHMAN_DIRTY) {
+                               ce->ce_flags          &= ~CE_WATCHMAN_DIRTY;
+                               istate->cache_changed |= WATCHMAN_CHANGED;
+                       }
                        continue;
+               }
                if (!new) {
                        const char *fmt;
 
@@ -1369,6 +1377,48 @@ static int verify_hdr(const struct cache_header *hdr, 
unsigned long size)
        return 0;
 }
 
+static void mark_no_watchman(size_t pos, void *data)
+{
+       struct index_state *istate = data;
+       assert(pos < istate->cache_nr);
+       istate->cache[pos]->ce_flags |= CE_WATCHMAN_DIRTY;
+}
+
+static int read_watchman_ext(struct index_state *istate, const void *data,
+                            unsigned long sz)
+{
+       struct ewah_bitmap *bitmap;
+       int ret, len;
+
+       if (memchr(data, 0, sz) == NULL)
+               return error("invalid extension");
+       len = strlen(data) + 1;
+       bitmap = ewah_new();
+       ret = ewah_read_mmap(bitmap, (const char *)data + len, sz - len);
+       if (ret != sz - len) {
+               ewah_free(bitmap);
+               return error("failed to parse ewah bitmap reading watchman 
index extension");
+       }
+       istate->last_update = xstrdup(data);
+       ewah_each_bit(bitmap, mark_no_watchman, istate);
+       ewah_free(bitmap);
+       return 0;
+}
+
+static void write_watchman_ext(struct strbuf *sb, struct index_state* istate)
+{
+       struct ewah_bitmap *bitmap;
+       int i;
+
+       strbuf_add(sb, istate->last_update, strlen(istate->last_update) + 1);
+       bitmap = ewah_new();
+       for (i = 0; i < istate->cache_nr; i++)
+               if (istate->cache[i]->ce_flags & CE_WATCHMAN_DIRTY)
+                       ewah_set(bitmap, i);
+       ewah_serialize_strbuf(bitmap, sb);
+       ewah_free(bitmap);
+}
+
 static int read_index_extension(struct index_state *istate,
                                const char *ext, void *data, unsigned long sz)
 {
@@ -1386,6 +1436,11 @@ static int read_index_extension(struct index_state 
*istate,
        case CACHE_EXT_UNTRACKED:
                istate->untracked = read_untracked_extension(data, sz);
                break;
+
+       case CACHE_EXT_WATCHMAN:
+               read_watchman_ext(istate, data, sz);
+               break;
+
        default:
                if (*ext < 'A' || 'Z' < *ext)
                        return error("index uses %.4s extension, which we do 
not understand",
@@ -1817,6 +1872,8 @@ int discard_index(struct index_state *istate)
        istate->untracked = NULL;
        istate->from_shm = 0;
        istate->to_shm   = 0;
+       free(istate->last_update);
+       istate->last_update = NULL;
        return 0;
 }
 
@@ -2214,6 +2271,16 @@ static int do_write_index(struct index_state *istate, 
int newfd,
                if (err)
                        return -1;
        }
+       if (!strip_extensions && istate->last_update) {
+               struct strbuf sb = STRBUF_INIT;
+
+               write_watchman_ext(&sb, istate);
+               err = write_index_ext_header(&c, newfd, CACHE_EXT_WATCHMAN, 
sb.len) < 0
+                       || ce_write(&c, newfd, sb.buf, sb.len) < 0;
+               strbuf_release(&sb);
+               if (err)
+                       return -1;
+       }
 
        if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
                return -1;
-- 
2.4.2.767.g62658d5-twtrsrc

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

Reply via email to