Merge branch 'ma/lockfile-cleanup'

Code clean-up to adjust to a more recent lockfile API convention that
allows lockfile instances kept on the stack.

* ma/lockfile-cleanup:
  lock_file: move static locks into functions
  lock_file: make function-local locks non-static
  refs.c: do not die if locking fails in `delete_pseudoref()`
  refs.c: do not die if locking fails in `write_pseudoref()`
  t/helper/test-write-cache: clean up lock-handling
This commit is contained in:
Junio C Hamano 2018-05-30 14:04:05 +09:00
Родитель 0e7af5f6d1 0fa5a2ed8d
Коммит 2f76ebc93c
18 изменённых файлов: 32 добавлений и 38 удалений

Просмотреть файл

@ -4058,7 +4058,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
{ {
struct patch *patch; struct patch *patch;
struct index_state result = { NULL }; struct index_state result = { NULL };
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
int res; int res;
/* Once we start supporting the reverse patch, it may be /* Once we start supporting the reverse patch, it may be

Просмотреть файл

@ -265,8 +265,6 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
return 0; return 0;
} }
static struct lock_file lock_file;
static const char ignore_error[] = static const char ignore_error[] =
N_("The following paths are ignored by one of your .gitignore files:\n"); N_("The following paths are ignored by one of your .gitignore files:\n");
@ -393,6 +391,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
int add_new_files; int add_new_files;
int require_pathspec; int require_pathspec;
char *seen = NULL; char *seen = NULL;
struct lock_file lock_file = LOCK_INIT;
git_config(add_config, NULL); git_config(add_config, NULL);

Просмотреть файл

@ -612,7 +612,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
suffix = broken; suffix = broken;
} }
} else if (dirty) { } else if (dirty) {
static struct lock_file index_lock; struct lock_file index_lock = LOCK_INIT;
struct rev_info revs; struct rev_info revs;
struct argv_array args = ARGV_ARRAY_INIT; struct argv_array args = ARGV_ARRAY_INIT;
int fd, result; int fd, result;

Просмотреть файл

@ -610,7 +610,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
continue; continue;
if (!indices_loaded) { if (!indices_loaded) {
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addf(&buf, "%s/wtindex", tmpdir); strbuf_addf(&buf, "%s/wtindex", tmpdir);
if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 || if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||

Просмотреть файл

@ -373,7 +373,7 @@ static int need_to_gc(void)
/* return NULL on success, else hostname running the gc */ /* return NULL on success, else hostname running the gc */
static const char *lock_repo_for_gc(int force, pid_t* ret_pid) static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
{ {
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
char my_host[HOST_NAME_MAX + 1]; char my_host[HOST_NAME_MAX + 1];
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct stat st; struct stat st;

Просмотреть файл

@ -647,7 +647,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
struct commit_list *remoteheads, struct commit_list *remoteheads,
struct commit *head) struct commit *head)
{ {
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
const char *head_arg = "HEAD"; const char *head_arg = "HEAD";
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
@ -805,7 +805,7 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
{ {
struct object_id result_tree, result_commit; struct object_id result_tree, result_commit;
struct commit_list *parents, **pptr = &parents; struct commit_list *parents, **pptr = &parents;
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
hold_locked_index(&lock, LOCK_DIE_ON_ERROR); hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);

Просмотреть файл

@ -72,7 +72,6 @@ static const char *add_slash(const char *path)
return path; return path;
} }
static struct lock_file lock_file;
#define SUBMODULE_WITH_GITDIR ((const char *)1) #define SUBMODULE_WITH_GITDIR ((const char *)1)
static void prepare_move_submodule(const char *src, int first, static void prepare_move_submodule(const char *src, int first,
@ -131,6 +130,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
struct stat st; struct stat st;
struct string_list src_for_dst = STRING_LIST_INIT_NODUP; struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
struct lock_file lock_file = LOCK_INIT;
git_config(git_default_config, NULL); git_config(git_default_config, NULL);

Просмотреть файл

@ -107,8 +107,6 @@ static int git_read_tree_config(const char *var, const char *value, void *cb)
return git_default_config(var, value, cb); return git_default_config(var, value, cb);
} }
static struct lock_file lock_file;
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
{ {
int i, stage = 0; int i, stage = 0;
@ -116,6 +114,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
struct tree_desc t[MAX_UNPACK_TREES]; struct tree_desc t[MAX_UNPACK_TREES];
struct unpack_trees_options opts; struct unpack_trees_options opts;
int prefix_set = 0; int prefix_set = 0;
struct lock_file lock_file = LOCK_INIT;
const struct option read_tree_options[] = { const struct option read_tree_options[] = {
{ OPTION_CALLBACK, 0, "index-output", NULL, N_("file"), { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
N_("write resulting index to <file>"), N_("write resulting index to <file>"),

Просмотреть файл

@ -876,7 +876,7 @@ static void refuse_unconfigured_deny_delete_current(void)
static int command_singleton_iterator(void *cb_data, struct object_id *oid); static int command_singleton_iterator(void *cb_data, struct object_id *oid);
static int update_shallow_ref(struct command *cmd, struct shallow_info *si) static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
{ {
static struct lock_file shallow_lock; struct lock_file shallow_lock = LOCK_INIT;
struct oid_array extra = OID_ARRAY_INIT; struct oid_array extra = OID_ARRAY_INIT;
struct check_connected_options opt = CHECK_CONNECTED_INIT; struct check_connected_options opt = CHECK_CONNECTED_INIT;
uint32_t mask = 1 << (cmd->index % 32); uint32_t mask = 1 << (cmd->index % 32);

Просмотреть файл

@ -233,8 +233,6 @@ static int check_local_mod(struct object_id *head, int index_only)
return errs; return errs;
} }
static struct lock_file lock_file;
static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
static int ignore_unmatch = 0; static int ignore_unmatch = 0;
@ -251,6 +249,7 @@ static struct option builtin_rm_options[] = {
int cmd_rm(int argc, const char **argv, const char *prefix) int cmd_rm(int argc, const char **argv, const char *prefix)
{ {
struct lock_file lock_file = LOCK_INIT;
int i; int i;
struct pathspec pathspec; struct pathspec pathspec;
char *seen; char *seen;

Просмотреть файл

@ -409,7 +409,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
int create_bundle(struct bundle_header *header, const char *path, int create_bundle(struct bundle_header *header, const char *path,
int argc, const char **argv) int argc, const char **argv)
{ {
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
int bundle_fd = -1; int bundle_fd = -1;
int bundle_to_stdout; int bundle_to_stdout;
int ref_count = 0; int ref_count = 0;

Просмотреть файл

@ -1817,7 +1817,7 @@ static void dump_marks_helper(FILE *f,
static void dump_marks(void) static void dump_marks(void)
{ {
static struct lock_file mark_lock; struct lock_file mark_lock = LOCK_INIT;
FILE *f; FILE *f;
if (!export_marks_file || (import_marks_file && !import_marks_file_done)) if (!export_marks_file || (import_marks_file && !import_marks_file_done))

16
refs.c
Просмотреть файл

@ -660,7 +660,7 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
{ {
const char *filename; const char *filename;
int fd; int fd;
static struct lock_file lock; struct lock_file lock = LOCK_INIT;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int ret = -1; int ret = -1;
@ -670,8 +670,7 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
filename = git_path("%s", pseudoref); filename = git_path("%s", pseudoref);
fd = hold_lock_file_for_update_timeout(&lock, filename, fd = hold_lock_file_for_update_timeout(&lock, filename, 0,
LOCK_DIE_ON_ERROR,
get_files_ref_lock_timeout_ms()); get_files_ref_lock_timeout_ms());
if (fd < 0) { if (fd < 0) {
strbuf_addf(err, "could not open '%s' for writing: %s", strbuf_addf(err, "could not open '%s' for writing: %s",
@ -706,20 +705,23 @@ done:
static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid) static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
{ {
static struct lock_file lock;
const char *filename; const char *filename;
filename = git_path("%s", pseudoref); filename = git_path("%s", pseudoref);
if (old_oid && !is_null_oid(old_oid)) { if (old_oid && !is_null_oid(old_oid)) {
struct lock_file lock = LOCK_INIT;
int fd; int fd;
struct object_id actual_old_oid; struct object_id actual_old_oid;
fd = hold_lock_file_for_update_timeout( fd = hold_lock_file_for_update_timeout(
&lock, filename, LOCK_DIE_ON_ERROR, &lock, filename, 0,
get_files_ref_lock_timeout_ms()); get_files_ref_lock_timeout_ms());
if (fd < 0) if (fd < 0) {
die_errno(_("Could not open '%s' for writing"), filename); error_errno(_("could not open '%s' for writing"),
filename);
return -1;
}
if (read_ref(pseudoref, &actual_old_oid)) if (read_ref(pseudoref, &actual_old_oid))
die("could not read ref '%s'", pseudoref); die("could not read ref '%s'", pseudoref);
if (oidcmp(&actual_old_oid, old_oid)) { if (oidcmp(&actual_old_oid, old_oid)) {

Просмотреть файл

@ -2991,7 +2991,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
{ {
struct files_ref_store *refs = struct files_ref_store *refs =
files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
static struct lock_file reflog_lock; struct lock_file reflog_lock = LOCK_INIT;
struct expire_reflog_cb cb; struct expire_reflog_cb cb;
struct ref_lock *lock; struct ref_lock *lock;
struct strbuf log_file_sb = STRBUF_INIT; struct strbuf log_file_sb = STRBUF_INIT;

Просмотреть файл

@ -703,10 +703,9 @@ out:
return ret; return ret;
} }
static struct lock_file index_lock;
static void update_paths(struct string_list *update) static void update_paths(struct string_list *update)
{ {
struct lock_file index_lock = LOCK_INIT;
int i; int i;
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);

Просмотреть файл

@ -353,7 +353,7 @@ void advertise_shallow_grafts(int fd)
*/ */
void prune_shallow(int show_only) void prune_shallow(int show_only)
{ {
static struct lock_file shallow_lock; struct lock_file shallow_lock = LOCK_INIT;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int fd; int fd;

Просмотреть файл

@ -4,10 +4,10 @@
#include "tree.h" #include "tree.h"
#include "cache-tree.h" #include "cache-tree.h"
static struct lock_file index_lock;
int cmd__scrap_cache_tree(int ac, const char **av) int cmd__scrap_cache_tree(int ac, const char **av)
{ {
struct lock_file index_lock = LOCK_INIT;
setup_git_directory(); setup_git_directory();
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
if (read_cache() < 0) if (read_cache() < 0)

Просмотреть файл

@ -2,22 +2,18 @@
#include "cache.h" #include "cache.h"
#include "lockfile.h" #include "lockfile.h"
static struct lock_file index_lock;
int cmd__write_cache(int argc, const char **argv) int cmd__write_cache(int argc, const char **argv)
{ {
int i, cnt = 1, lockfd; struct lock_file index_lock = LOCK_INIT;
int i, cnt = 1;
if (argc == 2) if (argc == 2)
cnt = strtol(argv[1], NULL, 0); cnt = strtol(argv[1], NULL, 0);
setup_git_directory(); setup_git_directory();
read_cache(); read_cache();
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
lockfd = hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
if (0 <= lockfd) { if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
write_locked_index(&the_index, &index_lock, COMMIT_LOCK); die("unable to write index file");
} else {
rollback_lock_file(&index_lock);
}
} }
return 0; return 0;