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