Revert "MinGW: Add missing file mode bit defines"
[git/dscho.git] / reachable.c
blob3fc6b1d320faad3a528db016a0d800ff1bdde4f0
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;
74 if (obj->flags & SEEN)
75 return;
76 obj->flags |= SEEN;
78 if (parse_tag(tag) < 0)
79 die("bad tag object %s", sha1_to_hex(obj->sha1));
80 if (tag->tagged)
81 add_object(tag->tagged, p, NULL, name);
84 static void walk_commit_list(struct rev_info *revs)
86 int i;
87 struct commit *commit;
88 struct object_array objects = OBJECT_ARRAY_INIT;
90 /* Walk all commits, process their trees */
91 while ((commit = get_revision(revs)) != NULL)
92 process_tree(commit->tree, &objects, NULL, "");
94 /* Then walk all the pending objects, recursively processing them too */
95 for (i = 0; i < revs->pending.nr; i++) {
96 struct object_array_entry *pending = revs->pending.objects + i;
97 struct object *obj = pending->item;
98 const char *name = pending->name;
99 if (obj->type == OBJ_TAG) {
100 process_tag((struct tag *) obj, &objects, name);
101 continue;
103 if (obj->type == OBJ_TREE) {
104 process_tree((struct tree *)obj, &objects, NULL, name);
105 continue;
107 if (obj->type == OBJ_BLOB) {
108 process_blob((struct blob *)obj, &objects, NULL, name);
109 continue;
111 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
115 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
116 const char *email, unsigned long timestamp, int tz,
117 const char *message, void *cb_data)
119 struct object *object;
120 struct rev_info *revs = (struct rev_info *)cb_data;
122 object = parse_object(osha1);
123 if (object)
124 add_pending_object(revs, object, "");
125 object = parse_object(nsha1);
126 if (object)
127 add_pending_object(revs, object, "");
128 return 0;
131 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
133 struct object *object = parse_object(sha1);
134 struct rev_info *revs = (struct rev_info *)cb_data;
136 if (!object)
137 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
138 add_pending_object(revs, object, "");
140 return 0;
143 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
145 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
146 return 0;
149 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
151 struct tree *tree = lookup_tree(sha1);
152 if (tree)
153 add_pending_object(revs, &tree->object, "");
156 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
158 int i;
160 if (it->entry_count >= 0)
161 add_one_tree(it->sha1, revs);
162 for (i = 0; i < it->subtree_nr; i++)
163 add_cache_tree(it->down[i]->cache_tree, revs);
166 static void add_cache_refs(struct rev_info *revs)
168 int i;
170 read_cache();
171 for (i = 0; i < active_nr; i++) {
173 * The index can contain blobs and GITLINKs, GITLINKs are hashes
174 * that don't actually point to objects in the repository, it's
175 * almost guaranteed that they are NOT blobs, so we don't call
176 * lookup_blob() on them, to avoid populating the hash table
177 * with invalid information
179 if (S_ISGITLINK(active_cache[i]->ce_mode))
180 continue;
182 lookup_blob(active_cache[i]->sha1);
184 * We could add the blobs to the pending list, but quite
185 * frankly, we don't care. Once we've looked them up, and
186 * added them as objects, we've really done everything
187 * there is to do for a blob
190 if (active_cache_tree)
191 add_cache_tree(active_cache_tree, revs);
194 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
197 * Set up revision parsing, and mark us as being interested
198 * in all object types, not just commits.
200 revs->tag_objects = 1;
201 revs->blob_objects = 1;
202 revs->tree_objects = 1;
204 /* Add all refs from the index file */
205 add_cache_refs(revs);
207 /* Add all external refs */
208 for_each_ref(add_one_ref, revs);
210 /* Add all reflog info */
211 if (mark_reflog)
212 for_each_reflog(add_one_reflog, revs);
215 * Set up the revision walk - this will move all commits
216 * from the pending list to the commit walking list.
218 if (prepare_revision_walk(revs))
219 die("revision walk setup failed");
220 walk_commit_list(revs);