Code

Introduce an unlink(2) wrapper which gives warning if unlink failed
[git.git] / builtin-prune.c
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 "dir.h"
10 static const char * const prune_usage[] = {
11         "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
12         NULL
13 };
14 static int show_only;
15 static int verbose;
16 static unsigned long expire;
18 static int prune_tmp_object(const char *path, const char *filename)
19 {
20         const char *fullpath = mkpath("%s/%s", path, filename);
21         if (expire) {
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         }
28         printf("Removing stale temporary file %s\n", fullpath);
29         if (!show_only)
30                 unlink(fullpath);
31         return 0;
32 }
34 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
35 {
36         const char *fullpath = mkpath("%s/%s", path, filename);
37         if (expire) {
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         }
44         if (show_only || verbose) {
45                 enum object_type type = sha1_object_info(sha1, NULL);
46                 printf("%s %s\n", sha1_to_hex(sha1),
47                        (type > 0) ? typename(type) : "unknown");
48         }
49         if (!show_only)
50                 unlink(fullpath);
51         return 0;
52 }
54 static int prune_dir(int i, char *path)
55 {
56         DIR *dir = opendir(path);
57         struct dirent *de;
59         if (!dir)
60                 return 0;
62         while ((de = readdir(dir)) != NULL) {
63                 char name[100];
64                 unsigned char sha1[20];
66                 if (is_dot_or_dotdot(de->d_name))
67                         continue;
68                 if (strlen(de->d_name) == 38) {
69                         sprintf(name, "%02x", i);
70                         memcpy(name+2, de->d_name, 39);
71                         if (get_sha1_hex(name, sha1) < 0)
72                                 break;
74                         /*
75                          * Do we know about this object?
76                          * It must have been reachable
77                          */
78                         if (lookup_object(sha1))
79                                 continue;
81                         prune_object(path, de->d_name, sha1);
82                         continue;
83                 }
84                 if (!prefixcmp(de->d_name, "tmp_obj_")) {
85                         prune_tmp_object(path, de->d_name);
86                         continue;
87                 }
88                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
89         }
90         if (!show_only)
91                 rmdir(path);
92         closedir(dir);
93         return 0;
94 }
96 static void prune_object_dir(const char *path)
97 {
98         int i;
99         for (i = 0; i < 256; i++) {
100                 static char dir[4096];
101                 sprintf(dir, "%s/%02x", path, i);
102                 prune_dir(i, dir);
103         }
106 /*
107  * Write errors (particularly out of space) can result in
108  * failed temporary packs (and more rarely indexes and other
109  * files begining with "tmp_") accumulating in the object
110  * and the pack directories.
111  */
112 static void remove_temporary_files(const char *path)
114         DIR *dir;
115         struct dirent *de;
117         dir = opendir(path);
118         if (!dir) {
119                 fprintf(stderr, "Unable to open directory %s\n", path);
120                 return;
121         }
122         while ((de = readdir(dir)) != NULL)
123                 if (!prefixcmp(de->d_name, "tmp_"))
124                         prune_tmp_object(path, de->d_name);
125         closedir(dir);
128 int cmd_prune(int argc, const char **argv, const char *prefix)
130         struct rev_info revs;
131         const struct option options[] = {
132                 OPT_BOOLEAN('n', NULL, &show_only,
133                             "do not remove, show only"),
134                 OPT_BOOLEAN('v', NULL, &verbose,
135                         "report pruned objects"),
136                 OPT_DATE(0, "expire", &expire,
137                          "expire objects older than <time>"),
138                 OPT_END()
139         };
140         char *s;
142         save_commit_buffer = 0;
143         init_revisions(&revs, prefix);
145         argc = parse_options(argc, argv, options, prune_usage, 0);
146         while (argc--) {
147                 unsigned char sha1[20];
148                 const char *name = *argv++;
150                 if (!get_sha1(name, sha1)) {
151                         struct object *object = parse_object(sha1);
152                         if (!object)
153                                 die("bad object: %s", name);
154                         add_pending_object(&revs, object, "");
155                 }
156                 else
157                         die("unrecognized argument: %s", name);
158         }
159         mark_reachable_objects(&revs, 1);
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;