gc: make --prune useful again by accepting an optional parameter
With this patch, "git gc --no-prune" will not prune any loose (and dangling) object, and "git gc --prune=5.minutes.ago" will prune all loose objects older than 5 minutes. This patch benefitted from suggestions by Thomas Rast and Jan Krᅵger. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Feb 14, 2009 at 23:10 UTC
58e9d9d472ff4dbc6cfbf7459878778c739395eb
3 files changed
+59
-8
Documentation/git-gc.txt
+9
-1
@@ -8,7 +8,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
8
9
SYNOPSIS
10
--------
11
-'git gc' [--aggressive] [--auto] [--quiet]
11
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
12
13
DESCRIPTION
14
-----------
@@ -59,6 +59,14 @@ are consolidated into a single pack by using the `-A` option of
59
'git-repack'. Setting `gc.autopacklimit` to 0 disables
60
automatic consolidation of packs.
61
62
+--prune=<date>::
63
+ Prune loose objects older than date (default is 2 weeks ago,
64
+ overrideable by the config variable `gc.pruneExpire`). This
65
+ option is on by default.
66
+
67
+--no-prune::
68
+ Do not prune any loose objects.
69
+
70
--quiet::
71
Suppress all progress reports.
72
builtin-gc.c
+12
-7
@@ -161,7 +161,8 @@ static int need_to_gc(void)
161
*/
162
if (too_many_packs())
163
append_option(argv_repack,
164
- !strcmp(prune_expire, "now") ? "-a" : "-A",
164
+ prune_expire && !strcmp(prune_expire, "now") ?
165
+ "-a" : "-A",
166
MAX_ADD);
167
else if (!too_many_loose_objects())
168
return 0;
@@ -173,14 +174,15 @@ static int need_to_gc(void)
174
175
int cmd_gc(int argc, const char **argv, const char *prefix)
176
{
176
- int prune = 0;
177
int aggressive = 0;
178
int auto_gc = 0;
179
int quiet = 0;
180
char buf[80];
181
182
struct option builtin_gc_options[] = {
183
- OPT_BOOLEAN(0, "prune", &prune, "prune unreferenced objects (deprecated)"),
183
+ { OPTION_STRING, 0, "prune", &prune_expire, "date",
184
+ "prune unreferenced objects",
185
+ PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
186
OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"),
187
OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"),
188
OPT_BOOLEAN('q', "quiet", &quiet, "suppress progress reports"),
@@ -218,7 +220,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
220
"\"git help gc\" for more information.\n");
221
} else
222
append_option(argv_repack,
221
- !strcmp(prune_expire, "now") ? "-a" : "-A",
223
+ prune_expire && !strcmp(prune_expire, "now")
224
+ ? "-a" : "-A",
225
MAX_ADD);
226
227
if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
@@ -230,9 +233,11 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
233
if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
234
return error(FAILED_RUN, argv_repack[0]);
235
233
- argv_prune[2] = prune_expire;
234
- if (run_command_v_opt(argv_prune, RUN_GIT_CMD))
235
- return error(FAILED_RUN, argv_prune[0]);
236
+ if (prune_expire) {
237
+ argv_prune[2] = prune_expire;
238
+ if (run_command_v_opt(argv_prune, RUN_GIT_CMD))
239
+ return error(FAILED_RUN, argv_prune[0]);
240
+ }
241
242
if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
243
return error(FAILED_RUN, argv_rerere[0]);
t/t5304-prune.sh
+38
@@ -112,4 +112,42 @@ test_expect_success 'prune: do not prune heads listed as an argument' '
112
113
'
114
115
+test_expect_success 'gc --no-prune' '
116
+
117
+ before=$(git count-objects | sed "s/ .*//") &&
118
+ BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
119
+ BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
120
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
121
+ test -f $BLOB_FILE &&
122
+ test-chmtime =-$((86400*5001)) $BLOB_FILE &&
123
+ git config gc.pruneExpire 2.days.ago &&
124
+ git gc --no-prune &&
125
+ test 1 = $(git count-objects | sed "s/ .*//") &&
126
+ test -f $BLOB_FILE
127
+
128
+'
129
+
130
+test_expect_success 'gc respects gc.pruneExpire' '
131
+
132
+ git config gc.pruneExpire 5002.days.ago &&
133
+ git gc &&
134
+ test -f $BLOB_FILE &&
135
+ git config gc.pruneExpire 5000.days.ago &&
136
+ git gc &&
137
+ test ! -f $BLOB_FILE
138
+
139
+'
140
+
141
+test_expect_success 'gc --prune=<date>' '
142
+
143
+ BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
144
+ BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
145
+ test-chmtime =-$((86400*5001)) $BLOB_FILE &&
146
+ git gc --prune=5002.days.ago &&
147
+ test -f $BLOB_FILE &&
148
+ git gc --prune=5000.days.ago &&
149
+ test ! -f $BLOB_FILE
150
+
151
+'
152
+
153
test_done