Skip to content

Commit

Permalink
read-cache: new API write_locked_index instead of write_index/write_c…
Browse files Browse the repository at this point in the history
…ache

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Jun 13, 2014
1 parent 33c297a commit 03b8664
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 93 deletions.
6 changes: 2 additions & 4 deletions builtin/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)
int cmd_add(int argc, const char **argv, const char *prefix)
{
int exit_status = 0;
int newfd;
struct pathspec pathspec;
struct dir_struct dir;
int flags;
Expand Down Expand Up @@ -345,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
add_new_files = !take_worktree_changes && !refresh_only;
require_pathspec = !take_worktree_changes;

newfd = hold_locked_index(&lock_file, 1);
hold_locked_index(&lock_file, 1);

flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
(show_only ? ADD_CACHE_PRETEND : 0) |
Expand Down Expand Up @@ -443,8 +442,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)

finish:
if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file))
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("Unable to write new index file"));
}

Expand Down
9 changes: 4 additions & 5 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -3644,7 +3644,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
{
struct patch *patch;
struct index_state result = { NULL };
int fd;
static struct lock_file lock;

/* Once we start supporting the reverse patch, it may be
* worth showing the new sha1 prefix, but until then...
Expand Down Expand Up @@ -3682,8 +3682,8 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
die ("Could not add %s to temporary index", name);
}

fd = open(filename, O_WRONLY | O_CREAT, 0666);
if (fd < 0 || write_index(&result, fd) || close(fd))
hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
if (write_locked_index(&result, &lock, COMMIT_LOCK))
die ("Could not write temporary index to %s", filename);

discard_index(&result);
Expand Down Expand Up @@ -4501,8 +4501,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
}

if (update_index) {
if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file))
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("Unable to write new index file"));
}

Expand Down
3 changes: 1 addition & 2 deletions builtin/checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
checkout_all(prefix, prefix_length);

if (0 <= newfd &&
(write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file)))
write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die("Unable to write new index file");
return 0;
}
11 changes: 4 additions & 7 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,
int flag;
struct commit *head;
int errs = 0;
int newfd;
struct lock_file *lock_file;

if (opts->track != BRANCH_TRACK_UNSPECIFIED)
Expand Down Expand Up @@ -256,7 +255,7 @@ static int checkout_paths(const struct checkout_opts *opts,

lock_file = xcalloc(1, sizeof(struct lock_file));

newfd = hold_locked_index(lock_file, 1);
hold_locked_index(lock_file, 1);
if (read_cache_preload(&opts->pathspec) < 0)
return error(_("corrupt index file"));

Expand Down Expand Up @@ -352,8 +351,7 @@ static int checkout_paths(const struct checkout_opts *opts,
}
}

if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(lock_file))
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));

read_ref_full("HEAD", rev, 0, &flag);
Expand Down Expand Up @@ -444,8 +442,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
{
int ret;
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
int newfd = hold_locked_index(lock_file, 1);

hold_locked_index(lock_file, 1);
if (read_cache_preload(NULL) < 0)
return error(_("corrupt index file"));

Expand Down Expand Up @@ -553,8 +551,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
}
}

if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(lock_file))
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));

if (!opts->force && !opts->quiet)
Expand Down
7 changes: 3 additions & 4 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ static int checkout(void)
struct unpack_trees_options opts;
struct tree *tree;
struct tree_desc t;
int err = 0, fd;
int err = 0;

if (option_no_checkout)
return 0;
Expand All @@ -640,7 +640,7 @@ static int checkout(void)
setup_work_tree();

lock_file = xcalloc(1, sizeof(struct lock_file));
fd = hold_locked_index(lock_file, 1);
hold_locked_index(lock_file, 1);

memset(&opts, 0, sizeof opts);
opts.update = 1;
Expand All @@ -656,8 +656,7 @@ static int checkout(void)
if (unpack_trees(1, &t, &opts) < 0)
die(_("unable to checkout working tree"));

if (write_cache(fd, active_cache, active_nr) ||
commit_locked_index(lock_file))
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));

err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
Expand Down
33 changes: 14 additions & 19 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ static void refresh_cache_or_die(int refresh_flags)
static char *prepare_index(int argc, const char **argv, const char *prefix,
const struct commit *current_head, int is_status)
{
int fd;
struct string_list partial;
struct pathspec pathspec;
int refresh_flags = REFRESH_QUIET;
Expand All @@ -321,12 +320,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,

if (interactive) {
char *old_index_env = NULL;
fd = hold_locked_index(&index_lock, 1);
hold_locked_index(&index_lock, 1);

refresh_cache_or_die(refresh_flags);

if (write_cache(fd, active_cache, active_nr) ||
close_lock_file(&index_lock))
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
die(_("unable to create temporary index"));

old_index_env = getenv(INDEX_ENVIRONMENT);
Expand Down Expand Up @@ -360,12 +358,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
* (B) on failure, rollback the real index.
*/
if (all || (also && pathspec.nr)) {
fd = hold_locked_index(&index_lock, 1);
hold_locked_index(&index_lock, 1);
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
refresh_cache_or_die(refresh_flags);
update_main_cache_tree(WRITE_TREE_SILENT);
if (write_cache(fd, active_cache, active_nr) ||
close_lock_file(&index_lock))
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;
Expand All @@ -381,12 +378,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
* We still need to refresh the index here.
*/
if (!only && !pathspec.nr) {
fd = hold_locked_index(&index_lock, 1);
hold_locked_index(&index_lock, 1);
refresh_cache_or_die(refresh_flags);
if (active_cache_changed) {
update_main_cache_tree(WRITE_TREE_SILENT);
if (write_cache(fd, active_cache, active_nr) ||
commit_locked_index(&index_lock))
if (write_locked_index(&the_index, &index_lock,
COMMIT_LOCK))
die(_("unable to write new_index file"));
} else {
rollback_lock_file(&index_lock);
Expand Down Expand Up @@ -432,24 +429,22 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
if (read_cache() < 0)
die(_("cannot read the index"));

fd = hold_locked_index(&index_lock, 1);
hold_locked_index(&index_lock, 1);
add_remove_files(&partial);
refresh_cache(REFRESH_QUIET);
if (write_cache(fd, active_cache, active_nr) ||
close_lock_file(&index_lock))
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
die(_("unable to write new_index file"));

fd = hold_lock_file_for_update(&false_lock,
git_path("next-index-%"PRIuMAX,
(uintmax_t) getpid()),
LOCK_DIE_ON_ERROR);
hold_lock_file_for_update(&false_lock,
git_path("next-index-%"PRIuMAX,
(uintmax_t) getpid()),
LOCK_DIE_ON_ERROR);

create_base_index(current_head);
add_remove_files(&partial);
refresh_cache(REFRESH_QUIET);

if (write_cache(fd, active_cache, active_nr) ||
close_lock_file(&false_lock))
if (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))
die(_("unable to write temporary index file"));

discard_cache();
Expand Down
12 changes: 4 additions & 8 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,22 +657,19 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
struct commit_list *remoteheads,
struct commit *head, const char *head_arg)
{
int index_fd;
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));

index_fd = hold_locked_index(lock, 1);
hold_locked_index(lock, 1);
refresh_cache(REFRESH_QUIET);
if (active_cache_changed &&
(write_cache(index_fd, active_cache, active_nr) ||
commit_locked_index(lock)))
write_locked_index(&the_index, lock, COMMIT_LOCK))
return error(_("Unable to write index."));
rollback_lock_file(lock);

