factor out refresh_and_write_cache function

Getting the lock for the index, refreshing it and then writing it is a pattern that happens more than once throughout the codebase, and isn't trivial to get right. Factor out the refresh_and_write_cache function from builtin/am.c to read-cache.c, so it can be re-used in other places in a subsequent commit. Note that we return different error codes for failing to refresh the cache, and failing to write the index. The current caller only cares about failing to write the index. However for other callers we're going to convert in subsequent patches we will need this distinction. Helped-by: Martin Ågren <martin.agren@gmail.com> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Sep 11, 2019 at 19:20 UTC 22184497a36a90e541617a51de6671ca5948612c
3 files changed +41 -14
builtin/am.c
+2 -14
@@ -1071,19 +1071,6 @@ static const char *msgnum(const struct am_state *state)
1071 return sb.buf;
1072 }
1073
1074 -/**
1075 - * Refresh and write index.
1076 - */
1077 -static void refresh_and_write_cache(void)
1078 -{
1079 - struct lock_file lock_file = LOCK_INIT;
1080 -
1081 - hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
1082 - refresh_cache(REFRESH_QUIET);
1083 - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1084 - die(_("unable to write index file"));
1085 -}
1086 -
1074 /**
1075 * Dies with a user-friendly message on how to proceed after resolving the
1076 * problem. This message can be overridden with state->resolvemsg.
@@ -1703,7 +1690,8 @@ static void am_run(struct am_state *state, int resume)
1690
1691 unlink(am_path(state, "dirtyindex"));
1692
1706 - refresh_and_write_cache();
1693 + if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0)
1694 + die(_("unable to write index file"));
1695
1696 if (repo_index_has_changes(the_repository, NULL, &sb)) {
1697 write_state_bool(state, "dirtyindex", 1);
cache.h
+18
@@ -414,6 +414,7 @@ extern struct index_state the_index;
414 #define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
415 #define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
416 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
417 +#define refresh_and_write_cache(refresh_flags, write_flags, gentle) repo_refresh_and_write_index(the_repository, (refresh_flags), (write_flags), (gentle), NULL, NULL, NULL)
418 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
419 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
420 #define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
@@ -812,6 +813,23 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
813 #define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
814 #define REFRESH_PROGRESS 0x0040 /* show progress bar if stderr is tty */
815 int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
816 +/*
817 + * Refresh the index and write it to disk.
818 + *
819 + * 'refresh_flags' is passed directly to 'refresh_index()', while
820 + * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
821 + * the lockfile is always either committed or rolled back.
822 + *
823 + * If 'gentle' is passed, errors locking the index are ignored.
824 + *
825 + * Return 1 if refreshing the index returns an error, -1 if writing
826 + * the index to disk fails, 0 on success.
827 + *
828 + * Note that if refreshing the index returns an error, we still write
829 + * out the index (unless locking fails).
830 + */
831 +int repo_refresh_and_write_index(struct repository*, unsigned int refresh_flags, unsigned int write_flags, int gentle, const struct pathspec *, char *seen, const char *header_msg);
832 +
833 struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
834
835 void set_alternate_index_output(const char *);
read-cache.c
+21
@@ -1472,6 +1472,27 @@ static void show_file(const char * fmt, const char * name, int in_porcelain,
1472 printf(fmt, name);
1473 }
1474
1475 +int repo_refresh_and_write_index(struct repository *repo,
1476 + unsigned int refresh_flags,
1477 + unsigned int write_flags,
1478 + int gentle,
1479 + const struct pathspec *pathspec,
1480 + char *seen, const char *header_msg)
1481 +{
1482 + struct lock_file lock_file = LOCK_INIT;
1483 + int fd, ret = 0;
1484 +
1485 + fd = repo_hold_locked_index(repo, &lock_file, 0);
1486 + if (!gentle && fd < 0)
1487 + return -1;
1488 + if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
1489 + ret = 1;
1490 + if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags))
1491 + ret = -1;
1492 + return ret;
1493 +}
1494 +
1495 +
1496 int refresh_index(struct index_state *istate, unsigned int flags,
1497 const struct pathspec *pathspec,
1498 char *seen, const char *header_msg)