Merge branch 'ep/shell-assign-and-export-vars'
[alt-git.git] / builtin / gc.c
blob85f5c2bc62ec1ead593f5aa2e3d7077e65f2f061
1 /*
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
13 #include "builtin.h"
14 #include "cache.h"
15 #include "parse-options.h"
16 #include "run-command.h"
17 #include "sigchain.h"
18 #include "argv-array.h"
19 #include "commit.h"
21 #define FAILED_RUN "failed to run %s"
23 static const char * const builtin_gc_usage[] = {
24 N_("git gc [options]"),
25 NULL
28 static int pack_refs = 1;
29 static int aggressive_depth = 250;
30 static int aggressive_window = 250;
31 static int gc_auto_threshold = 6700;
32 static int gc_auto_pack_limit = 50;
33 static int detach_auto = 1;
34 static const char *prune_expire = "2.weeks.ago";
36 static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
37 static struct argv_array reflog = ARGV_ARRAY_INIT;
38 static struct argv_array repack = ARGV_ARRAY_INIT;
39 static struct argv_array prune = ARGV_ARRAY_INIT;
40 static struct argv_array rerere = ARGV_ARRAY_INIT;
42 static char *pidfile;
44 static void remove_pidfile(void)
46 if (pidfile)
47 unlink(pidfile);
50 static void remove_pidfile_on_signal(int signo)
52 remove_pidfile();
53 sigchain_pop(signo);
54 raise(signo);
57 static int gc_config(const char *var, const char *value, void *cb)
59 if (!strcmp(var, "gc.packrefs")) {
60 if (value && !strcmp(value, "notbare"))
61 pack_refs = -1;
62 else
63 pack_refs = git_config_bool(var, value);
64 return 0;
66 if (!strcmp(var, "gc.aggressivewindow")) {
67 aggressive_window = git_config_int(var, value);
68 return 0;
70 if (!strcmp(var, "gc.aggressivedepth")) {
71 aggressive_depth = git_config_int(var, value);
72 return 0;
74 if (!strcmp(var, "gc.auto")) {
75 gc_auto_threshold = git_config_int(var, value);
76 return 0;
78 if (!strcmp(var, "gc.autopacklimit")) {
79 gc_auto_pack_limit = git_config_int(var, value);
80 return 0;
82 if (!strcmp(var, "gc.autodetach")) {
83 detach_auto = git_config_bool(var, value);
84 return 0;
86 if (!strcmp(var, "gc.pruneexpire")) {
87 if (value && strcmp(value, "now")) {
88 unsigned long now = approxidate("now");
89 if (approxidate(value) >= now)
90 return error(_("Invalid %s: '%s'"), var, value);
92 return git_config_string(&prune_expire, var, value);
94 return git_default_config(var, value, cb);
97 static int too_many_loose_objects(void)
100 * Quickly check if a "gc" is needed, by estimating how
101 * many loose objects there are. Because SHA-1 is evenly
102 * distributed, we can check only one and get a reasonable
103 * estimate.
105 char path[PATH_MAX];
106 const char *objdir = get_object_directory();
107 DIR *dir;
108 struct dirent *ent;
109 int auto_threshold;
110 int num_loose = 0;
111 int needed = 0;
113 if (gc_auto_threshold <= 0)
114 return 0;
116 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
117 warning(_("insanely long object directory %.*s"), 50, objdir);
118 return 0;
120 dir = opendir(path);
121 if (!dir)
122 return 0;
124 auto_threshold = (gc_auto_threshold + 255) / 256;
125 while ((ent = readdir(dir)) != NULL) {
126 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
127 ent->d_name[38] != '\0')
128 continue;
129 if (++num_loose > auto_threshold) {
130 needed = 1;
131 break;
134 closedir(dir);
135 return needed;
138 static int too_many_packs(void)
140 struct packed_git *p;
141 int cnt;
143 if (gc_auto_pack_limit <= 0)
144 return 0;
146 prepare_packed_git();
147 for (cnt = 0, p = packed_git; p; p = p->next) {
148 if (!p->pack_local)
149 continue;
150 if (p->pack_keep)
151 continue;
153 * Perhaps check the size of the pack and count only
154 * very small ones here?
156 cnt++;
158 return gc_auto_pack_limit <= cnt;
161 static void add_repack_all_option(void)
163 if (prune_expire && !strcmp(prune_expire, "now"))
164 argv_array_push(&repack, "-a");
165 else {
166 argv_array_push(&repack, "-A");
167 if (prune_expire)
168 argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
172 static int need_to_gc(void)
175 * Setting gc.auto to 0 or negative can disable the
176 * automatic gc.
178 if (gc_auto_threshold <= 0)
179 return 0;
182 * If there are too many loose objects, but not too many
183 * packs, we run "repack -d -l". If there are too many packs,
184 * we run "repack -A -d -l". Otherwise we tell the caller
185 * there is no need.
187 if (too_many_packs())
188 add_repack_all_option();
189 else if (!too_many_loose_objects())
190 return 0;
192 if (run_hook_le(NULL, "pre-auto-gc", NULL))
193 return 0;
194 return 1;
197 /* return NULL on success, else hostname running the gc */
198 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
200 static struct lock_file lock;
201 char my_host[128];
202 struct strbuf sb = STRBUF_INIT;
203 struct stat st;
204 uintmax_t pid;
205 FILE *fp;
206 int fd;
208 if (pidfile)
209 /* already locked */
210 return NULL;
212 if (gethostname(my_host, sizeof(my_host)))
213 strcpy(my_host, "unknown");
215 fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
216 LOCK_DIE_ON_ERROR);
217 if (!force) {
218 static char locking_host[128];
219 int should_exit;
220 fp = fopen(git_path("gc.pid"), "r");
221 memset(locking_host, 0, sizeof(locking_host));
222 should_exit =
223 fp != NULL &&
224 !fstat(fileno(fp), &st) &&
226 * 12 hour limit is very generous as gc should
227 * never take that long. On the other hand we
228 * don't really need a strict limit here,
229 * running gc --auto one day late is not a big
230 * problem. --force can be used in manual gc
231 * after the user verifies that no gc is
232 * running.
234 time(NULL) - st.st_mtime <= 12 * 3600 &&
235 fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
236 /* be gentle to concurrent "gc" on remote hosts */
237 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
238 if (fp != NULL)
239 fclose(fp);
240 if (should_exit) {
241 if (fd >= 0)
242 rollback_lock_file(&lock);
243 *ret_pid = pid;
244 return locking_host;
248 strbuf_addf(&sb, "%"PRIuMAX" %s",
249 (uintmax_t) getpid(), my_host);
250 write_in_full(fd, sb.buf, sb.len);
251 strbuf_release(&sb);
252 commit_lock_file(&lock);
254 pidfile = git_pathdup("gc.pid");
255 sigchain_push_common(remove_pidfile_on_signal);
256 atexit(remove_pidfile);
258 return NULL;
261 int cmd_gc(int argc, const char **argv, const char *prefix)
263 int aggressive = 0;
264 int auto_gc = 0;
265 int quiet = 0;
266 int force = 0;
267 const char *name;
268 pid_t pid;
270 struct option builtin_gc_options[] = {
271 OPT__QUIET(&quiet, N_("suppress progress reporting")),
272 { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
273 N_("prune unreferenced objects"),
274 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
275 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
276 OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
277 OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
278 OPT_END()
281 if (argc == 2 && !strcmp(argv[1], "-h"))
282 usage_with_options(builtin_gc_usage, builtin_gc_options);
284 argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
285 argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
286 argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
287 argv_array_pushl(&prune, "prune", "--expire", NULL );
288 argv_array_pushl(&rerere, "rerere", "gc", NULL);
290 git_config(gc_config, NULL);
292 if (pack_refs < 0)
293 pack_refs = !is_bare_repository();
295 argc = parse_options(argc, argv, prefix, builtin_gc_options,
296 builtin_gc_usage, 0);
297 if (argc > 0)
298 usage_with_options(builtin_gc_usage, builtin_gc_options);
300 if (aggressive) {
301 argv_array_push(&repack, "-f");
302 if (aggressive_depth > 0)
303 argv_array_pushf(&repack, "--depth=%d", aggressive_depth);
304 if (aggressive_window > 0)
305 argv_array_pushf(&repack, "--window=%d", aggressive_window);
307 if (quiet)
308 argv_array_push(&repack, "-q");
310 if (auto_gc) {
312 * Auto-gc should be least intrusive as possible.
314 if (!need_to_gc())
315 return 0;
316 if (!quiet) {
317 if (detach_auto)
318 fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
319 else
320 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
321 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
323 if (detach_auto)
325 * failure to daemonize is ok, we'll continue
326 * in foreground
328 daemonize();
329 } else
330 add_repack_all_option();
332 name = lock_repo_for_gc(force, &pid);
333 if (name) {
334 if (auto_gc)
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 (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
341 return error(FAILED_RUN, pack_refs_cmd.argv[0]);
343 if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
344 return error(FAILED_RUN, reflog.argv[0]);
346 if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
347 return error(FAILED_RUN, repack.argv[0]);
349 if (prune_expire) {
350 argv_array_push(&prune, prune_expire);
351 if (quiet)
352 argv_array_push(&prune, "--no-progress");
353 if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
354 return error(FAILED_RUN, prune.argv[0]);
357 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
358 return error(FAILED_RUN, rerere.argv[0]);
360 if (auto_gc && too_many_loose_objects())
361 warning(_("There are too many unreachable loose objects; "
362 "run 'git prune' to remove them."));
364 return 0;