summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ågren <martin.agren@gmail.com>2017-10-05 20:32:08 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-10-06 01:07:18 (GMT)
commit2954e5ec4361c6932593663859aa437583edb500 (patch)
tree0227b2bd8e5cb3485574b72377ceaa430048c684
parent02ae242fdde2ba8acb6b87e32df2b1e33786e78b (diff)
downloadgit-2954e5ec4361c6932593663859aa437583edb500.zip
git-2954e5ec4361c6932593663859aa437583edb500.tar.gz
git-2954e5ec4361c6932593663859aa437583edb500.tar.bz2
cache-tree: simplify locking logic
After we have taken the lock using `LOCK_DIE_ON_ERROR`, we know that `newfd` is non-negative. So when we check for exactly that property before calling `write_locked_index()`, the outcome is guaranteed. If we write and commit successfully, we set `newfd = -1`, so that we can later avoid calling `rollback_lock_file` on an already-committed lock. But we might just as well unconditionally call `rollback_lock_file()` -- it will be a no-op if we have already committed. All in all, we use `newfd` as a bool and the only benefit we get from it is that we can avoid calling a no-op. Remove `newfd` so that we have one variable less to reason about. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--cache-tree.c12
1 files changed, 4 insertions, 8 deletions
diff --git a/cache-tree.c b/cache-tree.c
index 71d092e..f646f56 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -602,11 +602,11 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
{
- int entries, was_valid, newfd;
+ int entries, was_valid;
struct lock_file lock_file = LOCK_INIT;
int ret = 0;
- newfd = hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
+ hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
entries = read_index_from(index_state, index_path);
if (entries < 0) {
@@ -625,10 +625,7 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
ret = WRITE_TREE_UNMERGED_INDEX;
goto out;
}
- if (0 <= newfd) {
- if (!write_locked_index(index_state, &lock_file, COMMIT_LOCK))
- newfd = -1;
- }
+ write_locked_index(index_state, &lock_file, COMMIT_LOCK);
/* Not being able to write is fine -- we are only interested
* in updating the cache-tree part, and if the next caller
* ends up using the old index with unupdated cache-tree part
@@ -650,8 +647,7 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
hashcpy(sha1, index_state->cache_tree->oid.hash);
out:
- if (0 <= newfd)
- rollback_lock_file(&lock_file);
+ rollback_lock_file(&lock_file);
return ret;
}