Skip to content

Commit ccdc4ec

Browse files
committed
diff/status: refactor opportunistic index update
When we had to refresh the index internally before running diff or status, we opportunistically updated the $GIT_INDEX_FILE so that later invocation of git can use the lstat(2) we already did in this invocation. Make them share a helper function to do so. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87b5054 commit ccdc4ec

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

builtin/commit.c

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

10921092
fd = hold_locked_index(&index_lock, 0);
1093-
if (0 <= fd) {
1094-
if (active_cache_changed &&
1095-
!write_cache(fd, active_cache, active_nr))
1096-
commit_locked_index(&index_lock);
1097-
else
1098-
rollback_lock_file(&index_lock);
1099-
}
1093+
if (0 <= fd)
1094+
update_index_if_able(&the_index, &index_lock);
11001095

11011096
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
11021097
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
@@ -520,6 +520,7 @@ extern NORETURN void unable_to_lock_index_die(const char *path, int err);
520520
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
521521
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
522522
extern int commit_lock_file(struct lock_file *);
523+
extern void update_index_if_able(struct index_state *, struct lock_file *);
523524

524525
extern int hold_locked_index(struct lock_file *, int);
525526
extern int commit_locked_index(struct lock_file *);

read-cache.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,18 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
15451545
return result;
15461546
}
15471547

1548+
/*
1549+
* Opportunisticly update the index but do not complain if we can't
1550+
*/
1551+
void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
1552+
{
1553+
if (istate->cache_changed &&
1554+
!write_index(istate, lockfile->fd))
1555+
commit_locked_index(lockfile);
1556+
else
1557+
rollback_lock_file(lockfile);
1558+
}
1559+
15481560
int write_index(struct index_state *istate, int newfd)
15491561
{
15501562
git_SHA_CTX c;

0 commit comments

Comments
 (0)