Merge branch 'dk/blame-el' into pu
[git/spearce.git] / builtin-gc.c
blobc92511a2772a0954d081d2ad18b358c31a7cff18
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"
18 #define FAILED_RUN "failed to run %s"
20 static const char * const builtin_gc_usage[] = {
21 "git gc [options]",
22 NULL
25 static char do_rev_cache = 0;
26 static int pack_refs = 1;
27 static int aggressive_window = 250;
28 static int gc_auto_threshold = 6700;
29 static int gc_auto_pack_limit = 50;
30 static const char *prune_expire = "2.weeks.ago";
32 #define MAX_ADD 10
33 static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
34 static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
35 static const char *argv_repack[MAX_ADD] = {"repack", "-d", "-l", NULL};
36 static const char *argv_prune[] = {"prune", "--expire", NULL, NULL};
37 static const char *argv_rerere[] = {"rerere", "gc", NULL};
38 static const char *argv_rev_cache[] = {"rev-cache", "fuse", "--all", "--ignore-size", NULL};
40 static int gc_config(const char *var, const char *value, void *cb)
42 if (!strcmp(var, "gc.revcache")) {
43 do_rev_cache = 1;
44 return 0;
46 if (!strcmp(var, "gc.packrefs")) {
47 if (value && !strcmp(value, "notbare"))
48 pack_refs = -1;
49 else
50 pack_refs = git_config_bool(var, value);
51 return 0;
53 if (!strcmp(var, "gc.aggressivewindow")) {
54 aggressive_window = git_config_int(var, value);
55 return 0;
57 if (!strcmp(var, "gc.auto")) {
58 gc_auto_threshold = git_config_int(var, value);
59 return 0;
61 if (!strcmp(var, "gc.autopacklimit")) {
62 gc_auto_pack_limit = git_config_int(var, value);
63 return 0;
65 if (!strcmp(var, "gc.pruneexpire")) {
66 if (value && strcmp(value, "now")) {
67 unsigned long now = approxidate("now");
68 if (approxidate(value) >= now)
69 return error("Invalid %s: '%s'", var, value);
71 return git_config_string(&prune_expire, var, value);
73 return git_default_config(var, value, cb);
76 static void append_option(const char **cmd, const char *opt, int max_length)
78 int i;
80 for (i = 0; cmd[i]; i++)
83 if (i + 2 >= max_length)
84 die("Too many options specified");
85 cmd[i++] = opt;
86 cmd[i] = NULL;
89 static int too_many_loose_objects(void)
92 * Quickly check if a "gc" is needed, by estimating how
93 * many loose objects there are. Because SHA-1 is evenly
94 * distributed, we can check only one and get a reasonable
95 * estimate.
97 char path[PATH_MAX];
98 const char *objdir = get_object_directory();
99 DIR *dir;
100 struct dirent *ent;
101 int auto_threshold;
102 int num_loose = 0;
103 int needed = 0;
105 if (gc_auto_threshold <= 0)
106 return 0;
108 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
109 warning("insanely long object directory %.*s", 50, objdir);
110 return 0;
112 dir = opendir(path);
113 if (!dir)
114 return 0;
116 auto_threshold = (gc_auto_threshold + 255) / 256;
117 while ((ent = readdir(dir)) != NULL) {
118 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
119 ent->d_name[38] != '\0')
120 continue;
121 if (++num_loose > auto_threshold) {
122 needed = 1;
123 break;
126 closedir(dir);
127 return needed;
130 static int too_many_packs(void)
132 struct packed_git *p;
133 int cnt;
135 if (gc_auto_pack_limit <= 0)
136 return 0;
138 prepare_packed_git();
139 for (cnt = 0, p = packed_git; p; p = p->next) {
140 if (!p->pack_local)
141 continue;
142 if (p->pack_keep)
143 continue;
145 * Perhaps check the size of the pack and count only
146 * very small ones here?
148 cnt++;
150 return gc_auto_pack_limit <= cnt;
153 static int need_to_gc(void)
156 * Setting gc.auto to 0 or negative can disable the
157 * automatic gc.
159 if (gc_auto_threshold <= 0)
160 return 0;
163 * If there are too many loose objects, but not too many
164 * packs, we run "repack -d -l". If there are too many packs,
165 * we run "repack -A -d -l". Otherwise we tell the caller
166 * there is no need.
168 if (too_many_packs())
169 append_option(argv_repack,
170 prune_expire && !strcmp(prune_expire, "now") ?
171 "-a" : "-A",
172 MAX_ADD);
173 else if (!too_many_loose_objects())
174 return 0;
176 if (run_hook(NULL, "pre-auto-gc", NULL))
177 return 0;
178 return 1;
181 int cmd_gc(int argc, const char **argv, const char *prefix)
183 int aggressive = 0;
184 int auto_gc = 0;
185 int quiet = 0;
186 char buf[80];
188 struct option builtin_gc_options[] = {
189 { OPTION_STRING, 0, "prune", &prune_expire, "date",
190 "prune unreferenced objects",
191 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
192 OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"),
193 OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"),
194 OPT_BOOLEAN('q', "quiet", &quiet, "suppress progress reports"),
195 OPT_END()
198 git_config(gc_config, NULL);
200 if (pack_refs < 0)
201 pack_refs = !is_bare_repository();
203 argc = parse_options(argc, argv, prefix, builtin_gc_options,
204 builtin_gc_usage, 0);
205 if (argc > 0)
206 usage_with_options(builtin_gc_usage, builtin_gc_options);
208 if (aggressive) {
209 append_option(argv_repack, "-f", MAX_ADD);
210 append_option(argv_repack, "--depth=250", MAX_ADD);
211 if (aggressive_window > 0) {
212 sprintf(buf, "--window=%d", aggressive_window);
213 append_option(argv_repack, buf, MAX_ADD);
216 if (quiet)
217 append_option(argv_repack, "-q", MAX_ADD);
219 if (auto_gc) {
221 * Auto-gc should be least intrusive as possible.
223 if (!need_to_gc())
224 return 0;
225 fprintf(stderr, "Auto packing your repository for optimum "
226 "performance. You may also\n"
227 "run \"git gc\" manually. See "
228 "\"git help gc\" for more information.\n");
229 } else
230 append_option(argv_repack,
231 prune_expire && !strcmp(prune_expire, "now")
232 ? "-a" : "-A",
233 MAX_ADD);
235 if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
236 return error(FAILED_RUN, argv_pack_refs[0]);
238 if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
239 return error(FAILED_RUN, argv_reflog[0]);
241 if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
242 return error(FAILED_RUN, argv_repack[0]);
244 if (prune_expire) {
245 argv_prune[2] = prune_expire;
246 if (run_command_v_opt(argv_prune, RUN_GIT_CMD))
247 return error(FAILED_RUN, argv_prune[0]);
250 if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
251 return error(FAILED_RUN, argv_rerere[0]);
253 if (do_rev_cache && run_command_v_opt(argv_rev_cache, RUN_GIT_CMD))
254 return error(FAILED_RUN, argv_rev_cache[0]);
256 if (auto_gc && too_many_loose_objects())
257 warning("There are too many unreachable loose objects; "
258 "run 'git prune' to remove them.");
260 return 0;