From 5ea82279c066f51d446b344e82492a3554409d7d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 19 Aug 2017 11:16:01 -0700 Subject: rerere: represent time duration in timestamp_t internally The two configuration variables, gc.rerereResolved and gc.rerereUnresolved, are measured in days and are passed as such into the prune_one() helper function, which worked in time_t to see if an entry in the rerere database is past its expiry. Instead, have the caller turn the number of days into the expiry timestamp. Further, use timestamp_t instead of time_t. This will make it possible to extend the way the configuration variable is spelled by using date.c::parse_expiry_date() that gives the expiry timestamp in timestamp_t. Signed-off-by: Junio C Hamano diff --git a/rerere.c b/rerere.c index 70634d4..f0b4bce 100644 --- a/rerere.c +++ b/rerere.c @@ -1133,14 +1133,14 @@ int rerere_forget(struct pathspec *pathspec) * Garbage collection support */ -static time_t rerere_created_at(struct rerere_id *id) +static timestamp_t rerere_created_at(struct rerere_id *id) { struct stat st; return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime; } -static time_t rerere_last_used_at(struct rerere_id *id) +static timestamp_t rerere_last_used_at(struct rerere_id *id) { struct stat st; @@ -1157,11 +1157,11 @@ static void unlink_rr_item(struct rerere_id *id) id->collection->status[id->variant] = 0; } -static void prune_one(struct rerere_id *id, time_t now, - int cutoff_resolve, int cutoff_noresolve) +static void prune_one(struct rerere_id *id, + timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve) { - time_t then; - int cutoff; + timestamp_t then; + timestamp_t cutoff; then = rerere_last_used_at(id); if (then) @@ -1172,25 +1172,35 @@ static void prune_one(struct rerere_id *id, time_t now, return; cutoff = cutoff_noresolve; } - if (then < now - cutoff * 86400) + if (then < cutoff) unlink_rr_item(id); } +static void config_get_expiry(const char *key, timestamp_t *cutoff, timestamp_t now) +{ + int days; + + if (!git_config_get_int(key, &days)) { + const int scale = 86400; + *cutoff = now - days * scale; + } +} + void rerere_gc(struct string_list *rr) { struct string_list to_remove = STRING_LIST_INIT_DUP; DIR *dir; struct dirent *e; int i; - time_t now = time(NULL); - int cutoff_noresolve = 15; - int cutoff_resolve = 60; + timestamp_t now = time(NULL); + timestamp_t cutoff_noresolve = now - 15 * 86400; + timestamp_t cutoff_resolve = now - 60 * 86400; if (setup_rerere(rr, 0) < 0) return; - git_config_get_int("gc.rerereresolved", &cutoff_resolve); - git_config_get_int("gc.rerereunresolved", &cutoff_noresolve); + config_get_expiry("gc.rerereresolved", &cutoff_resolve, now); + config_get_expiry("gc.rerereunresolved", &cutoff_noresolve, now); git_config(git_default_config, NULL); dir = opendir(git_path("rr-cache")); if (!dir) @@ -1211,7 +1221,7 @@ void rerere_gc(struct string_list *rr) for (id.variant = 0, id.collection = rr_dir; id.variant < id.collection->status_nr; id.variant++) { - prune_one(&id, now, cutoff_resolve, cutoff_noresolve); + prune_one(&id, cutoff_resolve, cutoff_noresolve); if (id.collection->status[id.variant]) now_empty = 0; } -- cgit v0.10.2-6-g49f6