read-cache: new API write_locked_index instead of write_index/write_cache

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jun 13, 2014 at 19:19 UTC 03b86647722f11ccc321cd7279aa49b811d17cc2
20 files changed +86 -93
builtin/add.c
+2 -4
@@ -299,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)
299 int cmd_add(int argc, const char **argv, const char *prefix)
300 {
301 int exit_status = 0;
302 - int newfd;
302 struct pathspec pathspec;
303 struct dir_struct dir;
304 int flags;
@@ -345,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
344 add_new_files = !take_worktree_changes && !refresh_only;
345 require_pathspec = !take_worktree_changes;
346
348 - newfd = hold_locked_index(&lock_file, 1);
347 + hold_locked_index(&lock_file, 1);
348
349 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
350 (show_only ? ADD_CACHE_PRETEND : 0) |
@@ -443,8 +442,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
442
443 finish:
444 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))
446 die(_("Unable to write new index file"));
447 }
448
builtin/apply.c
+4 -5
@@ -3644,7 +3644,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
3644 {
3645 struct patch *patch;
3646 struct index_state result = { NULL };
3647 - int fd;
3647 + static struct lock_file lock;
3648
3649 /* Once we start supporting the reverse patch, it may be
3650 * worth showing the new sha1 prefix, but until then...
@@ -3682,8 +3682,8 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
3682 die ("Could not add %s to temporary index", name);
3683 }
3684
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))
3687 die ("Could not write temporary index to %s", filename);
3688
3689 discard_index(&result);
@@ -4501,8 +4501,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
4501 }
4502
4503 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))
4505 die(_("Unable to write new index file"));
4506 }
4507
builtin/checkout-index.c
+1 -2
@@ -279,8 +279,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
279 checkout_all(prefix, prefix_length);
280
281 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))
283 die("Unable to write new index file");
284 return 0;
285 }
builtin/checkout.c
+4 -7
@@ -225,7 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,
225 int flag;
226 struct commit *head;
227 int errs = 0;
228 - int newfd;
228 struct lock_file *lock_file;
229
230 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
@@ -256,7 +255,7 @@ static int checkout_paths(const struct checkout_opts *opts,
255
256 lock_file = xcalloc(1, sizeof(struct lock_file));
257
259 - newfd = hold_locked_index(lock_file, 1);
258 + hold_locked_index(lock_file, 1);
259 if (read_cache_preload(&opts->pathspec) < 0)
260 return error(_("corrupt index file"));
261
@@ -352,8 +351,7 @@ static int checkout_paths(const struct checkout_opts *opts,
351 }
352 }
353
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))
355 die(_("unable to write new index file"));
356
357 read_ref_full("HEAD", rev, 0, &flag);
@@ -444,8 +442,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
442 {
443 int ret;
444 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
447 - int newfd = hold_locked_index(lock_file, 1);
445
446 + hold_locked_index(lock_file, 1);
447 if (read_cache_preload(NULL) < 0)
448 return error(_("corrupt index file"));
449
@@ -553,8 +551,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
551 }
552 }
553
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))
555 die(_("unable to write new index file"));
556
557 if (!opts->force && !opts->quiet)
builtin/clone.c
+3 -4
@@ -616,7 +616,7 @@ static int checkout(void)
616 struct unpack_trees_options opts;
617 struct tree *tree;
618 struct tree_desc t;
619 - int err = 0, fd;
619 + int err = 0;
620
621 if (option_no_checkout)
622 return 0;
@@ -640,7 +640,7 @@ static int checkout(void)
640 setup_work_tree();
641
642 lock_file = xcalloc(1, sizeof(struct lock_file));
643 - fd = hold_locked_index(lock_file, 1);
643 + hold_locked_index(lock_file, 1);
644
645 memset(&opts, 0, sizeof opts);
646 opts.update = 1;
@@ -656,8 +656,7 @@ static int checkout(void)
656 if (unpack_trees(1, &t, &opts) < 0)
657 die(_("unable to checkout working tree"));
658
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))
660 die(_("unable to write new index file"));
661
662 err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
builtin/commit.c
+14 -19
@@ -305,7 +305,6 @@ static void refresh_cache_or_die(int refresh_flags)
305 static char *prepare_index(int argc, const char **argv, const char *prefix,
306 const struct commit *current_head, int is_status)
307 {
308 - int fd;
308 struct string_list partial;
309 struct pathspec pathspec;
310 int refresh_flags = REFRESH_QUIET;
@@ -321,12 +320,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
320
321 if (interactive) {
322 char *old_index_env = NULL;
324 - fd = hold_locked_index(&index_lock, 1);
323 + hold_locked_index(&index_lock, 1);
324
325 refresh_cache_or_die(refresh_flags);
326
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))
328 die(_("unable to create temporary index"));
329
330 old_index_env = getenv(INDEX_ENVIRONMENT);
@@ -360,12 +358,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
358 * (B) on failure, rollback the real index.
359 */
360 if (all || (also && pathspec.nr)) {
363 - fd = hold_locked_index(&index_lock, 1);
361 + hold_locked_index(&index_lock, 1);
362 add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
363 refresh_cache_or_die(refresh_flags);
364 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))
366 die(_("unable to write new_index file"));
367 commit_style = COMMIT_NORMAL;
368 return index_lock.filename;
@@ -381,12 +378,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
378 * We still need to refresh the index here.
379 */
380 if (!only && !pathspec.nr) {
384 - fd = hold_locked_index(&index_lock, 1);
381 + hold_locked_index(&index_lock, 1);
382 refresh_cache_or_die(refresh_flags);
383 if (active_cache_changed) {
384 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))
387 die(_("unable to write new_index file"));
388 } else {
389 rollback_lock_file(&index_lock);
@@ -432,24 +429,22 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
429 if (read_cache() < 0)
430 die(_("cannot read the index"));
431
435 - fd = hold_locked_index(&index_lock, 1);
432 + hold_locked_index(&index_lock, 1);
433 add_remove_files(&partial);
434 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))
436 die(_("unable to write new_index file"));
437
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);
442
443 create_base_index(current_head);
444 add_remove_files(&partial);
445 refresh_cache(REFRESH_QUIET);
446
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))
448 die(_("unable to write temporary index file"));
449
450 discard_cache();
builtin/merge.c
+4 -8
@@ -657,14 +657,12 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
657 struct commit_list *remoteheads,
658 struct commit *head, const char *head_arg)
659 {
660 - int index_fd;
660 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
661
663 - index_fd = hold_locked_index(lock, 1);
662 + hold_locked_index(lock, 1);
663 refresh_cache(REFRESH_QUIET);
664 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))
666 return error(_("Unable to write index."));
667 rollback_lock_file(lock);
668
@@ -672,7 +670,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
670 int clean, x;
671 struct commit *result;
672 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
675 - int index_fd;
673 struct commit_list *reversed = NULL;
674 struct merge_options o;
675 struct commit_list *j;
@@ -700,12 +697,11 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
697 for (j = common; j; j = j->next)
698 commit_list_insert(j->item, &reversed);
699
703 - index_fd = hold_locked_index(lock, 1);
700 + hold_locked_index(lock, 1);
701 clean = merge_recursive(&o, head,
702 remoteheads->item, reversed, &result);
703 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))
705 die (_("unable to write %s"), get_index_file());
706 rollback_lock_file(lock);
707 return clean ? 0 : 1;
builtin/mv.c
+3 -4
@@ -63,7 +63,7 @@ static struct lock_file lock_file;
63
64 int cmd_mv(int argc, const char **argv, const char *prefix)
65 {
66 - int i, newfd, gitmodules_modified = 0;
66 + int i, gitmodules_modified = 0;
67 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
68 struct option builtin_mv_options[] = {
69 OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -85,7 +85,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
85 if (--argc < 1)
86 usage_with_options(builtin_mv_usage, builtin_mv_options);
87
88 - newfd = hold_locked_index(&lock_file, 1);
88 + hold_locked_index(&lock_file, 1);
89 if (read_cache() < 0)
90 die(_("index file corrupt"));
91
@@ -275,8 +275,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
275 stage_updated_gitmodules();
276
277 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))
279 die(_("Unable to write new index file"));
280 }
281
builtin/read-tree.c
+3 -4
@@ -99,7 +99,7 @@ static struct lock_file lock_file;
99
100 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
101 {
102 - int i, newfd, stage = 0;
102 + int i, stage = 0;
103 unsigned char sha1[20];
104 struct tree_desc t[MAX_UNPACK_TREES];
105 struct unpack_trees_options opts;
@@ -149,7 +149,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
149 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
150 read_tree_usage, 0);
151
152 - newfd = hold_locked_index(&lock_file, 1);
152 + hold_locked_index(&lock_file, 1);
153
154 prefix_set = opts.prefix ? 1 : 0;
155 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)
233 if (nr_trees == 1 && !opts.prefix)
234 prime_cache_tree(&active_cache_tree, trees[0]);
235
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))
237 die("unable to write new index file");
238 return 0;
239 }
builtin/reset.c
+2 -3
@@ -351,7 +351,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
351
352 if (reset_type != SOFT) {
353 struct lock_file *lock = xcalloc(1, sizeof(*lock));
354 - int newfd = hold_locked_index(lock, 1);
354 + hold_locked_index(lock, 1);
355 if (reset_type == MIXED) {
356 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
357 if (read_from_tree(&pathspec, sha1, intent_to_add))
@@ -367,8 +367,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
367 die(_("Could not reset index file to revision '%s'."), rev);
368 }
369
370 - if (write_cache(newfd, active_cache, active_nr) ||
371 - commit_locked_index(lock))
370 + if (write_locked_index(&the_index, lock, COMMIT_LOCK))
371 die(_("Could not write new index file."));
372 }
373
builtin/rm.c
+3 -4
@@ -278,7 +278,7 @@ static struct option builtin_rm_options[] = {
278
279 int cmd_rm(int argc, const char **argv, const char *prefix)
280 {
281 - int i, newfd;
281 + int i;
282 struct pathspec pathspec;
283 char *seen;
284
@@ -293,7 +293,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
293 if (!index_only)
294 setup_work_tree();
295
296 - newfd = hold_locked_index(&lock_file, 1);
296 + hold_locked_index(&lock_file, 1);
297
298 if (read_cache() < 0)
299 die(_("index file corrupt"));
@@ -427,8 +427,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
427 }
428
429 if (active_cache_changed) {
430 - if (write_cache(newfd, active_cache, active_nr) ||
431 - commit_locked_index(&lock_file))
430 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
431 die(_("Unable to write new index file"));
432 }
433
builtin/update-index.c
+1 -2
@@ -921,8 +921,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
921 exit(128);
922 unable_to_lock_index_die(get_index_file(), lock_error);
923 }
924 - if (write_cache(newfd, active_cache, active_nr) ||
925 - commit_locked_index(lock_file))
924 + if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
925 die("Unable to write new index file");
926 }
927
cache-tree.c
+1 -2
@@ -595,8 +595,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
595 active_nr, flags) < 0)
596 return WRITE_TREE_UNMERGED_INDEX;
597 if (0 <= newfd) {
598 - if (!write_cache(newfd, active_cache, active_nr) &&
599 - !commit_lock_file(lock_file))
598 + if (!write_locked_index(&the_index, lock_file, COMMIT_LOCK))
599 newfd = -1;
600 }
601 /* Not being able to write is fine -- we are only interested
cache.h
+4 -2
@@ -301,7 +301,6 @@ extern void free_name_hash(struct index_state *istate);
301 #define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec))
302 #define is_cache_unborn() is_index_unborn(&the_index)
303 #define read_cache_unmerged() read_index_unmerged(&the_index)
304 -#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
304 #define discard_cache() discard_index(&the_index)
305 #define unmerged_cache() unmerged_index(&the_index)
306 #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
@@ -456,12 +455,15 @@ extern int daemonize(void);
455 } while (0)
456
457 /* Initialize and use the cache information */
458 +struct lock_file;
459 extern int read_index(struct index_state *);
460 extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);
461 extern int read_index_from(struct index_state *, const char *path);
462 extern int is_index_unborn(struct index_state *);
463 extern int read_index_unmerged(struct index_state *);
464 -extern int write_index(struct index_state *, int newfd);
464 +#define COMMIT_LOCK (1 << 0)
465 +#define CLOSE_LOCK (1 << 1)
466 +extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
467 extern int discard_index(struct index_state *);
468 extern int unmerged_index(const struct index_state *);
469 extern int verify_path(const char *path);
merge-recursive.c
+3 -4
@@ -1986,7 +1986,7 @@ int merge_recursive_generic(struct merge_options *o,
1986 const unsigned char **base_list,
1987 struct commit **result)
1988 {
1989 - int clean, index_fd;
1989 + int clean;
1990 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1991 struct commit *head_commit = get_ref(head, o->branch1);
1992 struct commit *next_commit = get_ref(merge, o->branch2);
@@ -2003,12 +2003,11 @@ int merge_recursive_generic(struct merge_options *o,
2003 }
2004 }
2005
2006 - index_fd = hold_locked_index(lock, 1);
2006 + hold_locked_index(lock, 1);
2007 clean = merge_recursive(o, head_commit, next_commit, ca,
2008 result);
2009 if (active_cache_changed &&
2010 - (write_cache(index_fd, active_cache, active_nr) ||
2011 - commit_locked_index(lock)))
2010 + write_locked_index(&the_index, lock, COMMIT_LOCK))
2011 return error(_("Unable to write index."));
2012
2013 return clean ? 0 : 1;
merge.c
+3 -4
@@ -66,13 +66,13 @@ int checkout_fast_forward(const unsigned char *head,
66 struct tree *trees[MAX_UNPACK_TREES];
67 struct unpack_trees_options opts;
68 struct tree_desc t[MAX_UNPACK_TREES];
69 - int i, fd, nr_trees = 0;
69 + int i, nr_trees = 0;
70 struct dir_struct dir;
71 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
72
73 refresh_cache(REFRESH_QUIET);
74
75 - fd = hold_locked_index(lock_file, 1);
75 + hold_locked_index(lock_file, 1);
76
77 memset(&trees, 0, sizeof(trees));
78 memset(&opts, 0, sizeof(opts));
@@ -105,8 +105,7 @@ int checkout_fast_forward(const unsigned char *head,
105 }
106 if (unpack_trees(nr_trees, t, &opts))
107 return -1;
108 - if (write_cache(fd, active_cache, active_nr) ||
109 - commit_locked_index(lock_file))
108 + if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
109 die(_("unable to write new index file"));
110 return 0;
111 }
read-cache.c
+24 -4
@@ -1779,13 +1779,11 @@ static int has_racy_timestamp(struct index_state *istate)
1779 void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
1780 {
1781 if ((istate->cache_changed || has_racy_timestamp(istate)) &&
1782 - !write_index(istate, lockfile->fd))
1783 - commit_locked_index(lockfile);
1784 - else
1782 + write_locked_index(istate, lockfile, COMMIT_LOCK))
1783 rollback_lock_file(lockfile);
1784 }
1785
1788 -int write_index(struct index_state *istate, int newfd)
1786 +static int do_write_index(struct index_state *istate, int newfd)
1787 {
1788 git_SHA_CTX c;
1789 struct cache_header hdr;
@@ -1877,6 +1875,28 @@ int write_index(struct index_state *istate, int newfd)
1875 return 0;
1876 }
1877
1878 +static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
1879 + unsigned flags)
1880 +{
1881 + int ret = do_write_index(istate, lock->fd);
1882 + if (ret)
1883 + return ret;
1884 + assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
1885 + (COMMIT_LOCK | CLOSE_LOCK));
1886 + if (flags & COMMIT_LOCK)
1887 + return commit_locked_index(lock);
1888 + else if (flags & CLOSE_LOCK)
1889 + return close_lock_file(lock);
1890 + else
1891 + return ret;
1892 +}
1893 +
1894 +int write_locked_index(struct index_state *istate, struct lock_file *lock,
1895 + unsigned flags)
1896 +{
1897 + return do_write_locked_index(istate, lock, flags);
1898 +}
1899 +
1900 /*
1901 * Read the index file that is potentially unmerged into given
1902 * index_state, dropping any unmerged entries. Returns true if
rerere.c
+1 -2
@@ -492,8 +492,7 @@ static int update_paths(struct string_list *update)
492 }
493
494 if (!status && active_cache_changed) {
495 - if (write_cache(fd, active_cache, active_nr) ||
496 - commit_locked_index(&index_lock))
495 + if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
496 die("Unable to write new index file");
497 } else if (fd >= 0)
498 rollback_lock_file(&index_lock);
sequencer.c
+4 -6
@@ -294,11 +294,11 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
294 {
295 struct merge_options o;
296 struct tree *result, *next_tree, *base_tree, *head_tree;
297 - int clean, index_fd;
297 + int clean;
298 const char **xopt;
299 static struct lock_file index_lock;
300
301 - index_fd = hold_locked_index(&index_lock, 1);
301 + hold_locked_index(&index_lock, 1);
302
303 read_cache();
304
@@ -319,8 +319,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
319 next_tree, base_tree, &result);
320
321 if (active_cache_changed &&
322 - (write_cache(index_fd, active_cache, active_nr) ||
323 - commit_locked_index(&index_lock)))
322 + write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
323 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
324 die(_("%s: Unable to write new index file"), action_name(opts));
325 rollback_lock_file(&index_lock);
@@ -680,8 +679,7 @@ static void read_and_refresh_cache(struct replay_opts *opts)
679 die(_("git %s: failed to read the index"), action_name(opts));
680 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
681 if (the_index.cache_changed && index_fd >= 0) {
683 - if (write_index(&the_index, index_fd) ||
684 - commit_locked_index(&index_lock))
682 + if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
683 die(_("git %s: failed to refresh the index"), action_name(opts));
684 }
685 rollback_lock_file(&index_lock);
test-scrap-cache-tree.c
+2 -3
@@ -6,12 +6,11 @@ static struct lock_file index_lock;
6
7 int main(int ac, char **av)
8 {
9 - int fd = hold_locked_index(&index_lock, 1);
9 + hold_locked_index(&index_lock, 1);
10 if (read_cache() < 0)
11 die("unable to read index file");
12 active_cache_tree = NULL;
13 - if (write_cache(fd, active_cache, active_nr)
14 - || commit_lock_file(&index_lock))
13 + if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
14 die("unable to write index file");
15 return 0;
16 }