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
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
[] = {
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";
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")) {
46 if (!strcmp(var
, "gc.packrefs")) {
47 if (value
&& !strcmp(value
, "notbare"))
50 pack_refs
= git_config_bool(var
, value
);
53 if (!strcmp(var
, "gc.aggressivewindow")) {
54 aggressive_window
= git_config_int(var
, value
);
57 if (!strcmp(var
, "gc.auto")) {
58 gc_auto_threshold
= git_config_int(var
, value
);
61 if (!strcmp(var
, "gc.autopacklimit")) {
62 gc_auto_pack_limit
= git_config_int(var
, value
);
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
)
80 for (i
= 0; cmd
[i
]; i
++)
83 if (i
+ 2 >= max_length
)
84 die("Too many options specified");
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
98 const char *objdir
= get_object_directory();
105 if (gc_auto_threshold
<= 0)
108 if (sizeof(path
) <= snprintf(path
, sizeof(path
), "%s/17", objdir
)) {
109 warning("insanely long object directory %.*s", 50, objdir
);
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')
121 if (++num_loose
> auto_threshold
) {
130 static int too_many_packs(void)
132 struct packed_git
*p
;
135 if (gc_auto_pack_limit
<= 0)
138 prepare_packed_git();
139 for (cnt
= 0, p
= packed_git
; p
; p
= p
->next
) {
145 * Perhaps check the size of the pack and count only
146 * very small ones here?
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
159 if (gc_auto_threshold
<= 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
168 if (too_many_packs())
169 append_option(argv_repack
,
170 prune_expire
&& !strcmp(prune_expire
, "now") ?
173 else if (!too_many_loose_objects())
176 if (run_hook(NULL
, "pre-auto-gc", NULL
))
181 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
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"),
198 git_config(gc_config
, NULL
);
201 pack_refs
= !is_bare_repository();
203 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
204 builtin_gc_usage
, 0);
206 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
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
);
217 append_option(argv_repack
, "-q", MAX_ADD
);
221 * Auto-gc should be least intrusive as possible.
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");
230 append_option(argv_repack
,
231 prune_expire
&& !strcmp(prune_expire
, "now")
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]);
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.");