7 #include "parse-options.h"
11 static const char * const prune_usage
[] = {
12 N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
17 static unsigned long expire
;
18 static int show_progress
= -1;
20 static int prune_tmp_file(const char *fullpath
)
23 if (lstat(fullpath
, &st
))
24 return error("Could not stat '%s'", fullpath
);
25 if (st
.st_mtime
> expire
)
27 if (show_only
|| verbose
)
28 printf("Removing stale temporary file %s\n", fullpath
);
30 unlink_or_warn(fullpath
);
34 static int prune_object(const char *fullpath
, const unsigned char *sha1
)
37 if (lstat(fullpath
, &st
))
38 return error("Could not stat '%s'", fullpath
);
39 if (st
.st_mtime
> expire
)
41 if (show_only
|| verbose
) {
42 enum object_type type
= sha1_object_info(sha1
, NULL
);
43 printf("%s %s\n", sha1_to_hex(sha1
),
44 (type
> 0) ? typename(type
) : "unknown");
47 unlink_or_warn(fullpath
);
51 static int prune_dir(int i
, struct strbuf
*path
)
53 size_t baselen
= path
->len
;
54 DIR *dir
= opendir(path
->buf
);
60 while ((de
= readdir(dir
)) != NULL
) {
62 unsigned char sha1
[20];
64 if (is_dot_or_dotdot(de
->d_name
))
66 if (strlen(de
->d_name
) == 38) {
67 sprintf(name
, "%02x", i
);
68 memcpy(name
+2, de
->d_name
, 39);
69 if (get_sha1_hex(name
, sha1
) < 0)
73 * Do we know about this object?
74 * It must have been reachable
76 if (lookup_object(sha1
))
79 strbuf_addf(path
, "/%s", de
->d_name
);
80 prune_object(path
->buf
, sha1
);
81 strbuf_setlen(path
, baselen
);
84 if (starts_with(de
->d_name
, "tmp_obj_")) {
85 strbuf_addf(path
, "/%s", de
->d_name
);
86 prune_tmp_file(path
->buf
);
87 strbuf_setlen(path
, baselen
);
90 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
->buf
, de
->d_name
);
98 static void prune_object_dir(const char *path
)
100 struct strbuf buf
= STRBUF_INIT
;
104 strbuf_addstr(&buf
, path
);
105 strbuf_addch(&buf
, '/');
108 for (i
= 0; i
< 256; i
++) {
109 strbuf_addf(&buf
, "%02x", i
);
111 strbuf_setlen(&buf
, baselen
);
116 * Write errors (particularly out of space) can result in
117 * failed temporary packs (and more rarely indexes and other
118 * files beginning with "tmp_") accumulating in the object
119 * and the pack directories.
121 static void remove_temporary_files(const char *path
)
128 fprintf(stderr
, "Unable to open directory %s\n", path
);
131 while ((de
= readdir(dir
)) != NULL
)
132 if (starts_with(de
->d_name
, "tmp_"))
133 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
137 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
139 struct rev_info revs
;
140 struct progress
*progress
= NULL
;
141 const struct option options
[] = {
142 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
143 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
144 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
145 OPT_EXPIRY_DATE(0, "expire", &expire
,
146 N_("expire objects older than <time>")),
152 save_commit_buffer
= 0;
153 read_replace_refs
= 0;
154 init_revisions(&revs
, prefix
);
156 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
158 unsigned char sha1
[20];
159 const char *name
= *argv
++;
161 if (!get_sha1(name
, sha1
)) {
162 struct object
*object
= parse_object_or_die(sha1
, name
);
163 add_pending_object(&revs
, object
, "");
166 die("unrecognized argument: %s", name
);
169 if (show_progress
== -1)
170 show_progress
= isatty(2);
172 progress
= start_progress_delay("Checking connectivity", 0, 0, 2);
174 mark_reachable_objects(&revs
, 1, progress
);
175 stop_progress(&progress
);
176 prune_object_dir(get_object_directory());
178 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
179 remove_temporary_files(get_object_directory());
180 s
= mkpathdup("%s/pack", get_object_directory());
181 remove_temporary_files(s
);