]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-prune.c
builtin-prune: make file-scope static struct to an argument.
[thirdparty/git.git] / builtin-prune.c
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
13 static const char prune_usage[] = "git-prune [-n]";
14 static int show_only;
15
16 static 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
34 static 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
77 static 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
87 static 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
100 static 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
134 static 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
152 static 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
183 static 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
197 static 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 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
207
208 return 0;
209 }
210
211 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
212 {
213 struct tree *tree = lookup_tree(sha1);
214 add_pending_object(revs, &tree->object, "");
215 }
216
217 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
218 {
219 int i;
220
221 if (it->entry_count >= 0)
222 add_one_tree(it->sha1, revs);
223 for (i = 0; i < it->subtree_nr; i++)
224 add_cache_tree(it->down[i]->cache_tree, revs);
225 }
226
227 static void add_cache_refs(struct rev_info *revs)
228 {
229 int i;
230
231 read_cache();
232 for (i = 0; i < active_nr; i++) {
233 lookup_blob(active_cache[i]->sha1);
234 /*
235 * We could add the blobs to the pending list, but quite
236 * frankly, we don't care. Once we've looked them up, and
237 * added them as objects, we've really done everything
238 * there is to do for a blob
239 */
240 }
241 if (active_cache_tree)
242 add_cache_tree(active_cache_tree, revs);
243 }
244
245 int cmd_prune(int argc, const char **argv, const char *prefix)
246 {
247 int i;
248 struct rev_info revs;
249
250 for (i = 1; i < argc; i++) {
251 const char *arg = argv[i];
252 if (!strcmp(arg, "-n")) {
253 show_only = 1;
254 continue;
255 }
256 usage(prune_usage);
257 }
258
259 save_commit_buffer = 0;
260
261 /*
262 * Set up revision parsing, and mark us as being interested
263 * in all object types, not just commits.
264 */
265 init_revisions(&revs, prefix);
266 revs.tag_objects = 1;
267 revs.blob_objects = 1;
268 revs.tree_objects = 1;
269
270 /* Add all external refs, along with its reflog info */
271 for_each_ref(add_one_ref, &revs);
272
273 /* Add all refs from the index file */
274 add_cache_refs(&revs);
275
276 /*
277 * Set up the revision walk - this will move all commits
278 * from the pending list to the commit walking list.
279 */
280 prepare_revision_walk(&revs);
281
282 walk_commit_list(&revs);
283
284 prune_object_dir(get_object_directory());
285
286 sync();
287 prune_packed_objects(show_only);
288 return 0;
289 }