Skip to content

Commit 75d9a25

Browse files
Martin Ågrengitster
authored andcommitted
t/helper/test-write-cache: clean up lock-handling
Die in case writing the index fails, so that the caller can notice (instead of, say, being impressed by how performant the writing is). While at it, note that after opening a lock with `LOCK_DIE_ON_ERROR`, we do not need to worry about whether we succeeded. Also, we can move the `struct lock_file` into the function and drop the staticness. (Placing `struct lock_file`s on the stack used to be a bad idea, because the temp- and lockfile-machinery would keep a pointer into the struct. But after 076aa2c (tempfile: auto-allocate tempfiles on heap, 2017-09-05), we can safely have lockfiles on the stack.) Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 468165c commit 75d9a25

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

t/helper/test-write-cache.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
#include "cache.h"
22
#include "lockfile.h"
33

4-
static struct lock_file index_lock;
5-
64
int cmd_main(int argc, const char **argv)
75
{
8-
int i, cnt = 1, lockfd;
6+
struct lock_file index_lock = LOCK_INIT;
7+
int i, cnt = 1;
98
if (argc == 2)
109
cnt = strtol(argv[1], NULL, 0);
1110
setup_git_directory();
1211
read_cache();
1312
for (i = 0; i < cnt; i++) {
14-
lockfd = hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
15-
if (0 <= lockfd) {
16-
write_locked_index(&the_index, &index_lock, COMMIT_LOCK);
17-
} else {
18-
rollback_lock_file(&index_lock);
19-
}
13+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
14+
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
15+
die("unable to write index file");
2016
}
2117

2218
return 0;

0 commit comments

Comments
 (0)