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
;
48 static void git_config_date_string(const char *key
, const char **output
)
50 if (git_config_get_string_const(key
, output
))
52 if (strcmp(*output
, "now")) {
53 unsigned long now
= approxidate("now");
54 if (approxidate(*output
) >= now
)
55 git_die_config(key
, _("Invalid %s: '%s'"), key
, *output
);
59 static void gc_config(void)
63 if (!git_config_get_value("gc.packrefs", &value
)) {
64 if (value
&& !strcmp(value
, "notbare"))
67 pack_refs
= git_config_bool("gc.packrefs", value
);
70 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
71 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
72 git_config_get_int("gc.auto", &gc_auto_threshold
);
73 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
74 git_config_get_bool("gc.autodetach", &detach_auto
);
75 git_config_date_string("gc.pruneexpire", &prune_expire
);
76 git_config_date_string("gc.pruneworktreesexpire", &prune_worktrees_expire
);
77 git_config(git_default_config
, NULL
);
80 static int too_many_loose_objects(void)
83 * Quickly check if a "gc" is needed, by estimating how
84 * many loose objects there are. Because SHA-1 is evenly
85 * distributed, we can check only one and get a reasonable
89 const char *objdir
= get_object_directory();
96 if (gc_auto_threshold
<= 0)
99 if (sizeof(path
) <= snprintf(path
, sizeof(path
), "%s/17", objdir
)) {
100 warning(_("insanely long object directory %.*s"), 50, objdir
);
107 auto_threshold
= (gc_auto_threshold
+ 255) / 256;
108 while ((ent
= readdir(dir
)) != NULL
) {
109 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
110 ent
->d_name
[38] != '\0')
112 if (++num_loose
> auto_threshold
) {
121 static int too_many_packs(void)
123 struct packed_git
*p
;
126 if (gc_auto_pack_limit
<= 0)
129 prepare_packed_git();
130 for (cnt
= 0, p
= packed_git
; p
; p
= p
->next
) {
136 * Perhaps check the size of the pack and count only
137 * very small ones here?
141 return gc_auto_pack_limit
<= cnt
;
144 static void add_repack_all_option(void)
146 if (prune_expire
&& !strcmp(prune_expire
, "now"))
147 argv_array_push(&repack
, "-a");
149 argv_array_push(&repack
, "-A");
151 argv_array_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
155 static int need_to_gc(void)
158 * Setting gc.auto to 0 or negative can disable the
161 if (gc_auto_threshold
<= 0)
165 * If there are too many loose objects, but not too many
166 * packs, we run "repack -d -l". If there are too many packs,
167 * we run "repack -A -d -l". Otherwise we tell the caller
170 if (too_many_packs())
171 add_repack_all_option();
172 else if (!too_many_loose_objects())
175 if (run_hook_le(NULL
, "pre-auto-gc", NULL
))
180 /* return NULL on success, else hostname running the gc */
181 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
183 static struct lock_file lock
;
185 struct strbuf sb
= STRBUF_INIT
;
192 if (is_tempfile_active(&pidfile
))
196 if (gethostname(my_host
, sizeof(my_host
)))
197 strcpy(my_host
, "unknown");
199 pidfile_path
= git_pathdup("gc.pid");
200 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
203 static char locking_host
[128];
205 fp
= fopen(pidfile_path
, "r");
206 memset(locking_host
, 0, sizeof(locking_host
));
209 !fstat(fileno(fp
), &st
) &&
211 * 12 hour limit is very generous as gc should
212 * never take that long. On the other hand we
213 * don't really need a strict limit here,
214 * running gc --auto one day late is not a big
215 * problem. --force can be used in manual gc
216 * after the user verifies that no gc is
219 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
220 fscanf(fp
, "%"PRIuMAX
" %127c", &pid
, locking_host
) == 2 &&
221 /* be gentle to concurrent "gc" on remote hosts */
222 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
227 rollback_lock_file(&lock
);
234 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
235 (uintmax_t) getpid(), my_host
);
236 write_in_full(fd
, sb
.buf
, sb
.len
);
238 commit_lock_file(&lock
);
239 register_tempfile(&pidfile
, pidfile_path
);
244 static int gc_before_repack(void)
246 if (pack_refs
&& run_command_v_opt(pack_refs_cmd
.argv
, RUN_GIT_CMD
))
247 return error(FAILED_RUN
, pack_refs_cmd
.argv
[0]);
249 if (prune_reflogs
&& run_command_v_opt(reflog
.argv
, RUN_GIT_CMD
))
250 return error(FAILED_RUN
, reflog
.argv
[0]);
257 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
266 struct option builtin_gc_options
[] = {
267 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
268 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
269 N_("prune unreferenced objects"),
270 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
271 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
272 OPT_BOOL(0, "auto", &auto_gc
, N_("enable auto-gc mode")),
273 OPT_BOOL(0, "force", &force
, N_("force running gc even if there may be another gc running")),
277 if (argc
== 2 && !strcmp(argv
[1], "-h"))
278 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
280 argv_array_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
281 argv_array_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
282 argv_array_pushl(&repack
, "repack", "-d", "-l", NULL
);
283 argv_array_pushl(&prune
, "prune", "--expire", NULL
);
284 argv_array_pushl(&prune_worktrees
, "prune", "--worktrees", "--expire", NULL
);
285 argv_array_pushl(&rerere
, "rerere", "gc", NULL
);
290 pack_refs
= !is_bare_repository();
292 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
293 builtin_gc_usage
, 0);
295 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
298 argv_array_push(&repack
, "-f");
299 if (aggressive_depth
> 0)
300 argv_array_pushf(&repack
, "--depth=%d", aggressive_depth
);
301 if (aggressive_window
> 0)
302 argv_array_pushf(&repack
, "--window=%d", aggressive_window
);
305 argv_array_push(&repack
, "-q");
309 * Auto-gc should be least intrusive as possible.
315 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
317 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
318 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
321 if (gc_before_repack())
324 * 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 (gc_before_repack())
343 if (run_command_v_opt(repack
.argv
, RUN_GIT_CMD
))
344 return error(FAILED_RUN
, repack
.argv
[0]);
347 argv_array_push(&prune
, prune_expire
);
349 argv_array_push(&prune
, "--no-progress");
350 if (run_command_v_opt(prune
.argv
, RUN_GIT_CMD
))
351 return error(FAILED_RUN
, prune
.argv
[0]);
354 if (prune_worktrees_expire
) {
355 argv_array_push(&prune_worktrees
, prune_worktrees_expire
);
356 if (run_command_v_opt(prune_worktrees
.argv
, RUN_GIT_CMD
))
357 return error(FAILED_RUN
, prune_worktrees
.argv
[0]);
360 if (run_command_v_opt(rerere
.argv
, RUN_GIT_CMD
))
361 return error(FAILED_RUN
, rerere
.argv
[0]);
363 if (auto_gc
&& too_many_loose_objects())
364 warning(_("There are too many unreachable loose objects; "
365 "run 'git prune' to remove them."));