Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 03b8664

Browse files
pcloudsgitster
authored andcommitted
read-cache: new API write_locked_index instead of write_index/write_cache
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33c297a commit 03b8664

20 files changed

+86
-93
lines changed

builtin/add.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)
299299
int cmd_add(int argc, const char **argv, const char *prefix)
300300
{
301301
int exit_status = 0;
302-
int newfd;
303302
struct pathspec pathspec;
304303
struct dir_struct dir;
305304
int flags;
@@ -345,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
345344
add_new_files = !take_worktree_changes && !refresh_only;
346345
require_pathspec = !take_worktree_changes;
347346

348-
newfd = hold_locked_index(&lock_file, 1);
347+
hold_locked_index(&lock_file, 1);
349348

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

444443
finish:
445444
if (active_cache_changed) {
446-
if (write_cache(newfd, active_cache, active_nr) ||
447-
commit_locked_index(&lock_file))
445+
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
448446
die(_("Unable to write new index file"));
449447
}
450448

builtin/apply.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,7 +3644,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
36443644
{
36453645
struct patch *patch;
36463646
struct index_state result = { NULL };
3647-
int fd;
3647+
static struct lock_file lock;
36483648

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

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

36893689
discard_index(&result);
@@ -4501,8 +4501,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
45014501
}
45024502

45034503
if (update_index) {
4504-
if (write_cache(newfd, active_cache, active_nr) ||
4505-
commit_locked_index(&lock_file))
4504+
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
45064505
die(_("Unable to write new index file"));
45074506
}
45084507

builtin/checkout-index.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
279279
checkout_all(prefix, prefix_length);
280280

281281
if (0 <= newfd &&
282-
(write_cache(newfd, active_cache, active_nr) ||
283-
commit_locked_index(&lock_file)))
282+
write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
284283
die("Unable to write new index file");
285284
return 0;
286285
}

builtin/checkout.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,
225225
int flag;
226226
struct commit *head;
227227
int errs = 0;
228-
int newfd;
229228
struct lock_file *lock_file;
230229

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

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

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

@@ -352,8 +351,7 @@ static int checkout_paths(const struct checkout_opts *opts,
352351
}
353352
}
354353

355-
if (write_cache(newfd, active_cache, active_nr) ||
356-
commit_locked_index(lock_file))
354+
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
357355
die(_("unable to write new index file"));
358356

359357
read_ref_full("HEAD", rev, 0, &flag);
@@ -444,8 +442,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
444442
{
445443
int ret;
446444
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
447-
int newfd = hold_locked_index(lock_file, 1);
448445

446+
hold_locked_index(lock_file, 1);
449447
if (read_cache_preload(NULL) < 0)
450448
return error(_("corrupt index file"));
451449

@@ -553,8 +551,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
553551
}
554552
}
555553

556-
if (write_cache(newfd, active_cache, active_nr) ||
557-
commit_locked_index(lock_file))
554+
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
558555
die(_("unable to write new index file"));
559556

560557
if (!opts->force && !opts->quiet)

builtin/clone.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static int checkout(void)
616616
struct unpack_trees_options opts;
617617
struct tree *tree;
618618
struct tree_desc t;
619-
int err = 0, fd;
619+
int err = 0;
620620

621621
if (option_no_checkout)
622622
return 0;
@@ -640,7 +640,7 @@ static int checkout(void)
640640
setup_work_tree();
641641

642642
lock_file = xcalloc(1, sizeof(struct lock_file));
643-
fd = hold_locked_index(lock_file, 1);
643+
hold_locked_index(lock_file, 1);
644644

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

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

663662
err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),

