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
17 #include "parse-options.h"
18 #include "run-command.h"
20 #include "argv-array.h"
23 #define FAILED_RUN "failed to run %s"
25 static const char * const builtin_gc_usage
[] = {
26 N_("git gc [<options>]"),
30 static int pack_refs
= 1;
31 static int prune_reflogs
= 1;
32 static int aggressive_depth
= 50;
33 static int aggressive_window
= 250;
34 static int gc_auto_threshold
= 6700;
35 static int gc_auto_pack_limit
= 50;
36 static int detach_auto
= 1;
37 static timestamp_t gc_log_expire_time
;
38 static const char *gc_log_expire
= "1.day.ago";
39 static const char *prune_expire
= "2.weeks.ago";
40 static const char *prune_worktrees_expire
= "3.months.ago";
42 static struct argv_array pack_refs_cmd
= ARGV_ARRAY_INIT
;
43 static struct argv_array reflog
= ARGV_ARRAY_INIT
;
44 static struct argv_array repack
= ARGV_ARRAY_INIT
;
45 static struct argv_array prune
= ARGV_ARRAY_INIT
;
46 static struct argv_array prune_worktrees
= ARGV_ARRAY_INIT
;
47 static struct argv_array rerere
= ARGV_ARRAY_INIT
;
49 static struct tempfile pidfile
;
50 static struct lock_file log_lock
;
52 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
54 static void clean_pack_garbage(void)
57 for (i
= 0; i
< pack_garbage
.nr
; i
++)
58 unlink_or_warn(pack_garbage
.items
[i
].string
);
59 string_list_clear(&pack_garbage
, 0);
62 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
64 if (seen_bits
== PACKDIR_FILE_IDX
)
65 string_list_append(&pack_garbage
, path
);
68 static void process_log_file(void)
71 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
73 * Perhaps there was an i/o error or another
74 * unlikely situation. Try to make a note of
75 * this in gc.log along with any existing
78 int saved_errno
= errno
;
79 fprintf(stderr
, _("Failed to fstat %s: %s"),
80 get_tempfile_path(&log_lock
.tempfile
),
81 strerror(saved_errno
));
83 commit_lock_file(&log_lock
);
85 } else if (st
.st_size
) {
86 /* There was some error recorded in the lock file */
87 commit_lock_file(&log_lock
);
89 /* No error, clean up any old gc.log */
90 unlink(git_path("gc.log"));
91 rollback_lock_file(&log_lock
);
95 static void process_log_file_at_exit(void)
101 static void process_log_file_on_signal(int signo
)
108 static void gc_config(void)
112 if (!git_config_get_value("gc.packrefs", &value
)) {
113 if (value
&& !strcmp(value
, "notbare"))
116 pack_refs
= git_config_bool("gc.packrefs", value
);
119 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
120 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
121 git_config_get_int("gc.auto", &gc_auto_threshold
);
122 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
123 git_config_get_bool("gc.autodetach", &detach_auto
);
124 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
125 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
126 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
128 git_config(git_default_config
, NULL
);
131 static int too_many_loose_objects(void)
134 * Quickly check if a "gc" is needed, by estimating how
135 * many loose objects there are. Because SHA-1 is evenly
136 * distributed, we can check only one and get a reasonable
145 if (gc_auto_threshold
<= 0)
148 dir
= opendir(git_path("objects/17"));
152 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
153 while ((ent
= readdir(dir
)) != NULL
) {
154 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
155 ent
->d_name
[38] != '\0')
157 if (++num_loose
> auto_threshold
) {
166 static int too_many_packs(void)
168 struct packed_git
*p
;
171 if (gc_auto_pack_limit
<= 0)
174 prepare_packed_git();
175 for (cnt
= 0, p
= packed_git
; p
; p
= p
->next
) {
181 * Perhaps check the size of the pack and count only
182 * very small ones here?
186 return gc_auto_pack_limit
< cnt
;
189 static void add_repack_all_option(void)
191 if (prune_expire
&& !strcmp(prune_expire
, "now"))
192 argv_array_push(&repack
, "-a");
194 argv_array_push(&repack
, "-A");
196 argv_array_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
200 static void add_repack_incremental_option(void)
202 argv_array_push(&repack
, "--no-write-bitmap-index");
205 static int need_to_gc(void)
208 * Setting gc.auto to 0 or negative can disable the
211 if (gc_auto_threshold
<= 0)
215 * If there are too many loose objects, but not too many
216 * packs, we run "repack -d -l". If there are too many packs,
217 * we run "repack -A -d -l". Otherwise we tell the caller
220 if (too_many_packs())
221 add_repack_all_option();
222 else if (too_many_loose_objects())
223 add_repack_incremental_option();
227 if (run_hook_le(NULL
, "pre-auto-gc", NULL
))
232 /* return NULL on success, else hostname running the gc */
233 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
235 static struct lock_file lock
;
236 char my_host
[HOST_NAME_MAX
+ 1];
237 struct strbuf sb
= STRBUF_INIT
;
244 if (is_tempfile_active(&pidfile
))
248 if (xgethostname(my_host
, sizeof(my_host
)))
249 xsnprintf(my_host
, sizeof(my_host
), "unknown");
251 pidfile_path
= git_pathdup("gc.pid");
252 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
255 static char locking_host
[HOST_NAME_MAX
+ 1];
256 static char *scan_fmt
;
260 scan_fmt
= xstrfmt("%s %%%dc", "%"SCNuMAX
, HOST_NAME_MAX
);
261 fp
= fopen(pidfile_path
, "r");
262 memset(locking_host
, 0, sizeof(locking_host
));
265 !fstat(fileno(fp
), &st
) &&
267 * 12 hour limit is very generous as gc should
268 * never take that long. On the other hand we
269 * don't really need a strict limit here,
270 * running gc --auto one day late is not a big
271 * problem. --force can be used in manual gc
272 * after the user verifies that no gc is
275 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
276 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
277 /* be gentle to concurrent "gc" on remote hosts */
278 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
283 rollback_lock_file(&lock
);
290 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
291 (uintmax_t) getpid(), my_host
);
292 write_in_full(fd
, sb
.buf
, sb
.len
);
294 commit_lock_file(&lock
);
295 register_tempfile(&pidfile
, pidfile_path
);
300 static int report_last_gc_error(void)
302 struct strbuf sb
= STRBUF_INIT
;
305 char *gc_log_path
= git_pathdup("gc.log");
307 if (stat(gc_log_path
, &st
)) {
311 ret
= error_errno(_("Can't stat %s"), gc_log_path
);
315 if (st
.st_mtime
< gc_log_expire_time
)
318 ret
= strbuf_read_file(&sb
, gc_log_path
, 0);
320 ret
= error(_("The last gc run reported the following. "
321 "Please correct the root cause\n"
323 "Automatic cleanup will not be performed "
324 "until the file is removed.\n\n"
326 gc_log_path
, sb
.buf
);
333 static int gc_before_repack(void)
335 if (pack_refs
&& run_command_v_opt(pack_refs_cmd
.argv
, RUN_GIT_CMD
))
336 return error(FAILED_RUN
, pack_refs_cmd
.argv
[0]);
338 if (prune_reflogs
&& run_command_v_opt(reflog
.argv
, RUN_GIT_CMD
))
339 return error(FAILED_RUN
, reflog
.argv
[0]);
346 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
356 struct option builtin_gc_options
[] = {
357 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
358 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
359 N_("prune unreferenced objects"),
360 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
361 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
362 OPT_BOOL(0, "auto", &auto_gc
, N_("enable auto-gc mode")),
363 OPT_BOOL(0, "force", &force
, N_("force running gc even if there may be another gc running")),
367 if (argc
== 2 && !strcmp(argv
[1], "-h"))
368 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
370 argv_array_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
371 argv_array_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
372 argv_array_pushl(&repack
, "repack", "-d", "-l", NULL
);
373 argv_array_pushl(&prune
, "prune", "--expire", NULL
);
374 argv_array_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
375 argv_array_pushl(&rerere
, "rerere", "gc", NULL
);
377 /* default expiry time, overwritten in gc_config */
379 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
380 die(_("Failed to parse gc.logexpiry value %s"), gc_log_expire
);
383 pack_refs
= !is_bare_repository();
385 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
386 builtin_gc_usage
, 0);
388 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
391 argv_array_push(&repack
, "-f");
392 if (aggressive_depth
> 0)
393 argv_array_pushf(&repack
, "--depth=%d", aggressive_depth
);
394 if (aggressive_window
> 0)
395 argv_array_pushf(&repack
, "--window=%d", aggressive_window
);
398 argv_array_push(&repack
, "-q");
402 * Auto-gc should be least intrusive as possible.
408 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
410 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
411 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
414 if (report_last_gc_error())
417 if (lock_repo_for_gc(force
, &pid
))
419 if (gc_before_repack())
421 delete_tempfile(&pidfile
);
424 * failure to daemonize is ok, we'll continue
427 daemonized
= !daemonize();
430 add_repack_all_option();
432 name
= lock_repo_for_gc(force
, &pid
);
435 return 0; /* be quiet on --auto */
436 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
437 name
, (uintmax_t)pid
);
441 hold_lock_file_for_update(&log_lock
,
444 dup2(get_lock_file_fd(&log_lock
), 2);
445 sigchain_push_common(process_log_file_on_signal
);
446 atexit(process_log_file_at_exit
);
449 if (gc_before_repack())
452 if (!repository_format_precious_objects
) {
453 if (run_command_v_opt(repack
.argv
, RUN_GIT_CMD
))
454 return error(FAILED_RUN
, repack
.argv
[0]);
457 argv_array_push(&prune
, prune_expire
);
459 argv_array_push(&prune
, "--no-progress");
460 if (run_command_v_opt(prune
.argv
, RUN_GIT_CMD
))
461 return error(FAILED_RUN
, prune
.argv
[0]);
465 if (prune_worktrees_expire
) {
466 argv_array_push(&prune_worktrees
, prune_worktrees_expire
);
467 if (run_command_v_opt(prune_worktrees
.argv
, RUN_GIT_CMD
))
468 return error(FAILED_RUN
, prune_worktrees
.argv
[0]);
471 if (run_command_v_opt(rerere
.argv
, RUN_GIT_CMD
))
472 return error(FAILED_RUN
, rerere
.argv
[0]);
474 report_garbage
= report_pack_garbage
;
475 reprepare_packed_git();
476 if (pack_garbage
.nr
> 0)
477 clean_pack_garbage();
479 if (auto_gc
&& too_many_loose_objects())
480 warning(_("There are too many unreachable loose objects; "
481 "run 'git prune' to remove them."));
484 unlink(git_path("gc.log"));