Skip to content

Commit 3e8de55

Browse files
tgummererdscho
authored andcommitted
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 <[email protected]> Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7974060 commit 3e8de55

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

builtin/am.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,19 +1071,6 @@ static const char *msgnum(const struct am_state *state)
10711071
return sb.buf;
10721072
}
10731073

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-
10871074
/**
10881075
* Dies with a user-friendly message on how to proceed after resolving the
10891076
* problem. This message can be overridden with state->resolvemsg.
@@ -1703,7 +1690,8 @@ static void am_run(struct am_state *state, int resume)
17031690

17041691
unlink(am_path(state, "dirtyindex"));
17051692

1706-
refresh_and_write_cache();
1693+
if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0)
1694+
die(_("unable to write index file"));
17071695

17081696
if (repo_index_has_changes(the_repository, NULL, &sb)) {
17091697
write_state_bool(state, "dirtyindex", 1);

cache.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ extern struct index_state the_index;
414414
#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
415415
#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
416416
#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)
417418
#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
418419
#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
419420
#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
812813
#define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
813814
#define REFRESH_PROGRESS 0x0040 /* show progress bar if stderr is tty */
814815
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+
815833
struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
816834

817835
void set_alternate_index_output(const char *);

read-cache.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,27 @@ static void show_file(const char * fmt, const char * name, int in_porcelain,
14721472
printf(fmt, name);
14731473
}
14741474

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+
14751496
int refresh_index(struct index_state *istate, unsigned int flags,
14761497
const struct pathspec *pathspec,
14771498
char *seen, const char *header_msg)

0 commit comments

Comments
 (0)