Skip to content

Commit b3e83cc

Browse files
committed
hold_locked_index(): align error handling with hold_lockfile_for_update()
Callers of the hold_locked_index() function pass 0 when they want to prepare to write a new version of the index file without wishing to die or emit an error message when the request fails (e.g. somebody else already held the lock), and pass 1 when they want the call to die upon failure. This option is called LOCK_DIE_ON_ERROR by the underlying lockfile API, and the hold_locked_index() function translates the paramter to LOCK_DIE_ON_ERROR when calling the hold_lock_file_for_update(). Replace these hardcoded '1' with LOCK_DIE_ON_ERROR and stop translating. Callers other than the ones that are replaced with this change pass '0' to the function; no behaviour change is intended with this patch. Signed-off-by: Junio C Hamano <[email protected]> --- Among the callers of hold_locked_index() that passes 0: - diff.c::refresh_index_quietly() at the end of "git diff" is an opportunistic update; it leaks the lockfile structure but it is just before the program exits and nobody should care. - builtin/describe.c::cmd_describe(), builtin/commit.c::cmd_status(), sequencer.c::read_and_refresh_cache() are all opportunistic updates and they are OK. - builtin/update-index.c::cmd_update_index() takes a lock upfront but we may end up not needing to update the index (i.e. the entries may be fully up-to-date), in which case we do not need to issue an error upon failure to acquire the lock. We do diagnose and die if we indeed need to update, so it is OK. - wt-status.c::require_clean_work_tree() IS BUGGY. It asks silence, does not check the returned value. Compare with callsites like cmd_describe() and cmd_status() to notice that it is wrong to call update_index_if_able() unconditionally.
1 parent 89d38fb commit b3e83cc

18 files changed

+27
-29
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4688,7 +4688,7 @@ static int apply_patch(struct apply_state *state,
46884688
state->index_file,
46894689
LOCK_DIE_ON_ERROR);
46904690
else
4691-
state->newfd = hold_locked_index(state->lock_file, 1);
4691+
state->newfd = hold_locked_index(state->lock_file, LOCK_DIE_ON_ERROR);
46924692
}
46934693

