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_object(const char *path, const char *filename)
22 const char *fullpath = mkpath("%s/%s", path, filename);
24 if (lstat(fullpath, &st))
25 return error("Could not stat '%s'", fullpath);
26 if (st.st_mtime > expire)
28 if (show_only || verbose)
29 printf("Removing stale temporary file %s\n", fullpath);
31 unlink_or_warn(fullpath);
35 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
37 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)
43 if (show_only || verbose) {
44 enum object_type type = sha1_object_info(sha1, NULL);
45 printf("%s %s\n", sha1_to_hex(sha1),
46 (type > 0) ? typename(type) : "unknown");
49 unlink_or_warn(fullpath);
53 static int prune_dir(int i, char *path)
55 DIR *dir = opendir(path);
61 while ((de = readdir(dir)) != NULL) {
63 unsigned char sha1[20];
65 if (is_dot_or_dotdot(de->d_name))
67 if (strlen(de->d_name) == 38) {
68 sprintf(name, "%02x", i);
69 memcpy(name+2, de->d_name, 39);
70 if (get_sha1_hex(name, sha1) < 0)
74 * Do we know about this object?
75 * It must have been reachable
77 if (lookup_object(sha1))
80 prune_object(path, de->d_name, sha1);
83 if (!prefixcmp(de->d_name, "tmp_obj_")) {
84 prune_tmp_object(path, de->d_name);
87 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
95 static void prune_object_dir(const char *path)
98 for (i = 0; i < 256; i++) {
99 static char dir[4096];
100 sprintf(dir, "%s/%02x", path, i);
106 * Write errors (particularly out of space) can result in
107 * failed temporary packs (and more rarely indexes and other
108 * files beginning with "tmp_") accumulating in the object
109 * and the pack directories.
111 static void remove_temporary_files(const char *path)
118 fprintf(stderr, "Unable to open directory %s\n", path);
121 while ((de = readdir(dir)) != NULL)
122 if (!prefixcmp(de->d_name, "tmp_"))
123 prune_tmp_object(path, de->d_name);
127 int cmd_prune(int argc, const char **argv, const char *prefix)
129 struct rev_info revs;
130 struct progress *progress = NULL;
131 const struct option options[] = {
132 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
133 OPT__VERBOSE(&verbose, N_("report pruned objects")),
134 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
135 OPT_EXPIRY_DATE(0, "expire", &expire,
136 N_("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_or_die(sha1, name);
153 add_pending_object(&revs, object, "");
156 die("unrecognized argument: %s", name);
159 if (show_progress == -1)
160 show_progress = isatty(2);
162 progress = start_progress_delay("Checking connectivity", 0, 0, 2);
164 mark_reachable_objects(&revs, 1, progress);
165 stop_progress(&progress);
166 prune_object_dir(get_object_directory());
168 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
169 remove_temporary_files(get_object_directory());
170 s = mkpathdup("%s/pack", get_object_directory());
171 remove_temporary_files(s);