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
16 #include "parse-options.h"
17 #include "run-command.h"
19 #include "argv-array.h"
22 #define FAILED_RUN "failed to run %s"
24 static const char * const builtin_gc_usage
[] = {
25 N_("git gc [<options>]"),
29 static int pack_refs
= 1;
30 static int prune_reflogs
= 1;
31 static int aggressive_depth
= 250;
32 static int aggressive_window
= 250;
33 static int gc_auto_threshold
= 6700;
34 static int gc_auto_pack_limit
= 50;
35 static int detach_auto
= 1;
36 static const char *prune_expire
= "2.weeks.ago";
37 static const char *prune_worktrees_expire
= "3.months.ago";
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
;
43 static struct argv_array prune_worktrees
= ARGV_ARRAY_INIT
;
44 static struct argv_array rerere
= ARGV_ARRAY_INIT
;
46 static struct tempfile pidfile
;
47 static struct lock_file log_lock
;
49 static void git_config_date_string(const char *key
, const char **output
)
51 if (git_config_get_string_const(key
, output
))
53 if (strcmp(*output
, "now")) {
54 unsigned long now
= approxidate("now");
55 if (approxidate(*output
) >= now
)
56 git_die_config(key
, _("Invalid %s: '%s'"), key
, *output
);
60 static void process_log_file(void)
63 if (!fstat(get_lock_file_fd(&log_lock
), &st
) && st
.st_size
)
64 commit_lock_file(&log_lock
);
66 rollback_lock_file(&log_lock
);
69 static void process_log_file_at_exit(void)
75 static void process_log_file_on_signal(int signo
)
82 static void gc_config(void)
86 if (!git_config_get_value("gc.packrefs", &value
)) {
87 if (value
&& !strcmp(value
, "notbare"))
90 pack_refs
= git_config_bool("gc.packrefs", value
);
93 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
94 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
95 git_config_get_int("gc.auto", &gc_auto_threshold
);
96 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
97 git_config_get_bool("gc.autodetach", &detach_auto
);
98 git_config_date_string("gc.pruneexpire", &prune_expire
);
99 git_config_date_string("gc.worktreepruneexpire", &prune_worktrees_expire
);
100 git_config(git_default_config
, NULL
);
103 static int too_many_loose_objects(void)
106 * Quickly check if a "gc" is needed, by estimating how
107 * many loose objects there are. Because SHA-1 is evenly
108 * distributed, we can check only one and get a reasonable
112 const char *objdir
= get_object_directory();
119 if (gc_auto_threshold
<= 0)
122 if (sizeof(path
) <= snprintf(path
, sizeof(path
), "%s/17", objdir
)) {
123 warning(_("insanely long object directory %.*s"), 50, objdir
);
130 auto_threshold
= (gc_auto_threshold
+ 255) / 256;
131 while ((ent
= readdir(dir
)) != NULL
) {
132 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
133 ent
->d_name
[38] != '\0')
135 if (++num_loose
> auto_threshold
) {
144 static int too_many_packs(void)
146 struct packed_git
*p
;
149 if (gc_auto_pack_limit
<= 0)
152 prepare_packed_git();
153 for (cnt
= 0, p
= packed_git
; p
; p
= p
->next
) {
159 * Perhaps check the size of the pack and count only
160 * very small ones here?
164 return gc_auto_pack_limit
<= cnt
;
167 static void add_repack_all_option(void)
169 if (prune_expire
&& !strcmp(prune_expire
, "now"))
170 argv_array_push(&repack
, "-a");
172 argv_array_push(&repack
, "-A");
174 argv_array_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
178 static int need_to_gc(void)
181 * Setting gc.auto to 0 or negative can disable the
184 if (gc_auto_threshold
<= 0)
188 * If there are too many loose objects, but not too many
189 * packs, we run "repack -d -l". If there are too many packs,
190 * we run "repack -A -d -l". Otherwise we tell the caller
193 if (too_many_packs())
194 add_repack_all_option();
195 else if (!too_many_loose_objects())
198 if (run_hook_le(NULL
, "pre-auto-gc", NULL
))
203 /* return NULL on success, else hostname running the gc */
204 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
206 static struct lock_file lock
;
208 struct strbuf sb
= STRBUF_INIT
;
215 if (is_tempfile_active(&pidfile
))
219 if (gethostname(my_host
, sizeof(my_host
)))
220 strcpy(my_host
, "unknown");
222 pidfile_path
= git_pathdup("gc.pid");
223 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
226 static char locking_host
[128];
228 fp
= fopen(pidfile_path
, "r");
229 memset(locking_host
, 0, sizeof(locking_host
));
232 !fstat(fileno(fp
), &st
) &&
234 * 12 hour limit is very generous as gc should
235 * never take that long. On the other hand we
236 * don't really need a strict limit here,
237 * running gc --auto one day late is not a big
238 * problem. --force can be used in manual gc
239 * after the user verifies that no gc is
242 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
243 fscanf(fp
, "%"PRIuMAX
" %127c", &pid
, locking_host
) == 2 &&
244 /* be gentle to concurrent "gc" on remote hosts */
245 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
250 rollback_lock_file(&lock
);
257 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
258 (uintmax_t) getpid(), my_host
);
259 write_in_full(fd
, sb
.buf
, sb
.len
);
261 commit_lock_file(&lock
);
262 register_tempfile(&pidfile
, pidfile_path
);
267 static int report_last_gc_error(void)
269 struct strbuf sb
= STRBUF_INIT
;
272 ret
= strbuf_read_file(&sb
, git_path("gc.log"), 0);
274 return error(_("The last gc run reported the following. "
275 "Please correct the root cause\n"
277 "Automatic cleanup will not be performed "
278 "until the file is removed.\n\n"
280 git_path("gc.log"), sb
.buf
);
285 static int gc_before_repack(void)
287 if (pack_refs
&& run_command_v_opt(pack_refs_cmd
.argv
, RUN_GIT_CMD
))
288 return error(FAILED_RUN
, pack_refs_cmd
.argv
[0]);
290 if (prune_reflogs
&& run_command_v_opt(reflog
.argv
, RUN_GIT_CMD
))
291 return error(FAILED_RUN
, reflog
.argv
[0]);
298 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
308 struct option builtin_gc_options
[] = {
309 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
310 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
311 N_("prune unreferenced objects"),
312 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
313 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
314 OPT_BOOL(0, "auto", &auto_gc
, N_("enable auto-gc mode")),
315 OPT_BOOL(0, "force", &force
, N_("force running gc even if there may be another gc running")),
319 if (argc
== 2 && !strcmp(argv
[1], "-h"))
320 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
322 argv_array_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
323 argv_array_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
324 argv_array_pushl(&repack
, "repack", "-d", "-l", NULL
);
325 argv_array_pushl(&prune
, "prune", "--expire", NULL
);
326 argv_array_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
327 argv_array_pushl(&rerere
, "rerere", "gc", NULL
);
332 pack_refs
= !is_bare_repository();
334 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
335 builtin_gc_usage
, 0);
337 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
340 argv_array_push(&repack
, "-f");
341 if (aggressive_depth
> 0)
342 argv_array_pushf(&repack
, "--depth=%d", aggressive_depth
);
343 if (aggressive_window
> 0)
344 argv_array_pushf(&repack
, "--window=%d", aggressive_window
);
347 argv_array_push(&repack
, "-q");
351 * Auto-gc should be least intrusive as possible.
357 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
359 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
360 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
363 if (report_last_gc_error())
366 if (gc_before_repack())
369 * failure to daemonize is ok, we'll continue
372 daemonized
= !daemonize();
375 add_repack_all_option();
377 name
= lock_repo_for_gc(force
, &pid
);
380 return 0; /* be quiet on --auto */
381 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
382 name
, (uintmax_t)pid
);
386 hold_lock_file_for_update(&log_lock
,
389 dup2(get_lock_file_fd(&log_lock
), 2);
390 sigchain_push_common(process_log_file_on_signal
);
391 atexit(process_log_file_at_exit
);
394 if (gc_before_repack())
397 if (run_command_v_opt(repack
.argv
, RUN_GIT_CMD
))
398 return error(FAILED_RUN
, repack
.argv
[0]);
401 argv_array_push(&prune
, prune_expire
);
403 argv_array_push(&prune
, "--no-progress");
404 if (run_command_v_opt(prune
.argv
, RUN_GIT_CMD
))
405 return error(FAILED_RUN
, prune
.argv
[0]);
408 if (prune_worktrees_expire
) {
409 argv_array_push(&prune_worktrees
, prune_worktrees_expire
);
410 if (run_command_v_opt(prune_worktrees
.argv
, RUN_GIT_CMD
))
411 return error(FAILED_RUN
, prune_worktrees
.argv
[0]);
414 if (run_command_v_opt(rerere
.argv
, RUN_GIT_CMD
))
415 return error(FAILED_RUN
, rerere
.argv
[0]);
417 if (auto_gc
&& too_many_loose_objects())
418 warning(_("There are too many unreachable loose objects; "
419 "run 'git prune' to remove them."));