Skip to content

Commit db86e61

Browse files
committed
Merge branch 'mh/tempfile'
The "lockfile" API has been rebuilt on top of a new "tempfile" API. * mh/tempfile: credential-cache--daemon: use tempfile module credential-cache--daemon: delete socket from main() gc: use tempfile module to handle gc.pid file lock_repo_for_gc(): compute the path to "gc.pid" only once diff: use tempfile module setup_temporary_shallow(): use tempfile module write_shared_index(): use tempfile module register_tempfile(): new function to handle an existing temporary file tempfile: add several functions for creating temporary files prepare_tempfile_object(): new function, extracted from create_tempfile() tempfile: a new module for handling temporary files commit_lock_file(): use get_locked_file_path() lockfile: add accessor get_lock_file_path() lockfile: add accessors get_lock_file_fd() and get_lock_file_fp() create_bundle(): duplicate file descriptor to avoid closing it twice lockfile: move documentation to lockfile.h and lockfile.c
2 parents 424f89f + 9e90331 commit db86e61

File tree

18 files changed

+969
-630
lines changed

18 files changed

+969
-630
lines changed

Documentation/technical/api-lockfile.txt

Lines changed: 0 additions & 220 deletions
This file was deleted.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ LIB_OBJS += string-list.o
786786
LIB_OBJS += submodule.o
787787
LIB_OBJS += symlinks.o
788788
LIB_OBJS += tag.o
789+
LIB_OBJS += tempfile.o
789790
LIB_OBJS += trace.o
790791
LIB_OBJS += trailer.o
791792
LIB_OBJS += transport.o

builtin/am.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "dir.h"
1111
#include "run-command.h"
1212
#include "quote.h"
13+
#include "tempfile.h"
1314
#include "lockfile.h"
1415
#include "cache-tree.h"
1516
#include "refs.h"

builtin/commit.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
324324
struct string_list partial;
325325
struct pathspec pathspec;
326326
int refresh_flags = REFRESH_QUIET;
327+
const char *ret;
327328

328329
if (is_status)
329330
refresh_flags |= REFRESH_UNMERGED;
@@ -344,7 +345,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
344345
die(_("unable to create temporary index"));
345346

346347
old_index_env = getenv(INDEX_ENVIRONMENT);
347-
setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
348+
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
348349

349350
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
350351
die(_("interactive add failed"));
@@ -355,7 +356,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
355356
unsetenv(INDEX_ENVIRONMENT);
356357

357358
discard_cache();
358-
read_cache_from(index_lock.filename.buf);
359+
read_cache_from(get_lock_file_path(&index_lock));
359360
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
360361
if (reopen_lock_file(&index_lock) < 0)
361362
die(_("unable to write index file"));
@@ -365,7 +366,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
365366
warning(_("Failed to update main cache tree"));
366367

367368
commit_style = COMMIT_NORMAL;
368-
return index_lock.filename.buf;
369+
return get_lock_file_path(&index_lock);
369370
}
370371

371372
/*
@@ -388,7 +389,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
388389
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
389390
die(_("unable to write new_index file"));
390391
commit_style = COMMIT_NORMAL;
391-
return index_lock.filename.buf;
392+
return get_lock_file_path(&index_lock);
392393
}
393394

394395
/*
@@ -475,9 +476,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
475476
die(_("unable to write temporary index file"));
476477

477478
discard_cache();
478-
read_cache_from(false_lock.filename.buf);
479-
480-
return false_lock.filename.buf;
479+
ret = get_lock_file_path(&false_lock);
480+
read_cache_from(ret);
481+
return ret;
481482
}
482483

483484
static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,

builtin/gc.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include "builtin.h"
14+
#include "tempfile.h"
1415
#include "lockfile.h"
1516
#include "parse-options.h"
1617
#include "run-command.h"
@@ -42,20 +43,7 @@ static struct argv_array prune = ARGV_ARRAY_INIT;
4243
static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
4344
static struct argv_array rerere = ARGV_ARRAY_INIT;
4445

45-
static char *pidfile;
46-
47-
static void remove_pidfile(void)
48-
{
49-
if (pidfile)
50-
unlink(pidfile);
51-
}
52-
53-
static void remove_pidfile_on_signal(int signo)
54-
{
55-
remove_pidfile();
56-
sigchain_pop(signo);
57-
raise(signo);
58-
}
46+
static struct tempfile pidfile;
5947

6048
static void git_config_date_string(const char *key, const char **output)
6149
{
@@ -199,20 +187,22 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
199187
uintmax_t pid;
200188
FILE *fp;
201189
int fd;
190+
char *pidfile_path;
202191

203-
if (pidfile)
192+
if (is_tempfile_active(&pidfile))
204193
/* already locked */
205194
return NULL;
206195

207196
if (gethostname(my_host, sizeof(my_host)))
208197
strcpy(my_host, "unknown");
209198

210-
fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
199+
pidfile_path = git_pathdup("gc.pid");
200+
fd = hold_lock_file_for_update(&lock, pidfile_path,
211201
LOCK_DIE_ON_ERROR);
212202
if (!force) {
213203
static char locking_host[128];
214204
int should_exit;
215-
fp = fopen(git_path("gc.pid"), "r");
205+
fp = fopen(pidfile_path, "r");
216206
memset(locking_host, 0, sizeof(locking_host));
217207
should_exit =
218208
fp != NULL &&
@@ -236,6 +226,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
236226
if (fd >= 0)
237227
rollback_lock_file(&lock);
238228
*ret_pid = pid;
229+
free(pidfile_path);
239230
return locking_host;
240231
}
241232
}
@@ -245,11 +236,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
245236
write_in_full(fd, sb.buf, sb.len);
246237
strbuf_release(&sb);
247238
commit_lock_file(&lock);
248-
249-
pidfile = git_pathdup("gc.pid");
250-
sigchain_push_common(remove_pidfile_on_signal);
251-
atexit(remove_pidfile);
252-
239+
register_tempfile(&pidfile, pidfile_path);
240+
free(pidfile_path);
253241
return NULL;
254242
}
255243

builtin/pull.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "dir.h"
1616
#include "refs.h"
1717
#include "revision.h"
18+
#include "tempfile.h"
1819
#include "lockfile.h"
1920

2021
enum rebase_type {

0 commit comments

Comments
 (0)