From: "René Scharfe" <l.s.r@web.de>
To: git@vger.kernel.org
Subject: [PATCH 3/5] lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}()
Date: Tue, 14 Jul 2026 19:59:54 +0200 [thread overview]
Message-ID: <20260714175956.54601-4-l.s.r@web.de> (raw)
In-Reply-To: <20260714175956.54601-1-l.s.r@web.de>
Add variants of hold_lock_file_for_update_timeout_mode() that handle
arbitrary repositories.
Signed-off-by: René Scharfe <l.s.r@web.de>
---
lockfile.c | 30 ++++++++++++++++++++++--------
lockfile.h | 31 +++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 8 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index 7add2f136a..100f603771 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -2,11 +2,14 @@
* Copyright (c) 2005, Junio C Hamano
*/
+#define USE_THE_REPOSITORY_VARIABLE
+
#include "git-compat-util.h"
#include "abspath.h"
#include "gettext.h"
#include "lockfile.h"
#include "parse.h"
+#include "repository.h"
#include "strbuf.h"
#include "wrapper.h"
@@ -162,8 +165,8 @@ static int read_lock_pid(const char *pid_path, uintmax_t *pid_out)
}
/* Make sure errno contains a meaningful value on error */
-static int lock_file(struct lock_file *lk, const char *path, int flags,
- int mode)
+static int lock_file(struct repository *r, struct lock_file *lk,
+ const char *path, int flags, int mode)
{
struct strbuf base_path = STRBUF_INIT;
struct strbuf lock_path = STRBUF_INIT;
@@ -176,7 +179,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
get_lock_path(&lock_path, base_path.buf);
get_pid_path(&pid_path, base_path.buf);
- lk->tempfile = create_tempfile_mode(lock_path.buf, mode);
+ lk->tempfile = repo_create_tempfile_mode(r, lock_path.buf, mode);
if (lk->tempfile)
lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode);
@@ -200,8 +203,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
* timeout_ms milliseconds. If timeout_ms is 0, try locking the file
* exactly once. If timeout_ms is -1, try indefinitely.
*/
-static int lock_file_timeout(struct lock_file *lk, const char *path,
- int flags, long timeout_ms, int mode)
+static int lock_file_timeout(struct repository *r, struct lock_file *lk,
+ const char *path, int flags, long timeout_ms,
+ int mode)
{
int n = 1;
int multiplier = 1;
@@ -209,7 +213,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
static int random_initialized = 0;
if (timeout_ms == 0)
- return lock_file(lk, path, flags, mode);
+ return lock_file(r, lk, path, flags, mode);
if (!random_initialized) {
srand((unsigned int)getpid());
@@ -223,7 +227,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
long backoff_ms, wait_ms;
int fd;
- fd = lock_file(lk, path, flags, mode);
+ fd = lock_file(r, lk, path, flags, mode);
if (fd >= 0)
return fd; /* success */
@@ -308,7 +312,17 @@ int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
const char *path, int flags,
long timeout_ms, int mode)
{
- int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);
+ return repo_hold_lock_file_for_update_timeout_mode(the_repository,
+ lk, path, flags,
+ timeout_ms, mode);
+}
+
+int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
+ struct lock_file *lk,
+ const char *path, int flags,
+ long timeout_ms, int mode)
+{
+ int fd = lock_file_timeout(r, lk, path, flags, timeout_ms, mode);
if (fd < 0) {
if (flags & LOCK_DIE_ON_ERROR)
unable_to_lock_die(path, errno);
diff --git a/lockfile.h b/lockfile.h
index e7233f28de..1667612674 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -189,6 +189,11 @@ int hold_lock_file_for_update_timeout_mode(
struct lock_file *lk, const char *path,
int flags, long timeout_ms, int mode);
+int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
+ struct lock_file *lk,
+ const char *path, int flags,
+ long timeout_ms, int mode);
+
static inline int hold_lock_file_for_update_timeout(
struct lock_file *lk, const char *path,
int flags, long timeout_ms)
@@ -197,6 +202,16 @@ static inline int hold_lock_file_for_update_timeout(
timeout_ms, 0666);
}
+static inline int repo_hold_lock_file_for_update_timeout(struct repository *r,
+ struct lock_file *lk,
+ const char *path,
+ int flags,
+ long timeout_ms)
+{
+ return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
+ timeout_ms, 0666);
+}
+
/*
* Attempt to create a lockfile for the file at `path` and return a
* file descriptor for writing to it, or -1 on error. The flags
@@ -208,6 +223,13 @@ static inline int hold_lock_file_for_update(
return hold_lock_file_for_update_timeout(lk, path, flags, 0);
}
+static inline int repo_hold_lock_file_for_update(struct repository *r,
+ struct lock_file *lk,
+ const char *path, int flags)
+{
+ return repo_hold_lock_file_for_update_timeout(r, lk, path, flags, 0);
+}
+
static inline int hold_lock_file_for_update_mode(
struct lock_file *lk, const char *path,
int flags, int mode)
@@ -215,6 +237,15 @@ static inline int hold_lock_file_for_update_mode(
return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode);
}
+static inline int repo_hold_lock_file_for_update_mode(struct repository *r,
+ struct lock_file *lk,
+ const char *path,
+ int flags, int mode)
+{
+ return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
+ 0, mode);
+}
+
/*
* Return a nonzero value iff `lk` is currently locked.
*/
--
2.55.0
next prev parent reply other threads:[~2026-07-14 18:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 17:59 [PATCH 0/5] tempfile: stop using the_repository René Scharfe
2026-07-14 17:59 ` [PATCH 1/5] tempfile: add repo_create_tempfile{,_mode}() René Scharfe
2026-07-14 17:59 ` [PATCH 2/5] refs/packed: use repo_create_tempfile() René Scharfe
2026-07-14 17:59 ` René Scharfe [this message]
2026-07-14 17:59 ` [PATCH 4/5] tempfile: stop using the_repository René Scharfe
2026-07-14 17:59 ` [PATCH 5/5] use repo_hold_lock_file_for_update{,_mode,_timeout}() with custom repos René Scharfe
2026-07-14 20:45 ` [PATCH 0/5] tempfile: stop using the_repository 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=20260714175956.54601-4-l.s.r@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
/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