Add "--expire <time>" option to 'git prune'
Earlier, 'git prune' would prune all loose unreachable objects. This could be quite dangerous, as the objects could be used in an ongoing operation. This patch adds a mode to expire only loose, unreachable objects which are older than a certain time. For example, by git prune --expire 14.days you can prune only those objects which are loose, unreachable and older than 14 days (and thus probably outdated). The implementation uses st.st_mtime rather than st.st_ctime, because it can be tested better, using 'touch -d <time>' (and omitting the test when the platform does not support that command line switch). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 29, 2007 at 20:59 UTC
f01913e4190bed98f918ec575229943a0ee47d83
3 files changed
+42
-2
Documentation/git-prune.txt
+4
-1
@@ -8,7 +8,7 @@ git-prune - Prune all unreachable objects from the object database
8
9
SYNOPSIS
10
--------
11
-'git-prune' [-n] [--] [<head>...]
11
+'git-prune' [-n] [--expire <expire>] [--] [<head>...]
12
13
DESCRIPTION
14
-----------
@@ -31,6 +31,9 @@ OPTIONS
31
\--::
32
Do not interpret any more arguments as options.
33
34
+\--expire <time>::
35
+ Only expire loose objects older than <time>.
36
+
37
<head>...::
38
In addition to objects
39
reachable from any of our references, keep objects
builtin-prune.c
+20
-1
@@ -7,15 +7,24 @@
7
8
static const char prune_usage[] = "git-prune [-n]";
9
static int show_only;
10
+static unsigned long expire;
11
12
static int prune_object(char *path, const char *filename, const unsigned char *sha1)
13
{
14
+ const char *fullpath = mkpath("%s/%s", path, filename);
15
+ if (expire) {
16
+ struct stat st;
17
+ if (lstat(fullpath, &st))
18
+ return error("Could not stat '%s'", fullpath);
19
+ if (st.st_mtime > expire)
20
+ return 0;
21
+ }
22
if (show_only) {
23
enum object_type type = sha1_object_info(sha1, NULL);
24
printf("%s %s\n", sha1_to_hex(sha1),
25
(type > 0) ? typename(type) : "unknown");
26
} else
18
- unlink(mkpath("%s/%s", path, filename));
27
+ unlink(fullpath);
28
return 0;
29
}
30
@@ -85,6 +94,16 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
94
show_only = 1;
95
continue;
96
}
97
+ if (!strcmp(arg, "--expire")) {
98
+ if (++i < argc) {
99
+ expire = approxidate(argv[i]);
100
+ continue;
101
+ }
102
+ }
103
+ else if (!prefixcmp(arg, "--expire=")) {
104
+ expire = approxidate(arg + 9);
105
+ continue;
106
+ }
107
usage(prune_usage);
108
}
109
t/t1410-reflog.sh
+18
@@ -175,4 +175,22 @@ test_expect_success 'recover and check' '
175
176
'
177
178
+test_expect_success 'prune --expire' '
179
+
180
+ before=$(git count-objects | sed "s/ .*//") &&
181
+ BLOB=$(echo aleph | git hash-object -w --stdin) &&
182
+ BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
183
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
184
+ test -f $BLOB_FILE &&
185
+ git reset --hard &&
186
+ git prune --expire=1.hour.ago &&
187
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
188
+ test -f $BLOB_FILE &&
189
+ test-chmtime -86500 $BLOB_FILE &&
190
+ git prune --expire 1.day &&
191
+ test $before = $(git count-objects | sed "s/ .*//") &&
192
+ ! test -f $BLOB_FILE
193
+
194
+'
195
+
196
test_done