Code

process_tag: handle tag->tagged == NULL
[git.git] / reachable.c
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)
15 {
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 */
22 }
24 static void process_gitlink(const unsigned char *sha1,
25                             struct object_array *p,
26                             struct name_path *path,
27                             const char *name)
28 {
29         /* I don't think we want to recurse into this, really. */
30 }
32 static void process_tree(struct tree *tree,
33                          struct object_array *p,
34                          struct name_path *path,
35                          const char *name)
36 {
37         struct object *obj = &tree->object;
38         struct tree_desc desc;
39         struct name_entry entry;
40         struct name_path me;
42         if (obj->flags & SEEN)
43                 return;
44         obj->flags |= SEEN;
45         if (parse_tree(tree) < 0)
46                 die("bad tree object %s", sha1_to_hex(obj->sha1));
47         name = xstrdup(name);
48         add_object(obj, p, path, name);
49         me.up = path;
50         me.elem = name;
51         me.elem_len = strlen(name);
53         init_tree_desc(&desc, tree->buffer, tree->size);
55         while (tree_entry(&desc, &entry)) {
56                 if (S_ISDIR(entry.mode))
57                         process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
58                 else if (S_ISGITLINK(entry.mode))
59                         process_gitlink(entry.sha1, p, &me, entry.path);
60                 else
61                         process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
62         }
63         free(tree->buffer);
64         tree->buffer = NULL;
65 }
67 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
68 {
69         struct object *obj = &tag->object;
70         struct name_path me;
72         if (obj->flags & SEEN)
73                 return;
74         obj->flags |= SEEN;
76         me.up = NULL;
77         me.elem = "tag:/";
78         me.elem_len = 5;
80         if (parse_tag(tag) < 0)
81                 die("bad tag object %s", sha1_to_hex(obj->sha1));
82         if (tag->tagged)
83                 add_object(tag->tagged, p, NULL, name);
84 }
86 static void walk_commit_list(struct rev_info *revs)
87 {
88         int i;
89         struct commit *commit;
90         struct object_array objects = { 0, 0, NULL };
92         /* Walk all commits, process their trees */
93         while ((commit = get_revision(revs)) != NULL)
94                 process_tree(commit->tree, &objects, NULL, "");
96         /* Then walk all the pending objects, recursively processing them too */
97         for (i = 0; i < revs->pending.nr; i++) {
98                 struct object_array_entry *pending = revs->pending.objects + i;
99                 struct object *obj = pending->item;
100                 const char *name = pending->name;
101                 if (obj->type == OBJ_TAG) {
102                         process_tag((struct tag *) obj, &objects, name);
103                         continue;
104                 }
105                 if (obj->type == OBJ_TREE) {
106                         process_tree((struct tree *)obj, &objects, NULL, name);
107                         continue;
108                 }
109                 if (obj->type == OBJ_BLOB) {
110                         process_blob((struct blob *)obj, &objects, NULL, name);
111                         continue;
112                 }
113                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
114         }
117 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
118                 const char *email, unsigned long timestamp, int tz,
119                 const char *message, void *cb_data)
121         struct object *object;
122         struct rev_info *revs = (struct rev_info *)cb_data;
124         object = parse_object(osha1);
125         if (object)
126                 add_pending_object(revs, object, "");
127         object = parse_object(nsha1);
128         if (object)
129                 add_pending_object(revs, object, "");
130         return 0;
133 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
135         struct object *object = parse_object(sha1);
136         struct rev_info *revs = (struct rev_info *)cb_data;
138         if (!object)
139                 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
140         add_pending_object(revs, object, "");
142         return 0;
145 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
147         for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
148         return 0;
151 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
153         struct tree *tree = lookup_tree(sha1);
154         if (tree)
155                 add_pending_object(revs, &tree->object, "");
158 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
160         int i;
162         if (it->entry_count >= 0)
163                 add_one_tree(it->sha1, revs);
164         for (i = 0; i < it->subtree_nr; i++)
165                 add_cache_tree(it->down[i]->cache_tree, revs);
168 static void add_cache_refs(struct rev_info *revs)
170         int i;
172         read_cache();
173         for (i = 0; i < active_nr; i++) {
174                 /*
175                  * The index can contain blobs and GITLINKs, GITLINKs are hashes
176                  * that don't actually point to objects in the repository, it's
177                  * almost guaranteed that they are NOT blobs, so we don't call
178                  * lookup_blob() on them, to avoid populating the hash table
179                  * with invalid information
180                  */
181                 if (S_ISGITLINK(ntohl(active_cache[i]->ce_mode)))
182                         continue;
184                 lookup_blob(active_cache[i]->sha1);
185                 /*
186                  * We could add the blobs to the pending list, but quite
187                  * frankly, we don't care. Once we've looked them up, and
188                  * added them as objects, we've really done everything
189                  * there is to do for a blob
190                  */
191         }
192         if (active_cache_tree)
193                 add_cache_tree(active_cache_tree, revs);
196 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
198         /*
199          * Set up revision parsing, and mark us as being interested
200          * in all object types, not just commits.
201          */
202         revs->tag_objects = 1;
203         revs->blob_objects = 1;
204         revs->tree_objects = 1;
206         /* Add all refs from the index file */
207         add_cache_refs(revs);
209         /* Add all external refs */
210         for_each_ref(add_one_ref, revs);
212         /* Add all reflog info */
213         if (mark_reflog)
214                 for_each_reflog(add_one_reflog, revs);
216         /*
217          * Set up the revision walk - this will move all commits
218          * from the pending list to the commit walking list.
219          */
220         prepare_revision_walk(revs);
221         walk_commit_list(revs);