process_tag: handle tag->tagged == NULL
[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
8d2244ba
AP
24static void process_gitlink(const unsigned char *sha1,
25 struct object_array *p,
26 struct name_path *path,
27 const char *name)
28{
29 /* I don't think we want to recurse into this, really. */
30}
31
94421474
JH
32static void process_tree(struct tree *tree,
33 struct object_array *p,
34 struct name_path *path,
35 const char *name)
36{
37 struct object *obj = &tree->object;
38 struct tree_desc desc;
39 struct name_entry entry;
40 struct name_path me;
41
42 if (obj->flags & SEEN)
43 return;
44 obj->flags |= SEEN;
45 if (parse_tree(tree) < 0)
46 die("bad tree object %s", sha1_to_hex(obj->sha1));
47 name = xstrdup(name);
48 add_object(obj, p, path, name);
49 me.up = path;
50 me.elem = name;
51 me.elem_len = strlen(name);
52
6fda5e51 53 init_tree_desc(&desc, tree->buffer, tree->size);
94421474
JH
54
55 while (tree_entry(&desc, &entry)) {
56 if (S_ISDIR(entry.mode))
57 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
b941ffac 58 else if (S_ISGITLINK(entry.mode))
8d2244ba 59 process_gitlink(entry.sha1, p, &me, entry.path);
94421474
JH
60 else
61 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
62 }
63 free(tree->buffer);
64 tree->buffer = NULL;
65}
66
67static void process_tag(struct tag *tag, struct object_array *p, const char *name)
68{
69 struct object *obj = &tag->object;
70 struct name_path me;
71
72 if (obj->flags & SEEN)
73 return;
74 obj->flags |= SEEN;
75
76 me.up = NULL;
77 me.elem = "tag:/";
78 me.elem_len = 5;
79
80 if (parse_tag(tag) < 0)
81 die("bad tag object %s", sha1_to_hex(obj->sha1));
cc369347
MK
82 if (tag->tagged)
83 add_object(tag->tagged, p, NULL, name);
94421474
JH
84}
85
86static void walk_commit_list(struct rev_info *revs)
87{
88 int i;
89 struct commit *commit;
90 struct object_array objects = { 0, 0, NULL };
91
92 /* Walk all commits, process their trees */
93 while ((commit = get_revision(revs)) != NULL)
94 process_tree(commit->tree, &objects, NULL, "");
95
96 /* Then walk all the pending objects, recursively processing them too */
97 for (i = 0; i < revs->pending.nr; i++) {
98 struct object_array_entry *pending = revs->pending.objects + i;
99 struct object *obj = pending->item;
100 const char *name = pending->name;
101 if (obj->type == OBJ_TAG) {
102 process_tag((struct tag *) obj, &objects, name);
103 continue;
104 }
105 if (obj->type == OBJ_TREE) {
106 process_tree((struct tree *)obj, &objects, NULL, name);
107 continue;
108 }
109 if (obj->type == OBJ_BLOB) {
110 process_blob((struct blob *)obj, &objects, NULL, name);
111 continue;
112 }
113 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
114 }
115}
116
883d60fa
JS
117static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
118 const char *email, unsigned long timestamp, int tz,
119 const char *message, void *cb_data)
94421474
JH
120{
121 struct object *object;
122 struct rev_info *revs = (struct rev_info *)cb_data;
123
124 object = parse_object(osha1);
125 if (object)
126 add_pending_object(revs, object, "");
127 object = parse_object(nsha1);
128 if (object)
129 add_pending_object(revs, object, "");
130 return 0;
131}
132
133static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
134{
135 struct object *object = parse_object(sha1);
136 struct rev_info *revs = (struct rev_info *)cb_data;
137
138 if (!object)
139 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
140 add_pending_object(revs, object, "");
141
142 return 0;
143}
144
145static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
146{
147 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
148 return 0;
149}
150
151static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
152{
153 struct tree *tree = lookup_tree(sha1);
c3406635
MK
154 if (tree)
155 add_pending_object(revs, &tree->object, "");
94421474
JH
156}
157
158static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
159{
160 int i;
161
162 if (it->entry_count >= 0)
163 add_one_tree(it->sha1, revs);
164 for (i = 0; i < it->subtree_nr; i++)
165 add_cache_tree(it->down[i]->cache_tree, revs);
166}
167
168static void add_cache_refs(struct rev_info *revs)
169{
170 int i;
171
172 read_cache();
173 for (i = 0; i < active_nr; i++) {
8d2244ba
AP
174 /*
175 * The index can contain blobs and GITLINKs, GITLINKs are hashes
176 * that don't actually point to objects in the repository, it's
177 * almost guaranteed that they are NOT blobs, so we don't call
178 * lookup_blob() on them, to avoid populating the hash table
179 * with invalid information
180 */
b941ffac 181 if (S_ISGITLINK(ntohl(active_cache[i]->ce_mode)))
8d2244ba
AP
182 continue;
183
94421474
JH
184 lookup_blob(active_cache[i]->sha1);
185 /*
186 * We could add the blobs to the pending list, but quite
187 * frankly, we don't care. Once we've looked them up, and
188 * added them as objects, we've really done everything
189 * there is to do for a blob
190 */
191 }
192 if (active_cache_tree)
193 add_cache_tree(active_cache_tree, revs);
194}
195
196void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
197{
198 /*
199 * Set up revision parsing, and mark us as being interested
200 * in all object types, not just commits.
201 */
202 revs->tag_objects = 1;
203 revs->blob_objects = 1;
204 revs->tree_objects = 1;
205
206 /* Add all refs from the index file */
207 add_cache_refs(revs);
208
209 /* Add all external refs */
210 for_each_ref(add_one_ref, revs);
211
eb8381c8 212 /* Add all reflog info */
94421474 213 if (mark_reflog)
eb8381c8 214 for_each_reflog(add_one_reflog, revs);
94421474
JH
215
216 /*
217 * Set up the revision walk - this will move all commits
218 * from the pending list to the commit walking list.
219 */
220 prepare_revision_walk(revs);
221 walk_commit_list(revs);
222}