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