Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Aug 25, 2015
2 parents 424f89f + 9e90331 commit db86e61
Show file tree
Hide file tree
Showing 18 changed files with 969 additions and 630 deletions.
220 changes: 0 additions & 220 deletions Documentation/technical/api-lockfile.txt

This file was deleted.

1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -786,6 +786,7 @@ LIB_OBJS += string-list.o
LIB_OBJS += submodule.o
LIB_OBJS += symlinks.o
LIB_OBJS += tag.o
LIB_OBJS += tempfile.o
LIB_OBJS += trace.o
LIB_OBJS += trailer.o
LIB_OBJS += transport.o
Expand Down
1 change: 1 addition & 0 deletions builtin/am.c
Expand Up @@ -10,6 +10,7 @@
#include "dir.h"
#include "run-command.h"
#include "quote.h"
#include "tempfile.h"
#include "lockfile.h"
#include "cache-tree.h"
#include "refs.h"
Expand Down
15 changes: 8 additions & 7 deletions builtin/commit.c
Expand Up @@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
struct string_list partial;
struct pathspec pathspec;
int refresh_flags = REFRESH_QUIET;
const char *ret;

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

old_index_env = getenv(INDEX_ENVIRONMENT);
setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);

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

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

commit_style = COMMIT_NORMAL;
return index_lock.filename.buf;
return get_lock_file_path(&index_lock);
}

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

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

discard_cache();
read_cache_from(false_lock.filename.buf);

return false_lock.filename.buf;
ret = get_lock_file_path(&false_lock);
read_cache_from(ret);
return ret;
}

static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
Expand Down
32 changes: 10 additions & 22 deletions builtin/gc.c
Expand Up @@ -11,6 +11,7 @@
*/

#include "builtin.h"
#include "tempfile.h"
#include "lockfile.h"
#include "parse-options.h"
#include "run-command.h"
Expand Down Expand Up @@ -42,20 +43,7 @@ static struct argv_array prune = ARGV_ARRAY_INIT;
static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
static struct argv_array rerere = ARGV_ARRAY_INIT;

static char *pidfile;

static void remove_pidfile(void)
{
if (pidfile)
unlink(pidfile);
}

static void remove_pidfile_on_signal(int signo)
{
remove_pidfile();
sigchain_pop(signo);
raise(signo);
}
static struct tempfile pidfile;

static void git_config_date_string(const char *key, const char **output)
{
Expand Down Expand Up @@ -199,20 +187,22 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
uintmax_t pid;
FILE *fp;
int fd;
char *pidfile_path;

if (pidfile)
if (is_tempfile_active(&pidfile))
/* already locked */
return NULL;

if (gethostname(my_host, sizeof(my_host)))
strcpy(my_host, "unknown");

fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
pidfile_path = git_pathdup("gc.pid");
fd = hold_lock_file_for_update(&lock, pidfile_path,
LOCK_DIE_ON_ERROR);
if (!force) {
static char locking_host[128];
int should_exit;
fp = fopen(git_path("gc.pid"), "r");
fp = fopen(pidfile_path, "r");
memset(locking_host, 0, sizeof(locking_host));
should_exit =
fp != NULL &&
Expand All @@ -236,6 +226,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
if (fd >= 0)
rollback_lock_file(&lock);
*ret_pid = pid;
free(pidfile_path);
return locking_host;
}
}
Expand All @@ -245,11 +236,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
write_in_full(fd, sb.buf, sb.len);
strbuf_release(&sb);
commit_lock_file(&lock);

pidfile = git_pathdup("gc.pid");
sigchain_push_common(remove_pidfile_on_signal);
atexit(remove_pidfile);

register_tempfile(&pidfile, pidfile_path);
free(pidfile_path);
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions builtin/pull.c
Expand Up @@ -15,6 +15,7 @@
#include "dir.h"
#include "refs.h"
#include "revision.h"
#include "tempfile.h"
#include "lockfile.h"

enum rebase_type {
Expand Down

0 comments on commit db86e61

Please sign in to comment.