Skip to content

Commit f24e079

Browse files
committed
Merge branch 'jh/close-index-before-stat' into maint
The timestamp of the index file is now taken after the file is closed, to help Windows, on which a stale timestamp is reported by fstat() on a file that is opened for writing and data was written but not yet closed. * jh/close-index-before-stat: read-cache: close index.lock in do_write_index
2 parents f4683b4 + 9f41c7a commit f24e079

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

read-cache.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,9 +2187,10 @@ void update_index_if_able(struct index_state *istate, struct lock_file *lockfile
21872187
rollback_lock_file(lockfile);
21882188
}
21892189

2190-
static int do_write_index(struct index_state *istate, int newfd,
2190+
static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
21912191
int strip_extensions)
21922192
{
2193+
int newfd = tempfile->fd;
21932194
git_SHA_CTX c;
21942195
struct cache_header hdr;
21952196
int i, err, removed, extended, hdr_version;
@@ -2298,7 +2299,11 @@ static int do_write_index(struct index_state *istate, int newfd,
22982299
return -1;
22992300
}
23002301

2301-
if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
2302+
if (ce_flush(&c, newfd, istate->sha1))
2303+
return -1;
2304+
if (close_tempfile(tempfile))
2305+
return error(_("could not close '%s'"), tempfile->filename.buf);
2306+
if (stat(tempfile->filename.buf, &st))
23022307
return -1;
23032308
istate->timestamp.sec = (unsigned int)st.st_mtime;
23042309
istate->timestamp.nsec = ST_MTIME_NSEC(st);
@@ -2321,7 +2326,7 @@ static int commit_locked_index(struct lock_file *lk)
23212326
static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
23222327
unsigned flags)
23232328
{
2324-
int ret = do_write_index(istate, get_lock_file_fd(lock), 0);
2329+
int ret = do_write_index(istate, &lock->tempfile, 0);
23252330
if (ret)
23262331
return ret;
23272332
assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
@@ -2418,7 +2423,7 @@ static int write_shared_index(struct index_state *istate,
24182423
return do_write_locked_index(istate, lock, flags);
24192424
}
24202425
move_cache_to_base_index(istate);
2421-
ret = do_write_index(si->base, fd, 1);
2426+
ret = do_write_index(si->base, &temporary_sharedindex, 1);
24222427
if (ret) {
24232428
delete_tempfile(&temporary_sharedindex);
24242429
return ret;

0 commit comments

Comments
 (0)