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 v2 1/4] repository: add repo_try_hold_locked_index()
Date: Sat, 17 Oct 2026 00:32:28 -0700 [thread overview]
Message-ID: <20261017073231.3357477-2-quark.zju@gmail.com> (raw)
In-Reply-To: <20261017073231.3357477-1-quark.zju@gmail.com>
From: Jun Wu <quark@meta.com>
Refactoring to make the next commit easier to reason about.
repo_hold_locked_index() has 2 categories of users:
1. Lock is required, or fail - they might want to wait for the lock,
with the intention to not fail.
2. Lock is optional, won't fail if lock cannot be obtained - they
don't need to wait for the lock.
Migrate the 2nd category, identified by repo_update_index_if_able(),
to a separate repo_try_hold_locked_index() to clarify intention.
For now, the two functions are the same. The next commit changes
repo_hold_locked_index().
Alternative approach considered: a LOCK_NO_RETRY flag could express
this, but hold_lock_file_for_update_timeout() already takes
timeout_ms, so the flag would duplicate or conflict with it.
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jun Wu <quark@meta.com>
---
builtin/commit.c | 2 +-
builtin/describe.c | 4 ++--
builtin/diff.c | 2 +-
builtin/merge.c | 2 +-
builtin/rebase.c | 2 +-
repository.c | 9 +++++++++
repository.h | 7 +++++++
sequencer.c | 2 +-
wt-status.c | 2 +-
9 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 840b6b4083..a808c0ba39 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1632,7 +1632,7 @@ struct repository *repo UNUSED)
&s.pathspec, NULL, NULL);
if (use_optional_locks())
- fd = repo_hold_locked_index(the_repository, &index_lock, 0);
+ fd = repo_try_hold_locked_index(the_repository, &index_lock, 0);
else
fd = -1;
diff --git a/builtin/describe.c b/builtin/describe.c
index c0abc931a5..f28030e970 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -764,8 +764,8 @@ int cmd_describe(int argc,
repo_read_index(the_repository);
refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED,
NULL, NULL, NULL);
- fd = repo_hold_locked_index(the_repository,
- &index_lock, 0);
+ fd = repo_try_hold_locked_index(the_repository,
+ &index_lock, 0);
if (0 <= fd)
repo_update_index_if_able(the_repository, &index_lock);
diff --git a/builtin/diff.c b/builtin/diff.c
index 18b1083e98..c6c6de629a 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -239,7 +239,7 @@ static void refresh_index_quietly(void)
struct lock_file lock_file = LOCK_INIT;
int fd;
- fd = repo_hold_locked_index(the_repository, &lock_file, 0);
+ fd = repo_try_hold_locked_index(the_repository, &lock_file, 0);
if (fd < 0)
return;
discard_index(the_repository->index);
diff --git a/builtin/merge.c b/builtin/merge.c
index 5b4eb23a83..1e9ede5bb5 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -347,7 +347,7 @@ static int save_state(struct object_id *stash)
int fd;
int rc = -1;
- fd = repo_hold_locked_index(the_repository, &lock_file, 0);
+ fd = repo_try_hold_locked_index(the_repository, &lock_file, 0);
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
if (0 <= fd)
repo_update_index_if_able(the_repository, &lock_file);
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 535db60181..6df9e5b058 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1371,7 +1371,7 @@ int cmd_rebase(int argc,
if (repo_get_oid(the_repository, "HEAD", &head))
die(_("Cannot read HEAD"));
- fd = repo_hold_locked_index(the_repository, &lock_file, 0);
+ fd = repo_try_hold_locked_index(the_repository, &lock_file, 0);
if (repo_read_index(the_repository) < 0)
die(_("could not read index"));
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
diff --git a/repository.c b/repository.c
index b857e1c580..a74b1118cb 100644
--- a/repository.c
+++ b/repository.c
@@ -477,3 +477,12 @@ int repo_hold_locked_index(struct repository *repo,
BUG("the repo hasn't been setup");
return repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);
}
+
+int repo_try_hold_locked_index(struct repository *repo,
+ struct lock_file *lf,
+ int flags)
+{
+ if (!repo->index_file)
+ BUG("the repo hasn't been setup");
+ return repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);
+}
diff --git a/repository.h b/repository.h
index 11f5c2ed10..253a5f1d3f 100644
--- a/repository.h
+++ b/repository.h
@@ -288,6 +288,13 @@ int repo_hold_locked_index(struct repository *repo,
struct lock_file *lf,
int flags);
+/*
+ * Try to obtain the index lock in a non-blocking way.
+ */
+int repo_try_hold_locked_index(struct repository *repo,
+ struct lock_file *lf,
+ int flags);
+
int repo_read_index_unmerged(struct repository *);
/*
* Opportunistically update the index but do not complain if we can't.
diff --git a/sequencer.c b/sequencer.c
index efbfd1315b..889807ba45 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -4772,7 +4772,7 @@ static void create_autostash_internal(struct repository *r,
if (path && refname)
BUG("can only pass path or refname");
- fd = repo_hold_locked_index(r, &lock_file, 0);
+ fd = repo_try_hold_locked_index(r, &lock_file, 0);
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
if (0 <= fd)
repo_update_index_if_able(r, &lock_file);
diff --git a/wt-status.c b/wt-status.c
index 57772c7501..0b19161de8 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -2791,7 +2791,7 @@ int require_clean_work_tree(struct repository *r,
struct lock_file lock_file = LOCK_INIT;
int err = 0, fd;
- fd = repo_hold_locked_index(r, &lock_file, 0);
+ fd = repo_try_hold_locked_index(r, &lock_file, 0);
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
if (0 <= fd)
repo_update_index_if_able(r, &lock_file);
--
2.56.0
next prev parent reply other threads:[~2026-10-17 7:32 UTC|newest]
Thread overview: 18+ 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-14 2:38 ` Jun Wu
2026-10-14 16:29 ` D. Ben Knoble
2026-10-13 22:05 ` Junio C Hamano
2026-10-14 22:12 ` Jun Wu
2026-10-13 17:23 ` [PATCH 2/2] commit: lock index before reading Jun Wu
2026-10-14 10:04 ` Jeff King
2026-10-15 0:19 ` Jun Wu
2026-10-13 22:05 ` [PATCH 0/2] Add a way to wait for index lock Junio C Hamano
2026-10-14 22:12 ` Jun Wu
2026-10-14 10:14 ` Jeff King
2026-10-17 7:32 ` [PATCH v2 0/4] Wait for the index lock instead of failing Jun Wu
2026-10-17 7:32 ` Jun Wu [this message]
2026-10-17 7:32 ` [PATCH v2 2/4] repository: add core.indexLockTimeout Jun Wu
2026-10-17 7:32 ` [PATCH v2 3/4] commit: fix write race Jun Wu
2026-10-17 7:32 ` [PATCH v2 4/4] reset: " Jun Wu
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=20261017073231.3357477-2-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