Skip to content

Commit 02ae242

Browse files
Martin Ågrengitster
authored andcommitted
checkout-index: simplify locking logic
`newfd` starts out negative. If we then take the lock, `newfd` will become non-negative. We later check for exactly that property before calling `write_locked_index()`. That is, we are simply using `newfd` as a boolean to keep track of whether we took the lock or not. (We always use `newfd` and `lock_file` together, so they really are mirroring each other.) Drop `newfd` and check with `is_lock_file_locked()` instead. While at it, move the `static struct lock_file` into `cmd_checkout_index()` and make it non-static. It is only used in this function, and after 076aa2c (tempfile: auto-allocate tempfiles on heap, 2017-09-05), we can have lockfiles on the stack. Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5de134c commit 02ae242

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

builtin/checkout-index.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ static const char * const builtin_checkout_index_usage[] = {
129129
NULL
130130
};
131131

132-
static struct lock_file lock_file;
133-
134132
static int option_parse_stage(const struct option *opt,
135133
const char *arg, int unset)
136134
{
@@ -150,7 +148,7 @@ static int option_parse_stage(const struct option *opt,
150148
int cmd_checkout_index(int argc, const char **argv, const char *prefix)
151149
{
152150
int i;
153-
int newfd = -1;
151+
struct lock_file lock_file = LOCK_INIT;
154152
int all = 0;
155153
int read_from_stdin = 0;
156154
int prefix_length;
@@ -206,7 +204,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
206204
if (index_opt && !state.base_dir_len && !to_tempfile) {
207205
state.refresh_cache = 1;
208206
state.istate = &the_index;
209-
newfd = hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
207+
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
210208
}
211209

212210
/* Check out named files first */
@@ -251,7 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
251249
if (all)
252250
checkout_all(prefix, prefix_length);
253251

254-
if (0 <= newfd &&
252+
if (is_lock_file_locked(&lock_file) &&
255253
write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
256254
die("Unable to write new index file");
257255
return 0;

0 commit comments

Comments
 (0)