More topics from the second batch for 1.8.3
[git/gitweb.git] / builtin / prune.c
blob85843d4f1728a907aafab9938b4c827990a7695f
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 N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
13 NULL
15 static int show_only;
16 static int verbose;
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);
23 struct stat st;
24 if (lstat(fullpath, &st))
25 return error("Could not stat '%s'", fullpath);
26 if (st.st_mtime > expire)
27 return 0;
28 if (show_only || verbose)
29 printf("Removing stale temporary file %s\n", fullpath);
30 if (!show_only)
31 unlink_or_warn(fullpath);
32 return 0;
35 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
37 const char *fullpath = mkpath("%s/%s", path, filename);
38 struct stat st;
39 if (lstat(fullpath, &st))
40 return error("Could not stat '%s'", fullpath);
41 if (st.st_mtime > expire)
42 return 0;
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");
48 if (!show_only)
49 unlink_or_warn(fullpath);
50 return 0;
53 static int prune_dir(int i, char *path)
55 DIR *dir = opendir(path);
56 struct dirent *de;
58 if (!dir)
59 return 0;
61 while ((de = readdir(dir)) != NULL) {
62 char name[100];
63 unsigned char sha1[20];
65 if (is_dot_or_dotdot(de->d_name))
66 continue;
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)
71 break;
74 * Do we know about this object?
75 * It must have been reachable
77 if (lookup_object(sha1))
78 continue;
80 prune_object(path, de->d_name, sha1);
81 continue;
83 if (!prefixcmp(de->d_name, "tmp_obj_")) {
84 prune_tmp_object(path, de->d_name);
85 continue;
87 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
89 closedir(dir);
90 if (!show_only)
91 rmdir(path);
92 return 0;
95 static void prune_object_dir(const char *path)
97 int i;
98 for (i = 0; i < 256; i++) {
99 static char dir[4096];
100 sprintf(dir, "%s/%02x", path, i);
101 prune_dir(i, dir);
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)
113 DIR *dir;
114 struct dirent *de;
116 dir = opendir(path);
117 if (!dir) {
118 fprintf(stderr, "Unable to open directory %s\n", path);
119 return;
121 while ((de = readdir(dir)) != NULL)
122 if (!prefixcmp(de->d_name, "tmp_"))
123 prune_tmp_object(path, de->d_name);
124 closedir(dir);
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_DATE(0, "expire", &expire,
136 N_("expire objects older than <time>")),
137 OPT_END()
139 char *s;
141 expire = ULONG_MAX;
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);
147 while (argc--) {
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, "");
155 else
156 die("unrecognized argument: %s", name);
159 if (show_progress == -1)
160 show_progress = isatty(2);
161 if (show_progress)
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);
169 remove_temporary_files(get_object_directory());
170 s = mkpathdup("%s/pack", get_object_directory());
171 remove_temporary_files(s);
172 free(s);
173 return 0;