]> git.ipfire.org Git - thirdparty/git.git/blame - reachable.c
prune: handle --progress/no-progress
[thirdparty/git.git] / reachable.c
CommitLineData
94421474
JH
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"
dc347195 10#include "progress.h"
94421474
JH
11
12static void process_blob(struct blob *blob,
13 struct object_array *p,
14 struct name_path *path,
15 const char *name)
16{
17 struct object *obj = &blob->object;
18
f7de5a56
MK
19 if (!blob)
20 die("bad blob object");
94421474
JH
21 if (obj->flags & SEEN)
22 return;
23 obj->flags |= SEEN;
24 /* Nothing to do, really .. The blob lookup was the important part */
25}
26
8d2244ba
AP
27static void process_gitlink(const unsigned char *sha1,
28 struct object_array *p,
29 struct name_path *path,
30 const char *name)
31{
32 /* I don't think we want to recurse into this, really. */
33}
34
94421474
JH
35static void process_tree(struct tree *tree,
36 struct object_array *p,
37 struct name_path *path,
38 const char *name)
39{
40 struct object *obj = &tree->object;
41 struct tree_desc desc;
42 struct name_entry entry;
43 struct name_path me;
44
f7de5a56
MK
45 if (!tree)
46 die("bad tree object");
94421474
JH
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));
94421474
JH
52 add_object(obj, p, path, name);
53 me.up = path;
54 me.elem = name;
55 me.elem_len = strlen(name);
56
6fda5e51 57 init_tree_desc(&desc, tree->buffer, tree->size);
94421474
JH
58
59 while (tree_entry(&desc, &entry)) {
60 if (S_ISDIR(entry.mode))
61 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
b941ffac 62 else if (S_ISGITLINK(entry.mode))
8d2244ba 63 process_gitlink(entry.sha1, p, &me, entry.path);
94421474
JH
64 else
65 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
66 }
67 free(tree->buffer);
68 tree->buffer = NULL;
69}
70
71static void process_tag(struct tag *tag, struct object_array *p, const char *name)
72{
73 struct object *obj = &tag->object;
94421474
JH
74
75 if (obj->flags & SEEN)
76 return;
77 obj->flags |= SEEN;
78
94421474
JH
79 if (parse_tag(tag) < 0)
80 die("bad tag object %s", sha1_to_hex(obj->sha1));
cc369347
MK
81 if (tag->tagged)
82 add_object(tag->tagged, p, NULL, name);
94421474
JH
83}
84
dc347195 85static void walk_commit_list(struct rev_info *revs, struct progress *progress)
94421474
JH
86{
87 int i;
88 struct commit *commit;
3cd47459 89 struct object_array objects = OBJECT_ARRAY_INIT;
dc347195 90 uint32_t count = 0;
94421474
JH
91
92 /* Walk all commits, process their trees */
dc347195 93 while ((commit = get_revision(revs)) != NULL) {
94421474 94 process_tree(commit->tree, &objects, NULL, "");
dc347195
NTND
95 display_progress(progress, ++count);
96 }
94421474
JH
97
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;
dc347195 103 display_progress(progress, ++count);
94421474
JH
104 if (obj->type == OBJ_TAG) {
105 process_tag((struct tag *) obj, &objects, name);
106 continue;
107 }
108 if (obj->type == OBJ_TREE) {
109 process_tree((struct tree *)obj, &objects, NULL, name);
110 continue;
111 }
112 if (obj->type == OBJ_BLOB) {
113 process_blob((struct blob *)obj, &objects, NULL, name);
114 continue;
115 }
116 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
117 }
118}
119
883d60fa
JS
120static 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)
94421474
JH
123{
124 struct object *object;
125 struct rev_info *revs = (struct rev_info *)cb_data;
126
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;
134}
135
136static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
137{
138 struct object *object = parse_object(sha1);
139 struct rev_info *revs = (struct rev_info *)cb_data;
140
141 if (!object)
142 die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
143 add_pending_object(revs, object, "");
144
145 return 0;
146}
147
148static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
149{
150 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
151 return 0;
152}
153
154static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
155{
156 struct tree *tree = lookup_tree(sha1);
c3406635
MK
157 if (tree)
158 add_pending_object(revs, &tree->object, "");
94421474
JH
159}
160
161static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
162{
163 int i;
164
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);
169}
170
171static void add_cache_refs(struct rev_info *revs)
172{
173 int i;
174
175 read_cache();
176 for (i = 0; i < active_nr; i++) {
8d2244ba
AP
177 /*
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
183 */
7a51ed66 184 if (S_ISGITLINK(active_cache[i]->ce_mode))
8d2244ba
AP
185 continue;
186
94421474
JH
187 lookup_blob(active_cache[i]->sha1);
188 /*
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
193 */
194 }
195 if (active_cache_tree)
196 add_cache_tree(active_cache_tree, revs);
197}
198
dc347195
NTND
199void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
200 struct progress *progress)
94421474
JH
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
eb8381c8 216 /* Add all reflog info */
94421474 217 if (mark_reflog)
eb8381c8 218 for_each_reflog(add_one_reflog, revs);
94421474
JH
219
220 /*
221 * Set up the revision walk - this will move all commits
222 * from the pending list to the commit walking list.
223 */
3d51e1b5
MK
224 if (prepare_revision_walk(revs))
225 die("revision walk setup failed");
dc347195 226 walk_commit_list(revs, progress);
94421474 227}