@matheusbsbs / matheus-bagliano / commits / 25ee9731c1
Fork 0

gc: call "prune --expire 2.weeks.ago" by default

The only reason we did not call "prune" in git-gc was that it is an inherently dangerous operation: if there is a commit going on, you will prune loose objects that were just created, and are, in fact, needed by the commit object just about to be created. Since it is dangerous, we told users so. That led to many users not even daring to run it when it was actually safe. Besides, they are users, and should not have to remember such details as when to call git-gc with --prune, or to call git-prune directly. Of course, the consequence was that "git gc --auto" gets triggered much more often than we would like, since unreferenced loose objects (such as left-overs from a rebase or a reset --hard) were never pruned. Alas, git-prune recently learnt the option --expire <minimum-age>, which makes it a much safer operation. This allows us to call prune from git-gc, with a grace period of 2 weeks for the unreferenced loose objects (this value was determined in a discussion on the git list as a safe one). If you want to override this grace period, just set the config variable gc.pruneExpire to a different value; an example would be [gc] pruneExpire = 6.months.ago or even "never", if you feel really paranoid. Note that this new behaviour makes "--prune" be a no-op. While adding a test to t5304-prune.sh (since it really tests the implicit call to "prune"), also the original test for "prune --expire" was moved there from t1410-reflog.sh, where it did not belong. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Johannes Schindelin committed Mar 12, 2008 at 21:55 UTC 25ee9731c137d0a24b0f4879eb0b0cce9b77d5b0
5 files changed +73 -32
Documentation/config.txt
+4
@@ -590,6 +590,10 @@ gc.packrefs::
590 at some stage, and setting this to `false` will continue to
591 prevent `git pack-refs` from being run from `git gc`.
592
593 +gc.pruneexpire::
594 + When `git gc` is run, it will call `prune --expire 2.weeks.ago`.
595 + Override the grace period with this config variable.
596 +
597 gc.reflogexpire::
598 `git reflog expire` removes reflog entries older than
599 this time; defaults to 90 days.
Documentation/git-gc.txt
+5 -12
@@ -8,7 +8,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
8
9 SYNOPSIS
10 --------
11 -'git-gc' [--prune] [--aggressive] [--auto] [--quiet]
11 +'git-gc' [--aggressive] [--auto] [--quiet]
12
13 DESCRIPTION
14 -----------
@@ -25,17 +25,6 @@ operating performance. Some git commands may automatically run
25 OPTIONS
26 -------
27
28 ---prune::
29 - Usually `git-gc` packs refs, expires old reflog entries,
30 - packs loose objects,
31 - and removes old 'rerere' records. Removal
32 - of unreferenced loose objects is an unsafe operation
33 - while other git operations are in progress, so it is not
34 - done by default. Pass this option if you want it, and only
35 - when you know nobody else is creating new objects in the
36 - repository at the same time (e.g. never use this option
37 - in a cron script).
38 -
28 --aggressive::
29 Usually 'git-gc' runs very quickly while providing good disk
30 space utilization and performance. This option will cause
@@ -104,6 +93,10 @@ the value, the more time is spent optimizing the delta compression. See
93 the documentation for the --window' option in linkgit:git-repack[1] for
94 more details. This defaults to 10.
95
96 +The optional configuration variable 'gc.pruneExpire' controls how old
97 +the unreferenced loose objects have to be before they are pruned. The
98 +default is "2 weeks ago".
99 +
100 See Also
101 --------
102 linkgit:git-prune[1]
builtin-gc.c
+15 -2
@@ -26,12 +26,13 @@ static int pack_refs = 1;
26 static int aggressive_window = -1;
27 static int gc_auto_threshold = 6700;
28 static int gc_auto_pack_limit = 20;
29 +static char *prune_expire = "2.weeks.ago";
30
31 #define MAX_ADD 10
32 static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
33 static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
34 static const char *argv_repack[MAX_ADD] = {"repack", "-d", "-l", NULL};
34 -static const char *argv_prune[] = {"prune", NULL};
35 +static const char *argv_prune[] = {"prune", "--expire", NULL, NULL};
36 static const char *argv_rerere[] = {"rerere", "gc", NULL};
37
38 static int gc_config(const char *var, const char *value)
@@ -55,6 +56,17 @@ static int gc_config(const char *var, const char *value)
56 gc_auto_pack_limit = git_config_int(var, value);
57 return 0;
58 }
59 + if (!strcmp(var, "gc.pruneexpire")) {
60 + if (!value)
61 + return config_error_nonbool(var);
62 + if (strcmp(value, "now")) {
63 + unsigned long now = approxidate("now");
64 + if (approxidate(value) >= now)
65 + return error("Invalid %s: '%s'", var, value);
66 + }
67 + prune_expire = xstrdup(value);
68 + return 0;
69 + }
70 return git_default_config(var, value);
71 }
72
@@ -234,7 +246,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
246 if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
247 return error(FAILED_RUN, argv_repack[0]);
248
237 - if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
249 + argv_prune[2] = prune_expire;
250 + if (run_command_v_opt(argv_prune, RUN_GIT_CMD))
251 return error(FAILED_RUN, argv_prune[0]);
252
253 if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
t/t1410-reflog.sh
-18
@@ -202,22 +202,4 @@ test_expect_success 'delete' '
202
203 '
204
205 -test_expect_success 'prune --expire' '
206 -
207 - before=$(git count-objects | sed "s/ .*//") &&
208 - BLOB=$(echo aleph | git hash-object -w --stdin) &&
209 - BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
210 - test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
211 - test -f $BLOB_FILE &&
212 - git reset --hard &&
213 - git prune --expire=1.hour.ago &&
214 - test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
215 - test -f $BLOB_FILE &&
216 - test-chmtime -86500 $BLOB_FILE &&
217 - git prune --expire 1.day &&
218 - test $before = $(git count-objects | sed "s/ .*//") &&
219 - ! test -f $BLOB_FILE
220 -
221 -'
222 -
205 test_done
t/t5304-prune.sh
+49
@@ -29,4 +29,53 @@ test_expect_success 'prune stale packs' '
29
30 '
31
32 +test_expect_success 'prune --expire' '
33 +
34 + before=$(git count-objects | sed "s/ .*//") &&
35 + BLOB=$(echo aleph | git hash-object -w --stdin) &&
36 + BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
37 + test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
38 + test -f $BLOB_FILE &&
39 + git prune --expire=1.hour.ago &&
40 + test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
41 + test -f $BLOB_FILE &&
42 + test-chmtime -86500 $BLOB_FILE &&
43 + git prune --expire 1.day &&
44 + test $before = $(git count-objects | sed "s/ .*//") &&
45 + ! test -f $BLOB_FILE
46 +
47 +'
48 +
49 +test_expect_success 'gc: implicit prune --expire' '
50 +
51 + before=$(git count-objects | sed "s/ .*//") &&
52 + BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
53 + BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
54 + test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
55 + test -f $BLOB_FILE &&
56 + test-chmtime -$((86400*14-30)) $BLOB_FILE &&
57 + git gc &&
58 + test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
59 + test -f $BLOB_FILE &&
60 + test-chmtime -$((86400*14+1)) $BLOB_FILE &&
61 + git gc &&
62 + test $before = $(git count-objects | sed "s/ .*//") &&
63 + ! test -f $BLOB_FILE
64 +
65 +'
66 +
67 +test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
68 +
69 + git config gc.pruneExpire invalid &&
70 + test_must_fail git gc
71 +
72 +'
73 +
74 +test_expect_success 'gc: start with ok gc.pruneExpire' '
75 +
76 + git config gc.pruneExpire 2.days.ago &&
77 + git gc
78 +
79 +'
80 +
81 test_done