read-cache: save index SHA-1 after reading
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Fri, 13 Jun 2014 12:19:35 +0000 (19:19 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 13 Jun 2014 18:49:39 +0000 (11:49 -0700)
Also update SHA-1 after writing. If we do not do that, the second
read_index() will see "initialized" variable already set and not read
.git/index again, which is fine, except istate->sha1 now has a stale
value.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
read-cache.c
unpack-trees.c
diff --git a/cache.h b/cache.h
index bf09d1cf4be5bd67171df604212ed752a100f69c..41cdcd063318bcfbbf6fdc1dbe1be71f2282a819 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -286,6 +286,7 @@ struct index_state {
                 initialized : 1;
        struct hashmap name_hash;
        struct hashmap dir_hash;
+       unsigned char sha1[20];
 };
 
 extern struct index_state the_index;
index d1ad2277208286d08cfbd88b10ce9f12ee1855b9..b4653c083a6af62ca583ad2f0c9dfb35594bdbcb 100644 (file)
@@ -1481,6 +1481,7 @@ int read_index_from(struct index_state *istate, const char *path)
        if (verify_hdr(hdr, mmap_size) < 0)
                goto unmap;
 
+       hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
        istate->version = ntohl(hdr->hdr_version);
        istate->cache_nr = ntohl(hdr->hdr_entries);
        istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -1616,7 +1617,7 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,
                (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
 }
 
-static int ce_flush(git_SHA_CTX *context, int fd)
+static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
 {
        unsigned int left = write_buffer_len;
 
@@ -1634,6 +1635,7 @@ static int ce_flush(git_SHA_CTX *context, int fd)
 
        /* Append the SHA1 signature at the end */
        git_SHA1_Final(write_buffer + left, context);
+       hashcpy(sha1, write_buffer + left);
        left += 20;
        return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
 }
@@ -1872,7 +1874,7 @@ static int do_write_index(struct index_state *istate, int newfd)
                        return -1;
        }
 
-       if (ce_flush(&c, newfd) || fstat(newfd, &st))
+       if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
                return -1;
        istate->timestamp.sec = (unsigned int)st.st_mtime;
        istate->timestamp.nsec = ST_MTIME_NSEC(st);
index 26f65c72fd09f9906be5bcee06024a066d67175b..f594932080ca5a733ae0ebe5d2584053905ec84e 100644 (file)
@@ -1046,6 +1046,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
        o->result.timestamp.sec = o->src_index->timestamp.sec;
        o->result.timestamp.nsec = o->src_index->timestamp.nsec;
        o->result.version = o->src_index->version;
+       hashcpy(o->result.sha1, o->src_index->sha1);
        o->merge_size = len;
        mark_all_ce_unused(o->src_index);