46944694
if (state->check_index && read_apply_cache(state) < 0) {

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
361361
add_new_files = !take_worktree_changes && !refresh_only;
362362
require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
363363

364-
hold_locked_index(&lock_file, 1);
364+
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
365365

366366
flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
367367
(show_only ? ADD_CACHE_PRETEND : 0) |

builtin/am.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ static void refresh_and_write_cache(void)
11191119
{
11201120
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
11211121

1122-
hold_locked_index(lock_file, 1);
1122+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
11231123
refresh_cache(REFRESH_QUIET);
11241124
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
11251125
die(_("unable to write index file"));
@@ -1976,7 +1976,7 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
19761976
return -1;
19771977

19781978
lock_file = xcalloc(1, sizeof(struct lock_file));
1979-
hold_locked_index(lock_file, 1);
1979+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
19801980

19811981
refresh_cache(REFRESH_QUIET);
19821982

@@ -2016,7 +2016,7 @@ static int merge_tree(struct tree *tree)
20162016
return -1;
20172017

20182018
lock_file = xcalloc(1, sizeof(struct lock_file));
2019-
hold_locked_index(lock_file, 1);
2019+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
20202020

20212021
memset(&opts, 0, sizeof(opts));
20222022
opts.head_idx = 1;

builtin/checkout-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
205205
if (index_opt && !state.base_dir_len && !to_tempfile) {
206206
state.refresh_cache = 1;
207207
state.istate = &the_index;
208-
newfd = hold_locked_index(&lock_file, 1);
208+
newfd = hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
209209
}
210210

211211
/* Check out named files first */

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static int checkout_paths(const struct checkout_opts *opts,
274274

275275
lock_file = xcalloc(1, sizeof(struct lock_file));
276276

277-
hold_locked_index(lock_file, 1);
277+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
278278
if (read_cache_preload(&opts->pathspec) < 0)
279279
return error(_("index file corrupt"));
280280

@@ -467,7 +467,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
467467
int ret;
468468
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
469469

470-
hold_locked_index(lock_file, 1);
470+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
471471
if (read_cache_preload(NULL) < 0)
472472
return error(_("index file corrupt"));
473473

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ static int checkout(int submodule_progress)
711711
setup_work_tree();
712712

713713
lock_file = xcalloc(1, sizeof(struct lock_file));
714-
hold_locked_index(lock_file, 1);
714+
hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
715715

716716
memset(&opts, 0, sizeof opts);
717717
opts.update = 1;

builtin/commit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
351351

352352
if (interactive) {
353353
char *old_index_env = NULL;
354-
hold_locked_index(&index_lock, 1);
354+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
355355

356356
refresh_cache_or_die(refresh_flags);
357357

@@ -396,7 +396,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
396396
* (B) on failure, rollback the real index.
397397
*/
398398
if (all || (also && pathspec.nr)) {
399-
hold_locked_index(&index_lock, 1);
399+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
400400
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
401401
refresh_cache_or_die(refresh_flags);
402402
update_main_cache_tree(WRITE_TREE_SILENT);
@@ -416,7 +416,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
416416
* We still need to refresh the index here.
417417
*/
418418
if (!only && !pathspec.nr) {
419-
hold_locked_index(&index_lock, 1);
419+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
420420
refresh_cache_or_die(refresh_flags);
421421
if (active_cache_changed
422422
|| !cache_tree_fully_valid(active_cache_tree))
@@ -468,7 +468,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
468468
if (read_cache() < 0)
469469
die(_("cannot read the index"));
470470

471-
hold_locked_index(&index_lock, 1);
471+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
472472
add_remove_files(&partial);
473473
refresh_cache(REFRESH_QUIET);
474474
update_main_cache_tree(WRITE_TREE_SILENT);

builtin/merge.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
634634
{
635635
static struct lock_file lock;
636636

637-
hold_locked_index(&lock, 1);
637+
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
638638
refresh_cache(REFRESH_QUIET);
639639
if (active_cache_changed &&
640640
write_locked_index(&the_index, &lock, COMMIT_LOCK))
@@ -671,7 +671,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
671671
for (j = common; j; j = j->next)
672672
commit_list_insert(j->item, &reversed);
673673

674-
hold_locked_index(&lock, 1);
674+
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
675675
clean = merge_recursive(&o, head,
676676
remoteheads->item, reversed, &result);
677677
if (clean < 0)
@@ -781,7 +781,7 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
781781
struct commit_list *parents, **pptr = &parents;
782782
static struct lock_file lock;
783783

784-
hold_locked_index(&lock, 1);
784+
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
785785
refresh_cache(REFRESH_QUIET);
786786
if (active_cache_changed &&
787787
write_locked_index(&the_index, &lock, COMMIT_LOCK))

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
126126
if (--argc < 1)
127127
usage_with_options(builtin_mv_usage, builtin_mv_options);
128128

129-
hold_locked_index(&lock_file, 1);
129+
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
130130
if (read_cache() < 0)
131131
die(_("index file corrupt"));
132132

builtin/read-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
150150
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
151151
read_tree_usage, 0);
152152

153-
hold_locked_index(&lock_file, 1);
153+
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
154154

155155
prefix_set = opts.prefix ? 1 : 0;
156156
if (1 < opts.merge + opts.reset + prefix_set)

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
354354

355355
if (reset_type != SOFT) {
356356
struct lock_file *lock = xcalloc(1, sizeof(*lock));
357-
hold_locked_index(lock, 1);
357+
hold_locked_index(lock, LOCK_DIE_ON_ERROR);
358358
if (reset_type == MIXED) {
359359
int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
360360
if (read_from_tree(&pathspec, &oid, intent_to_add))

builtin/rm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
292292
if (!index_only)
293293
setup_work_tree();
294294

295-
hold_locked_index(&lock_file, 1);
295+
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
296296

297297
if (read_cache() < 0)
298298
die(_("index file corrupt"));

builtin/update-index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10121012
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
10131013
lock_file = xcalloc(1, sizeof(struct lock_file));
10141014

1015+
/* we will diagnose later if it turns out that we need to update it */
10151016
newfd = hold_locked_index(lock_file, 0);
10161017
if (newfd < 0)
10171018
lock_error = errno;

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ int merge_recursive_generic(struct merge_options *o,
21242124
}
21252125
}
21262126

2127-
hold_locked_index(lock, 1);
2127+
hold_locked_index(lock, LOCK_DIE_ON_ERROR);
21282128
clean = merge_recursive(o, head_commit, next_commit, ca,
21292129
result);
21302130
if (clean < 0)

read-cache.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,9 @@ static int read_index_extension(struct index_state *istate,
14251425
return 0;
14261426
}
14271427

1428-
int hold_locked_index(struct lock_file *lk, int die_on_error)
1428+
int hold_locked_index(struct lock_file *lk, int lock_flags)
14291429
{
1430-
return hold_lock_file_for_update(lk, get_index_file(),
1431-
die_on_error
1432-
? LOCK_DIE_ON_ERROR
1433-
: 0);
1430+
return hold_lock_file_for_update(lk, get_index_file(), lock_flags);
14341431
}
14351432

14361433
int read_index(struct index_state *istate)

rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ static void update_paths(struct string_list *update)
708708
{
709709
int i;
710710

711-
hold_locked_index(&index_lock, 1);
711+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
712712

713713
for (i = 0; i < update->nr; i++) {
714714
struct string_list_item *item = &update->items[i];

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
370370
char **xopt;
371371
static struct lock_file index_lock;
372372

373-
hold_locked_index(&index_lock, 1);
373+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
374374

375375
read_cache();
376376

t/helper/test-scrap-cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static struct lock_file index_lock;
88
int cmd_main(int ac, const char **av)
99
{
1010
setup_git_directory();
11-
hold_locked_index(&index_lock, 1);
11+
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
1212
if (read_cache() < 0)
1313
die("unable to read index file");
1414
active_cache_tree = NULL;

0 commit comments

Comments
 (0)