7 #include "parse-options.h"
9 static const char * const prune_usage
[] = {
10 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
15 static unsigned long expire
;
17 static int prune_tmp_object(const char *path
, const char *filename
)
19 const char *fullpath
= mkpath("%s/%s", path
, filename
);
22 if (lstat(fullpath
, &st
))
23 return error("Could not stat '%s'", fullpath
);
24 if (st
.st_mtime
> expire
)
27 printf("Removing stale temporary file %s\n", fullpath
);
33 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
35 const char *fullpath
= mkpath("%s/%s", path
, filename
);
38 if (lstat(fullpath
, &st
))
39 return error("Could not stat '%s'", fullpath
);
40 if (st
.st_mtime
> expire
)
43 if (show_only
|| verbose
) {
44 enum object_type type
= sha1_object_info(sha1
, NULL
);
45 printf("%s %s\n", sha1_to_hex(sha1
),
46 (type
> 0) ? typename(type
) : "unknown");
53 static int prune_dir(int i
, char *path
)
55 DIR *dir
= opendir(path
);
61 while ((de
= readdir(dir
)) != NULL
) {
63 unsigned char sha1
[20];
64 int len
= strlen(de
->d_name
);
68 if (de
->d_name
[1] != '.')
71 if (de
->d_name
[0] != '.')
75 sprintf(name
, "%02x", i
);
76 memcpy(name
+2, de
->d_name
, len
+1);
77 if (get_sha1_hex(name
, sha1
) < 0)
81 * Do we know about this object?
82 * It must have been reachable
84 if (lookup_object(sha1
))
87 prune_object(path
, de
->d_name
, sha1
);
90 if (!prefixcmp(de
->d_name
, "tmp_obj_")) {
91 prune_tmp_object(path
, de
->d_name
);
94 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
102 static void prune_object_dir(const char *path
)
105 for (i
= 0; i
< 256; i
++) {
106 static char dir
[4096];
107 sprintf(dir
, "%s/%02x", path
, i
);
113 * Write errors (particularly out of space) can result in
114 * failed temporary packs (and more rarely indexes and other
115 * files begining with "tmp_") accumulating in the object
116 * and the pack directories.
118 static void remove_temporary_files(const char *path
)
125 fprintf(stderr
, "Unable to open directory %s\n", path
);
128 while ((de
= readdir(dir
)) != NULL
)
129 if (!prefixcmp(de
->d_name
, "tmp_"))
130 prune_tmp_object(path
, de
->d_name
);
134 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
136 struct rev_info revs
;
137 const struct option options
[] = {
138 OPT_BOOLEAN('n', NULL
, &show_only
,
139 "do not remove, show only"),
140 OPT_BOOLEAN('v', NULL
, &verbose
,
141 "report pruned objects"),
142 OPT_DATE(0, "expire", &expire
,
143 "expire objects older than <time>"),
148 save_commit_buffer
= 0;
149 init_revisions(&revs
, prefix
);
151 argc
= parse_options(argc
, argv
, options
, prune_usage
, 0);
153 unsigned char sha1
[20];
154 const char *name
= *argv
++;
156 if (!get_sha1(name
, sha1
)) {
157 struct object
*object
= parse_object(sha1
);
159 die("bad object: %s", name
);
160 add_pending_object(&revs
, object
, "");
163 die("unrecognized argument: %s", name
);
165 mark_reachable_objects(&revs
, 1);
166 prune_object_dir(get_object_directory());
168 prune_packed_objects(show_only
);
169 remove_temporary_files(get_object_directory());
170 s
= xstrdup(mkpath("%s/pack", get_object_directory()));
171 remove_temporary_files(s
);