Documentation: "git-checkout <tree> <path>" takes any tree-ish
[git/jnareb-git/bp-gitweb.git] / reachable.c
bloba6a334822a91f7325399740b0aa05e95f6fb4a70
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 (obj->flags & SEEN)
19 return;
20 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,
27 const char *name)
29 struct object *obj = &tree->object;
30 struct tree_desc desc;
31 struct name_entry entry;
32 struct name_path me;
34 if (obj->flags & SEEN)
35 return;
36 obj->flags |= SEEN;
37 if (parse_tree(tree) < 0)
38 die("bad tree object %s", sha1_to_hex(obj->sha1));
39 name = xstrdup(name);
40 add_object(obj, p, path, name);
41 me.up = path;
42 me.elem = 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);
51 else
52 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
54 free(tree->buffer);
55 tree->buffer = NULL;
58 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
60 struct object *obj = &tag->object;
61 struct name_path me;
63 if (obj->flags & SEEN)
64 return;
65 obj->flags |= SEEN;
67 me.up = NULL;
68 me.elem = "tag:/";
69 me.elem_len = 5;
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)
78 int i;
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);
93 continue;
95 if (obj->type == OBJ_TREE) {
96 process_tree((struct tree *)obj, &objects, NULL, name);
97 continue;
99 if (obj->type == OBJ_BLOB) {
100 process_blob((struct blob *)obj, &objects, NULL, name);
101 continue;
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);
115 if (object)
116 add_pending_object(revs, object, "");
117 object = parse_object(nsha1);
118 if (object)
119 add_pending_object(revs, object, "");
120 return 0;
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;
128 if (!object)
129 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
130 add_pending_object(revs, object, "");
132 return 0;
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);
138 return 0;
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)
149 int i;
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)
159 int i;
161 read_cache();
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 from refs */
192 if (mark_reflog)
193 for_each_ref(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);