builtin/commit.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ static void refresh_cache_or_die(int refresh_flags)
305305
static char *prepare_index(int argc, const char **argv, const char *prefix,
306306
const struct commit *current_head, int is_status)
307307
{
308-
int fd;
309308
struct string_list partial;
310309
struct pathspec pathspec;
311310
int refresh_flags = REFRESH_QUIET;
@@ -321,12 +320,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
321320

322321
if (interactive) {
323322
char *old_index_env = NULL;
324-
fd = hold_locked_index(&index_lock, 1);
323+
hold_locked_index(&index_lock, 1);
325324

326325
refresh_cache_or_die(refresh_flags);
327326

328-
if (write_cache(fd, active_cache, active_nr) ||
329-
close_lock_file(&index_lock))
327+
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
330328
die(_("unable to create temporary index"));
331329

332330
old_index_env = getenv(INDEX_ENVIRONMENT);
@@ -360,12 +358,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
360358
* (B) on failure, rollback the real index.
361359
*/
362360
if (all || (also && pathspec.nr)) {
363-
fd = hold_locked_index(&index_lock, 1);
361+
hold_locked_index(&index_lock, 1);
364362
add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
365363
refresh_cache_or_die(refresh_flags);
366364
update_main_cache_tree(WRITE_TREE_SILENT);
367-
if (write_cache(fd, active_cache, active_nr) ||
368-
close_lock_file(&index_lock))
365+
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
369366
die(_("unable to write new_index file"));
370367
commit_style = COMMIT_NORMAL;
371368
return index_lock.filename;
@@ -381,12 +378,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
381378
* We still need to refresh the index here.
382379
*/
383380
if (!only && !pathspec.nr) {
384-
fd = hold_locked_index(&index_lock, 1);
381+
hold_locked_index(&index_lock, 1);
385382
refresh_cache_or_die(refresh_flags);
386383
if (active_cache_changed) {
387384
update_main_cache_tree(WRITE_TREE_SILENT);
388-
if (write_cache(fd, active_cache, active_nr) ||
389-
commit_locked_index(&index_lock))
385+
if (write_locked_index(&the_index, &index_lock,
386+
COMMIT_LOCK))
390387
die(_("unable to write new_index file"));
391388
} else {
392389
rollback_lock_file(&index_lock);
@@ -432,24 +429,22 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
432429
if (read_cache() < 0)
433430
die(_("cannot read the index"));
434431

435-
fd = hold_locked_index(&index_lock, 1);
432+
hold_locked_index(&index_lock, 1);
436433
add_remove_files(&partial);
437434
refresh_cache(REFRESH_QUIET);
438-
if (write_cache(fd, active_cache, active_nr) ||
439-
close_lock_file(&index_lock))
435+
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
440436
die(_("unable to write new_index file"));
441437

442-
fd = hold_lock_file_for_update(&false_lock,
443-
git_path("next-index-%"PRIuMAX,
444-
(uintmax_t) getpid()),
445-
LOCK_DIE_ON_ERROR);
438+
hold_lock_file_for_update(&false_lock,
439+
git_path("next-index-%"PRIuMAX,
440+
(uintmax_t) getpid()),
441+
LOCK_DIE_ON_ERROR);
446442

447443
create_base_index(current_head);
448444
add_remove_files(&partial);
449445
refresh_cache(REFRESH_QUIET);
450446

451-
if (write_cache(fd, active_cache, active_nr) ||
452-
close_lock_file(&false_lock))
447+
if (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))
453448
die(_("unable to write temporary index file"));
454449

455450
discard_cache();

builtin/merge.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -657,22 +657,19 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
657657
struct commit_list *remoteheads,
658658
struct commit *head, const char *head_arg)
659659
{
660-
int index_fd;
661660
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
662661

663-
index_fd = hold_locked_index(lock, 1);
662+
hold_locked_index(lock, 1);
664663
refresh_cache(REFRESH_QUIET);
665664
if (active_cache_changed &&
666-
(write_cache(index_fd, active_cache, active_nr) ||
667-
commit_locked_index(lock)))
665+
write_locked_index(&the_index, lock, COMMIT_LOCK))
668666
return error(_("Unable to write index."));
669667
rollback_lock_file(lock);
670668

671669
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
672670
int clean, x;
673671
struct commit *result;
674672
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
675-
int index_fd;
676673
struct commit_list *reversed = NULL;
677674
struct merge_options o;
678675
struct commit_list *j;
@@ -700,12 +697,11 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
700697
for (j = common; j; j = j->next)
701698
commit_list_insert(j->item, &reversed);
702699

703-
index_fd = hold_locked_index(lock, 1);
700+
hold_locked_index(lock, 1);
704701
clean = merge_recursive(&o, head,
705702
remoteheads->item, reversed, &result);
706703
if (active_cache_changed &&
707-
(write_cache(index_fd, active_cache, active_nr) ||
708-
commit_locked_index(lock)))
704+
write_locked_index(&the_index, lock, COMMIT_LOCK))
709705
die (_("unable to write %s"), get_index_file());
710706
rollback_lock_file(lock);
711707
return clean ? 0 : 1;

builtin/mv.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static struct lock_file lock_file;
6363

6464
int cmd_mv(int argc, const char **argv, const char *prefix)
6565
{
66-
int i, newfd, gitmodules_modified = 0;
66+
int i, gitmodules_modified = 0;
6767
int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
6868
struct option builtin_mv_options[] = {
6969
OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -85,7 +85,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
8585
if (--argc < 1)
8686
usage_with_options(builtin_mv_usage, builtin_mv_options);
8787

88-
newfd = hold_locked_index(&lock_file, 1);
88+
hold_locked_index(&lock_file, 1);
8989
if (read_cache() < 0)
9090
die(_("index file corrupt"));
9191

@@ -275,8 +275,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
275275
stage_updated_gitmodules();
276276

277277
if (active_cache_changed) {
278-
if (write_cache(newfd, active_cache, active_nr) ||
279-
commit_locked_index(&lock_file))
278+
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
280279
die(_("Unable to write new index file"));
281280
}
282281

builtin/read-tree.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static struct lock_file lock_file;
9999

100100
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
101101
{
102-
int i, newfd, stage = 0;
102+
int i, stage = 0;
103103
unsigned char sha1[20];
104104
struct tree_desc t[MAX_UNPACK_TREES];
105105
struct unpack_trees_options opts;
@@ -149,7 +149,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
149149
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
150150
read_tree_usage, 0);
151151

152-
newfd = hold_locked_index(&lock_file, 1);
152+
hold_locked_index(&lock_file, 1);
153153

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

236-
if (write_cache(newfd, active_cache, active_nr) ||
237-
commit_locked_index(&lock_file))
236+
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
238237
die("unable to write new index file");
239238
return 0;
240239
}

builtin/reset.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
351351

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

370-
if (write_cache(newfd, active_cache, active_nr) ||
371-
commit_locked_index(lock))
370+
if (write_locked_index(&the_index, lock, COMMIT_LOCK))
372371
die(_("Could not write new index file."));
373372
}
374373

0 commit comments

Comments
 (0)