if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
int clean, x;
struct commit *result;
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
int index_fd;
struct commit_list *reversed = NULL;
struct merge_options o;
struct commit_list *j;
Expand Down Expand Up @@ -700,12 +697,11 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
for (j = common; j; j = j->next)
commit_list_insert(j->item, &reversed);

index_fd = hold_locked_index(lock, 1);
hold_locked_index(lock, 1);
clean = merge_recursive(&o, head,
remoteheads->item, reversed, &result);
if (active_cache_changed &&
(write_cache(index_fd, active_cache, active_nr) ||
commit_locked_index(lock)))
write_locked_index(&the_index, lock, COMMIT_LOCK))
die (_("unable to write %s"), get_index_file());
rollback_lock_file(lock);
return clean ? 0 : 1;
Expand Down
7 changes: 3 additions & 4 deletions builtin/mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static struct lock_file lock_file;

int cmd_mv(int argc, const char **argv, const char *prefix)
{
int i, newfd, gitmodules_modified = 0;
int i, gitmodules_modified = 0;
int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
struct option builtin_mv_options[] = {
OPT__VERBOSE(&verbose, N_("be verbose")),
Expand All @@ -85,7 +85,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
if (--argc < 1)
usage_with_options(builtin_mv_usage, builtin_mv_options);

newfd = hold_locked_index(&lock_file, 1);
hold_locked_index(&lock_file, 1);
if (read_cache() < 0)
die(_("index file corrupt"));

Expand Down Expand Up @@ -275,8 +275,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
stage_updated_gitmodules();

if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file))
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("Unable to write new index file"));
}

