Code

cvsserver: detect early of we are up to date and avoid costly rev-list
[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_tree(struct tree *tree,
25                          struct object_array *p,
26                          struct name_path *path,
27                          const char *name)
28 {
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);
53         }
54         free(tree->buffer);
55         tree->buffer = NULL;
56 }
58 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
59 {
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);
74 }
76 static void walk_commit_list(struct rev_info *revs)
77 {
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;
94                 }
95                 if (obj->type == OBJ_TREE) {
96                         process_tree((struct tree *)obj, &objects, NULL, name);
97                         continue;
98                 }
99                 if (obj->type == OBJ_BLOB) {
100                         process_blob((struct blob *)obj, &objects, NULL, name);
101                         continue;
102                 }
103                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
104         }
107 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
109         struct object *object;
110         struct rev_info *revs = (struct rev_info *)cb_data;
112         object = parse_object(osha1);
113         if (object)
114                 add_pending_object(revs, object, "");
115         object = parse_object(nsha1);
116         if (object)
117                 add_pending_object(revs, object, "");
118         return 0;
121 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
123         struct object *object = parse_object(sha1);
124         struct rev_info *revs = (struct rev_info *)cb_data;
126         if (!object)
127                 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
128         add_pending_object(revs, object, "");
130         return 0;
133 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
135         for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
136         return 0;
139 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
141         struct tree *tree = lookup_tree(sha1);
142         add_pending_object(revs, &tree->object, "");
145 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
147         int i;
149         if (it->entry_count >= 0)
150                 add_one_tree(it->sha1, revs);
151         for (i = 0; i < it->subtree_nr; i++)
152                 add_cache_tree(it->down[i]->cache_tree, revs);
155 static void add_cache_refs(struct rev_info *revs)
157         int i;
159         read_cache();
160         for (i = 0; i < active_nr; i++) {
161                 lookup_blob(active_cache[i]->sha1);
162                 /*
163                  * We could add the blobs to the pending list, but quite
164                  * frankly, we don't care. Once we've looked them up, and
165                  * added them as objects, we've really done everything
166                  * there is to do for a blob
167                  */
168         }
169         if (active_cache_tree)
170                 add_cache_tree(active_cache_tree, revs);
173 void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
175         /*
176          * Set up revision parsing, and mark us as being interested
177          * in all object types, not just commits.
178          */
179         revs->tag_objects = 1;
180         revs->blob_objects = 1;
181         revs->tree_objects = 1;
183         /* Add all refs from the index file */
184         add_cache_refs(revs);
186         /* Add all external refs */
187         for_each_ref(add_one_ref, revs);
189         /* Add all reflog info from refs */
190         if (mark_reflog)
191                 for_each_ref(add_one_reflog, revs);
193         /*
194          * Set up the revision walk - this will move all commits
195          * from the pending list to the commit walking list.
196          */
197         prepare_revision_walk(revs);
198         walk_commit_list(revs);