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.

In addition, there's a list of directories in the untracked-cache
to invalidate (because they have new or modified entries).

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 ++
 dir.h        |   3 ++
 read-cache.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 122 insertions(+), 2 deletions(-)

diff --git a/cache.h b/cache.h
index 0aeb994..f4f7eef 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/dir.h b/dir.h
index 3ec3fb0..3d540de 100644
--- a/dir.h
+++ b/dir.h
@@ -142,6 +142,9 @@ struct untracked_cache {
        int gitignore_invalidated;
        int dir_invalidated;
        int dir_opened;
+       /* watchman invalidation data */
+       unsigned int use_watchman : 1;
+       struct string_list invalid_untracked;
 };
 
 struct dir_struct {
diff --git a/read-cache.c b/read-cache.c
index 7215a17..59d892e 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;
@@ -1220,8 +1223,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;
 
@@ -1365,6 +1373,94 @@ 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;
+       uint32_t bitmap_size;
+       uint32_t untracked_nr;
+
+       if (memchr(data, 0, sz) == NULL)
+               return error("invalid extension");
+
+       len = strlen(data) + 1;
+       memcpy(&bitmap_size, (const char *)data + len, 4);
+       memcpy(&untracked_nr, (const char *)data + len + 4, 4);
+       untracked_nr = ntohl(untracked_nr);
+       bitmap_size = ntohl(bitmap_size);
+
+       bitmap = ewah_new();
+       ret = ewah_read_mmap(bitmap, (const char *)data + len + 8, bitmap_size);
+       if (ret != bitmap_size) {
+               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);
+
+       /*
+        * TODO: update the untracked cache from the untracked data in this
+        * extension.
+        */
+       return 0;
+}
+
+static int untracked_entry_append(struct string_list_item *item, void *sbvoid)
+{
+       struct strbuf *sb = sbvoid;
+
+       strbuf_addstr(sb, item->string);
+       strbuf_addch(sb, 0);
+       return 0;
+}
+
+void write_watchman_ext(struct strbuf *sb, struct index_state* istate)
+{
+       struct ewah_bitmap *bitmap;
+       int i;
+       int ewah_start;
+       int ewah_size = 0;
+       int fixup = 0;
+
+       strbuf_add(sb, istate->last_update, strlen(istate->last_update) + 1);
+       fixup = sb->len;
+       strbuf_add(sb, &ewah_size, 4); /* we'll fix this up later */
+       if (istate->untracked) {
+               uint32_t nr = istate->untracked->invalid_untracked.nr;
+               nr = htonl(nr);
+               strbuf_add(sb, &nr, 4);
+       } else {
+               /* zero */
+               strbuf_add(sb, &ewah_size, 4);
+       }
+
+       ewah_start = sb->len;
+       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);
+
+       /* fix up size field */
+       ewah_size = sb->len - ewah_start;
+       ewah_size = htonl(ewah_size);
+       memcpy(sb->buf + fixup, &ewah_size, 4);
+
+       if (istate->untracked)
+               for_each_string_list(&istate->untracked->invalid_untracked,
+                                    untracked_entry_append, sb);
+}
+
 static int read_index_extension(struct index_state *istate,
                                const char *ext, void *data, unsigned long sz)
 {
@@ -1382,6 +1478,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",
@@ -1808,6 +1909,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;
 }
 
@@ -2205,6 +2308,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