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 <gitster@pobox.com>
Junio C Hamano committed
Mar 21, 2011 at 10:16 UTC
ccdc4ec3044bd108ae1e20d772f078c10df114b3
4 files changed
+16
-13
builtin/commit.c
+2
-7
@@ -1090,13 +1090,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1090
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
1091
1092
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);
1095
1096
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1097
s.in_merge = in_merge;
builtin/diff.c
+1
-6
@@ -197,12 +197,7 @@ static void refresh_index_quietly(void)
197
discard_cache();
198
read_cache();
199
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);
201
}
202
203
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
cache.h
+1
@@ -520,6 +520,7 @@ extern NORETURN void unable_to_lock_index_die(const char *path, int err);
520
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
521
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
522
extern int commit_lock_file(struct lock_file *);
523
+extern void update_index_if_able(struct index_state *, struct lock_file *);
524
525
extern int hold_locked_index(struct lock_file *, int);
526
extern int commit_locked_index(struct lock_file *);
read-cache.c
+12
@@ -1545,6 +1545,18 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
1545
return result;
1546
}
1547
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
+
1560
int write_index(struct index_state *istate, int newfd)
1561
{
1562
git_SHA_CTX c;