Initialize tree descriptors with a helper function rather than by hand.
[git/git.git] / reachable.c
CommitLineData
94421474
JH
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
11static 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
24static 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
6fda5e51 45 init_tree_desc(&desc, tree->buffer, tree->size);
94421474
JH
46
47 while (tree_entry(&desc, &entry)) {
48 if (S_ISDIR(entry.mode))
49 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
50 else
51 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
52 }
53 free(tree->buffer);
54 tree->buffer = NULL;
55}
56
57static void process_tag(struct tag *tag, struct object_array *p, const char *name)
58{
59 struct object *obj = &tag->object;
60 struct name_path me;
61
62 if (obj->flags & SEEN)
63 return;
64 obj->flags |= SEEN;
65
66 me.up = NULL;
67 me.elem = "tag:/";
68 me.elem_len = 5;
69
70 if (parse_tag(tag) < 0)
71 die("bad tag object %s", sha1_to_hex(obj->sha1));
72 add_object(tag->tagged, p, NULL, name);
73}
74
75static void walk_commit_list(struct rev_info *revs)
76{
77 int i;
78 struct commit *commit;
79 struct object_array objects = { 0, 0, NULL };
80
81 /* Walk all commits, process their trees */
82 while ((commit = get_revision(revs)) != NULL)
83 process_tree(commit->tree, &objects, NULL, "");
84
85 /* Then walk all the pending objects, recursively processing them too */
86 for (i = 0; i < revs->pending.nr; i++) {
87 struct object_array_entry *pending = revs->pending.objects + i;
88 struct object *obj = pending->item;
89 const char *name = pending->name;
90 if (obj->type == OBJ_TAG) {
91 process_tag((struct tag *) obj, &objects, name);
92 continue;
93 }
94 if (obj->type == OBJ_TREE) {
95 process_tree((struct tree *)obj, &objects, NULL, name);
96 continue;
97 }
98 if (obj->type == OBJ_BLOB) {
99 process_blob((struct blob *)obj, &objects, NULL, name);
100 continue;
101 }
102 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
103 }
104}
105
883d60fa
JS
106static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
107 const char *email, unsigned long timestamp, int tz,
108 const char *message, void *cb_data)
94421474
JH
109{
110 struct object *object;
111 struct rev_info *revs = (struct rev_info *)cb_data;
112
113 object = parse_object(osha1);
114 if (object)
115 add_pending_object(revs, object, "");
116 object = parse_object(nsha1);
117 if (object)
118 add_pending_object(revs, object, "");
119 return 0;
120}
121
122static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
123{
124 struct object *object = parse_object(sha1);
125 struct rev_info *revs = (struct rev_info *)cb_data;
126
127 if (!object)
128 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
129 add_pending_object(revs, object, "");
130
131 return 0;
132}
133
134static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
135{
136 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
137 return 0;
138}
139
140static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
141{
142 struct tree *tree = lookup_tree(sha1);
143 add_pending_object(revs, &tree->object, "");
144}
145
146static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
147{
148 int i;
149
150 if (it->entry_count >= 0)
151 add_one_tree(it->sha1, revs);
152 for (i = 0; i < it->subtree_nr; i++)
153 add_cache_tree(it->down[i]->cache_tree, revs);
154}
155
156static void add_cache_refs(struct rev_info *revs)
157{
158 int i;
159
160 read_cache();
161 for (i = 0; i < active_nr; i++) {
162 lookup_blob(active_cache[i]->sha1);
163 /*
164 * We could add the blobs to the pending list, but quite
165 * frankly, we don't care. Once we've looked them up, and
166 * added them as objects, we've really done everything
167 * there is to do for a blob
168 */
169 }
170 if (active_cache_tree)
171 add_cache_tree(active_cache_tree, revs);
172}
173
174void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
175{
176 /*
177 * Set up revision parsing, and mark us as being interested
178 * in all object types, not just commits.
179 */
180 revs->tag_objects = 1;
181 revs->blob_objects = 1;
182 revs->tree_objects = 1;
183
184 /* Add all refs from the index file */
185 add_cache_refs(revs);
186
187 /* Add all external refs */
188 for_each_ref(add_one_ref, revs);
189
eb8381c8 190 /* Add all reflog info */
94421474 191 if (mark_reflog)
eb8381c8 192 for_each_reflog(add_one_reflog, revs);
94421474
JH
193
194 /*
195 * Set up the revision walk - this will move all commits
196 * from the pending list to the commit walking list.
197 */
198 prepare_revision_walk(revs);
199 walk_commit_list(revs);
200}