gc.c 6.3 KB
Newer Older
J
James Bowes 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * git gc builtin command
 *
 * Cleanup unreachable files and optimize the repository.
 *
 * Copyright (c) 2007 James Bowes
 *
 * Based on git-gc.sh, which is
 *
 * Copyright (c) 2006 Shawn O. Pearce
 */

13
#include "builtin.h"
J
James Bowes 已提交
14
#include "cache.h"
J
James Bowes 已提交
15
#include "parse-options.h"
J
James Bowes 已提交
16
#include "run-command.h"
J
Jeff King 已提交
17
#include "argv-array.h"
J
James Bowes 已提交
18 19 20

#define FAILED_RUN "failed to run %s"

J
James Bowes 已提交
21
static const char * const builtin_gc_usage[] = {
22
	N_("git gc [options]"),
J
James Bowes 已提交
23 24
	NULL
};
J
James Bowes 已提交
25

26
static int pack_refs = 1;
27
static int aggressive_window = 250;
J
Junio C Hamano 已提交
28
static int gc_auto_threshold = 6700;
29
static int gc_auto_pack_limit = 50;
30
static const char *prune_expire = "2.weeks.ago";
J
James Bowes 已提交
31

J
Jeff King 已提交
32 33 34 35 36
static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
static struct argv_array reflog = ARGV_ARRAY_INIT;
static struct argv_array repack = ARGV_ARRAY_INIT;
static struct argv_array prune = ARGV_ARRAY_INIT;
static struct argv_array rerere = ARGV_ARRAY_INIT;
J
James Bowes 已提交
37

38
static int gc_config(const char *var, const char *value, void *cb)
J
James Bowes 已提交
39 40
{
	if (!strcmp(var, "gc.packrefs")) {
41
		if (value && !strcmp(value, "notbare"))
J
James Bowes 已提交
42 43 44 45 46
			pack_refs = -1;
		else
			pack_refs = git_config_bool(var, value);
		return 0;
	}
47 48 49 50
	if (!strcmp(var, "gc.aggressivewindow")) {
		aggressive_window = git_config_int(var, value);
		return 0;
	}
J
Junio C Hamano 已提交
51 52 53 54
	if (!strcmp(var, "gc.auto")) {
		gc_auto_threshold = git_config_int(var, value);
		return 0;
	}
55 56 57 58
	if (!strcmp(var, "gc.autopacklimit")) {
		gc_auto_pack_limit = git_config_int(var, value);
		return 0;
	}
59
	if (!strcmp(var, "gc.pruneexpire")) {
60
		if (value && strcmp(value, "now")) {
61 62
			unsigned long now = approxidate("now");
			if (approxidate(value) >= now)
63
				return error(_("Invalid %s: '%s'"), var, value);
64
		}
65
		return git_config_string(&prune_expire, var, value);
66
	}
67
	return git_default_config(var, value, cb);
J
James Bowes 已提交
68 69
}

70
static int too_many_loose_objects(void)
J
Junio C Hamano 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	/*
	 * Quickly check if a "gc" is needed, by estimating how
	 * many loose objects there are.  Because SHA-1 is evenly
	 * distributed, we can check only one and get a reasonable
	 * estimate.
	 */
	char path[PATH_MAX];
	const char *objdir = get_object_directory();
	DIR *dir;
	struct dirent *ent;
	int auto_threshold;
	int num_loose = 0;
	int needed = 0;

86 87 88
	if (gc_auto_threshold <= 0)
		return 0;

J
Junio C Hamano 已提交
89
	if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
90
		warning(_("insanely long object directory %.*s"), 50, objdir);
J
Junio C Hamano 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		return 0;
	}
	dir = opendir(path);
	if (!dir)
		return 0;

	auto_threshold = (gc_auto_threshold + 255) / 256;
	while ((ent = readdir(dir)) != NULL) {
		if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
		    ent->d_name[38] != '\0')
			continue;
		if (++num_loose > auto_threshold) {
			needed = 1;
			break;
		}
	}
	closedir(dir);
	return needed;
}

111 112 113 114 115 116 117 118 119 120 121 122
static int too_many_packs(void)
{
	struct packed_git *p;
	int cnt;

	if (gc_auto_pack_limit <= 0)
		return 0;

	prepare_packed_git();
	for (cnt = 0, p = packed_git; p; p = p->next) {
		if (!p->pack_local)
			continue;
123
		if (p->pack_keep)
124 125 126 127 128 129 130 131 132 133
			continue;
		/*
		 * Perhaps check the size of the pack and count only
		 * very small ones here?
		 */
		cnt++;
	}
	return gc_auto_pack_limit <= cnt;
}

