Skip to content

Commit e93021b

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent d4a2024 commit e93021b

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ struct index_state {
286286
initialized : 1;
287287
struct hashmap name_hash;
288288
struct hashmap dir_hash;
289+
unsigned char sha1[20];
289290
};
290291

291292
extern struct index_state the_index;

read-cache.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ int read_index_from(struct index_state *istate, const char *path)
14811481
if (verify_hdr(hdr, mmap_size) < 0)
14821482
goto unmap;
14831483

1484+
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
14841485
istate->version = ntohl(hdr->hdr_version);
14851486
istate->cache_nr = ntohl(hdr->hdr_entries);
14861487
istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -1616,7 +1617,7 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,
16161617
(ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
16171618
}
16181619

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)
16201621
{
16211622
unsigned int left = write_buffer_len;
16221623

@@ -1634,6 +1635,7 @@ static int ce_flush(git_SHA_CTX *context, int fd)
16341635

16351636
/* Append the SHA1 signature at the end */
16361637
git_SHA1_Final(write_buffer + left, context);
1638+
hashcpy(sha1, write_buffer + left);
16371639
left += 20;
16381640
return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
16391641
}
@@ -1872,7 +1874,7 @@ static int do_write_index(struct index_state *istate, int newfd)
18721874
return -1;
18731875
}
18741876

1875-
if (ce_flush(&c, newfd) || fstat(newfd, &st))
1877+
if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
18761878
return -1;
18771879
istate->timestamp.sec = (unsigned int)st.st_mtime;
18781880
istate->timestamp.nsec = ST_MTIME_NSEC(st);

unpack-trees.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
10461046
o->result.timestamp.sec = o->src_index->timestamp.sec;
10471047
o->result.timestamp.nsec = o->src_index->timestamp.nsec;
10481048
o->result.version = o->src_index->version;
1049+
hashcpy(o->result.sha1, o->src_index->sha1);
10491050
o->merge_size = len;
10501051
mark_all_ce_unused(o->src_index);
10511052

0 commit comments

Comments
 (0)