Skip to content

Commit 4cee9bc

Browse files
committed
Merge branch 'rs/tempfile-wo-the-repository' into jch
The tempfile and lockfile APIs have been refactored to stop depending on the 'the_repository' global variable, and their callers have been updated to use the repository-aware variants. * rs/tempfile-wo-the-repository: use repo_hold_lock_file_for_update{,_mode,_timeout}() with custom repos tempfile: stop using the_repository lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}() refs/packed: use repo_create_tempfile() tempfile: add repo_create_tempfile{,_mode}()
2 parents dabd687 + 2e486bf commit 4cee9bc

21 files changed

Lines changed: 110 additions & 52 deletions

apply.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,7 +4287,8 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
42874287
}
42884288
}
42894289

4290-
hold_lock_file_for_update(&lock, state->fake_ancestor, LOCK_DIE_ON_ERROR);
4290+
repo_hold_lock_file_for_update(state->repo, &lock, state->fake_ancestor,
4291+
LOCK_DIE_ON_ERROR);
42914292
res = write_locked_index(&result, &lock, COMMIT_LOCK);
42924293
discard_index(&result);
42934294

@@ -4945,9 +4946,10 @@ static int apply_patch(struct apply_state *state,
49454946
state->update_index = (state->check_index || state->ita_only) && state->apply;
49464947
if (state->update_index && !is_lock_file_locked(&state->lock_file)) {
49474948
if (state->index_file)
4948-
hold_lock_file_for_update(&state->lock_file,
4949-
state->index_file,
4950-
LOCK_DIE_ON_ERROR);
4949+
repo_hold_lock_file_for_update(state->repo,
4950+
&state->lock_file,
4951+
state->index_file,
4952+
LOCK_DIE_ON_ERROR);
49514953
else
49524954
repo_hold_locked_index(state->repo, &state->lock_file,
49534955
LOCK_DIE_ON_ERROR);

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ static int run_dir_diff(struct repository *repo,
636636
struct lock_file lock = LOCK_INIT;
637637
strbuf_reset(&buf);
638638
strbuf_addf(&buf, "%s/wtindex", tmpdir.buf);
639-
if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
639+
if (repo_hold_lock_file_for_update(repo, &lock, buf.buf, 0) < 0 ||
640640
write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
641641
ret = error("could not write %s", buf.buf);
642642
goto finish;

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,
14141414
char *lock_path = xstrfmt("%s/maintenance", r->objects->sources->path);
14151415
enum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED;
14161416

1417-
if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1417+
if (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) {
14181418
/*
14191419
* Another maintenance command is running.
14201420
*

builtin/history.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ static int write_ondisk_index(struct repository *repo,
795795

796796
prime_cache_tree(repo, &index, tree);
797797

798-
if (hold_lock_file_for_update(&lock, path, 0) < 0) {
798+
if (repo_hold_lock_file_for_update(repo, &lock, path, 0) < 0) {
799799
ret = error_errno(_("unable to acquire index lock"));
800800
goto out;
801801
}

builtin/sparse-checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ static int write_patterns_and_update(struct repository *repo,
341341
if (safe_create_leading_directories(repo, sparse_filename))
342342
die(_("failed to create directory for sparse-checkout file"));
343343

344-
hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
344+
repo_hold_lock_file_for_update(repo, &lk, sparse_filename,
345+
LOCK_DIE_ON_ERROR);
345346

346347
result = update_working_directory(repo, pl);
347348
if (result) {

bundle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ int create_bundle(struct repository *r, const char *path,
519519
if (bundle_to_stdout)
520520
bundle_fd = 1;
521521
else
522-
bundle_fd = hold_lock_file_for_update(&lock, path,
523-
LOCK_DIE_ON_ERROR);
522+
bundle_fd = repo_hold_lock_file_for_update(r, &lock, path,
523+
LOCK_DIE_ON_ERROR);
524524

525525
if (version == -1)
526526
version = min_version;

commit-graph.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,8 +2127,8 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
21272127
if (ctx->split) {
21282128
char *lock_name = get_commit_graph_chain_filename(ctx->odb_source);
21292129

2130-
hold_lock_file_for_update_mode(&lk, lock_name,
2131-
LOCK_DIE_ON_ERROR, 0444);
2130+
repo_hold_lock_file_for_update_mode(ctx->r, &lk, lock_name,
2131+
LOCK_DIE_ON_ERROR, 0444);
21322132
free(lock_name);
21332133

21342134
graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
@@ -2146,8 +2146,9 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
21462146
f = hashfd(ctx->r->hash_algo,
21472147
get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
21482148
} else {
2149-
hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2150-
LOCK_DIE_ON_ERROR, 0444);
2149+
repo_hold_lock_file_for_update_mode(ctx->r, &lk,
2150+
ctx->graph_name,
2151+
LOCK_DIE_ON_ERROR, 0444);
21512152
f = hashfd(ctx->r->hash_algo,
21522153
get_lock_file_fd(&lk), get_lock_file_path(&lk));
21532154
}

config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,7 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
30373037
* The lock serves a purpose in addition to locking: the new
30383038
* contents of .git/config will be written into it.
30393039
*/
3040-
fd = hold_lock_file_for_update(&lock, config_filename, 0);
3040+
fd = repo_hold_lock_file_for_update(r, &lock, config_filename, 0);
30413041
if (fd < 0) {
30423042
error_errno(_("could not lock config file %s"), config_filename);
30433043
ret = CONFIG_NO_LOCK;
@@ -3382,7 +3382,7 @@ static int repo_config_copy_or_rename_section_in_file(
33823382
if (!config_filename)
33833383
config_filename = filename_buf = repo_git_path(r, "config");
33843384

3385-
out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
3385+
out_fd = repo_hold_lock_file_for_update(r, &lock, config_filename, 0);
33863386
if (out_fd < 0) {
33873387
ret = error(_("could not lock config file %s"), config_filename);
33883388
goto out;

lockfile.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
* Copyright (c) 2005, Junio C Hamano
33
*/
44

5+
#define USE_THE_REPOSITORY_VARIABLE
6+
57
#include "git-compat-util.h"
68
#include "abspath.h"
79
#include "gettext.h"
810
#include "lockfile.h"
911
#include "parse.h"
12+
#include "repository.h"
1013
#include "strbuf.h"
1114
#include "wrapper.h"
1215

@@ -162,8 +165,8 @@ static int read_lock_pid(const char *pid_path, uintmax_t *pid_out)
162165
}
163166

164167
/* Make sure errno contains a meaningful value on error */
165-
static int lock_file(struct lock_file *lk, const char *path, int flags,
166-
int mode)
168+
static int lock_file(struct repository *r, struct lock_file *lk,
169+
const char *path, int flags, int mode)
167170
{
168171
struct strbuf base_path = STRBUF_INIT;
169172
struct strbuf lock_path = STRBUF_INIT;
@@ -176,7 +179,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
176179
get_lock_path(&lock_path, base_path.buf);
177180
get_pid_path(&pid_path, base_path.buf);
178181

179-
lk->tempfile = create_tempfile_mode(lock_path.buf, mode);
182+
lk->tempfile = repo_create_tempfile_mode(r, lock_path.buf, mode);
180183
if (lk->tempfile)
181184
lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode);
182185

@@ -200,16 +203,17 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
200203
* timeout_ms milliseconds. If timeout_ms is 0, try locking the file
201204
* exactly once. If timeout_ms is -1, try indefinitely.
202205
*/
203-
static int lock_file_timeout(struct lock_file *lk, const char *path,
204-
int flags, long timeout_ms, int mode)
206+
static int lock_file_timeout(struct repository *r, struct lock_file *lk,
207+
const char *path, int flags, long timeout_ms,
208+
int mode)
205209
{
206210
int n = 1;
207211
int multiplier = 1;
208212
long remaining_ms = 0;
209213
static int random_initialized = 0;
210214

211215
if (timeout_ms == 0)
212-
return lock_file(lk, path, flags, mode);
216+
return lock_file(r, lk, path, flags, mode);
213217

214218
if (!random_initialized) {
215219
srand((unsigned int)getpid());
@@ -223,7 +227,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
223227
long backoff_ms, wait_ms;
224228
int fd;
225229

226-
fd = lock_file(lk, path, flags, mode);
230+
fd = lock_file(r, lk, path, flags, mode);
227231

228232
if (fd >= 0)
229233
return fd; /* success */
@@ -308,7 +312,17 @@ int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
308312
const char *path, int flags,
309313
long timeout_ms, int mode)
310314
{
311-
int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);
315+
return repo_hold_lock_file_for_update_timeout_mode(the_repository,
316+
lk, path, flags,
317+
timeout_ms, mode);
318+
}
319+
320+
int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
321+
struct lock_file *lk,
322+
const char *path, int flags,
323+
long timeout_ms, int mode)
324+
{
325+
int fd = lock_file_timeout(r, lk, path, flags, timeout_ms, mode);
312326
if (fd < 0) {
313327
if (flags & LOCK_DIE_ON_ERROR)
314328
unable_to_lock_die(path, errno);

lockfile.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ int hold_lock_file_for_update_timeout_mode(
189189
struct lock_file *lk, const char *path,
190190
int flags, long timeout_ms, int mode);
191191

192+
int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
193+
struct lock_file *lk,
194+
const char *path, int flags,
195+
long timeout_ms, int mode);
196+
192197
static inline int hold_lock_file_for_update_timeout(
193198
struct lock_file *lk, const char *path,
194199
int flags, long timeout_ms)
@@ -197,6 +202,16 @@ static inline int hold_lock_file_for_update_timeout(
197202
timeout_ms, 0666);
198203
}
199204

205+
static inline int repo_hold_lock_file_for_update_timeout(struct repository *r,
206+
struct lock_file *lk,
207+
const char *path,
208+
int flags,
209+
long timeout_ms)
210+
{
211+
return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
212+
timeout_ms, 0666);
213+
}
214+
200215
/*
201216
* Attempt to create a lockfile for the file at `path` and return a
202217
* file descriptor for writing to it, or -1 on error. The flags
@@ -208,13 +223,29 @@ static inline int hold_lock_file_for_update(
208223
return hold_lock_file_for_update_timeout(lk, path, flags, 0);
209224
}
210225

226+
static inline int repo_hold_lock_file_for_update(struct repository *r,
227+
struct lock_file *lk,
228+
const char *path, int flags)
229+
{
230+
return repo_hold_lock_file_for_update_timeout(r, lk, path, flags, 0);
231+
}
232+
211233
static inline int hold_lock_file_for_update_mode(
212234
struct lock_file *lk, const char *path,
213235
int flags, int mode)
214236
{
215237
return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode);
216238
}
217239

240+
static inline int repo_hold_lock_file_for_update_mode(struct repository *r,
241+
struct lock_file *lk,
242+
const char *path,
243+
int flags, int mode)
244+
{
245+
return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
246+
0, mode);
247+
}
248+
218249
/*
219250
* Return a nonzero value iff `lk` is currently locked.
220251
*/

0 commit comments

Comments
 (0)