prune: show progress while marking reachable objects
[git.git] / builtin / prune.c
blob6b39d3fdebcc1a2e105da277e2b34956ebddf1d4
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
7 #include "parse-options.h"
8 #include "progress.h"
9 #include "dir.h"
11 static const char * const prune_usage[] = {
12 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
13 NULL
15 static int show_only;
16 static int verbose;
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);
22 struct stat st;
23 if (lstat(fullpath, &st))
24 return error("Could not stat '%s'", fullpath);
25 if (st.st_mtime > expire)
26 return 0;
27 printf("Removing stale temporary file %s\n", fullpath);
28 if (!show_only)
29 unlink_or_warn(fullpath);
30 return 0;
33 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
35 const char *fullpath = mkpath("%s/%s", path, filename);
36 struct stat st;
37 if (lstat(fullpath, &st))
38 return error("Could not stat '%s'", fullpath);
39 if (st.st_mtime > expire)
40 return 0;
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");
46 if (!show_only)
47 unlink_or_warn(fullpath);
48 return 0;
51 static int prune_dir(int i, char *path)
53 DIR *dir = opendir(path);
54 struct dirent *de;
56 if (!dir)
57 return 0;
59 while ((de = readdir(dir)) != NULL) {
60 char name[100];
61 unsigned char sha1[20];
63 if (is_dot_or_dotdot(de->d_name))
64 continue;
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)
69 break;
72 * Do we know about this object?
73 * It must have been reachable
75 if (lookup_object(sha1))
76 continue;
78 prune_object(path, de->d_name, sha1);
79 continue;
81 if (!prefixcmp(de->d_name, "tmp_obj_")) {
82 prune_tmp_object(path, de->d_name);
83 continue;
85 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
87 if (!show_only)
88 rmdir(path);
89 closedir(dir);
90 return 0;
93 static void prune_object_dir(const char *path)
95 int i;
96 for (i = 0; i < 256; i++) {
97 static char dir[4096];
98 sprintf(dir, "%s/%02x", path, i);
99 prune_dir(i, dir);
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)
111 DIR *dir;
112 struct dirent *de;
114 dir = opendir(path);
115 if (!dir) {
116 fprintf(stderr, "Unable to open directory %s\n", path);
117 return;
119 while ((de = readdir(dir)) != NULL)
120 if (!prefixcmp(de->d_name, "tmp_"))
121 prune_tmp_object(path, de->d_name);
122 closedir(dir);
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>"),
134 OPT_END()
136 char *s;
138 expire = ULONG_MAX;
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);
144 while (argc--) {
145 unsigned char sha1[20];
146 const char *name = *argv++;
148 if (!get_sha1(name, sha1)) {
149 struct object *object = parse_object(sha1);
150 if (!object)
151 die("bad object: %s", name);
152 add_pending_object(&revs, object, "");
154 else
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);
166 free(s);
167 return 0;