read-cache: save index SHA-1 after reading

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>

Nguyễn Thái Ngọc Duy committed Jun 13, 2014 at 19:19 UTC e93021b20a73af8ff51f9ddd8c98f44fd103f360
3 files changed +6 -2
cache.h
+1
@@ -286,6 +286,7 @@ struct index_state {
286 initialized : 1;
287 struct hashmap name_hash;
288 struct hashmap dir_hash;
289 + unsigned char sha1[20];
290 };
291
292 extern struct index_state the_index;
read-cache.c
+4 -2
@@ -1481,6 +1481,7 @@ int read_index_from(struct index_state *istate, const char *path)
1481 if (verify_hdr(hdr, mmap_size) < 0)
1482 goto unmap;
1483
1484 + hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
1485 istate->version = ntohl(hdr->hdr_version);
1486 istate->cache_nr = ntohl(hdr->hdr_entries);
1487 istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -1616,7 +1617,7 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,
1617 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
1618 }
1619
1619 -static int ce_flush(git_SHA_CTX *context, int fd)
1620 +static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
1621 {
1622 unsigned int left = write_buffer_len;
1623
@@ -1634,6 +1635,7 @@ static int ce_flush(git_SHA_CTX *context, int fd)
1635
1636 /* Append the SHA1 signature at the end */
1637 git_SHA1_Final(write_buffer + left, context);
1638 + hashcpy(sha1, write_buffer + left);
1639 left += 20;
1640 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
1641 }
@@ -1872,7 +1874,7 @@ static int do_write_index(struct index_state *istate, int newfd)
1874 return -1;
1875 }
1876
1875 - if (ce_flush(&c, newfd) || fstat(newfd, &st))
1877 + if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
1878 return -1;
1879 istate->timestamp.sec = (unsigned int)st.st_mtime;
1880 istate->timestamp.nsec = ST_MTIME_NSEC(st);
unpack-trees.c
+1
@@ -1046,6 +1046,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1046 o->result.timestamp.sec = o->src_index->timestamp.sec;
1047 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
1048 o->result.version = o->src_index->version;
1049 + hashcpy(o->result.sha1, o->src_index->sha1);
1050 o->merge_size = len;
1051 mark_all_ce_unused(o->src_index);
1052