blob: 9b4232c8f30715327342c3d86c027d04c2a44c94 [file] [log] [blame]
James Bowes6757ada2007-03-13 21:58:22 -04001/*
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 */
12
Peter Hagervallbaffc0e2007-07-15 01:14:45 +020013#include "builtin.h"
James Bowes6757ada2007-03-13 21:58:22 -040014#include "cache.h"
James Bowes44c637c2007-11-01 21:02:27 -040015#include "parse-options.h"
James Bowes6757ada2007-03-13 21:58:22 -040016#include "run-command.h"
Jeff King234587f2012-04-18 14:10:19 -070017#include "argv-array.h"
James Bowes6757ada2007-03-13 21:58:22 -040018
19#define FAILED_RUN "failed to run %s"
20
James Bowes44c637c2007-11-01 21:02:27 -040021static const char * const builtin_gc_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020022 "git gc [options]",
James Bowes44c637c2007-11-01 21:02:27 -040023 NULL
24};
James Bowes6757ada2007-03-13 21:58:22 -040025
Linus Torvalds56752392007-05-24 11:41:39 -070026static int pack_refs = 1;
Johannes Schindelin1c192f32007-12-06 12:03:38 +000027static int aggressive_window = 250;
Junio C Hamano2c3c4392007-09-05 13:01:37 -070028static int gc_auto_threshold = 6700;
Junio C Hamano97063972008-03-23 00:04:48 -070029static int gc_auto_pack_limit = 50;
David Brysond3154b42008-09-30 13:28:58 -070030static const char *prune_expire = "2.weeks.ago";
James Bowes6757ada2007-03-13 21:58:22 -040031
Jeff King234587f2012-04-18 14:10:19 -070032static 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;
James Bowes6757ada2007-03-13 21:58:22 -040037
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010038static int gc_config(const char *var, const char *value, void *cb)
James Bowes6757ada2007-03-13 21:58:22 -040039{
40 if (!strcmp(var, "gc.packrefs")) {
Miklos Vajnac5e5a2c2008-02-08 15:26:18 +010041 if (value && !strcmp(value, "notbare"))
James Bowes6757ada2007-03-13 21:58:22 -040042 pack_refs = -1;
43 else
44 pack_refs = git_config_bool(var, value);
45 return 0;
46 }
Theodore Tso0d7566a2007-05-09 15:48:39 -040047 if (!strcmp(var, "gc.aggressivewindow")) {
48 aggressive_window = git_config_int(var, value);
49 return 0;
50 }
Junio C Hamano2c3c4392007-09-05 13:01:37 -070051 if (!strcmp(var, "gc.auto")) {
52 gc_auto_threshold = git_config_int(var, value);
53 return 0;
54 }
Junio C Hamano17815502007-09-17 00:55:13 -070055 if (!strcmp(var, "gc.autopacklimit")) {
56 gc_auto_pack_limit = git_config_int(var, value);
57 return 0;
58 }
Johannes Schindelin25ee9732008-03-12 21:55:47 +010059 if (!strcmp(var, "gc.pruneexpire")) {
David Brysond3154b42008-09-30 13:28:58 -070060 if (value && strcmp(value, "now")) {
Johannes Schindelin25ee9732008-03-12 21:55:47 +010061 unsigned long now = approxidate("now");
62 if (approxidate(value) >= now)
Ævar Arnfjörð Bjarmasonfea61282011-02-22 23:42:24 +000063 return error(_("Invalid %s: '%s'"), var, value);
Johannes Schindelin25ee9732008-03-12 21:55:47 +010064 }
David Brysond3154b42008-09-30 13:28:58 -070065 return git_config_string(&prune_expire, var, value);
Johannes Schindelin25ee9732008-03-12 21:55:47 +010066 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010067 return git_default_config(var, value, cb);
James Bowes6757ada2007-03-13 21:58:22 -040068}
69
Junio C Hamanoa087cc92007-09-17 00:44:17 -070070static int too_many_loose_objects(void)
Junio C Hamano2c3c4392007-09-05 13:01:37 -070071{
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 */
78 char path[PATH_MAX];
79 const char *objdir = get_object_directory();
80 DIR *dir;
81 struct dirent *ent;
82 int auto_threshold;
83 int num_loose = 0;
84 int needed = 0;
85
Junio C Hamano17815502007-09-17 00:55:13 -070086 if (gc_auto_threshold <= 0)
87 return 0;
88
Junio C Hamano2c3c4392007-09-05 13:01:37 -070089 if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
Ævar Arnfjörð Bjarmasonfea61282011-02-22 23:42:24 +000090 warning(_("insanely long object directory %.*s"), 50, objdir);
Junio C Hamano2c3c4392007-09-05 13:01:37 -070091 return 0;
92 }
93 dir = opendir(path);
94 if (!dir)
95 return 0;
96
97 auto_threshold = (gc_auto_threshold + 255) / 256;
98 while ((ent = readdir(dir)) != NULL) {
99 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
100 ent->d_name[38] != '\0')
101 continue;
102 if (++num_loose > auto_threshold) {
103 needed = 1;
104 break;
105 }
106 }
107 closedir(dir);
108 return needed;
109}
110
Junio C Hamano17815502007-09-17 00:55:13 -0700111static int too_many_packs(void)
112{
113 struct packed_git *p;
114 int cnt;
115
116 if (gc_auto_pack_limit <= 0)
117 return 0;
118
119 prepare_packed_git();
120 for (cnt = 0, p = packed_git; p; p = p->next) {
Junio C Hamano17815502007-09-17 00:55:13 -0700121 if (!p->pack_local)
122 continue;
Brandon Casey01af2492008-11-12 11:59:07 -0600123 if (p->pack_keep)
Junio C Hamano17815502007-09-17 00:55:13 -0700124 continue;
125 /*
126 * Perhaps check the size of the pack and count only
127 * very small ones here?
128 */
129 cnt++;
130 }
131 return gc_auto_pack_limit <= cnt;
132}
133
Jeff King7e52f562012-04-07 06:30:09 -0400134static void add_repack_all_option(void)
135{
136 if (prune_expire && !strcmp(prune_expire, "now"))
Jeff King234587f2012-04-18 14:10:19 -0700137 argv_array_push(&repack, "-a");
Jeff King7e52f562012-04-07 06:30:09 -0400138 else {
Jeff King234587f2012-04-18 14:10:19 -0700139 argv_array_push(&repack, "-A");
140 if (prune_expire)
141 argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
Jeff King7e52f562012-04-07 06:30:09 -0400142 }
143}
144
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700145static int need_to_gc(void)
146{
147 /*
Brandon Caseyb14d2552008-03-19 16:53:20 -0500148 * Setting gc.auto to 0 or negative can disable the
149 * automatic gc.
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700150 */
Brandon Caseyb14d2552008-03-19 16:53:20 -0500151 if (gc_auto_threshold <= 0)
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700152 return 0;
153
Junio C Hamano17815502007-09-17 00:55:13 -0700154 /*
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 */
Junio C Hamano17815502007-09-17 00:55:13 -0700160 if (too_many_packs())
Jeff King7e52f562012-04-07 06:30:09 -0400161 add_repack_all_option();
Junio C Hamano17815502007-09-17 00:55:13 -0700162 else if (!too_many_loose_objects())
163 return 0;
Miklos Vajnabde30542008-04-02 21:34:38 +0200164
Stephan Beyerae98a002009-01-16 20:09:59 +0100165 if (run_hook(NULL, "pre-auto-gc", NULL))
Miklos Vajnabde30542008-04-02 21:34:38 +0200166 return 0;
Junio C Hamano95143f92007-09-17 00:48:39 -0700167 return 1;
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700168}
169
James Bowes6757ada2007-03-13 21:58:22 -0400170int cmd_gc(int argc, const char **argv, const char *prefix)
171{
James Bowes44c637c2007-11-01 21:02:27 -0400172 int aggressive = 0;
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700173 int auto_gc = 0;
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100174 int quiet = 0;
James Bowes6757ada2007-03-13 21:58:22 -0400175
James Bowes44c637c2007-11-01 21:02:27 -0400176 struct option builtin_gc_options[] = {
Jonathan Nieder8c839682010-11-08 13:54:48 -0600177 OPT__QUIET(&quiet, "suppress progress reporting"),
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100178 { OPTION_STRING, 0, "prune", &prune_expire, "date",
179 "prune unreferenced objects",
180 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
James Bowes44c637c2007-11-01 21:02:27 -0400181 OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"),
182 OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"),
183 OPT_END()
184 };
185
Nguyễn Thái Ngọc Duy0c8151b2010-10-22 01:47:19 -0500186 if (argc == 2 && !strcmp(argv[1], "-h"))
187 usage_with_options(builtin_gc_usage, builtin_gc_options);
188
Jeff King234587f2012-04-18 14:10:19 -0700189 argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
190 argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
191 argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
192 argv_array_pushl(&prune, "prune", "--expire", NULL );
193 argv_array_pushl(&rerere, "rerere", "gc", NULL);
194
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100195 git_config(gc_config, NULL);
James Bowes6757ada2007-03-13 21:58:22 -0400196
197 if (pack_refs < 0)
198 pack_refs = !is_bare_repository();
199
Stephen Boyd37782922009-05-23 11:53:12 -0700200 argc = parse_options(argc, argv, prefix, builtin_gc_options,
201 builtin_gc_usage, 0);
James Bowes44c637c2007-11-01 21:02:27 -0400202 if (argc > 0)
203 usage_with_options(builtin_gc_usage, builtin_gc_options);
204
205 if (aggressive) {
Jeff King234587f2012-04-18 14:10:19 -0700206 argv_array_push(&repack, "-f");
207 argv_array_push(&repack, "--depth=250");
208 if (aggressive_window > 0)
209 argv_array_pushf(&repack, "--window=%d", aggressive_window);
James Bowes6757ada2007-03-13 21:58:22 -0400210 }
Frank Lichtenhelda0c14cb2008-02-29 22:53:39 +0100211 if (quiet)
Jeff King234587f2012-04-18 14:10:19 -0700212 argv_array_push(&repack, "-q");
James Bowes6757ada2007-03-13 21:58:22 -0400213
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700214 if (auto_gc) {
215 /*
216 * Auto-gc should be least intrusive as possible.
217 */
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700218 if (!need_to_gc())
219 return 0;
Ævar Arnfjörð Bjarmasonf6908ae2011-02-22 23:42:25 +0000220 if (quiet)
221 fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
222 else
223 fprintf(stderr,
224 _("Auto packing the repository for optimum performance. You may also\n"
225 "run \"git gc\" manually. See "
Andreas Schwabdaab4ee2011-06-19 10:03:26 +0200226 "\"git help gc\" for more information.\n"));
Brandon Caseya37cce32008-05-09 23:01:56 -0500227 } else
Jeff King7e52f562012-04-07 06:30:09 -0400228 add_repack_all_option();
Junio C Hamano2c3c4392007-09-05 13:01:37 -0700229
Jeff King234587f2012-04-18 14:10:19 -0700230 if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
231 return error(FAILED_RUN, pack_refs_cmd.argv[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400232
Jeff King234587f2012-04-18 14:10:19 -0700233 if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
234 return error(FAILED_RUN, reflog.argv[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400235
Jeff King234587f2012-04-18 14:10:19 -0700236 if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
237 return error(FAILED_RUN, repack.argv[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400238
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100239 if (prune_expire) {
Jeff King234587f2012-04-18 14:10:19 -0700240 argv_array_push(&prune, prune_expire);
Jeff Kingbf0a59b2011-11-08 00:34:08 -0500241 if (quiet)
Jeff King234587f2012-04-18 14:10:19 -0700242 argv_array_push(&prune, "--no-progress");
243 if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
244 return error(FAILED_RUN, prune.argv[0]);
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100245 }
James Bowes6757ada2007-03-13 21:58:22 -0400246
Jeff King234587f2012-04-18 14:10:19 -0700247 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
248 return error(FAILED_RUN, rerere.argv[0]);
James Bowes6757ada2007-03-13 21:58:22 -0400249
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700250 if (auto_gc && too_many_loose_objects())
Ævar Arnfjörð Bjarmasonfea61282011-02-22 23:42:24 +0000251 warning(_("There are too many unreachable loose objects; "
252 "run 'git prune' to remove them."));
Junio C Hamanoa087cc92007-09-17 00:44:17 -0700253
James Bowes6757ada2007-03-13 21:58:22 -0400254 return 0;
255}