git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] repository: add repo_try_hold_locked_index()

From
JWJun Wu <quark.zju@gmail.com>
Date
Oct 17, 2026, 07:32 UTC
Message-ID
<20261017073231.3357477-2-quark.zju@gmail.com>
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
Previous: Jun WuNext: Jun Wu
Message 16 of 20 in “Add a way to wait for index lock”
  1. 0/2 Add a way to wait for index lockJun Wu, Oct 13, 2026
  2. 1/2 repository: add core.indexLockTimeoutJun Wu, Oct 13, 2026
  3. D. Ben KnobleOct 13, 2026
  4. Jun WuOct 14, 2026
  5. D. Ben KnobleOct 14, 2026
  6. Junio C HamanoOct 13, 2026
  7. Jun WuOct 14, 2026
  8. Ben KnobleOct 19, 2026
  9. 2/2 commit: lock index before readingJun Wu, Oct 13, 2026
  10. Jeff KingOct 14, 2026
  11. Jun WuOct 15, 2026
  12. Junio C HamanoOct 13, 2026
  13. Jun WuOct 14, 2026
  14. Jeff KingOct 14, 2026
  15. 0/4 Wait for the index lock instead of failingJun Wu, Oct 17, 2026
  16. 1/4 repository: add repo_try_hold_locked_index()Jun Wu, Oct 17, 2026
  17. 2/4 repository: add core.indexLockTimeoutJun Wu, Oct 17, 2026
  18. 3/4 commit: fix write raceJun Wu, Oct 17, 2026
  19. 4/4 reset: fix write raceJun Wu, Oct 17, 2026
  20. Mihai CosmaOct 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.