prune: show progress while marking reachable objects
[git/mjg.git] / reachable.c
blob293d37d22481561b46cb8fbf747205a6ba73a420
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 #include "progress.h"
12 static void process_blob(struct blob *blob,
13 struct object_array *p,
14 struct name_path *path,
15 const char *name)
17 struct object *obj = &blob->object;
19 if (!blob)
20 die("bad blob object");
21 if (obj->flags & SEEN)
22 return;
23 obj->flags |= SEEN;
24 /* Nothing to do, really .. The blob lookup was the important part */
27 static void process_gitlink(const unsigned char *sha1,
28 struct object_array *p,
29 struct name_path *path,
30 const char *name)
32 /* I don't think we want to recurse into this, really. */
35 static void process_tree(struct tree *tree,
36 struct object_array *p,
37 struct name_path *path,
38 const char *name)
40 struct object *obj = &tree->object;
41 struct tree_desc desc;
42 struct name_entry entry;
43 struct name_path me;
45 if (!tree)
46 die("bad tree object");
47 if (obj->flags & SEEN)
48 return;
49 obj->flags |= SEEN;
50 if (parse_tree(tree) < 0)
51 die("bad tree object %s", sha1_to_hex(obj->sha1));
52 add_object(obj, p, path, name);
53 me.up = path;
54 me.elem = name;
55 me.elem_len = strlen(name);
57 init_tree_desc(&desc, tree->buffer, tree->size);
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);
67 free(tree->buffer);
68 tree->buffer = NULL;
71 static void process_tag(struct tag *tag, struct object_array *p, const char *name)
73 struct object *obj = &tag->object;
75 if (obj->flags & SEEN)
76 return;
77 obj->flags |= SEEN;
79 if (parse_tag(tag) < 0)
80 die("bad tag object %s", sha1_to_hex(obj->sha1));
81 if (tag->tagged)
82 add_object(tag->tagged, p, NULL, name);
85 static void walk_commit_list(struct rev_info *revs, struct progress *progress)
87 int i;
88 struct commit *commit;
89 struct object_array objects = OBJECT_ARRAY_INIT;
90 uint32_t count = 0;
92 /* Walk all commits, process their trees */
93 while ((commit = get_revision(revs)) != NULL) {
94 process_tree(commit->tree, &objects, NULL, "");
95 display_progress(progress, ++count);
98 /* Then walk all the pending objects, recursively processing them too */
99 for (i = 0; i < revs->pending.nr; i++) {
100 struct object_array_entry *pending = revs->pending.objects + i;
101 struct object *obj = pending->item;
102 const char *name = pending->name;
103 display_progress(progress, ++count);
104 if (obj->type == OBJ_TAG) {
105 process_tag((struct tag *) obj, &objects, name);
106 continue;
108 if (obj->type == OBJ_TREE) {
109 process_tree((struct tree *)obj, &objects, NULL, name);
110 continue;
112 if (obj->type == OBJ_BLOB) {
113 process_blob((struct blob *)obj, &objects, NULL, name);
114 continue;
116 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
120 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
121 const char *email, unsigned long timestamp, int tz,
122 const char *message, void *cb_data)
124 struct object *object;
125 struct rev_info *revs = (struct rev_info *)cb_data;
127 object = parse_object(osha1);
128 if (object)
129 add_pending_object(revs, object, "");
130 object = parse_object(nsha1);
131 if (object)
132 add_pending_object(revs, object, "");
133 return 0;
136 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
138 struct object *object = parse_object(sha1);
139 struct rev_info *revs = (struct rev_info *)cb_data;
141 if (!object)
142 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
143 add_pending_object(revs, object, "");
145 return 0;
148 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
150 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
151 return 0;
154 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
156 struct tree *tree = lookup_tree(sha1);
157 if (tree)
158 add_pending_object(revs, &tree->object, "");
161 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
163 int i;
165 if (it->entry_count >= 0)
166 add_one_tree(it->sha1, revs);
167 for (i = 0; i < it->subtree_nr; i++)
168 add_cache_tree(it->down[i]->cache_tree, revs);
171 static void add_cache_refs(struct rev_info *revs)
173 int i;
175 read_cache();
176 for (i = 0; i < active_nr; i++) {
178 * The index can contain blobs and GITLINKs, GITLINKs are hashes
179 * that don't actually point to objects in the repository, it's
180 * almost guaranteed that they are NOT blobs, so we don't call
181 * lookup_blob() on them, to avoid populating the hash table
182 * with invalid information
184 if (S_ISGITLINK(active_cache[i]->ce_mode))
185 continue;
187 lookup_blob(active_cache[i]->sha1);
189 * We could add the blobs to the pending list, but quite
190 * frankly, we don't care. Once we've looked them up, and
191 * added them as objects, we've really done everything
192 * there is to do for a blob
195 if (active_cache_tree)
196 add_cache_tree(active_cache_tree, revs);
199 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
200 struct progress *progress)
203 * Set up revision parsing, and mark us as being interested
204 * in all object types, not just commits.
206 revs->tag_objects = 1;
207 revs->blob_objects = 1;
208 revs->tree_objects = 1;
210 /* Add all refs from the index file */
211 add_cache_refs(revs);
213 /* Add all external refs */
214 for_each_ref(add_one_ref, revs);
216 /* Add all reflog info */
217 if (mark_reflog)
218 for_each_reflog(add_one_reflog, revs);
221 * Set up the revision walk - this will move all commits
222 * from the pending list to the commit walking list.
224 if (prepare_revision_walk(revs))
225 die("revision walk setup failed");
226 walk_commit_list(revs, progress);