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 <gitster@pobox.com>
Junio C Hamano committed
Aug 19, 2017 at 11:16 UTC
5ea82279c066f51d446b344e82492a3554409d7d
1 file changed
+23
-13
rerere.c
+23
-13
@@ -1133,14 +1133,14 @@ int rerere_forget(struct pathspec *pathspec)
1133
* Garbage collection support
1134
*/
1135
1136
-static time_t rerere_created_at(struct rerere_id *id)
1136
+static timestamp_t rerere_created_at(struct rerere_id *id)
1137
{
1138
struct stat st;
1139
1140
return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
1141
}
1142
1143
-static time_t rerere_last_used_at(struct rerere_id *id)
1143
+static timestamp_t rerere_last_used_at(struct rerere_id *id)
1144
{
1145
struct stat st;
1146
@@ -1157,11 +1157,11 @@ static void unlink_rr_item(struct rerere_id *id)
1157
id->collection->status[id->variant] = 0;
1158
}
1159
1160
-static void prune_one(struct rerere_id *id, time_t now,
1161
- int cutoff_resolve, int cutoff_noresolve)
1160
+static void prune_one(struct rerere_id *id,
1161
+ timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
1162
{
1163
- time_t then;
1164
- int cutoff;
1163
+ timestamp_t then;
1164
+ timestamp_t cutoff;
1165
1166
then = rerere_last_used_at(id);
1167
if (then)
@@ -1172,25 +1172,35 @@ static void prune_one(struct rerere_id *id, time_t now,
1172
return;
1173
cutoff = cutoff_noresolve;
1174
}
1175
- if (then < now - cutoff * 86400)
1175
+ if (then < cutoff)
1176
unlink_rr_item(id);
1177
}
1178
1179
+static void config_get_expiry(const char *key, timestamp_t *cutoff, timestamp_t now)
1180
+{
1181
+ int days;
1182
+
1183
+ if (!git_config_get_int(key, &days)) {
1184
+ const int scale = 86400;
1185
+ *cutoff = now - days * scale;
1186
+ }
1187
+}
1188
+
1189
void rerere_gc(struct string_list *rr)
1190
{
1191
struct string_list to_remove = STRING_LIST_INIT_DUP;
1192
DIR *dir;
1193
struct dirent *e;
1194
int i;
1185
- time_t now = time(NULL);
1186
- int cutoff_noresolve = 15;
1187
- int cutoff_resolve = 60;
1195
+ timestamp_t now = time(NULL);
1196
+ timestamp_t cutoff_noresolve = now - 15 * 86400;
1197
+ timestamp_t cutoff_resolve = now - 60 * 86400;
1198
1199
if (setup_rerere(rr, 0) < 0)
1200
return;
1201
1192
- git_config_get_int("gc.rerereresolved", &cutoff_resolve);
1193
- git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
1202
+ config_get_expiry("gc.rerereresolved", &cutoff_resolve, now);
1203
+ config_get_expiry("gc.rerereunresolved", &cutoff_noresolve, now);
1204
git_config(git_default_config, NULL);
1205
dir = opendir(git_path("rr-cache"));
1206
if (!dir)
@@ -1211,7 +1221,7 @@ void rerere_gc(struct string_list *rr)
1221
for (id.variant = 0, id.collection = rr_dir;
1222
id.variant < id.collection->status_nr;
1223
id.variant++) {
1214
- prune_one(&id, now, cutoff_resolve, cutoff_noresolve);
1224
+ prune_one(&id, cutoff_resolve, cutoff_noresolve);
1225
if (id.collection->status[id.variant])
1226
now_empty = 0;
1227
}