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
);
22 if (lstat(fullpath
, &st
))
23 return error("Could not stat '%s'", fullpath
);
24 if (st
.st_mtime
> expire
)
26 printf("Removing stale temporary file %s\n", fullpath
);
28 unlink_or_warn(fullpath
);
32 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
34 const char *fullpath
= mkpath("%s/%s", path
, filename
);
36 if (lstat(fullpath
, &st
))
37 return error("Could not stat '%s'", fullpath
);
38 if (st
.st_mtime
> expire
)
40 if (show_only
|| verbose
) {
41 enum object_type type
= sha1_object_info(sha1
, NULL
);
42 printf("%s %s\n", sha1_to_hex(sha1
),
43 (type
> 0) ? typename(type
) : "unknown");
46 unlink_or_warn(fullpath
);
50 static int prune_dir(int i
, char *path
)
52 DIR *dir
= opendir(path
);
58 while ((de
= readdir(dir
)) != NULL
) {
60 unsigned char sha1
[20];
62 if (is_dot_or_dotdot(de
->d_name
))
64 if (strlen(de
->d_name
) == 38) {
65 sprintf(name
, "%02x", i
);
66 memcpy(name
+2, de
->d_name
, 39);
67 if (get_sha1_hex(name
, sha1
) < 0)
71 * Do we know about this object?
72 * It must have been reachable
74 if (lookup_object(sha1
))
77 prune_object(path
, de
->d_name
, sha1
);
80 if (!prefixcmp(de
->d_name
, "tmp_obj_")) {
81 prune_tmp_object(path
, de
->d_name
);
84 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
92 static void prune_object_dir(const char *path
)
95 for (i
= 0; i
< 256; i
++) {
96 static char dir
[4096];
97 sprintf(dir
, "%s/%02x", path
, i
);
103 * Write errors (particularly out of space) can result in
104 * failed temporary packs (and more rarely indexes and other
105 * files beginning with "tmp_") accumulating in the object
106 * and the pack directories.
108 static void remove_temporary_files(const char *path
)
115 fprintf(stderr
, "Unable to open directory %s\n", path
);
118 while ((de
= readdir(dir
)) != NULL
)
119 if (!prefixcmp(de
->d_name
, "tmp_"))
120 prune_tmp_object(path
, de
->d_name
);
124 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
126 struct rev_info revs
;
127 const struct option options
[] = {
128 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
129 OPT__VERBOSE(&verbose
, "report pruned objects"),
130 OPT_DATE(0, "expire", &expire
,
131 "expire objects older than <time>"),
137 save_commit_buffer
= 0;
138 read_replace_refs
= 0;
139 init_revisions(&revs
, prefix
);
141 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
143 unsigned char sha1
[20];
144 const char *name
= *argv
++;
146 if (!get_sha1(name
, sha1
)) {
147 struct object
*object
= parse_object(sha1
);
149 die("bad object: %s", name
);
150 add_pending_object(&revs
, object
, "");
153 die("unrecognized argument: %s", name
);
155 mark_reachable_objects(&revs
, 1);
156 prune_object_dir(get_object_directory());
158 prune_packed_objects(show_only
);
159 remove_temporary_files(get_object_directory());
160 s
= xstrdup(mkpath("%s/pack", get_object_directory()));
161 remove_temporary_files(s
);