7 #include "parse-options.h"
11 static const char * const prune_usage
[] = {
12 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
17 static unsigned long expire
;
19 static int prune_tmp_object(const char *path
, const char *filename
)
21 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
)
27 printf("Removing stale temporary file %s\n", fullpath
);
29 unlink_or_warn(fullpath
);
33 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
35 const char *fullpath
= mkpath("%s/%s", path
, filename
);
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
, char *path
)
53 DIR *dir
= opendir(path
);
59 while ((de
= readdir(dir
)) != NULL
) {
61 unsigned char sha1
[20];
63 if (is_dot_or_dotdot(de
->d_name
))
65 if (strlen(de
->d_name
) == 38) {
66 sprintf(name
, "%02x", i
);
67 memcpy(name
+2, de
->d_name
, 39);
68 if (get_sha1_hex(name
, sha1
) < 0)
72 * Do we know about this object?
73 * It must have been reachable
75 if (lookup_object(sha1
))
78 prune_object(path
, de
->d_name
, sha1
);
81 if (!prefixcmp(de
->d_name
, "tmp_obj_")) {
82 prune_tmp_object(path
, de
->d_name
);
85 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
93 static void prune_object_dir(const char *path
)
96 for (i
= 0; i
< 256; i
++) {
97 static char dir
[4096];
98 sprintf(dir
, "%s/%02x", path
, i
);
104 * Write errors (particularly out of space) can result in
105 * failed temporary packs (and more rarely indexes and other
106 * files beginning with "tmp_") accumulating in the object
107 * and the pack directories.
109 static void remove_temporary_files(const char *path
)
116 fprintf(stderr
, "Unable to open directory %s\n", path
);
119 while ((de
= readdir(dir
)) != NULL
)
120 if (!prefixcmp(de
->d_name
, "tmp_"))
121 prune_tmp_object(path
, de
->d_name
);
125 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
127 struct rev_info revs
;
128 struct progress
*progress
;
129 const struct option options
[] = {
130 OPT__DRY_RUN(&show_only
, "do not remove, show only"),
131 OPT__VERBOSE(&verbose
, "report pruned objects"),
132 OPT_DATE(0, "expire", &expire
,
133 "expire objects older than <time>"),
139 save_commit_buffer
= 0;
140 read_replace_refs
= 0;
141 init_revisions(&revs
, prefix
);
143 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
145 unsigned char sha1
[20];
146 const char *name
= *argv
++;
148 if (!get_sha1(name
, sha1
)) {
149 struct object
*object
= parse_object(sha1
);
151 die("bad object: %s", name
);
152 add_pending_object(&revs
, object
, "");
155 die("unrecognized argument: %s", name
);
157 progress
= start_progress_delay("Checking connectivity", 0, 0, 2);
158 mark_reachable_objects(&revs
, 1, progress
);
159 stop_progress(&progress
);
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
);