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
14 #include "repository.h"
18 #include "parse-options.h"
19 #include "run-command.h"
21 #include "argv-array.h"
24 #include "object-store.h"
26 #define FAILED_RUN "failed to run %s"
28 static const char * const builtin_gc_usage
[] = {
29 N_("git gc [<options>]"),
33 static int pack_refs
= 1;
34 static int prune_reflogs
= 1;
35 static int aggressive_depth
= 50;
36 static int aggressive_window
= 250;
37 static int gc_auto_threshold
= 6700;
38 static int gc_auto_pack_limit
= 50;
39 static int detach_auto
= 1;
40 static timestamp_t gc_log_expire_time
;
41 static const char *gc_log_expire
= "1.day.ago";
42 static const char *prune_expire
= "2.weeks.ago";
43 static const char *prune_worktrees_expire
= "3.months.ago";
45 static struct argv_array pack_refs_cmd
= ARGV_ARRAY_INIT
;
46 static struct argv_array reflog
= ARGV_ARRAY_INIT
;
47 static struct argv_array repack
= ARGV_ARRAY_INIT
;
48 static struct argv_array prune
= ARGV_ARRAY_INIT
;
49 static struct argv_array prune_worktrees
= ARGV_ARRAY_INIT
;
50 static struct argv_array rerere
= ARGV_ARRAY_INIT
;
52 static struct tempfile
*pidfile
;
53 static struct lock_file log_lock
;
55 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
57 static void clean_pack_garbage(void)
60 for (i
= 0; i
< pack_garbage
.nr
; i
++)
61 unlink_or_warn(pack_garbage
.items
[i
].string
);
62 string_list_clear(&pack_garbage
, 0);
65 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
67 if (seen_bits
== PACKDIR_FILE_IDX
)
68 string_list_append(&pack_garbage
, path
);
71 static void process_log_file(void)
74 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
76 * Perhaps there was an i/o error or another
77 * unlikely situation. Try to make a note of
78 * this in gc.log along with any existing
81 int saved_errno
= errno
;
82 fprintf(stderr
, _("Failed to fstat %s: %s"),
83 get_tempfile_path(log_lock
.tempfile
),
84 strerror(saved_errno
));
86 commit_lock_file(&log_lock
);
88 } else if (st
.st_size
) {
89 /* There was some error recorded in the lock file */
90 commit_lock_file(&log_lock
);
92 /* No error, clean up any old gc.log */
93 unlink(git_path("gc.log"));
94 rollback_lock_file(&log_lock
);
98 static void process_log_file_at_exit(void)
104 static void process_log_file_on_signal(int signo
)
111 static void gc_config(void)
115 if (!git_config_get_value("gc.packrefs", &value
)) {
116 if (value
&& !strcmp(value
, "notbare"))
119 pack_refs
= git_config_bool("gc.packrefs", value
);
122 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
123 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
124 git_config_get_int("gc.auto", &gc_auto_threshold
);
125 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
126 git_config_get_bool("gc.autodetach", &detach_auto
);
127 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
128 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
129 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
131 git_config(git_default_config
, NULL
);
134 static int too_many_loose_objects(void)
137 * Quickly check if a "gc" is needed, by estimating how
138 * many loose objects there are. Because SHA-1 is evenly
139 * distributed, we can check only one and get a reasonable
148 if (gc_auto_threshold
<= 0)
151 dir
= opendir(git_path("objects/17"));
155 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
156 while ((ent
= readdir(dir
)) != NULL
) {
157 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
158 ent
->d_name
[38] != '\0')
160 if (++num_loose
> auto_threshold
) {
169 static int too_many_packs(void)
171 struct packed_git
*p
;
174 if (gc_auto_pack_limit
<= 0)
177 prepare_packed_git();
178 for (cnt
= 0, p
= get_packed_git(the_repository
); p
; p
= p
->next
) {
184 * Perhaps check the size of the pack and count only
185 * very small ones here?
189 return gc_auto_pack_limit
< cnt
;
192 static void add_repack_all_option(void)
194 if (prune_expire
&& !strcmp(prune_expire
, "now"))
195 argv_array_push(&repack
, "-a");
197 argv_array_push(&repack
, "-A");
199 argv_array_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
203 static void add_repack_incremental_option(void)
205 argv_array_push(&repack
, "--no-write-bitmap-index");
208 static int need_to_gc(void)
211 * Setting gc.auto to 0 or negative can disable the
214 if (gc_auto_threshold
<= 0)
218 * If there are too many loose objects, but not too many
219 * packs, we run "repack -d -l". If there are too many packs,
220 * we run "repack -A -d -l". Otherwise we tell the caller
223 if (too_many_packs())
224 add_repack_all_option();
225 else if (too_many_loose_objects())
226 add_repack_incremental_option();
230 if (run_hook_le(NULL
, "pre-auto-gc", NULL
))
235 /* return NULL on success, else hostname running the gc */
236 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
238 static struct lock_file lock
;
239 char my_host
[HOST_NAME_MAX
+ 1];
240 struct strbuf sb
= STRBUF_INIT
;
247 if (is_tempfile_active(pidfile
))
251 if (xgethostname(my_host
, sizeof(my_host
)))
252 xsnprintf(my_host
, sizeof(my_host
), "unknown");
254 pidfile_path
= git_pathdup("gc.pid");
255 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
258 static char locking_host
[HOST_NAME_MAX
+ 1];
259 static char *scan_fmt
;
263 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
264 fp
= fopen(pidfile_path
, "r");
265 memset(locking_host
, 0, sizeof(locking_host
));
268 !fstat(fileno(fp
), &st
) &&
270 * 12 hour limit is very generous as gc should
271 * never take that long. On the other hand we
272 * don't really need a strict limit here,
273 * running gc --auto one day late is not a big
274 * problem. --force can be used in manual gc
275 * after the user verifies that no gc is
278 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
279 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
280 /* be gentle to concurrent "gc" on remote hosts */
281 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
286 rollback_lock_file(&lock
);
293 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
294 (uintmax_t) getpid(), my_host
);
295 write_in_full(fd
, sb
.buf
, sb
.len
);
297 commit_lock_file(&lock
);
298 pidfile
= register_tempfile(pidfile_path
);
303 static int report_last_gc_error(void)
305 struct strbuf sb
= STRBUF_INIT
;
308 char *gc_log_path
= git_pathdup("gc.log");
310 if (stat(gc_log_path
, &st
)) {
314 ret
= error_errno(_("Can't stat %s"), gc_log_path
);
318 if (st
.st_mtime
< gc_log_expire_time
)
321 ret
= strbuf_read_file(&sb
, gc_log_path
, 0);
323 ret
= error(_("The last gc run reported the following. "
324 "Please correct the root cause\n"
326 "Automatic cleanup will not be performed "
327 "until the file is removed.\n\n"
329 gc_log_path
, sb
.buf
);
336 static int gc_before_repack(void)
338 if (pack_refs
&& run_command_v_opt(pack_refs_cmd
.argv
, RUN_GIT_CMD
))
339 return error(FAILED_RUN
, pack_refs_cmd
.argv
[0]);
341 if (prune_reflogs
&& run_command_v_opt(reflog
.argv
, RUN_GIT_CMD
))
342 return error(FAILED_RUN
, reflog
.argv
[0]);
349 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
359 struct option builtin_gc_options
[] = {
360 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
361 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
362 N_("prune unreferenced objects"),
363 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
364 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
365 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
366 PARSE_OPT_NOCOMPLETE
),
367 OPT_BOOL_F(0, "force", &force
,
368 N_("force running gc even if there may be another gc running"),
369 PARSE_OPT_NOCOMPLETE
),
373 if (argc
== 2 && !strcmp(argv
[1], "-h"))
374 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
376 argv_array_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
377 argv_array_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
378 argv_array_pushl(&repack
, "repack", "-d", "-l", NULL
);
379 argv_array_pushl(&prune
, "prune", "--expire", NULL
);
380 argv_array_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
381 argv_array_pushl(&rerere
, "rerere", "gc", NULL
);
383 /* default expiry time, overwritten in gc_config */
385 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
386 die(_("Failed to parse gc.logexpiry value %s"), gc_log_expire
);
389 pack_refs
= !is_bare_repository();
391 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
392 builtin_gc_usage
, 0);
394 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
397 argv_array_push(&repack
, "-f");
398 if (aggressive_depth
> 0)
399 argv_array_pushf(&repack
, "--depth=%d", aggressive_depth
);
400 if (aggressive_window
> 0)
401 argv_array_pushf(&repack
, "--window=%d", aggressive_window
);
404 argv_array_push(&repack
, "-q");
408 * Auto-gc should be least intrusive as possible.
414 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
416 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
417 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
420 if (report_last_gc_error())
423 if (lock_repo_for_gc(force
, &pid
))
425 if (gc_before_repack())
427 delete_tempfile(&pidfile
);
430 * failure to daemonize is ok, we'll continue
433 daemonized
= !daemonize();
436 add_repack_all_option();
438 name
= lock_repo_for_gc(force
, &pid
);
441 return 0; /* be quiet on --auto */
442 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
443 name
, (uintmax_t)pid
);
447 hold_lock_file_for_update(&log_lock
,
450 dup2(get_lock_file_fd(&log_lock
), 2);
451 sigchain_push_common(process_log_file_on_signal
);
452 atexit(process_log_file_at_exit
);
455 if (gc_before_repack())
458 if (!repository_format_precious_objects
) {
459 if (run_command_v_opt(repack
.argv
, RUN_GIT_CMD
))
460 return error(FAILED_RUN
, repack
.argv
[0]);
463 argv_array_push(&prune
, prune_expire
);
465 argv_array_push(&prune
, "--no-progress");
466 if (repository_format_partial_clone
)
467 argv_array_push(&prune
,
468 "--exclude-promisor-objects");
469 if (run_command_v_opt(prune
.argv
, RUN_GIT_CMD
))
470 return error(FAILED_RUN
, prune
.argv
[0]);
474 if (prune_worktrees_expire
) {
475 argv_array_push(&prune_worktrees
, prune_worktrees_expire
);
476 if (run_command_v_opt(prune_worktrees
.argv
, RUN_GIT_CMD
))
477 return error(FAILED_RUN
, prune_worktrees
.argv
[0]);
480 if (run_command_v_opt(rerere
.argv
, RUN_GIT_CMD
))
481 return error(FAILED_RUN
, rerere
.argv
[0]);
483 report_garbage
= report_pack_garbage
;
484 reprepare_packed_git();
485 if (pack_garbage
.nr
> 0)
486 clean_pack_garbage();
488 if (auto_gc
&& too_many_loose_objects())
489 warning(_("There are too many unreachable loose objects; "
490 "run 'git prune' to remove them."));
493 unlink(git_path("gc.log"));