git-send-email: Add --thread option
[git/aroben.git] / reachable.c
blobff3dd34962ec69320a67a4823b844755ebfe0e7d
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 init_tree_desc(&desc, tree->buffer, tree->size);
47 while (tree_entry(&desc, &entry)) {
48 if (S_ISDIR(entry.mode))
49 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
50 else
51 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
53 free(tree->buffer);
54 tree->buffer = NULL;
57 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
59 struct object *obj = &tag->object;
60 struct name_path me;
62 if (obj->flags & SEEN)
63 return;
64 obj->flags |= SEEN;
66 me.up = NULL;
67 me.elem = "tag:/";
68 me.elem_len = 5;
70 if (parse_tag(tag) < 0)
71 die("bad tag object %s", sha1_to_hex(obj->sha1));
72 add_object(tag->tagged, p, NULL, name);
75 static void walk_commit_list(struct rev_info *revs)
77 int i;
78 struct commit *commit;
79 struct object_array objects = { 0, 0, NULL };
81 /* Walk all commits, process their trees */
82 while ((commit = get_revision(revs)) != NULL)
83 process_tree(commit->tree, &objects, NULL, "");
85 /* Then walk all the pending objects, recursively processing them too */
86 for (i = 0; i < revs->pending.nr; i++) {
87 struct object_array_entry *pending = revs->pending.objects + i;
88 struct object *obj = pending->item;
89 const char *name = pending->name;
90 if (obj->type == OBJ_TAG) {
91 process_tag((struct tag *) obj, &objects, name);
92 continue;
94 if (obj->type == OBJ_TREE) {
95 process_tree((struct tree *)obj, &objects, NULL, name);
96 continue;
98 if (obj->type == OBJ_BLOB) {
99 process_blob((struct blob *)obj, &objects, NULL, name);
100 continue;
102 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
106 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
107 const char *email, unsigned long timestamp, int tz,
108 const char *message, void *cb_data)
110 struct object *object;
111 struct rev_info *revs = (struct rev_info *)cb_data;
113 object = parse_object(osha1);
114 if (object)
115 add_pending_object(revs, object, "");
116 object = parse_object(nsha1);
117 if (object)
118 add_pending_object(revs, object, "");
119 return 0;
122 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
124 struct object *object = parse_object(sha1);
125 struct rev_info *revs = (struct rev_info *)cb_data;
127 if (!object)
128 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
129 add_pending_object(revs, object, "");
131 return 0;
134 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
136 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
137 return 0;
140 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
142 struct tree *tree = lookup_tree(sha1);
143 add_pending_object(revs, &tree->object, "");
146 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
148 int i;
150 if (it->entry_count >= 0)
151 add_one_tree(it->sha1, revs);
152 for (i = 0; i < it->subtree_nr; i++)
153 add_cache_tree(it->down[i]->cache_tree, revs);
156 static void add_cache_refs(struct rev_info *revs)
158 int i;
160 read_cache();
161 for (i = 0; i < active_nr; i++) {
162 lookup_blob(active_cache[i]->sha1);
164 * We could add the blobs to the pending list, but quite
165 * frankly, we don't care. Once we've looked them up, and
166 * added them as objects, we've really done everything
167 * there is to do for a blob
170 if (active_cache_tree)
171 add_cache_tree(active_cache_tree, revs);
174 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
177 * Set up revision parsing, and mark us as being interested
178 * in all object types, not just commits.
180 revs->tag_objects = 1;
181 revs->blob_objects = 1;
182 revs->tree_objects = 1;
184 /* Add all refs from the index file */
185 add_cache_refs(revs);
187 /* Add all external refs */
188 for_each_ref(add_one_ref, revs);
190 /* Add all reflog info */
191 if (mark_reflog)
192 for_each_reflog(add_one_reflog, revs);
195 * Set up the revision walk - this will move all commits
196 * from the pending list to the commit walking list.
198 prepare_revision_walk(revs);
199 walk_commit_list(revs);