Skip to content

Commit e17d9e0

Browse files
quark-zjugitster
authored andcommitted
commit: lock index before reading
Take the index lock before reading the index, in one place, instead of separately in each `if` branch. `commit` now respects the `core.indexLockTimeout` config. History suggests no particular reason for reading before locking: - f5bbc32 (Port git commit to C., 2007-11-08): lock before reading - 2888605 (builtin-commit: fix partial-commit support, 2007-11-18): seems to accidentally move reading to before locking Signed-off-by: Jun Wu <quark@meta.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7eac4af commit e17d9e0

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

‎builtin/commit.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
389389
(!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
390390
die(_("No paths with --include/--only does not make sense."));
391391

392+
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
393+
392394
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
393395
die(_("index file corrupt"));
394396

395397
if (interactive) {
396398
char *old_index_env = NULL, *old_repo_index_file;
397-
repo_hold_locked_index(the_repository, &index_lock,
398-
LOCK_DIE_ON_ERROR);
399399

400400
refresh_cache_or_die(refresh_flags);
401401

@@ -453,8 +453,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
453453
*/
454454
if (all || (also && pathspec.nr)) {
455455
char *ps_matched = xcalloc(pathspec.nr, 1);
456-
repo_hold_locked_index(the_repository, &index_lock,
457-
LOCK_DIE_ON_ERROR);
458456
add_files_to_cache(the_repository, also ? prefix : NULL,
459457
&pathspec, ps_matched, 0, 0, 0 );
460458
if (!all && report_path_error(ps_matched, &pathspec))
@@ -480,8 +478,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
480478
* We still need to refresh the index here.
481479
*/
482480
if (!only && !pathspec.nr) {
483-
repo_hold_locked_index(the_repository, &index_lock,
484-
LOCK_DIE_ON_ERROR);
485481
refresh_cache_or_die(refresh_flags);
486482
if (the_repository->index->cache_changed
487483
|| !cache_tree_fully_valid(the_repository->index))
@@ -531,7 +527,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
531527
if (repo_read_index(the_repository) < 0)
532528
die(_("cannot read the index"));
533529

534-
repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
535530
add_remove_files(&partial);
536531
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
537532
cache_tree_update(the_repository->index, WRITE_TREE_SILENT);

‎t/t0032-index-lock-timeout.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ test_expect_success 'index locked, add waits with indexLockTimeout>0' '
2929
git -c core.indexLockTimeout=5000 add foo
3030
'
3131

32-
test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '
32+
test_expect_success 'index locked, commit waits with indexLockTimeout>0' '
3333
prepare_index_lock &&
34-
test_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init
34+
git -c core.indexLockTimeout=5000 commit -m init
3535
'
3636

3737
test_done

0 commit comments

Comments
 (0)