treewide: reduce includes of cache.h in other headers
[git.git] / reachable.c
blob55bb1143530fceb53f4f68450ecb455861cbbe2d
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "refs.h"
5 #include "tag.h"
6 #include "commit.h"
7 #include "blob.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "reachable.h"
11 #include "cache-tree.h"
12 #include "progress.h"
13 #include "list-objects.h"
14 #include "packfile.h"
15 #include "worktree.h"
16 #include "object-store.h"
17 #include "pack-bitmap.h"
18 #include "pack-mtimes.h"
20 struct connectivity_progress {
21 struct progress *progress;
22 unsigned long count;
25 static void update_progress(struct connectivity_progress *cp)
27 cp->count++;
28 if ((cp->count & 1023) == 0)
29 display_progress(cp->progress, cp->count);
32 static int add_one_ref(const char *path, const struct object_id *oid,
33 int flag, void *cb_data)
35 struct rev_info *revs = (struct rev_info *)cb_data;
36 struct object *object;
38 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
39 warning("symbolic ref is dangling: %s", path);
40 return 0;
43 object = parse_object_or_die(oid, path);
44 add_pending_object(revs, object, "");
46 return 0;
50 * The traversal will have already marked us as SEEN, so we
51 * only need to handle any progress reporting here.
53 static void mark_object(struct object *obj UNUSED,
54 const char *name UNUSED,
55 void *data)
57 update_progress(data);
60 static void mark_commit(struct commit *c, void *data)
62 mark_object(&c->object, NULL, data);
65 struct recent_data {
66 struct rev_info *revs;
67 timestamp_t timestamp;
68 report_recent_object_fn *cb;
69 int ignore_in_core_kept_packs;
72 static void add_recent_object(const struct object_id *oid,
73 struct packed_git *pack,
74 off_t offset,
75 timestamp_t mtime,
76 struct recent_data *data)
78 struct object *obj;
79 enum object_type type;
81 if (mtime <= data->timestamp)
82 return;
85 * We do not want to call parse_object here, because
86 * inflating blobs and trees could be very expensive.
87 * However, we do need to know the correct type for
88 * later processing, and the revision machinery expects
89 * commits and tags to have been parsed.
91 type = oid_object_info(the_repository, oid, NULL);
92 if (type < 0)
93 die("unable to get object info for %s", oid_to_hex(oid));
95 switch (type) {
96 case OBJ_TAG:
97 case OBJ_COMMIT:
98 obj = parse_object_or_die(oid, NULL);
99 break;
100 case OBJ_TREE:
101 obj = (struct object *)lookup_tree(the_repository, oid);
102 break;
103 case OBJ_BLOB:
104 obj = (struct object *)lookup_blob(the_repository, oid);
105 break;
106 default:
107 die("unknown object type for %s: %s",
108 oid_to_hex(oid), type_name(type));
111 if (!obj)
112 die("unable to lookup %s", oid_to_hex(oid));
114 add_pending_object(data->revs, obj, "");
115 if (data->cb)
116 data->cb(obj, pack, offset, mtime);
119 static int want_recent_object(struct recent_data *data,
120 const struct object_id *oid)
122 if (data->ignore_in_core_kept_packs &&
123 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
124 return 0;
125 return 1;
128 static int add_recent_loose(const struct object_id *oid,
129 const char *path, void *data)
131 struct stat st;
132 struct object *obj;
134 if (!want_recent_object(data, oid))
135 return 0;
137 obj = lookup_object(the_repository, oid);
139 if (obj && obj->flags & SEEN)
140 return 0;
142 if (stat(path, &st) < 0) {
144 * It's OK if an object went away during our iteration; this
145 * could be due to a simultaneous repack. But anything else
146 * we should abort, since we might then fail to mark objects
147 * which should not be pruned.
149 if (errno == ENOENT)
150 return 0;
151 return error_errno("unable to stat %s", oid_to_hex(oid));
154 add_recent_object(oid, NULL, 0, st.st_mtime, data);
155 return 0;
158 static int add_recent_packed(const struct object_id *oid,
159 struct packed_git *p,
160 uint32_t pos,
161 void *data)
163 struct object *obj;
164 timestamp_t mtime = p->mtime;
166 if (!want_recent_object(data, oid))
167 return 0;
169 obj = lookup_object(the_repository, oid);
171 if (obj && obj->flags & SEEN)
172 return 0;
173 if (p->is_cruft) {
174 if (load_pack_mtimes(p) < 0)
175 die(_("could not load cruft pack .mtimes"));
176 mtime = nth_packed_mtime(p, pos);
178 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
179 return 0;
182 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
183 timestamp_t timestamp,
184 report_recent_object_fn *cb,
185 int ignore_in_core_kept_packs)
187 struct recent_data data;
188 enum for_each_object_flags flags;
189 int r;
191 data.revs = revs;
192 data.timestamp = timestamp;
193 data.cb = cb;
194 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
196 r = for_each_loose_object(add_recent_loose, &data,
197 FOR_EACH_OBJECT_LOCAL_ONLY);
198 if (r)
199 return r;
201 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
202 if (ignore_in_core_kept_packs)
203 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
205 return for_each_packed_object(add_recent_packed, &data, flags);
208 static int mark_object_seen(const struct object_id *oid,
209 enum object_type type,
210 int exclude UNUSED,
211 uint32_t name_hash UNUSED,
212 struct packed_git *found_pack UNUSED,
213 off_t found_offset UNUSED)
215 struct object *obj = lookup_object_by_type(the_repository, oid, type);
216 if (!obj)
217 die("unable to create object '%s'", oid_to_hex(oid));
219 obj->flags |= SEEN;
220 return 0;
223 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
224 timestamp_t mark_recent, struct progress *progress)
226 struct connectivity_progress cp;
227 struct bitmap_index *bitmap_git;
230 * Set up revision parsing, and mark us as being interested
231 * in all object types, not just commits.
233 revs->tag_objects = 1;
234 revs->blob_objects = 1;
235 revs->tree_objects = 1;
237 /* Add all refs from the index file */
238 add_index_objects_to_pending(revs, 0);
240 /* Add all external refs */
241 for_each_ref(add_one_ref, revs);
243 /* detached HEAD is not included in the list above */
244 head_ref(add_one_ref, revs);
245 other_head_refs(add_one_ref, revs);
247 /* Add all reflog info */
248 if (mark_reflog)
249 add_reflogs_to_pending(revs, 0);
251 cp.progress = progress;
252 cp.count = 0;
254 bitmap_git = prepare_bitmap_walk(revs, 0);
255 if (bitmap_git) {
256 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
257 free_bitmap_index(bitmap_git);
258 } else {
259 if (prepare_revision_walk(revs))
260 die("revision walk setup failed");
261 traverse_commit_list(revs, mark_commit, mark_object, &cp);
264 if (mark_recent) {
265 revs->ignore_missing_links = 1;
266 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
267 NULL, 0))
268 die("unable to mark recent objects");
269 if (prepare_revision_walk(revs))
270 die("revision walk setup failed");
271 traverse_commit_list(revs, mark_commit, mark_object, &cp);
274 display_progress(cp.progress, cp.count);