Skip to content

Commit 44ec754

Browse files
committed
Merge branch 'jc/index-update-if-able' into maint
* jc/index-update-if-able: update $GIT_INDEX_FILE when there are racily clean entries diff/status: refactor opportunistic index update
2 parents be57695 + 483fbe2 commit 44ec754

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

builtin/commit.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,13 +1131,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
11311131
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
11321132

11331133
fd = hold_locked_index(&index_lock, 0);
1134-
if (0 <= fd) {
1135-
if (active_cache_changed &&
1136-
!write_cache(fd, active_cache, active_nr))
1137-
commit_locked_index(&index_lock);
1138-
else
1139-
rollback_lock_file(&index_lock);
1140-
}
1134+
if (0 <= fd)
1135+
update_index_if_able(&the_index, &index_lock);
11411136

11421137
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
11431138
s.in_merge = in_merge;

builtin/diff.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,7 @@ static void refresh_index_quietly(void)
197197
discard_cache();
198198
read_cache();
199199
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
200-
201-
if (active_cache_changed &&
202-
!write_cache(fd, active_cache, active_nr))
203-
commit_locked_index(lock_file);
204-
205-
rollback_lock_file(lock_file);
200+
update_index_if_able(&the_index, lock_file);
206201
}
207202

208203
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ extern NORETURN void unable_to_lock_index_die(const char *path, int err);
527527
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
528528
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
529529
extern int commit_lock_file(struct lock_file *);
530+
extern void update_index_if_able(struct index_state *, struct lock_file *);
530531

531532
extern int hold_locked_index(struct lock_file *, int);
532533
extern int commit_locked_index(struct lock_file *);

read-cache.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,31 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
15681568
return result;
15691569
}
15701570

1571+
static int has_racy_timestamp(struct index_state *istate)
1572+
{
1573+
int entries = istate->cache_nr;
1574+
int i;
1575+
1576+
for (i = 0; i < entries; i++) {
1577+
struct cache_entry *ce = istate->cache[i];
1578+
if (is_racy_timestamp(istate, ce))
1579+
return 1;
1580+
}
1581+
return 0;
1582+
}
1583+
1584+
/*
1585+
* Opportunisticly update the index but do not complain if we can't
1586+
*/
1587+
void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
1588+
{
1589+
if ((istate->cache_changed || has_racy_timestamp(istate)) &&
1590+
!write_index(istate, lockfile->fd))
1591+
commit_locked_index(lockfile);
1592+
else
1593+
rollback_lock_file(lockfile);
1594+
}
1595+
15711596
int write_index(struct index_state *istate, int newfd)
15721597
{
15731598
git_SHA_CTX c;

0 commit comments

Comments
 (0)