Skip to content

Commit b968372

Browse files
chriscoolgitster
authored andcommitted
read-cache: unlink old sharedindex files
Everytime split index is turned on, it creates a "sharedindex.XXXX" file in the git directory. This change makes sure that shared index files that haven't been used for a long time are removed when a new shared index file is created. The new "splitIndex.sharedIndexExpire" config variable is created to tell the delay after which an unused shared index file can be deleted. It defaults to "2.weeks.ago". A previous commit made sure that each time a split index file is created the mtime of the shared index file it references is updated. This makes sure that recently used shared index file will not be deleted. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 77d6797 commit b968372

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

read-cache.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,65 @@ static int write_split_index(struct index_state *istate,
22072207
return ret;
22082208
}
22092209

2210+
static const char *shared_index_expire = "2.weeks.ago";
2211+
2212+
static unsigned long get_shared_index_expire_date(void)
2213+
{
2214+
static unsigned long shared_index_expire_date;
2215+
static int shared_index_expire_date_prepared;
2216+
2217+
if (!shared_index_expire_date_prepared) {
2218+
git_config_get_expiry("splitindex.sharedindexexpire",
2219+
&shared_index_expire);
2220+
shared_index_expire_date = approxidate(shared_index_expire);
2221+
shared_index_expire_date_prepared = 1;
2222+
}
2223+
2224+
return shared_index_expire_date;
2225+
}
2226+
2227+
static int should_delete_shared_index(const char *shared_index_path)
2228+
{
2229+
struct stat st;
2230+
unsigned long expiration;
2231+
2232+
/* Check timestamp */
2233+
expiration = get_shared_index_expire_date();
2234+
if (!expiration)
2235+
return 0;
2236+
if (stat(shared_index_path, &st))
2237+
return error_errno(_("could not stat '%s"), shared_index_path);
2238+
if (st.st_mtime > expiration)
2239+
return 0;
2240+
2241+
return 1;
2242+
}
2243+
2244+
static int clean_shared_index_files(const char *current_hex)
2245+
{
2246+
struct dirent *de;
2247+
DIR *dir = opendir(get_git_dir());
2248+
2249+
if (!dir)
2250+
return error_errno(_("unable to open git dir: %s"), get_git_dir());
2251+
2252+
while ((de = readdir(dir)) != NULL) {
2253+
const char *sha1_hex;
2254+
const char *shared_index_path;
2255+
if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex))
2256+
continue;
2257+
if (!strcmp(sha1_hex, current_hex))
2258+
continue;
2259+
shared_index_path = git_path("%s", de->d_name);
2260+
if (should_delete_shared_index(shared_index_path) > 0 &&
2261+
unlink(shared_index_path))
2262+
warning_errno(_("unable to unlink: %s"), shared_index_path);
2263+
}
2264+
closedir(dir);
2265+
2266+
return 0;
2267+
}
2268+
22102269
static struct tempfile temporary_sharedindex;
22112270

22122271
static int write_shared_index(struct index_state *istate,
@@ -2228,8 +2287,11 @@ static int write_shared_index(struct index_state *istate,
22282287
}
22292288
ret = rename_tempfile(&temporary_sharedindex,
22302289
git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
2231-
if (!ret)
2290+
if (!ret) {
22322291
hashcpy(si->base_sha1, si->base->sha1);
2292+
clean_shared_index_files(sha1_to_hex(si->base->sha1));
2293+
}
2294+
22332295
return ret;
22342296
}
22352297

0 commit comments

Comments
 (0)