11 #include "cache-tree.h"
13 static const char prune_usage[] = "git-prune [-n]";
15 static struct rev_info revs;
17 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
23 if (sha1_object_info(sha1, buf, NULL))
27 printf("%s %s\n", sha1_to_hex(sha1), type);
30 unlink(mkpath("%s/%s", path, filename));
35 static int prune_dir(int i, char *path)
37 DIR *dir = opendir(path);
43 while ((de = readdir(dir)) != NULL) {
45 unsigned char sha1[20];
46 int len = strlen(de->d_name);
50 if (de->d_name[1] != '.')
53 if (de->d_name[0] != '.')
57 sprintf(name, "%02x", i);
58 memcpy(name+2, de->d_name, len+1);
59 if (get_sha1_hex(name, sha1) < 0)
63 * Do we know about this object?
64 * It must have been reachable
66 if (lookup_object(sha1))
69 prune_object(path, de->d_name, sha1);
72 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
78 static void prune_object_dir(const char *path)
81 for (i = 0; i < 256; i++) {
82 static char dir[4096];
83 sprintf(dir, "%s/%02x", path, i);
88 static void process_blob(struct blob *blob,
89 struct object_array *p,
90 struct name_path *path,
93 struct object *obj = &blob->object;
95 if (obj->flags & SEEN)
98 /* Nothing to do, really .. The blob lookup was the important part */
101 static void process_tree(struct tree *tree,
102 struct object_array *p,
103 struct name_path *path,
106 struct object *obj = &tree->object;
107 struct tree_desc desc;
108 struct name_entry entry;
111 if (obj->flags & SEEN)
114 if (parse_tree(tree) < 0)
115 die("bad tree object %s", sha1_to_hex(obj->sha1));
116 name = xstrdup(name);
117 add_object(obj, p, path, name);
120 me.elem_len = strlen(name);
122 desc.buf = tree->buffer;
123 desc.size = tree->size;
125 while (tree_entry(&desc, &entry)) {
126 if (S_ISDIR(entry.mode))
127 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
129 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
135 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
137 struct object *obj = &tag->object;
140 if (obj->flags & SEEN)
148 if (parse_tag(tag) < 0)
149 die("bad tag object %s", sha1_to_hex(obj->sha1));
150 add_object(tag->tagged, p, NULL, name);
153 static void walk_commit_list(struct rev_info *revs)
156 struct commit *commit;
157 struct object_array objects = { 0, 0, NULL };
159 /* Walk all commits, process their trees */
160 while ((commit = get_revision(revs)) != NULL)
161 process_tree(commit->tree, &objects, NULL, "");
163 /* Then walk all the pending objects, recursively processing them too */
164 for (i = 0; i < revs->pending.nr; i++) {
165 struct object_array_entry *pending = revs->pending.objects + i;
166 struct object *obj = pending->item;
167 const char *name = pending->name;
168 if (obj->type == OBJ_TAG) {
169 process_tag((struct tag *) obj, &objects, name);
172 if (obj->type == OBJ_TREE) {
173 process_tree((struct tree *)obj, &objects, NULL, name);
176 if (obj->type == OBJ_BLOB) {
177 process_blob((struct blob *)obj, &objects, NULL, name);
180 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
184 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
186 struct object *object;
188 object = parse_object(osha1);
190 add_pending_object(&revs, object, "");
191 object = parse_object(nsha1);
193 add_pending_object(&revs, object, "");
197 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
199 struct object *object = parse_object(sha1);
201 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
202 add_pending_object(&revs, object, "");
204 for_each_reflog_ent(path, add_one_reflog_ent, NULL);
209 static void add_one_tree(const unsigned char *sha1)
211 struct tree *tree = lookup_tree(sha1);
212 add_pending_object(&revs, &tree->object, "");
215 static void add_cache_tree(struct cache_tree *it)
219 if (it->entry_count >= 0)
220 add_one_tree(it->sha1);
221 for (i = 0; i < it->subtree_nr; i++)
222 add_cache_tree(it->down[i]->cache_tree);
225 static void add_cache_refs(void)
230 for (i = 0; i < active_nr; i++) {
231 lookup_blob(active_cache[i]->sha1);
233 * We could add the blobs to the pending list, but quite
234 * frankly, we don't care. Once we've looked them up, and
235 * added them as objects, we've really done everything
236 * there is to do for a blob
239 if (active_cache_tree)
240 add_cache_tree(active_cache_tree);
243 int cmd_prune(int argc, const char **argv, const char *prefix)
247 for (i = 1; i < argc; i++) {
248 const char *arg = argv[i];
249 if (!strcmp(arg, "-n")) {
257 * Set up revision parsing, and mark us as being interested
258 * in all object types, not just commits.
260 init_revisions(&revs, prefix);
261 revs.tag_objects = 1;
262 revs.blob_objects = 1;
263 revs.tree_objects = 1;
265 /* Add all external refs */
266 for_each_ref(add_one_ref, NULL);
268 /* Add all refs from the index file */
272 * Set up the revision walk - this will move all commits
273 * from the pending list to the commit walking list.
275 prepare_revision_walk(&revs);
277 walk_commit_list(&revs);
279 prune_object_dir(get_object_directory());
282 prune_packed_objects(show_only);