1/*
2* git gc builtin command
3*
4* Cleanup unreachable files and optimize the repository.
5*
6* Copyright (c) 2007 James Bowes
7*
8* Based on git-gc.sh, which is
9*
10* Copyright (c) 2006 Shawn O. Pearce
11*/
1213
#include "builtin.h"
14#include "cache.h"
15#include "parse-options.h"
16#include "run-command.h"
17#include "argv-array.h"
1819
#define FAILED_RUN "failed to run %s"
2021
static const char * const builtin_gc_usage[] = {
22N_("git gc [options]"),
23NULL
24};
2526
static int pack_refs = 1;
27static int aggressive_window = 250;
28static int gc_auto_threshold = 6700;
29static int gc_auto_pack_limit = 50;
30static const char *prune_expire = "2.weeks.ago";
3132
static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
33static struct argv_array reflog = ARGV_ARRAY_INIT;
34static struct argv_array repack = ARGV_ARRAY_INIT;
35static struct argv_array prune = ARGV_ARRAY_INIT;
36static struct argv_array rerere = ARGV_ARRAY_INIT;
3738
static int gc_config(const char *var, const char *value, void *cb)
39{
40if (!strcmp(var, "gc.packrefs")) {
41if (value && !strcmp(value, "notbare"))
42pack_refs = -1;
43else
44pack_refs = git_config_bool(var, value);
45return 0;
46}
47if (!strcmp(var, "gc.aggressivewindow")) {
48aggressive_window = git_config_int(var, value);
49return 0;
50}
51if (!strcmp(var, "gc.auto")) {
52gc_auto_threshold = git_config_int(var, value);
53return 0;
54}
55if (!strcmp(var, "gc.autopacklimit")) {
56gc_auto_pack_limit = git_config_int(var, value);
57return 0;
58}
59if (!strcmp(var, "gc.pruneexpire")) {
60if (value && strcmp(value, "now")) {
61unsigned long now = approxidate("now");
62if (approxidate(value) >= now)
63return error(_("Invalid %s: '%s'"), var, value);
64}
65return git_config_string(&prune_expire, var, value);
66}
67return git_default_config(var, value, cb);
68}
6970
static int too_many_loose_objects(void)
71{
72/*
73* Quickly check if a "gc" is needed, by estimating how
74* many loose objects there are. Because SHA-1 is evenly
75* distributed, we can check only one and get a reasonable
76* estimate.
77*/
78char path[PATH_MAX];
79const char *objdir = get_object_directory();
80DIR *dir;
81struct dirent *ent;
82int auto_threshold;
83int num_loose = 0;
84int needed = 0;
8586
if (gc_auto_threshold <= 0)
87return 0;
8889
if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
90warning(_("insanely long object directory %.*s"), 50, objdir);
91return 0;
92}
93dir = opendir(path);
94if (!dir)
95return 0;
9697
auto_threshold = (gc_auto_threshold + 255) / 256;
98while ((ent = readdir(dir)) != NULL) {
99if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
100ent->d_name[38] != '\0')
101continue;
102if (++num_loose > auto_threshold) {
103needed = 1;
104break;
105}
106}
107closedir(dir);
108return needed;
109}
110111
static int too_many_packs(void)
112{
113struct packed_git *p;
114int cnt;
115116
if (gc_auto_pack_limit <= 0)
117return 0;
118119
prepare_packed_git();
120for (cnt = 0, p = packed_git; p; p = p->next) {
121if (!p->pack_local)
122continue;
123if (p->pack_keep)
124continue;
125/*
126* Perhaps check the size of the pack and count only
127* very small ones here?
128*/
129cnt++;
130}
131return gc_auto_pack_limit <= cnt;
132}
133134
static void add_repack_all_option(void)
135{
136if (prune_expire && !strcmp(prune_expire, "now"))
137argv_array_push(&repack, "-a");
138else {
139argv_array_push(&repack, "-A");
140if (prune_expire)
141argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
142}
143}
144145
static int need_to_gc(void)
146{
147/*
148* Setting gc.auto to 0 or negative can disable the
149* automatic gc.
150*/
151if (gc_auto_threshold <= 0)
152return 0;
153154
/*
155* If there are too many loose objects, but not too many
156* packs, we run "repack -d -l". If there are too many packs,
157* we run "repack -A -d -l". Otherwise we tell the caller
158* there is no need.
159*/
160if (too_many_packs())
161add_repack_all_option();
162else if (!too_many_loose_objects())
163return 0;
164165
if (run_hook(NULL, "pre-auto-gc", NULL))
166return 0;
167return 1;
168}
169170
/* return NULL on success, else hostname running the gc */
171static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
172{
173static struct lock_file lock;
174static char locking_host[128];
175char my_host[128];
176struct strbuf sb = STRBUF_INIT;
177struct stat st;
178uintmax_t pid;
179FILE *fp;
180int fd, should_exit;
181182
if (gethostname(my_host, sizeof(my_host)))
183strcpy(my_host, "unknown");
184185
fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
186LOCK_DIE_ON_ERROR);
187if (!force) {
188fp = fopen(git_path("gc.pid"), "r");
189memset(locking_host, 0, sizeof(locking_host));
190should_exit =
191fp != NULL &&
192!fstat(fileno(fp), &st) &&
193/*
194* 12 hour limit is very generous as gc should
195* never take that long. On the other hand we
196* don't really need a strict limit here,
197* running gc --auto one day late is not a big
198* problem. --force can be used in manual gc
199* after the user verifies that no gc is
200* running.
201*/
202time(NULL) - st.st_mtime <= 12 * 3600 &&
203fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
204/* be gentle to concurrent "gc" on remote hosts */
205(strcmp(locking_host, my_host) || !kill(pid, 0));
206if (fp != NULL)
207fclose(fp);
208if (should_exit) {
209if (fd >= 0)
210rollback_lock_file(&lock);
211*ret_pid = pid;
212return locking_host;
213}
214}
215216
strbuf_addf(&sb, "%"PRIuMAX" %s",
217(uintmax_t) getpid(), my_host);
218write_in_full(fd, sb.buf, sb.len);
219strbuf_release(&sb);
220commit_lock_file(&lock);
221222
return NULL;
223}
224225
int cmd_gc(int argc, const char **argv, const char *prefix)
226{
227int aggressive = 0;
228int auto_gc = 0;
229int quiet = 0;
230int force = 0;
231const char *name;
232pid_t pid;
233234
struct option builtin_gc_options[] = {
235OPT__QUIET(&quiet, N_("suppress progress reporting")),
236{ OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
237N_("prune unreferenced objects"),
238PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
239OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
240OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")),
241OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
242OPT_END()
243};
244245
if (argc == 2 && !strcmp(argv[1], "-h"))
246usage_with_options(builtin_gc_usage, builtin_gc_options);
247248
argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
249argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
250argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
251argv_array_pushl(&prune, "prune", "--expire", NULL );
252argv_array_pushl(&rerere, "rerere", "gc", NULL);
253254
git_config(gc_config, NULL);
255256
if (pack_refs < 0)
257pack_refs = !is_bare_repository();
258259
argc = parse_options(argc, argv, prefix, builtin_gc_options,
260builtin_gc_usage, 0);
261if (argc > 0)
262usage_with_options(builtin_gc_usage, builtin_gc_options);
263264
if (aggressive) {
265argv_array_push(&repack, "-f");
266argv_array_push(&repack, "--depth=250");
267if (aggressive_window > 0)
268argv_array_pushf(&repack, "--window=%d", aggressive_window);
269}
270if (quiet)
271argv_array_push(&repack, "-q");
272273
if (auto_gc) {
274/*
275* Auto-gc should be least intrusive as possible.
276*/
277if (!need_to_gc())
278return 0;
279if (!quiet)
280fprintf(stderr,
281_("Auto packing the repository for optimum performance. You may also\n"
282"run \"git gc\" manually. See "
283"\"git help gc\" for more information.\n"));
284} else
285add_repack_all_option();
286287
name = lock_repo_for_gc(force, &pid);
288if (name) {
289if (auto_gc)
290return 0; /* be quiet on --auto */
291die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
292name, (uintmax_t)pid);
293}
294295
if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
296return error(FAILED_RUN, pack_refs_cmd.argv[0]);
297298
if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
299return error(FAILED_RUN, reflog.argv[0]);
300301
if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
302return error(FAILED_RUN, repack.argv[0]);
303304
if (prune_expire) {
305argv_array_push(&prune, prune_expire);
306if (quiet)
307argv_array_push(&prune, "--no-progress");
308if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
309return error(FAILED_RUN, prune.argv[0]);
310}
311312
if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
313return error(FAILED_RUN, rerere.argv[0]);
314315
if (auto_gc && too_many_loose_objects())
316warning(_("There are too many unreachable loose objects; "
317"run 'git prune' to remove them."));
318319
return 0;
320}