summaryrefslogtreecommitdiff
path: root/cache.h
diff options
context:
space:
mode:
authorMartin Ågren <martin.agren@gmail.com>2018-03-01 20:40:20 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-03-01 21:28:01 (GMT)
commit610008146ed1647bb1da6a098e314b8929ff213e (patch)
tree373d4290af3644d7123a623133c6f10ca9dc4f78 /cache.h
parent350292a1efb38bbcd6255a424df6adbfe78910ac (diff)
downloadgit-610008146ed1647bb1da6a098e314b8929ff213e.zip
git-610008146ed1647bb1da6a098e314b8929ff213e.tar.gz
git-610008146ed1647bb1da6a098e314b8929ff213e.tar.bz2
write_locked_index(): add flag to avoid writing unchanged index
We have several callers like if (active_cache_changed && write_locked_index(...)) handle_error(); rollback_lock_file(...); where the final rollback is needed because "!active_cache_changed" shortcuts the if-expression. There are also a few variants of this, including some if-else constructs that make it more clear when the explicit rollback is really needed. Teach `write_locked_index()` to take a new flag SKIP_IF_UNCHANGED and simplify the callers. Leave the most complicated of the callers (in builtin/update-index.c) unchanged. Rewriting it to use this new flag would end up duplicating logic. We could have made the new flag behave the other way round ("FORCE_WRITE"), but that could break existing users behind their backs. Let's take the more conservative approach. We can still migrate existing callers to use our new flag. Later we might even be able to flip the default, possibly without entirely ignoring the risk to in-flight or out-of-tree topics. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'cache.h')
-rw-r--r--cache.h4
1 files changed, 4 insertions, 0 deletions
diff --git a/cache.h b/cache.h
index 21fbcc2..5c88031 100644
--- a/cache.h
+++ b/cache.h
@@ -599,6 +599,7 @@ extern int read_index_unmerged(struct index_state *);
/* For use with `write_locked_index()`. */
#define COMMIT_LOCK (1 << 0)
+#define SKIP_IF_UNCHANGED (1 << 1)
/*
* Write the index while holding an already-taken lock. Close the lock,
@@ -615,6 +616,9 @@ extern int read_index_unmerged(struct index_state *);
* With `COMMIT_LOCK`, the lock is always committed or rolled back.
* Without it, the lock is closed, but neither committed nor rolled
* back.
+ *
+ * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
+ * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
*/
extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);