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