Expand Down
7 changes: 3 additions & 4 deletions builtin/read-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static struct lock_file lock_file;

int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
{
int i, newfd, stage = 0;
int i, stage = 0;
unsigned char sha1[20];
struct tree_desc t[MAX_UNPACK_TREES];
struct unpack_trees_options opts;
Expand Down Expand Up @@ -149,7 +149,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
read_tree_usage, 0);

newfd = hold_locked_index(&lock_file, 1);
hold_locked_index(&lock_file, 1);

prefix_set = opts.prefix ? 1 : 0;
if (1 < opts.merge + opts.reset + prefix_set)
Expand Down Expand Up @@ -233,8 +233,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
if (nr_trees == 1 && !opts.prefix)
prime_cache_tree(&active_cache_tree, trees[0]);

if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file))
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die("unable to write new index file");
return 0;
}
5 changes: 2 additions & 3 deletions builtin/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)

if (reset_type != SOFT) {
struct lock_file *lock = xcalloc(1, sizeof(*lock));
int newfd = hold_locked_index(lock, 1);
hold_locked_index(lock, 1);
if (reset_type == MIXED) {
int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
if (read_from_tree(&pathspec, sha1, intent_to_add))
Expand All @@ -367,8 +367,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
die(_("Could not reset index file to revision '%s'."), rev);
}

if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(lock))
if (write_locked_index(&the_index, lock, COMMIT_LOCK))
die(_("Could not write new index file."));
}

Expand Down
7 changes: 3 additions & 4 deletions builtin/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static struct option builtin_rm_options[] = {

int cmd_rm(int argc, const char **argv, const char *prefix)
{
int i, newfd;
int i;
struct pathspec pathspec;
char *seen;

Expand All @@ -293,7 +293,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
if (!index_only)
setup_work_tree();

newfd = hold_locked_index(&lock_file, 1);
hold_locked_index(&lock_file, 1);

if (read_cache() < 0)
die(_("index file corrupt"));
Expand Down Expand Up @@ -427,8 +427,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
}

if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(&lock_file))
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("Unable to write new index file"));
}

Expand Down
3 changes: 1 addition & 2 deletions builtin/update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
exit(128);
unable_to_lock_index_die(get_index_file(), lock_error);
}
if (write_cache(newfd, active_cache, active_nr) ||
commit_locked_index(lock_file))
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die("Unable to write new index file");
}

Expand Down
3 changes: 1 addition & 2 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
active_nr, flags) < 0)
return WRITE_TREE_UNMERGED_INDEX;
if (0 <= newfd) {
if (!write_cache(newfd, active_cache, active_nr) &&
!commit_lock_file(lock_file))
if (!write_locked_index(&the_index, lock_file, COMMIT_LOCK))
newfd = -1;
}
/* Not being able to write is fine -- we are only interested
Expand Down
Loading

0 comments on commit 03b8664

Please sign in to comment.