]>
Commit | Line | Data |
---|---|---|
6757ada4 JB |
1 | /* |
2 | * git gc builtin command | |
3 | * | |
4 | * Cleanup unreachable files and optimize the repository. | |
5 | * | |
6 | * Copyright (c) 2007 James Bowes | |
7 | * | |
8 | * Based on git-gc.sh, which is | |
9 | * | |
10 | * Copyright (c) 2006 Shawn O. Pearce | |
11 | */ | |
12 | ||
baffc0e7 | 13 | #include "builtin.h" |
ebebeaea | 14 | #include "tempfile.h" |
697cc8ef | 15 | #include "lockfile.h" |
44c637c8 | 16 | #include "parse-options.h" |
6757ada4 | 17 | #include "run-command.h" |
4c5baf02 | 18 | #include "sigchain.h" |
234587fc | 19 | #include "argv-array.h" |
eab3296c | 20 | #include "commit.h" |
6757ada4 JB |
21 | |
22 | #define FAILED_RUN "failed to run %s" | |
23 | ||
44c637c8 | 24 | static const char * const builtin_gc_usage[] = { |
9c9b4f2f | 25 | N_("git gc [<options>]"), |
44c637c8 JB |
26 | NULL |
27 | }; | |
6757ada4 | 28 | |
56752391 | 29 | static int pack_refs = 1; |
62aad184 | 30 | static int prune_reflogs = 1; |
07e7dbf0 | 31 | static int aggressive_depth = 50; |
1c192f34 | 32 | static int aggressive_window = 250; |
2c3c4399 | 33 | static int gc_auto_threshold = 6700; |
97063974 | 34 | static int gc_auto_pack_limit = 50; |
9f673f94 | 35 | static int detach_auto = 1; |
d3154b44 | 36 | static const char *prune_expire = "2.weeks.ago"; |
e3df33bb | 37 | static const char *prune_worktrees_expire = "3.months.ago"; |
6757ada4 | 38 | |
234587fc JK |
39 | static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT; |
40 | static struct argv_array reflog = ARGV_ARRAY_INIT; | |
41 | static struct argv_array repack = ARGV_ARRAY_INIT; | |
42 | static struct argv_array prune = ARGV_ARRAY_INIT; | |
e3df33bb | 43 | static struct argv_array prune_worktrees = ARGV_ARRAY_INIT; |
234587fc | 44 | static struct argv_array rerere = ARGV_ARRAY_INIT; |
6757ada4 | 45 | |
ebebeaea | 46 | static struct tempfile pidfile; |
329e6e87 | 47 | static struct lock_file log_lock; |
4c5baf02 | 48 | |
478f34d2 DK |
49 | static struct string_list pack_garbage = STRING_LIST_INIT_DUP; |
50 | ||
51 | static void clean_pack_garbage(void) | |
52 | { | |
53 | int i; | |
54 | for (i = 0; i < pack_garbage.nr; i++) | |
55 | unlink_or_warn(pack_garbage.items[i].string); | |
56 | string_list_clear(&pack_garbage, 0); | |
57 | } | |
58 | ||
59 | static void report_pack_garbage(unsigned seen_bits, const char *path) | |
60 | { | |
61 | if (seen_bits == PACKDIR_FILE_IDX) | |
62 | string_list_append(&pack_garbage, path); | |
63 | } | |
64 | ||
09dbb90b NTND |
65 | static void git_config_date_string(const char *key, const char **output) |
66 | { | |
67 | if (git_config_get_string_const(key, output)) | |
68 | return; | |
69 | if (strcmp(*output, "now")) { | |
70 | unsigned long now = approxidate("now"); | |
71 | if (approxidate(*output) >= now) | |
72 | git_die_config(key, _("Invalid %s: '%s'"), key, *output); | |
73 | } | |
74 | } | |
75 | ||
329e6e87 NTND |
76 | static void process_log_file(void) |
77 | { | |
78 | struct stat st; | |
076c8278 | 79 | if (!fstat(get_lock_file_fd(&log_lock), &st) && st.st_size) |
329e6e87 NTND |
80 | commit_lock_file(&log_lock); |
81 | else | |
82 | rollback_lock_file(&log_lock); | |
83 | } | |
84 | ||
85 | static void process_log_file_at_exit(void) | |
86 | { | |
87 | fflush(stderr); | |
88 | process_log_file(); | |
89 | } | |
90 | ||
91 | static void process_log_file_on_signal(int signo) | |
92 | { | |
93 | process_log_file(); | |
94 | sigchain_pop(signo); | |
95 | raise(signo); | |
96 | } | |
97 | ||
5801d3b4 | 98 | static void gc_config(void) |
6757ada4 | 99 | { |
5801d3b4 TA |
100 | const char *value; |
101 | ||
102 | if (!git_config_get_value("gc.packrefs", &value)) { | |
c5e5a2c0 | 103 | if (value && !strcmp(value, "notbare")) |
6757ada4 JB |
104 | pack_refs = -1; |
105 | else | |
5801d3b4 | 106 | pack_refs = git_config_bool("gc.packrefs", value); |
17815501 | 107 | } |
5801d3b4 TA |
108 | |
109 | git_config_get_int("gc.aggressivewindow", &aggressive_window); | |
110 | git_config_get_int("gc.aggressivedepth", &aggressive_depth); | |
111 | git_config_get_int("gc.auto", &gc_auto_threshold); | |
112 | git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit); | |
113 | git_config_get_bool("gc.autodetach", &detach_auto); | |
09dbb90b | 114 | git_config_date_string("gc.pruneexpire", &prune_expire); |
114ff888 | 115 | git_config_date_string("gc.worktreepruneexpire", &prune_worktrees_expire); |
5801d3b4 | 116 | git_config(git_default_config, NULL); |
6757ada4 JB |
117 | } |
118 | ||
a087cc98 | 119 | static int too_many_loose_objects(void) |
2c3c4399 JH |
120 | { |
121 | /* | |
122 | * Quickly check if a "gc" is needed, by estimating how | |
123 | * many loose objects there are. Because SHA-1 is evenly | |
124 | * distributed, we can check only one and get a reasonable | |
125 | * estimate. | |
126 | */ | |
127 | char path[PATH_MAX]; | |
128 | const char *objdir = get_object_directory(); | |
129 | DIR *dir; | |
130 | struct dirent *ent; | |
131 | int auto_threshold; | |
132 | int num_loose = 0; | |
133 | int needed = 0; | |
134 | ||
17815501 JH |
135 | if (gc_auto_threshold <= 0) |
136 | return 0; | |
137 | ||
2c3c4399 | 138 | if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) { |
fea6128b | 139 | warning(_("insanely long object directory %.*s"), 50, objdir); |
2c3c4399 JH |
140 | return 0; |
141 | } | |
142 | dir = opendir(path); | |
143 | if (!dir) | |
144 | return 0; | |
145 | ||
146 | auto_threshold = (gc_auto_threshold + 255) / 256; | |
147 | while ((ent = readdir(dir)) != NULL) { | |
148 | if (strspn(ent->d_name, "0123456789abcdef") != 38 || | |
149 | ent->d_name[38] != '\0') | |
150 | continue; | |
151 | if (++num_loose > auto_threshold) { | |
152 | needed = 1; | |
153 | break; | |
154 | } | |
155 | } | |
156 | closedir(dir); | |
157 | return needed; | |
158 | } | |
159 | ||
17815501 JH |
160 | static int too_many_packs(void) |
161 | { | |
162 | struct packed_git *p; | |
163 | int cnt; | |
164 | ||
165 | if (gc_auto_pack_limit <= 0) | |
166 | return 0; | |
167 | ||
168 | prepare_packed_git(); | |
169 | for (cnt = 0, p = packed_git; p; p = p->next) { | |
17815501 JH |
170 | if (!p->pack_local) |
171 | continue; | |
01af249f | 172 | if (p->pack_keep) |
17815501 JH |
173 | continue; |
174 | /* | |
175 | * Perhaps check the size of the pack and count only | |
176 | * very small ones here? | |
177 | */ | |
178 | cnt++; | |
179 | } | |
5f4e3bf5 | 180 | return gc_auto_pack_limit < cnt; |
17815501 JH |
181 | } |
182 | ||
7e52f566 JK |
183 | static void add_repack_all_option(void) |
184 | { | |
185 | if (prune_expire && !strcmp(prune_expire, "now")) | |
234587fc | 186 | argv_array_push(&repack, "-a"); |
7e52f566 | 187 | else { |
234587fc JK |
188 | argv_array_push(&repack, "-A"); |
189 | if (prune_expire) | |
190 | argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire); | |
7e52f566 JK |
191 | } |
192 | } | |
193 | ||
a087cc98 JH |
194 | static int need_to_gc(void) |
195 | { | |
196 | /* | |
b14d255b BC |
197 | * Setting gc.auto to 0 or negative can disable the |
198 | * automatic gc. | |
a087cc98 | 199 | */ |
b14d255b | 200 | if (gc_auto_threshold <= 0) |
95143f9e JH |
201 | return 0; |
202 | ||
17815501 JH |
203 | /* |
204 | * If there are too many loose objects, but not too many | |
205 | * packs, we run "repack -d -l". If there are too many packs, | |
206 | * we run "repack -A -d -l". Otherwise we tell the caller | |
207 | * there is no need. | |
208 | */ | |
17815501 | 209 | if (too_many_packs()) |
7e52f566 | 210 | add_repack_all_option(); |
17815501 JH |
211 | else if (!too_many_loose_objects()) |
212 | return 0; | |
bde30540 | 213 | |
15048f8a | 214 | if (run_hook_le(NULL, "pre-auto-gc", NULL)) |
bde30540 | 215 | return 0; |
95143f9e | 216 | return 1; |
a087cc98 JH |
217 | } |
218 | ||
64a99eb4 NTND |
219 | /* return NULL on success, else hostname running the gc */ |
220 | static const char *lock_repo_for_gc(int force, pid_t* ret_pid) | |
221 | { | |
222 | static struct lock_file lock; | |
da25bdb7 | 223 | char my_host[HOST_NAME_MAX + 1]; |
64a99eb4 NTND |
224 | struct strbuf sb = STRBUF_INIT; |
225 | struct stat st; | |
226 | uintmax_t pid; | |
227 | FILE *fp; | |
4f1c0b21 | 228 | int fd; |
00539cef | 229 | char *pidfile_path; |
64a99eb4 | 230 | |
ebebeaea | 231 | if (is_tempfile_active(&pidfile)) |
4c5baf02 JN |
232 | /* already locked */ |
233 | return NULL; | |
234 | ||
64a99eb4 | 235 | if (gethostname(my_host, sizeof(my_host))) |
5096d490 | 236 | xsnprintf(my_host, sizeof(my_host), "unknown"); |
64a99eb4 | 237 | |
00539cef MH |
238 | pidfile_path = git_pathdup("gc.pid"); |
239 | fd = hold_lock_file_for_update(&lock, pidfile_path, | |
64a99eb4 NTND |
240 | LOCK_DIE_ON_ERROR); |
241 | if (!force) { | |
da25bdb7 RS |
242 | static char locking_host[HOST_NAME_MAX + 1]; |
243 | static char *scan_fmt; | |
4f1c0b21 | 244 | int should_exit; |
da25bdb7 RS |
245 | |
246 | if (!scan_fmt) | |
247 | scan_fmt = xstrfmt("%s %%%dc", "%"SCNuMAX, HOST_NAME_MAX); | |
00539cef | 248 | fp = fopen(pidfile_path, "r"); |
64a99eb4 NTND |
249 | memset(locking_host, 0, sizeof(locking_host)); |
250 | should_exit = | |
251 | fp != NULL && | |
252 | !fstat(fileno(fp), &st) && | |
253 | /* | |
254 | * 12 hour limit is very generous as gc should | |
255 | * never take that long. On the other hand we | |
256 | * don't really need a strict limit here, | |
257 | * running gc --auto one day late is not a big | |
258 | * problem. --force can be used in manual gc | |
259 | * after the user verifies that no gc is | |
260 | * running. | |
261 | */ | |
262 | time(NULL) - st.st_mtime <= 12 * 3600 && | |
da25bdb7 | 263 | fscanf(fp, scan_fmt, &pid, locking_host) == 2 && |
64a99eb4 | 264 | /* be gentle to concurrent "gc" on remote hosts */ |
ed7eda8b | 265 | (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM); |
64a99eb4 NTND |
266 | if (fp != NULL) |
267 | fclose(fp); | |
268 | if (should_exit) { | |
269 | if (fd >= 0) | |
270 | rollback_lock_file(&lock); | |
271 | *ret_pid = pid; | |
00539cef | 272 | free(pidfile_path); |
64a99eb4 NTND |
273 | return locking_host; |
274 | } | |
275 | } | |
276 | ||
277 | strbuf_addf(&sb, "%"PRIuMAX" %s", | |
278 | (uintmax_t) getpid(), my_host); | |
279 | write_in_full(fd, sb.buf, sb.len); | |
280 | strbuf_release(&sb); | |
281 | commit_lock_file(&lock); | |
ebebeaea MH |
282 | register_tempfile(&pidfile, pidfile_path); |
283 | free(pidfile_path); | |
64a99eb4 NTND |
284 | return NULL; |
285 | } | |
286 | ||
329e6e87 NTND |
287 | static int report_last_gc_error(void) |
288 | { | |
289 | struct strbuf sb = STRBUF_INIT; | |
290 | int ret; | |
291 | ||
292 | ret = strbuf_read_file(&sb, git_path("gc.log"), 0); | |
293 | if (ret > 0) | |
294 | return error(_("The last gc run reported the following. " | |
295 | "Please correct the root cause\n" | |
296 | "and remove %s.\n" | |
297 | "Automatic cleanup will not be performed " | |
298 | "until the file is removed.\n\n" | |
299 | "%s"), | |
300 | git_path("gc.log"), sb.buf); | |
301 | strbuf_release(&sb); | |
302 | return 0; | |
303 | } | |
304 | ||
62aad184 NTND |
305 | static int gc_before_repack(void) |
306 | { | |
307 | if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD)) | |
308 | return error(FAILED_RUN, pack_refs_cmd.argv[0]); | |
309 | ||
310 | if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD)) | |
311 | return error(FAILED_RUN, reflog.argv[0]); | |
312 | ||
313 | pack_refs = 0; | |
314 | prune_reflogs = 0; | |
315 | return 0; | |
316 | } | |
317 | ||
6757ada4 JB |
318 | int cmd_gc(int argc, const char **argv, const char *prefix) |
319 | { | |
44c637c8 | 320 | int aggressive = 0; |
2c3c4399 | 321 | int auto_gc = 0; |
a0c14cbb | 322 | int quiet = 0; |
64a99eb4 NTND |
323 | int force = 0; |
324 | const char *name; | |
325 | pid_t pid; | |
329e6e87 | 326 | int daemonized = 0; |
6757ada4 | 327 | |
44c637c8 | 328 | struct option builtin_gc_options[] = { |
6705c162 NTND |
329 | OPT__QUIET(&quiet, N_("suppress progress reporting")), |
330 | { OPTION_STRING, 0, "prune", &prune_expire, N_("date"), | |
331 | N_("prune unreferenced objects"), | |
58e9d9d4 | 332 | PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire }, |
d5d09d47 SB |
333 | OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")), |
334 | OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")), | |
64a99eb4 | 335 | OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")), |
44c637c8 JB |
336 | OPT_END() |
337 | }; | |
338 | ||
0c8151b6 NTND |
339 | if (argc == 2 && !strcmp(argv[1], "-h")) |
340 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
341 | ||
234587fc JK |
342 | argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL); |
343 | argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL); | |
344 | argv_array_pushl(&repack, "repack", "-d", "-l", NULL); | |
2cfe2a78 | 345 | argv_array_pushl(&prune, "prune", "--expire", NULL); |
df0b6cfb | 346 | argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL); |
234587fc JK |
347 | argv_array_pushl(&rerere, "rerere", "gc", NULL); |
348 | ||
5801d3b4 | 349 | gc_config(); |
6757ada4 JB |
350 | |
351 | if (pack_refs < 0) | |
352 | pack_refs = !is_bare_repository(); | |
353 | ||
37782920 SB |
354 | argc = parse_options(argc, argv, prefix, builtin_gc_options, |
355 | builtin_gc_usage, 0); | |
44c637c8 JB |
356 | if (argc > 0) |
357 | usage_with_options(builtin_gc_usage, builtin_gc_options); | |
358 | ||
359 | if (aggressive) { | |
234587fc | 360 | argv_array_push(&repack, "-f"); |
125f8146 NTND |
361 | if (aggressive_depth > 0) |
362 | argv_array_pushf(&repack, "--depth=%d", aggressive_depth); | |
234587fc JK |
363 | if (aggressive_window > 0) |
364 | argv_array_pushf(&repack, "--window=%d", aggressive_window); | |
6757ada4 | 365 | } |
a0c14cbb | 366 | if (quiet) |
234587fc | 367 | argv_array_push(&repack, "-q"); |
6757ada4 | 368 | |
2c3c4399 JH |
369 | if (auto_gc) { |
370 | /* | |
371 | * Auto-gc should be least intrusive as possible. | |
372 | */ | |
2c3c4399 JH |
373 | if (!need_to_gc()) |
374 | return 0; | |
9f673f94 NTND |
375 | if (!quiet) { |
376 | if (detach_auto) | |
377 | fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n")); | |
378 | else | |
379 | fprintf(stderr, _("Auto packing the repository for optimum performance.\n")); | |
380 | fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n")); | |
381 | } | |
62aad184 | 382 | if (detach_auto) { |
329e6e87 NTND |
383 | if (report_last_gc_error()) |
384 | return -1; | |
385 | ||
62aad184 NTND |
386 | if (gc_before_repack()) |
387 | return -1; | |
9f673f94 NTND |
388 | /* |
389 | * failure to daemonize is ok, we'll continue | |
390 | * in foreground | |
391 | */ | |
329e6e87 | 392 | daemonized = !daemonize(); |
62aad184 | 393 | } |
a37cce3b | 394 | } else |
7e52f566 | 395 | add_repack_all_option(); |
2c3c4399 | 396 | |
64a99eb4 NTND |
397 | name = lock_repo_for_gc(force, &pid); |
398 | if (name) { | |
399 | if (auto_gc) | |
400 | return 0; /* be quiet on --auto */ | |
401 | die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"), | |
402 | name, (uintmax_t)pid); | |
403 | } | |
404 | ||
329e6e87 NTND |
405 | if (daemonized) { |
406 | hold_lock_file_for_update(&log_lock, | |
407 | git_path("gc.log"), | |
408 | LOCK_DIE_ON_ERROR); | |
076c8278 | 409 | dup2(get_lock_file_fd(&log_lock), 2); |
329e6e87 NTND |
410 | sigchain_push_common(process_log_file_on_signal); |
411 | atexit(process_log_file_at_exit); | |
412 | } | |
413 | ||
62aad184 NTND |
414 | if (gc_before_repack()) |
415 | return -1; | |
6757ada4 | 416 | |
067fbd41 JK |
417 | if (!repository_format_precious_objects) { |
418 | if (run_command_v_opt(repack.argv, RUN_GIT_CMD)) | |
419 | return error(FAILED_RUN, repack.argv[0]); | |
420 | ||
421 | if (prune_expire) { | |
422 | argv_array_push(&prune, prune_expire); | |
423 | if (quiet) | |
424 | argv_array_push(&prune, "--no-progress"); | |
425 | if (run_command_v_opt(prune.argv, RUN_GIT_CMD)) | |
426 | return error(FAILED_RUN, prune.argv[0]); | |
427 | } | |
58e9d9d4 | 428 | } |
6757ada4 | 429 | |
e3df33bb NTND |
430 | if (prune_worktrees_expire) { |
431 | argv_array_push(&prune_worktrees, prune_worktrees_expire); | |
432 | if (run_command_v_opt(prune_worktrees.argv, RUN_GIT_CMD)) | |
433 | return error(FAILED_RUN, prune_worktrees.argv[0]); | |
434 | } | |
435 | ||
234587fc JK |
436 | if (run_command_v_opt(rerere.argv, RUN_GIT_CMD)) |
437 | return error(FAILED_RUN, rerere.argv[0]); | |
6757ada4 | 438 | |
478f34d2 DK |
439 | report_garbage = report_pack_garbage; |
440 | reprepare_packed_git(); | |
441 | if (pack_garbage.nr > 0) | |
442 | clean_pack_garbage(); | |
443 | ||
a087cc98 | 444 | if (auto_gc && too_many_loose_objects()) |
fea6128b ÆAB |
445 | warning(_("There are too many unreachable loose objects; " |
446 | "run 'git prune' to remove them.")); | |
a087cc98 | 447 | |
6757ada4 JB |
448 | return 0; |
449 | } |