summaryrefslogtreecommitdiff
path: root/read-cache.c
diff options
context:
space:
mode:
authorThomas Gummerer <t.gummerer@gmail.com>2019-09-11 18:20:25 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-09-20 16:58:21 (GMT)
commit22184497a36a90e541617a51de6671ca5948612c (patch)
treea70eaa44081f73758d7665191e42e3877fe884e2 /read-cache.c
parent745f6812895b31c02b29bdfe4ae8e5498f776c26 (diff)
downloadgit-22184497a36a90e541617a51de6671ca5948612c.zip
git-22184497a36a90e541617a51de6671ca5948612c.tar.gz
git-22184497a36a90e541617a51de6671ca5948612c.tar.bz2
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>
Diffstat (limited to 'read-cache.c')
-rw-r--r--read-cache.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/read-cache.c b/read-cache.c
index 52ffa8a..7e646e0 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1472,6 +1472,27 @@ static void show_file(const char * fmt, const char * name, int in_porcelain,
printf(fmt, name);
}
+int repo_refresh_and_write_index(struct repository *repo,
+ unsigned int refresh_flags,
+ unsigned int write_flags,
+ int gentle,
+ const struct pathspec *pathspec,
+ char *seen, const char *header_msg)
+{
+ struct lock_file lock_file = LOCK_INIT;
+ int fd, ret = 0;
+
+ fd = repo_hold_locked_index(repo, &lock_file, 0);
+ if (!gentle && fd < 0)
+ return -1;
+ if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
+ ret = 1;
+ if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags))
+ ret = -1;
+ return ret;
+}
+
+
int refresh_index(struct index_state *istate, unsigned int flags,
const struct pathspec *pathspec,
char *seen, const char *header_msg)