From: Jun Wu <quark.zju@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, peff@peff.net, ps@pks.im, Jun Wu <quark@meta.com>
Subject: [PATCH 2/2] commit: lock index before reading
Date: Tue, 13 Oct 2026 10:23:15 -0700 [thread overview]
Message-ID: <20261013172315.2997446-3-quark.zju@gmail.com> (raw)
In-Reply-To: <20261013172315.2997446-1-quark.zju@gmail.com>
From: Jun Wu <quark@meta.com>
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:
- f5bbc3225c (Port git commit to C., 2007-11-08): lock before reading
- 2888605c64 (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>
---
builtin/commit.c | 9 ++-------
t/t0032-index-lock-timeout.sh | 4 ++--
2 files changed, 4 insertions(+), 9 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 840b6b4083..08dd53ad4e 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -389,13 +389,13 @@ static const char *prepare_index(const char **argv, const char *prefix,
(!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
die(_("No paths with --include/--only does not make sense."));
+ repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
+
if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
die(_("index file corrupt"));
if (interactive) {
char *old_index_env = NULL, *old_repo_index_file;
- repo_hold_locked_index(the_repository, &index_lock,
- LOCK_DIE_ON_ERROR);
refresh_cache_or_die(refresh_flags);
@@ -453,8 +453,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
*/
if (all || (also && pathspec.nr)) {
char *ps_matched = xcalloc(pathspec.nr, 1);
- repo_hold_locked_index(the_repository, &index_lock,
- LOCK_DIE_ON_ERROR);
add_files_to_cache(the_repository, also ? prefix : NULL,
&pathspec, ps_matched, 0, 0, 0 );
if (!all && report_path_error(ps_matched, &pathspec))
@@ -480,8 +478,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
* We still need to refresh the index here.
*/
if (!only && !pathspec.nr) {
- repo_hold_locked_index(the_repository, &index_lock,
- LOCK_DIE_ON_ERROR);
refresh_cache_or_die(refresh_flags);
if (the_repository->index->cache_changed
|| !cache_tree_fully_valid(the_repository->index))
@@ -531,7 +527,6 @@ static const char *prepare_index(const char **argv, const char *prefix,
if (repo_read_index(the_repository) < 0)
die(_("cannot read the index"));
- repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
add_remove_files(&partial);
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
diff --git a/t/t0032-index-lock-timeout.sh b/t/t0032-index-lock-timeout.sh
index ca334c3eef..0c36a7cdfe 100755
--- a/t/t0032-index-lock-timeout.sh
+++ b/t/t0032-index-lock-timeout.sh
@@ -29,9 +29,9 @@ test_expect_success 'index locked, add waits with indexLockTimeout>0' '
git -c core.indexLockTimeout=5000 add foo
'
-test_expect_success 'index locked, commit fails even with indexLockTimeout>0 (read index before lock)' '
+test_expect_success 'index locked, commit waits with indexLockTimeout>0' '
prepare_index_lock &&
- test_must_fail_with_index_lock git -c core.indexLockTimeout=5000 commit -m init
+ git -c core.indexLockTimeout=5000 commit -m init
'
test_done
--
2.56.0
next prev parent reply other threads:[~2026-10-13 17:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 17:23 [PATCH 0/2] Add a way to wait for index lock Jun Wu
2026-10-13 17:23 ` [PATCH 1/2] repository: add core.indexLockTimeout Jun Wu
2026-10-13 21:40 ` D. Ben Knoble
2026-10-13 22:05 ` Junio C Hamano
2026-10-13 17:23 ` Jun Wu [this message]
2026-10-13 22:05 ` [PATCH 0/2] Add a way to wait for index lock Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261013172315.2997446-3-quark.zju@gmail.com \
--to=quark.zju@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=ps@pks.im \
--cc=quark@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox