Move traversal of reachable objects into a separate library.
This moves major part of builtin-prune into a separate file, reachable.c. It is used to mark the objects that are reachable from refs, and optionally from reflogs. The patch looks very large, but if you look at it with diff -C, which this message is formatted in, most of them are copied lines and there are very little additions. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 6, 2007 at 02:16 UTC
94421474e068c2f0a7bef3d658216a0f1e75b906
4 files changed
+208
-193
Makefile
+1
@@ -251,6 +251,7 @@ LIB_OBJS = \
251
interpolate.o \
252
lockfile.o \
253
object.o pack-check.o patch-delta.o path.o pkt-line.o sideband.o \
254
+ reachable.o \
255
quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
256
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
257
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
builtin-prune.c
+2
-193
@@ -1,14 +1,9 @@
1
#include "cache.h"
2
-#include "refs.h"
3
-#include "tag.h"
2
#include "commit.h"
5
-#include "tree.h"
6
-#include "blob.h"
7
-#include "tree-walk.h"
3
#include "diff.h"
4
#include "revision.h"
5
#include "builtin.h"
11
-#include "cache-tree.h"
6
+#include "reachable.h"
7
8
static const char prune_usage[] = "git-prune [-n]";
9
static int show_only;
@@ -84,168 +79,6 @@ static void prune_object_dir(const char *path)
79
}
80
}
81
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
- return 0;
207
-}
208
-
209
-static 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
-
215
-static 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
-
221
-static 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
-
231
-static 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
-
82
int cmd_prune(int argc, const char **argv, const char *prefix)
83
{
84
int i;
@@ -261,32 +94,8 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
94
}
95
96
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
- */
97
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);
98
+ mark_reachable_objects(&revs, 1);
99
100
prune_object_dir(get_object_directory());
101
reachable.c
new
+199
@@ -0,0 +1,199 @@
1
+#include "cache.h"
2
+#include "refs.h"
3
+#include "tag.h"
4
+#include "commit.h"
5
+#include "blob.h"
6
+#include "diff.h"
7
+#include "revision.h"
8
+#include "reachable.h"
9
+#include "cache-tree.h"
10
+
11
+static void process_blob(struct blob *blob,
12
+ struct object_array *p,
13
+ struct name_path *path,
14
+ const char *name)
15
+{
16
+ struct object *obj = &blob->object;
17
+
18
+ if (obj->flags & SEEN)
19
+ return;
20
+ obj->flags |= SEEN;
21
+ /* Nothing to do, really .. The blob lookup was the important part */
22
+}
23
+
24
+static void process_tree(struct tree *tree,
25
+ struct object_array *p,
26
+ struct name_path *path,
27
+ const char *name)
28
+{
29
+ struct object *obj = &tree->object;
30
+ struct tree_desc desc;
31
+ struct name_entry entry;
32
+ struct name_path me;
33
+
34
+ if (obj->flags & SEEN)
35
+ return;
36
+ obj->flags |= SEEN;
37
+ if (parse_tree(tree) < 0)
38
+ die("bad tree object %s", sha1_to_hex(obj->sha1));
39
+ name = xstrdup(name);
40
+ add_object(obj, p, path, name);
41
+ me.up = path;
42
+ me.elem = name;
43
+ me.elem_len = strlen(name);
44
+
45
+ desc.buf = tree->buffer;
46
+ desc.size = tree->size;
47
+
48
+ while (tree_entry(&desc, &entry)) {
49
+ if (S_ISDIR(entry.mode))
50
+ process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
51
+ else
52
+ process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
53
+ }
54
+ free(tree->buffer);
55
+ tree->buffer = NULL;
56
+}
57
+
58
+static void process_tag(struct tag *tag, struct object_array *p, const char *name)
59
+{
60
+ struct object *obj = &tag->object;
61
+ struct name_path me;
62
+
63
+ if (obj->flags & SEEN)
64
+ return;
65
+ obj->flags |= SEEN;
66
+
67
+ me.up = NULL;
68
+ me.elem = "tag:/";
69
+ me.elem_len = 5;
70
+
71
+ if (parse_tag(tag) < 0)
72
+ die("bad tag object %s", sha1_to_hex(obj->sha1));
73
+ add_object(tag->tagged, p, NULL, name);
74
+}
75
+
76
+static void walk_commit_list(struct rev_info *revs)
77
+{
78
+ int i;
79
+ struct commit *commit;
80
+ struct object_array objects = { 0, 0, NULL };
81
+
82
+ /* Walk all commits, process their trees */
83
+ while ((commit = get_revision(revs)) != NULL)
84
+ process_tree(commit->tree, &objects, NULL, "");
85
+
86
+ /* Then walk all the pending objects, recursively processing them too */
87
+ for (i = 0; i < revs->pending.nr; i++) {
88
+ struct object_array_entry *pending = revs->pending.objects + i;
89
+ struct object *obj = pending->item;
90
+ const char *name = pending->name;
91
+ if (obj->type == OBJ_TAG) {
92
+ process_tag((struct tag *) obj, &objects, name);
93
+ continue;
94
+ }
95
+ if (obj->type == OBJ_TREE) {
96
+ process_tree((struct tree *)obj, &objects, NULL, name);
97
+ continue;
98
+ }
99
+ if (obj->type == OBJ_BLOB) {
100
+ process_blob((struct blob *)obj, &objects, NULL, name);
101
+ continue;
102
+ }
103
+ die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
104
+ }
105
+}
106
+
107
+static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
108
+{
109
+ struct object *object;
110
+ struct rev_info *revs = (struct rev_info *)cb_data;
111
+
112
+ object = parse_object(osha1);
113
+ if (object)
114
+ add_pending_object(revs, object, "");
115
+ object = parse_object(nsha1);
116
+ if (object)
117
+ add_pending_object(revs, object, "");
118
+ return 0;
119
+}
120
+
121
+static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
122
+{
123
+ struct object *object = parse_object(sha1);
124
+ struct rev_info *revs = (struct rev_info *)cb_data;
125
+
126
+ if (!object)
127
+ die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
128
+ add_pending_object(revs, object, "");
129
+
130
+ return 0;
131
+}
132
+
133
+static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
134
+{
135
+ for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
136
+ return 0;
137
+}
138
+
139
+static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
140
+{
141
+ struct tree *tree = lookup_tree(sha1);
142
+ add_pending_object(revs, &tree->object, "");
143
+}
144
+
145
+static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
146
+{
147
+ int i;
148
+
149
+ if (it->entry_count >= 0)
150
+ add_one_tree(it->sha1, revs);
151
+ for (i = 0; i < it->subtree_nr; i++)
152
+ add_cache_tree(it->down[i]->cache_tree, revs);
153
+}
154
+
155
+static void add_cache_refs(struct rev_info *revs)
156
+{
157
+ int i;
158
+
159
+ read_cache();
160
+ for (i = 0; i < active_nr; i++) {
161
+ lookup_blob(active_cache[i]->sha1);
162
+ /*
163
+ * We could add the blobs to the pending list, but quite
164
+ * frankly, we don't care. Once we've looked them up, and
165
+ * added them as objects, we've really done everything
166
+ * there is to do for a blob
167
+ */
168
+ }
169
+ if (active_cache_tree)
170
+ add_cache_tree(active_cache_tree, revs);
171
+}
172
+
173
+void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
174
+{
175
+ /*
176
+ * Set up revision parsing, and mark us as being interested
177
+ * in all object types, not just commits.
178
+ */
179
+ revs->tag_objects = 1;
180
+ revs->blob_objects = 1;
181
+ revs->tree_objects = 1;
182
+
183
+ /* Add all refs from the index file */
184
+ add_cache_refs(revs);
185
+
186
+ /* Add all external refs */
187
+ for_each_ref(add_one_ref, revs);
188
+
189
+ /* Add all reflog info from refs */
190
+ if (mark_reflog)
191
+ for_each_ref(add_one_reflog, revs);
192
+
193
+ /*
194
+ * Set up the revision walk - this will move all commits
195
+ * from the pending list to the commit walking list.
196
+ */
197
+ prepare_revision_walk(revs);
198
+ walk_commit_list(revs);
199
+}
reachable.h
new
+6
@@ -0,0 +1,6 @@
1
+#ifndef REACHEABLE_H
2
+#define REACHEABLE_H
3
+
4
+extern void mark_reachable_objects(struct rev_info *revs, int mark_reflog);
5
+
6
+#endif