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