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
);
30 unlink_or_warn(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");
50 unlink_or_warn(fullpath
);
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 read_replace_refs
= 0;
144 init_revisions(&revs
, prefix
);
146 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
148 unsigned char sha1
[20];
149 const char *name
= *argv
++;
151 if (!get_sha1(name
, sha1
)) {
152 struct object
*object
= parse_object(sha1
);
154 die("bad object: %s", name
);
155 add_pending_object(&revs
, object
, "");
158 die("unrecognized argument: %s", name
);
160 mark_reachable_objects(&revs
, 1);
161 prune_object_dir(get_object_directory());
163 prune_packed_objects(show_only
);
164 remove_temporary_files(get_object_directory());
165 s
= xstrdup(mkpath("%s/pack", get_object_directory()));
166 remove_temporary_files(s
);