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

[PATCH 3/5] lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}()

From
René Scharfe <l.s.r@web.de>
Date
Jul 14, 2026, 17:59 UTC
Message-ID
<20260714175956.54601-4-l.s.r@web.de>
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
Previous: René ScharfeNext: René Scharfe
Message 6 of 15 in “tempfile: stop using the_repository”
  1. 0/5 tempfile: stop using the_repositoryRené Scharfe, Jul 14, 2026
  2. 1/5 tempfile: add repo_create_tempfile{,_mode}()René Scharfe, Jul 14, 2026
  3. Patrick SteinhardtJul 15, 2026
  4. René ScharfeJul 15, 2026
  5. 2/5 refs/packed: use repo_create_tempfile()René Scharfe, Jul 14, 2026
  6. 3/5 lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}()René Scharfe, Jul 14, 2026
  7. 4/5 tempfile: stop using the_repositoryRené Scharfe, Jul 14, 2026
  8. Patrick SteinhardtJul 15, 2026
  9. 5/5 use repo_hold_lock_file_for_update{,_mode,_timeout}() with custom reposRené Scharfe, Jul 14, 2026
  10. Patrick SteinhardtJul 15, 2026
  11. René ScharfeJul 18, 2026
  12. Junio C HamanoJul 19, 2026
  13. Patrick SteinhardtJul 21, 2026
  14. Junio C HamanoJul 14, 2026
  15. Patrick SteinhardtJul 15, 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.