134 135 136
static void add_repack_all_option(void)
{
	if (prune_expire && !strcmp(prune_expire, "now"))
J
Jeff King 已提交
137
		argv_array_push(&repack, "-a");
138
	else {
J
Jeff King 已提交
139 140 141
		argv_array_push(&repack, "-A");
		if (prune_expire)
			argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
142 143 144
	}
}

145 146 147
static int need_to_gc(void)
{
	/*
148 149
	 * Setting gc.auto to 0 or negative can disable the
	 * automatic gc.
150
	 */
151
	if (gc_auto_threshold <= 0)
152 153
		return 0;

154 155 156 157 158 159 160
	/*
	 * If there are too many loose objects, but not too many
	 * packs, we run "repack -d -l".  If there are too many packs,
	 * we run "repack -A -d -l".  Otherwise we tell the caller
	 * there is no need.
	 */
	if (too_many_packs())
161
		add_repack_all_option();
162 163
	else if (!too_many_loose_objects())
		return 0;
164

165
	if (run_hook(NULL, "pre-auto-gc", NULL))
166
		return 0;
167
	return 1;
168 169
}

J
James Bowes 已提交
170 171
int cmd_gc(int argc, const char **argv, const char *prefix)
{
J
James Bowes 已提交
172
	int aggressive = 0;
J
Junio C Hamano 已提交
173
	int auto_gc = 0;
F
Frank Lichtenheld 已提交
174
	int quiet = 0;
J
James Bowes 已提交
175

J
James Bowes 已提交
176
	struct option builtin_gc_options[] = {
177 178 179
		OPT__QUIET(&quiet, N_("suppress progress reporting")),
		{ OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
			N_("prune unreferenced objects"),
180
			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
181 182
		OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
		OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
J
James Bowes 已提交
183 184 185
		OPT_END()
	};

186 187 188
	if (argc == 2 && !strcmp(argv[1], "-h"))
		usage_with_options(builtin_gc_usage, builtin_gc_options);

J
Jeff King 已提交
189 190 191 192 193 194
	argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
	argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
	argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
	argv_array_pushl(&prune, "prune", "--expire", NULL );
	argv_array_pushl(&rerere, "rerere", "gc", NULL);

195
	git_config(gc_config, NULL);
J
James Bowes 已提交
196 197 198 199

	if (pack_refs < 0)
		pack_refs = !is_bare_repository();

200 201
	argc = parse_options(argc, argv, prefix, builtin_gc_options,
			     builtin_gc_usage, 0);
J
James Bowes 已提交
202 203 204 205
	if (argc > 0)
		usage_with_options(builtin_gc_usage, builtin_gc_options);

	if (aggressive) {
J
Jeff King 已提交
206 207 208 209
		argv_array_push(&repack, "-f");
		argv_array_push(&repack, "--depth=250");
		if (aggressive_window > 0)
			argv_array_pushf(&repack, "--window=%d", aggressive_window);
J
James Bowes 已提交
210
	}
F
Frank Lichtenheld 已提交
211
	if (quiet)
J
Jeff King 已提交
212
		argv_array_push(&repack, "-q");
J
James Bowes 已提交
213

J
Junio C Hamano 已提交
214 215 216 217 218 219
	if (auto_gc) {
		/*
		 * Auto-gc should be least intrusive as possible.
		 */
		if (!need_to_gc())
			return 0;
220
		if (!quiet)
221 222 223
			fprintf(stderr,
					_("Auto packing the repository for optimum performance. You may also\n"
					"run \"git gc\" manually. See "
224
					"\"git help gc\" for more information.\n"));
225
	} else
226
		add_repack_all_option();
J
Junio C Hamano 已提交
227

J
Jeff King 已提交
228 229
	if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
		return error(FAILED_RUN, pack_refs_cmd.argv[0]);
J
James Bowes 已提交
230

J
Jeff King 已提交
231 232
	if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
		return error(FAILED_RUN, reflog.argv[0]);
J
James Bowes 已提交
233

J
Jeff King 已提交
234 235
	if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
		return error(FAILED_RUN, repack.argv[0]);
J
James Bowes 已提交
236

237
	if (prune_expire) {
J
Jeff King 已提交
238
		argv_array_push(&prune, prune_expire);
J
Jeff King 已提交
239
		if (quiet)
J
Jeff King 已提交
240 241 242
			argv_array_push(&prune, "--no-progress");
		if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
			return error(FAILED_RUN, prune.argv[0]);
243
	}
J
James Bowes 已提交
244

J
Jeff King 已提交
245 246
	if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
		return error(FAILED_RUN, rerere.argv[0]);
J
James Bowes 已提交
247

248
	if (auto_gc && too_many_loose_objects())
249 250
		warning(_("There are too many unreachable loose objects; "
			"run 'git prune' to remove them."));
251

J
James Bowes 已提交
252 253
	return 0;
}