9 #include "cache-tree.h"
11 static void process_blob(struct blob *blob,
12 struct object_array *p,
13 struct name_path *path,
16 struct object *obj = &blob->object;
18 if (obj->flags & SEEN)
21 /* Nothing to do, really .. The blob lookup was the important part */
24 static void process_tree(struct tree *tree,
25 struct object_array *p,
26 struct name_path *path,
29 struct object *obj = &tree->object;
30 struct tree_desc desc;
31 struct name_entry entry;
34 if (obj->flags & SEEN)
37 if (parse_tree(tree) < 0)
38 die("bad tree object %s", sha1_to_hex(obj->sha1));
40 add_object(obj, p, path, name);
43 me.elem_len = strlen(name);
45 desc.buf = tree->buffer;
46 desc.size = tree->size;
48 while (tree_entry(&desc, &entry)) {
49 if (S_ISDIR(entry.mode))
50 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
52 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
58 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
60 struct object *obj = &tag->object;
63 if (obj->flags & SEEN)
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);
76 static void walk_commit_list(struct rev_info *revs)
79 struct commit *commit;
80 struct object_array objects = { 0, 0, NULL };
82 /* Walk all commits, process their trees */
83 while ((commit = get_revision(revs)) != NULL)
84 process_tree(commit->tree, &objects, NULL, "");
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);
95 if (obj->type == OBJ_TREE) {
96 process_tree((struct tree *)obj, &objects, NULL, name);
99 if (obj->type == OBJ_BLOB) {
100 process_blob((struct blob *)obj, &objects, NULL, name);
103 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
107 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
108 const char *email, unsigned long timestamp, int tz,
109 const char *message, void *cb_data)
111 struct object *object;
112 struct rev_info *revs = (struct rev_info *)cb_data;
114 object = parse_object(osha1);
116 add_pending_object(revs, object, "");
117 object = parse_object(nsha1);
119 add_pending_object(revs, object, "");
123 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
125 struct object *object = parse_object(sha1);
126 struct rev_info *revs = (struct rev_info *)cb_data;
129 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
130 add_pending_object(revs, object, "");
135 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
137 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
141 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
143 struct tree *tree = lookup_tree(sha1);
144 add_pending_object(revs, &tree->object, "");
147 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
151 if (it->entry_count >= 0)
152 add_one_tree(it->sha1, revs);
153 for (i = 0; i < it->subtree_nr; i++)
154 add_cache_tree(it->down[i]->cache_tree, revs);
157 static void add_cache_refs(struct rev_info *revs)
162 for (i = 0; i < active_nr; i++) {
163 lookup_blob(active_cache[i]->sha1);
165 * We could add the blobs to the pending list, but quite
166 * frankly, we don't care. Once we've looked them up, and
167 * added them as objects, we've really done everything
168 * there is to do for a blob
171 if (active_cache_tree)
172 add_cache_tree(active_cache_tree, revs);
175 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
178 * Set up revision parsing, and mark us as being interested
179 * in all object types, not just commits.
181 revs->tag_objects = 1;
182 revs->blob_objects = 1;
183 revs->tree_objects = 1;
185 /* Add all refs from the index file */
186 add_cache_refs(revs);
188 /* Add all external refs */
189 for_each_ref(add_one_ref, revs);
191 /* Add all reflog info */
193 for_each_reflog(add_one_reflog, revs);
196 * Set up the revision walk - this will move all commits
197 * from the pending list to the commit walking list.
199 prepare_revision_walk(revs);
200 walk_commit_list(revs);