Merge branch 'jk/bundle-use-dash-for-stdfiles'
[git/debian.git] / reachable.c
blobc9dab2a66b9140d1ad0c207ee6fa65ff842dc0e7
1 #include "cache.h"
2 #include "hex.h"
3 #include "refs.h"
4 #include "tag.h"
5 #include "commit.h"
6 #include "blob.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "reachable.h"
10 #include "cache-tree.h"
11 #include "progress.h"
12 #include "list-objects.h"
13 #include "packfile.h"
14 #include "worktree.h"
15 #include "object-store.h"
16 #include "pack-bitmap.h"
17 #include "pack-mtimes.h"
19 struct connectivity_progress {
20 struct progress *progress;
21 unsigned long count;
24 static void update_progress(struct connectivity_progress *cp)
26 cp->count++;
27 if ((cp->count & 1023) == 0)
28 display_progress(cp->progress, cp->count);
31 static int add_one_ref(const char *path, const struct object_id *oid,
32 int flag, void *cb_data)
34 struct rev_info *revs = (struct rev_info *)cb_data;
35 struct object *object;
37 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
38 warning("symbolic ref is dangling: %s", path);
39 return 0;
42 object = parse_object_or_die(oid, path);
43 add_pending_object(revs, object, "");
45 return 0;
49 * The traversal will have already marked us as SEEN, so we
50 * only need to handle any progress reporting here.
52 static void mark_object(struct object *obj UNUSED,
53 const char *name UNUSED,
54 void *data)
56 update_progress(data);
59 static void mark_commit(struct commit *c, void *data)
61 mark_object(&c->object, NULL, data);
64 struct recent_data {
65 struct rev_info *revs;
66 timestamp_t timestamp;
67 report_recent_object_fn *cb;
68 int ignore_in_core_kept_packs;
71 static void add_recent_object(const struct object_id *oid,
72 struct packed_git *pack,
73 off_t offset,
74 timestamp_t mtime,
75 struct recent_data *data)
77 struct object *obj;
78 enum object_type type;
80 if (mtime <= data->timestamp)
81 return;
84 * We do not want to call parse_object here, because
85 * inflating blobs and trees could be very expensive.
86 * However, we do need to know the correct type for
87 * later processing, and the revision machinery expects
88 * commits and tags to have been parsed.
90 type = oid_object_info(the_repository, oid, NULL);
91 if (type < 0)
92 die("unable to get object info for %s", oid_to_hex(oid));
94 switch (type) {
95 case OBJ_TAG:
96 case OBJ_COMMIT:
97 obj = parse_object_or_die(oid, NULL);
98 break;
99 case OBJ_TREE:
100 obj = (struct object *)lookup_tree(the_repository, oid);
101 break;
102 case OBJ_BLOB:
103 obj = (struct object *)lookup_blob(the_repository, oid);
104 break;
105 default:
106 die("unknown object type for %s: %s",
107 oid_to_hex(oid), type_name(type));
110 if (!obj)
111 die("unable to lookup %s", oid_to_hex(oid));
113 add_pending_object(data->revs, obj, "");
114 if (data->cb)
115 data->cb(obj, pack, offset, mtime);
118 static int want_recent_object(struct recent_data *data,
119 const struct object_id *oid)
121 if (data->ignore_in_core_kept_packs &&
122 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
123 return 0;
124 return 1;
127 static int add_recent_loose(const struct object_id *oid,
128 const char *path, void *data)
130 struct stat st;
131 struct object *obj;
133 if (!want_recent_object(data, oid))
134 return 0;
136 obj = lookup_object(the_repository, oid);
138 if (obj && obj->flags & SEEN)
139 return 0;
141 if (stat(path, &st) < 0) {
143 * It's OK if an object went away during our iteration; this
144 * could be due to a simultaneous repack. But anything else
145 * we should abort, since we might then fail to mark objects
146 * which should not be pruned.
148 if (errno == ENOENT)
149 return 0;
150 return error_errno("unable to stat %s", oid_to_hex(oid));
153 add_recent_object(oid, NULL, 0, st.st_mtime, data);
154 return 0;
157 static int add_recent_packed(const struct object_id *oid,
158 struct packed_git *p,
159 uint32_t pos,
160 void *data)
162 struct object *obj;
163 timestamp_t mtime = p->mtime;
165 if (!want_recent_object(data, oid))
166 return 0;
168 obj = lookup_object(the_repository, oid);
170 if (obj && obj->flags & SEEN)
171 return 0;
172 if (p->is_cruft) {
173 if (load_pack_mtimes(p) < 0)
174 die(_("could not load cruft pack .mtimes"));
175 mtime = nth_packed_mtime(p, pos);
177 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
178 return 0;
181 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
182 timestamp_t timestamp,
183 report_recent_object_fn *cb,
184 int ignore_in_core_kept_packs)
186 struct recent_data data;
187 enum for_each_object_flags flags;
188 int r;
190 data.revs = revs;
191 data.timestamp = timestamp;
192 data.cb = cb;
193 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
195 r = for_each_loose_object(add_recent_loose, &data,
196 FOR_EACH_OBJECT_LOCAL_ONLY);
197 if (r)
198 return r;
200 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
201 if (ignore_in_core_kept_packs)
202 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
204 return for_each_packed_object(add_recent_packed, &data, flags);
207 static int mark_object_seen(const struct object_id *oid,
208 enum object_type type,
209 int exclude UNUSED,
210 uint32_t name_hash UNUSED,
211 struct packed_git *found_pack UNUSED,
212 off_t found_offset UNUSED)
214 struct object *obj = lookup_object_by_type(the_repository, oid, type);
215 if (!obj)
216 die("unable to create object '%s'", oid_to_hex(oid));
218 obj->flags |= SEEN;
219 return 0;
222 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
223 timestamp_t mark_recent, struct progress *progress)
225 struct connectivity_progress cp;
226 struct bitmap_index *bitmap_git;
229 * Set up revision parsing, and mark us as being interested
230 * in all object types, not just commits.
232 revs->tag_objects = 1;
233 revs->blob_objects = 1;
234 revs->tree_objects = 1;
236 /* Add all refs from the index file */
237 add_index_objects_to_pending(revs, 0);
239 /* Add all external refs */
240 for_each_ref(add_one_ref, revs);
242 /* detached HEAD is not included in the list above */
243 head_ref(add_one_ref, revs);
244 other_head_refs(add_one_ref, revs);
246 /* Add all reflog info */
247 if (mark_reflog)
248 add_reflogs_to_pending(revs, 0);
250 cp.progress = progress;
251 cp.count = 0;
253 bitmap_git = prepare_bitmap_walk(revs, 0);
254 if (bitmap_git) {
255 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
256 free_bitmap_index(bitmap_git);
257 } else {
258 if (prepare_revision_walk(revs))
259 die("revision walk setup failed");
260 traverse_commit_list(revs, mark_commit, mark_object, &cp);
263 if (mark_recent) {
264 revs->ignore_missing_links = 1;
265 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
266 NULL, 0))
267 die("unable to mark recent objects");
268 if (prepare_revision_walk(revs))
269 die("revision walk setup failed");
270 traverse_commit_list(revs, mark_commit, mark_object, &cp);
273 display_progress(cp.progress, cp.count);