2 * git gc builtin command
4 * Cleanup unreachable files and optimize the repository.
6 * Copyright (c) 2007 James Bowes
8 * Based on git-gc.sh, which is
10 * Copyright (c) 2006 Shawn O. Pearce
15 #include "parse-options.h"
16 #include "run-command.h"
18 #include "argv-array.h"
21 #define FAILED_RUN "failed to run %s"
23 static const char * const builtin_gc_usage[] = {
24 N_("git gc [options]"),
28 static int pack_refs = 1;
29 static int aggressive_depth = 250;
30 static int aggressive_window = 250;
31 static int gc_auto_threshold = 6700;
32 static int gc_auto_pack_limit = 50;
33 static int detach_auto = 1;
34 static const char *prune_expire = "2.weeks.ago";
36 static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
37 static struct argv_array reflog = ARGV_ARRAY_INIT;
38 static struct argv_array repack = ARGV_ARRAY_INIT;
39 static struct argv_array prune = ARGV_ARRAY_INIT;
40 static struct argv_array rerere = ARGV_ARRAY_INIT;
44 static void remove_pidfile(void)
50 static void remove_pidfile_on_signal(int signo)
57 static int gc_config(const char *var, const char *value, void *cb)
59 if (!strcmp(var, "gc.packrefs")) {
60 if (value && !strcmp(value, "notbare"))
63 pack_refs = git_config_bool(var, value);
66 if (!strcmp(var, "gc.aggressivewindow")) {
67 aggressive_window = git_config_int(var, value);
70 if (!strcmp(var, "gc.aggressivedepth")) {
71 aggressive_depth = git_config_int(var, value);
74 if (!strcmp(var, "gc.auto")) {
75 gc_auto_threshold = git_config_int(var, value);
78 if (!strcmp(var, "gc.autopacklimit")) {
79 gc_auto_pack_limit = git_config_int(var, value);
82 if (!strcmp(var, "gc.autodetach")) {
83 detach_auto = git_config_bool(var, value);
86 if (!strcmp(var, "gc.pruneexpire")) {
87 if (value && strcmp(value, "now")) {
88 unsigned long now = approxidate("now");
89 if (approxidate(value) >= now)
90 return error(_("Invalid %s: '%s'"), var, value);
92 return git_config_string(&prune_expire, var, value);
94 return git_default_config(var, value, cb);
97 static int too_many_loose_objects(void)
100 * Quickly check if a "gc" is needed, by estimating how
101 * many loose objects there are. Because SHA-1 is evenly
102 * distributed, we can check only one and get a reasonable
106 const char *objdir = get_object_directory();
113 if (gc_auto_threshold <= 0)
116 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
117 warning(_("insanely long object directory %.*s"), 50, objdir);
124 auto_threshold = (gc_auto_threshold + 255) / 256;
125 while ((ent = readdir(dir)) != NULL) {
126 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
127 ent->d_name[38] != '\0')
129 if (++num_loose > auto_threshold) {
138 static int too_many_packs(void)
140 struct packed_git *p;
143 if (gc_auto_pack_limit <= 0)
146 prepare_packed_git();
147 for (cnt = 0, p = packed_git; p; p = p->next) {
153 * Perhaps check the size of the pack and count only
154 * very small ones here?
158 return gc_auto_pack_limit <= cnt;
161 static void add_repack_all_option(void)
163 if (prune_expire && !strcmp(prune_expire, "now"))
164 argv_array_push(&repack, "-a");
166 argv_array_push(&repack, "-A");
168 argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
172 static int need_to_gc(void)
175 * Setting gc.auto to 0 or negative can disable the
178 if (gc_auto_threshold <= 0)
182 * If there are too many loose objects, but not too many
183 * packs, we run "repack -d -l". If there are too many packs,
184 * we run "repack -A -d -l". Otherwise we tell the caller
187 if (too_many_packs())
188 add_repack_all_option();
189 else if (!too_many_loose_objects())
192 if (run_hook_le(NULL, "pre-auto-gc", NULL))
197 /* return NULL on success, else hostname running the gc */
198 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
200 static struct lock_file lock;
202 struct strbuf sb = STRBUF_INIT;
212 if (gethostname(my_host, sizeof(my_host)))
213 strcpy(my_host, "unknown");
215 fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
218 static char locking_host[128];
220 fp = fopen(git_path("gc.pid"), "r");
221 memset(locking_host, 0, sizeof(locking_host));
224 !fstat(fileno(fp), &st) &&
226 * 12 hour limit is very generous as gc should
227 * never take that long. On the other hand we
228 * don't really need a strict limit here,
229 * running gc --auto one day late is not a big
230 * problem. --force can be used in manual gc
231 * after the user verifies that no gc is
234 time(NULL) - st.st_mtime <= 12 * 3600 &&
235 fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
236 /* be gentle to concurrent "gc" on remote hosts */
237 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
242 rollback_lock_file(&lock);
248 strbuf_addf(&sb, "%"PRIuMAX" %s",
249 (uintmax_t) getpid(), my_host);
250 write_in_full(fd, sb.buf, sb.len);
252 commit_lock_file(&lock);
254 pidfile = git_pathdup("gc.pid");
255 sigchain_push_common(remove_pidfile_on_signal);
256 atexit(remove_pidfile);
261 int cmd_gc(int argc, const char **argv, const char *prefix)
270 struct option builtin_gc_options[] = {
271 OPT__QUIET(&quiet, N_("suppress progress reporting")),
272 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
273 N_("prune unreferenced objects"),
274 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
275 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
276 OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
277 OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
281 if (argc == 2 && !strcmp(argv[1], "-h"))
282 usage_with_options(builtin_gc_usage, builtin_gc_options);
284 argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
285 argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
286 argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
287 argv_array_pushl(&prune, "prune", "--expire", NULL );
288 argv_array_pushl(&rerere, "rerere", "gc", NULL);
290 git_config(gc_config, NULL);
293 pack_refs = !is_bare_repository();
295 argc = parse_options(argc, argv, prefix, builtin_gc_options,
296 builtin_gc_usage, 0);
298 usage_with_options(builtin_gc_usage, builtin_gc_options);
301 argv_array_push(&repack, "-f");
302 if (aggressive_depth > 0)
303 argv_array_pushf(&repack, "--depth=%d", aggressive_depth);
304 if (aggressive_window > 0)
305 argv_array_pushf(&repack, "--window=%d", aggressive_window);
308 argv_array_push(&repack, "-q");
312 * Auto-gc should be least intrusive as possible.
318 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
320 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
321 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
325 * failure to daemonize is ok, we'll continue
330 add_repack_all_option();
332 name = lock_repo_for_gc(force, &pid);
335 return 0; /* be quiet on --auto */
336 die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
337 name, (uintmax_t)pid);
340 if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
341 return error(FAILED_RUN, pack_refs_cmd.argv[0]);
343 if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
344 return error(FAILED_RUN, reflog.argv[0]);
346 if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
347 return error(FAILED_RUN, repack.argv[0]);
350 argv_array_push(&prune, prune_expire);
352 argv_array_push(&prune, "--no-progress");
353 if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
354 return error(FAILED_RUN, prune.argv[0]);
357 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
358 return error(FAILED_RUN, rerere.argv[0]);
360 if (auto_gc && too_many_loose_objects())
361 warning(_("There are too many unreachable loose objects; "
362 "run 'git prune' to remove them."));