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(-)
Show changes to 9 files +24 −8
builtin/commit.c, builtin/describe.c, builtin/diff.c, builtin/merge.c, builtin/rebase.c, repository.c, repository.h, sequencer.c, wt-status.c
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);