Merge branch 'jn/apply-filename-with-sp'
[git/jrn.git] / reachable.c
bloba03fabf060081e9fa5e0d753cd0d9cf4bca5062e
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"
11 static void process_blob(struct blob *blob,
12 struct object_array *p,
13 struct name_path *path,
14 const char *name)
16 struct object *obj = &blob->object;
18 if (!blob)
19 die("bad blob object");
20 if (obj->flags & SEEN)
21 return;
22 obj->flags |= SEEN;
23 /* Nothing to do, really .. The blob lookup was the important part */
26 static void process_gitlink(const unsigned char *sha1,
27 struct object_array *p,
28 struct name_path *path,
29 const char *name)
31 /* I don't think we want to recurse into this, really. */
34 static void process_tree(struct tree *tree,
35 struct object_array *p,
36 struct name_path *path,
37 const char *name)
39 struct object *obj = &tree->object;
40 struct tree_desc desc;
41 struct name_entry entry;
42 struct name_path me;
44 if (!tree)
45 die("bad tree object");
46 if (obj->flags & SEEN)
47 return;
48 obj->flags |= SEEN;
49 if (parse_tree(tree) < 0)
50 die("bad tree object %s", sha1_to_hex(obj->sha1));
51 add_object(obj, p, path, name);
52 me.up = path;
53 me.elem = name;
54 me.elem_len = strlen(name);
56 init_tree_desc(&desc, tree->buffer, tree->size);
58 while (tree_entry(&desc, &entry)) {
59 if (S_ISDIR(entry.mode))
60 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
61 else if (S_ISGITLINK(entry.mode))
62 process_gitlink(entry.sha1, p, &me, entry.path);
63 else
64 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
66 free(tree->buffer);
67 tree->buffer = NULL;
70 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
72 struct object *obj = &tag->object;
73 struct name_path me;
75 if (obj->flags & SEEN)
76 return;
77 obj->flags |= SEEN;
79 me.up = NULL;
80 me.elem = "tag:/";
81 me.elem_len = 5;
83 if (parse_tag(tag) < 0)
84 die("bad tag object %s", sha1_to_hex(obj->sha1));
85 if (tag->tagged)
86 add_object(tag->tagged, p, NULL, name);
89 static void walk_commit_list(struct rev_info *revs)
91 int i;
92 struct commit *commit;
93 struct object_array objects = OBJECT_ARRAY_INIT;
95 /* Walk all commits, process their trees */
96 while ((commit = get_revision(revs)) != NULL)
97 process_tree(commit->tree, &objects, NULL, "");
99 /* Then walk all the pending objects, recursively processing them too */
100 for (i = 0; i < revs->pending.nr; i++) {
101 struct object_array_entry *pending = revs->pending.objects + i;
102 struct object *obj = pending->item;
103 const char *name = pending->name;
104 if (obj->type == OBJ_TAG) {
105 process_tag((struct tag *) obj, &objects, name);
106 continue;
108 if (obj->type == OBJ_TREE) {
109 process_tree((struct tree *)obj, &objects, NULL, name);
110 continue;
112 if (obj->type == OBJ_BLOB) {
113 process_blob((struct blob *)obj, &objects, NULL, name);
114 continue;
116 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
120 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
121 const char *email, unsigned long timestamp, int tz,
122 const char *message, void *cb_data)
124 struct object *object;
125 struct rev_info *revs = (struct rev_info *)cb_data;
127 object = parse_object(osha1);
128 if (object)
129 add_pending_object(revs, object, "");
130 object = parse_object(nsha1);
131 if (object)
132 add_pending_object(revs, object, "");
133 return 0;
136 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
138 struct object *object = parse_object(sha1);
139 struct rev_info *revs = (struct rev_info *)cb_data;
141 if (!object)
142 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
143 add_pending_object(revs, object, "");
145 return 0;
148 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
150 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
151 return 0;
154 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
156 struct tree *tree = lookup_tree(sha1);
157 if (tree)
158 add_pending_object(revs, &tree->object, "");
161 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
163 int i;
165 if (it->entry_count >= 0)
166 add_one_tree(it->sha1, revs);
167 for (i = 0; i < it->subtree_nr; i++)
168 add_cache_tree(it->down[i]->cache_tree, revs);
171 static void add_cache_refs(struct rev_info *revs)
173 int i;
175 read_cache();
176 for (i = 0; i < active_nr; i++) {
178 * The index can contain blobs and GITLINKs, GITLINKs are hashes
179 * that don't actually point to objects in the repository, it's
180 * almost guaranteed that they are NOT blobs, so we don't call
181 * lookup_blob() on them, to avoid populating the hash table
182 * with invalid information
184 if (S_ISGITLINK(active_cache[i]->ce_mode))
185 continue;
187 lookup_blob(active_cache[i]->sha1);
189 * We could add the blobs to the pending list, but quite
190 * frankly, we don't care. Once we've looked them up, and
191 * added them as objects, we've really done everything
192 * there is to do for a blob
195 if (active_cache_tree)
196 add_cache_tree(active_cache_tree, revs);
199 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
202 * Set up revision parsing, and mark us as being interested
203 * in all object types, not just commits.
205 revs->tag_objects = 1;
206 revs->blob_objects = 1;
207 revs->tree_objects = 1;
209 /* Add all refs from the index file */
210 add_cache_refs(revs);
212 /* Add all external refs */
213 for_each_ref(add_one_ref, revs);
215 /* Add all reflog info */
216 if (mark_reflog)
217 for_each_reflog(add_one_reflog, revs);
220 * Set up the revision walk - this will move all commits
221 * from the pending list to the commit walking list.
223 if (prepare_revision_walk(revs))
224 die("revision walk setup failed");
225 walk_commit_list(revs);