7 #include "parse-options.h"
10 static const char * const prune_usage[] = {
11 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
16 static unsigned long expire;
18 static int prune_tmp_object(const char *path, const char *filename)
20 const char *fullpath = mkpath("%s/%s", path, filename);
23 if (lstat(fullpath, &st))
24 return error("Could not stat '%s'", fullpath);
25 if (st.st_mtime > expire)
28 printf("Removing stale temporary file %s\n", fullpath);
34 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
36 const char *fullpath = mkpath("%s/%s", path, filename);
39 if (lstat(fullpath, &st))
40 return error("Could not stat '%s'", fullpath);
41 if (st.st_mtime > expire)
44 if (show_only || verbose) {
45 enum object_type type = sha1_object_info(sha1, NULL);
46 printf("%s %s\n", sha1_to_hex(sha1),
47 (type > 0) ? typename(type) : "unknown");
54 static int prune_dir(int i, char *path)
56 DIR *dir = opendir(path);
62 while ((de = readdir(dir)) != NULL) {
64 unsigned char sha1[20];
66 if (is_dot_or_dotdot(de->d_name))
68 if (strlen(de->d_name) == 38) {
69 sprintf(name, "%02x", i);
70 memcpy(name+2, de->d_name, 39);
71 if (get_sha1_hex(name, sha1) < 0)
75 * Do we know about this object?
76 * It must have been reachable
78 if (lookup_object(sha1))
81 prune_object(path, de->d_name, sha1);
84 if (!prefixcmp(de->d_name, "tmp_obj_")) {
85 prune_tmp_object(path, de->d_name);
88 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
96 static void prune_object_dir(const char *path)
99 for (i = 0; i < 256; i++) {
100 static char dir[4096];
101 sprintf(dir, "%s/%02x", path, i);
107 * Write errors (particularly out of space) can result in
108 * failed temporary packs (and more rarely indexes and other
109 * files begining with "tmp_") accumulating in the object
110 * and the pack directories.
112 static void remove_temporary_files(const char *path)
119 fprintf(stderr, "Unable to open directory %s\n", path);
122 while ((de = readdir(dir)) != NULL)
123 if (!prefixcmp(de->d_name, "tmp_"))
124 prune_tmp_object(path, de->d_name);
128 int cmd_prune(int argc, const char **argv, const char *prefix)
130 struct rev_info revs;
131 const struct option options[] = {
132 OPT_BOOLEAN('n', NULL, &show_only,
133 "do not remove, show only"),
134 OPT_BOOLEAN('v', NULL, &verbose,
135 "report pruned objects"),
136 OPT_DATE(0, "expire", &expire,
137 "expire objects older than <time>"),
142 save_commit_buffer = 0;
143 init_revisions(&revs, prefix);
145 argc = parse_options(argc, argv, options, prune_usage, 0);
147 unsigned char sha1[20];
148 const char *name = *argv++;
150 if (!get_sha1(name, sha1)) {
151 struct object *object = parse_object(sha1);
153 die("bad object: %s", name);
154 add_pending_object(&revs, object, "");
157 die("unrecognized argument: %s", name);
159 mark_reachable_objects(&revs, 1);
160 prune_object_dir(get_object_directory());
162 prune_packed_objects(show_only);
163 remove_temporary_files(get_object_directory());
164 s = xstrdup(mkpath("%s/pack", get_object_directory()));
165 remove_temporary_files(s);