git-gc --auto: run "repack -A -d -l" as necessary.
This teaches "git-gc --auto" to consolidate many packs into one without losing unreachable objects in them by using "repack -A" when there are too many packfiles that are not marked with *.keep in the repository. gc.autopacklimit configuration can be used to set the maximum number of packs a repository is allowed to have before this mechanism kicks in. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Sep 17, 2007 at 00:55 UTC
17815501a8f95c080891acd9537514adfe17c80e
3 files changed
+66
-7
Documentation/config.txt
+6
@@ -446,6 +446,12 @@ gc.auto::
446
light-weight garbage collection from time to time. Setting
447
this to 0 disables it.
448
449
+gc.autopacklimit::
450
+ When there are more than this many packs that are not
451
+ marked with `*.keep` file in the repository, `git gc
452
+ --auto` consolidates them into one larger pack. Setting
453
+ this to 0 disables this.
454
+
455
gc.packrefs::
456
`git gc` does not run `git pack-refs` in a bare repository by
457
default so that older dumb-transport clients can still fetch
Documentation/git-gc.txt
+6
-1
@@ -47,10 +47,15 @@ OPTIONS
47
With this option, `git gc` checks if there are too many
48
loose objects in the repository and runs
49
gitlink:git-repack[1] with `-d -l` option to pack them.
50
- The threshold is set with `gc.auto` configuration
50
+ The threshold for loose objects is set with `gc.auto` configuration
51
variable, and can be disabled by setting it to 0. Some
52
Porcelain commands use this after they perform operation
53
that could create many loose objects automatically.
54
+ Additionally, when there are too many packs are present,
55
+ they are consolidated into one larger pack by running
56
+ the `git-repack` command with `-A` option. The
57
+ threshold for number of packs is set with
58
+ `gc.autopacklimit` configuration variable.
59
60
Configuration
61
-------------
builtin-gc.c
+54
-6
@@ -21,6 +21,7 @@ static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
21
static int pack_refs = 1;
22
static int aggressive_window = -1;
23
static int gc_auto_threshold = 6700;
24
+static int gc_auto_pack_limit = 20;
25
26
#define MAX_ADD 10
27
static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
@@ -46,6 +47,10 @@ static int gc_config(const char *var, const char *value)
47
gc_auto_threshold = git_config_int(var, value);
48
return 0;
49
}
50
+ if (!strcmp(var, "gc.autopacklimit")) {
51
+ gc_auto_pack_limit = git_config_int(var, value);
52
+ return 0;
53
+ }
54
return git_default_config(var, value);
55
}
56
@@ -78,6 +83,9 @@ static int too_many_loose_objects(void)
83
int num_loose = 0;
84
int needed = 0;
85
86
+ if (gc_auto_threshold <= 0)
87
+ return 0;
88
+
89
if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
90
warning("insanely long object directory %.*s", 50, objdir);
91
return 0;
@@ -100,21 +108,61 @@ static int too_many_loose_objects(void)
108
return needed;
109
}
110
111
+static 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) {
121
+ char path[PATH_MAX];
122
+ size_t len;
123
+ int keep;
124
+
125
+ if (!p->pack_local)
126
+ continue;
127
+ len = strlen(p->pack_name);
128
+ if (PATH_MAX <= len + 1)
129
+ continue; /* oops, give up */
130
+ memcpy(path, p->pack_name, len-5);
131
+ memcpy(path + len - 5, ".keep", 6);
132
+ keep = access(p->pack_name, F_OK) && (errno == ENOENT);
133
+ if (keep)
134
+ continue;
135
+ /*
136
+ * Perhaps check the size of the pack and count only
137
+ * very small ones here?
138
+ */
139
+ cnt++;
140
+ }
141
+ return gc_auto_pack_limit <= cnt;
142
+}
143
+
144
static int need_to_gc(void)
145
{
146
int ac = 0;
147
148
/*
108
- * Setting gc.auto to 0 or negative can disable the
109
- * automatic gc
149
+ * Setting gc.auto and gc.autopacklimit to 0 or negative can
150
+ * disable the automatic gc.
151
*/
111
- if (gc_auto_threshold <= 0)
112
- return 0;
113
-
114
- if (!too_many_loose_objects())
152
+ if (gc_auto_threshold <= 0 && gc_auto_pack_limit <= 0)
153
return 0;
154
155
+ /*
156
+ * If there are too many loose objects, but not too many
157
+ * packs, we run "repack -d -l". If there are too many packs,
158
+ * we run "repack -A -d -l". Otherwise we tell the caller
159
+ * there is no need.
160
+ */
161
argv_repack[ac++] = "repack";
162
+ if (too_many_packs())
163
+ argv_repack[ac++] = "-A";
164
+ else if (!too_many_loose_objects())
165
+ return 0;
166
argv_repack[ac++] = "-d";
167
argv_repack[ac++] = "-l";
168
argv_repack[ac++] = NULL;