cd079b419cf2792550809a398ba1e7abf60d550f
1#include "cache.h"
2#include "refs.h"
3#include "tag.h"
4#include "commit.h"
5#include "tree.h"
6#include "blob.h"
7#include "tree-walk.h"
8#include "diff.h"
9#include "revision.h"
10#include "builtin.h"
11#include "cache-tree.h"
12
13static const char prune_usage[] = "git-prune [-n]";
14static int show_only;
15
16static int prune_object(char *path, const char *filename, const unsigned char *sha1)
17{
18 char buf[20];
19 const char *type;
20
21 if (show_only) {
22 if (sha1_object_info(sha1, buf, NULL))
23 type = "unknown";
24 else
25 type = buf;
26 printf("%s %s\n", sha1_to_hex(sha1), type);
27 return 0;
28 }
29 unlink(mkpath("%s/%s", path, filename));
30 rmdir(path);
31 return 0;
32}
33
34static int prune_dir(int i, char *path)
35{
36 DIR *dir = opendir(path);
37 struct dirent *de;
38
39 if (!dir)
40 return 0;
41
42 while ((de = readdir(dir)) != NULL) {
43 char name[100];
44 unsigned char sha1[20];
45 int len = strlen(de->d_name);
46
47 switch (len) {
48 case 2:
49 if (de->d_name[1] != '.')
50 break;
51 case 1:
52 if (de->d_name[0] != '.')
53 break;
54 continue;
55 case 38:
56 sprintf(name, "%02x", i);
57 memcpy(name+2, de->d_name, len+1);
58 if (get_sha1_hex(name, sha1) < 0)
59 break;
60
61 /*
62 * Do we know about this object?
63 * It must have been reachable
64 */
65 if (lookup_object(sha1))
66 continue;
67
68 prune_object(path, de->d_name, sha1);
69 continue;
70 }
71 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
72 }
73 closedir(dir);
74 return 0;
75}
76
77static void prune_object_dir(const char *path)
78{
79 int i;
80 for (i = 0; i < 256; i++) {
81 static char dir[4096];
82 sprintf(dir, "%s/%02x", path, i);
83 prune_dir(i, dir);
84 }
85}
86
87static void process_blob(struct blob *blob,
88 struct object_array *p,
89 struct name_path *path,
90 const char *name)
91{
92 struct object *obj = &blob->object;
93
94 if (obj->flags & SEEN)
95 return;
96 obj->flags |= SEEN;
97 /* Nothing to do, really .. The blob lookup was the important part */
98}
99
100static void process_tree(struct tree *tree,
101 struct object_array *p,
102 struct name_path *path,
103 const char *name)
104{
105 struct object *obj = &tree->object;
106 struct tree_desc desc;
107 struct name_entry entry;
108 struct name_path me;
109
110 if (obj->flags & SEEN)
111 return;
112 obj->flags |= SEEN;
113 if (parse_tree(tree) < 0)
114 die("bad tree object %s", sha1_to_hex(obj->sha1));
115 name = xstrdup(name);
116 add_object(obj, p, path, name);
117 me.up = path;
118 me.elem = name;
119 me.elem_len = strlen(name);
120
121 desc.buf = tree->buffer;
122 desc.size = tree->size;
123
124 while (tree_entry(&desc, &entry)) {
125 if (S_ISDIR(entry.mode))
126 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
127 else
128 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
129 }
130 free(tree->buffer);
131 tree->buffer = NULL;
132}
133
134static void process_tag(struct tag *tag, struct object_array *p, const char *name)
135{
136 struct object *obj = &tag->object;
137 struct name_path me;
138
139 if (obj->flags & SEEN)
140 return;
141 obj->flags |= SEEN;
142
143 me.up = NULL;
144 me.elem = "tag:/";
145 me.elem_len = 5;
146
147 if (parse_tag(tag) < 0)
148 die("bad tag object %s", sha1_to_hex(obj->sha1));
149 add_object(tag->tagged, p, NULL, name);
150}
151
152static void walk_commit_list(struct rev_info *revs)
153{
154 int i;
155 struct commit *commit;
156 struct object_array objects = { 0, 0, NULL };
157
158 /* Walk all commits, process their trees */
159 while ((commit = get_revision(revs)) != NULL)
160 process_tree(commit->tree, &objects, NULL, "");
161
162 /* Then walk all the pending objects, recursively processing them too */
163 for (i = 0; i < revs->pending.nr; i++) {
164 struct object_array_entry *pending = revs->pending.objects + i;
165 struct object *obj = pending->item;
166 const char *name = pending->name;
167 if (obj->type == OBJ_TAG) {
168 process_tag((struct tag *) obj, &objects, name);
169 continue;
170 }
171 if (obj->type == OBJ_TREE) {
172 process_tree((struct tree *)obj, &objects, NULL, name);
173 continue;
174 }
175 if (obj->type == OBJ_BLOB) {
176 process_blob((struct blob *)obj, &objects, NULL, name);
177 continue;
178 }
179 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
180 }
181}
182
183static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
184{
185 struct object *object;
186 struct rev_info *revs = (struct rev_info *)cb_data;
187
188 object = parse_object(osha1);
189 if (object)
190 add_pending_object(revs, object, "");
191 object = parse_object(nsha1);
192 if (object)
193 add_pending_object(revs, object, "");
194 return 0;
195}
196
197static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
198{
199 struct object *object = parse_object(sha1);
200 struct rev_info *revs = (struct rev_info *)cb_data;
201
202 if (!object)
203 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
204 add_pending_object(revs, object, "");
205
206 return 0;
207}
208
209static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
210{
211 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
212 return 0;
213}
214
215static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
216{
217 struct tree *tree = lookup_tree(sha1);
218 add_pending_object(revs, &tree->object, "");
219}
220
221static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
222{
223 int i;
224
225 if (it->entry_count >= 0)
226 add_one_tree(it->sha1, revs);
227 for (i = 0; i < it->subtree_nr; i++)
228 add_cache_tree(it->down[i]->cache_tree, revs);
229}
230
231static void add_cache_refs(struct rev_info *revs)
232{
233 int i;
234
235 read_cache();
236 for (i = 0; i < active_nr; i++) {
237 lookup_blob(active_cache[i]->sha1);
238 /*
239 * We could add the blobs to the pending list, but quite
240 * frankly, we don't care. Once we've looked them up, and
241 * added them as objects, we've really done everything
242 * there is to do for a blob
243 */
244 }
245 if (active_cache_tree)
246 add_cache_tree(active_cache_tree, revs);
247}
248
249int cmd_prune(int argc, const char **argv, const char *prefix)
250{
251 int i;
252 struct rev_info revs;
253
254 for (i = 1; i < argc; i++) {
255 const char *arg = argv[i];
256 if (!strcmp(arg, "-n")) {
257 show_only = 1;
258 continue;
259 }
260 usage(prune_usage);
261 }
262
263 save_commit_buffer = 0;
264
265 /*
266 * Set up revision parsing, and mark us as being interested
267 * in all object types, not just commits.
268 */
269 init_revisions(&revs, prefix);
270 revs.tag_objects = 1;
271 revs.blob_objects = 1;
272 revs.tree_objects = 1;
273
274 /* Add all external refs */
275 for_each_ref(add_one_ref, &revs);
276
277 /* Add all refs from the index file */
278 add_cache_refs(&revs);
279
280 /* Add all reflog info from refs */
281 for_each_ref(add_one_reflog, &revs);
282
283 /*
284 * Set up the revision walk - this will move all commits
285 * from the pending list to the commit walking list.
286 */
287 prepare_revision_walk(&revs);
288
289 walk_commit_list(&revs);
290
291 prune_object_dir(get_object_directory());
292
293 sync();
294 prune_packed_objects(show_only);
295 return 0;
296}