Skip to content

Commit b74c90f

Browse files
Martin Ågrengitster
authored andcommitted
read_cache: roll back lock in update_index_if_able()
`update_index_if_able()` used to always commit the lock or roll it back. Commit 03b8664 (read-cache: new API write_locked_index instead of write_index/write_cache, 2014-06-13) stopped rolling it back in case a write was not even attempted. This change in behavior is not motivated in the commit message and appears to be accidental: the `else`-path was removed, although that changed the behavior in case the `if` shortcuts. Reintroduce the rollback and document this behavior. While at it, move the documentation on this function from the function definition to the function declaration in cache.h. If `write_locked_index(..., COMMIT_LOCK)` fails, it will roll back the lock for us (see the previous commit). Noticed-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent df60cf5 commit b74c90f

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
734734
extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
735735
extern struct cache_entry *refresh_cache_entry(struct cache_entry *, unsigned int);
736736

737+
/*
738+
* Opportunistically update the index but do not complain if we can't.
739+
* The lockfile is always committed or rolled back.
740+
*/
737741
extern void update_index_if_able(struct index_state *, struct lock_file *);
738742

739743
extern int hold_locked_index(struct lock_file *, int);

read-cache.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,14 +2176,13 @@ static int has_racy_timestamp(struct index_state *istate)
21762176
return 0;
21772177
}
21782178

2179-
/*
2180-
* Opportunistically update the index but do not complain if we can't
2181-
*/
21822179
void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
21832180
{
21842181
if ((istate->cache_changed || has_racy_timestamp(istate)) &&
21852182
verify_index(istate))
21862183
write_locked_index(istate, lockfile, COMMIT_LOCK);
2184+
else
2185+
rollback_lock_file(lockfile);
21872186
}
21882187

21892188
/*

0 commit comments

Comments
 (0)