Merge branch 'en/header-cleanup'
[git/debian.git] / reachable.c
blobbe9d40923de957d60a13d0e2a1e082a87b744c47
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, const char *name, void *data)
54 update_progress(data);
57 static void mark_commit(struct commit *c, void *data)
59 mark_object(&c->object, NULL, data);
62 struct recent_data {
63 struct rev_info *revs;
64 timestamp_t timestamp;
65 report_recent_object_fn *cb;
66 int ignore_in_core_kept_packs;
69 static void add_recent_object(const struct object_id *oid,
70 struct packed_git *pack,
71 off_t offset,
72 timestamp_t mtime,
73 struct recent_data *data)
75 struct object *obj;
76 enum object_type type;
78 if (mtime <= data->timestamp)
79 return;
82 * We do not want to call parse_object here, because
83 * inflating blobs and trees could be very expensive.
84 * However, we do need to know the correct type for
85 * later processing, and the revision machinery expects
86 * commits and tags to have been parsed.
88 type = oid_object_info(the_repository, oid, NULL);
89 if (type < 0)
90 die("unable to get object info for %s", oid_to_hex(oid));
92 switch (type) {
93 case OBJ_TAG:
94 case OBJ_COMMIT:
95 obj = parse_object_or_die(oid, NULL);
96 break;
97 case OBJ_TREE:
98 obj = (struct object *)lookup_tree(the_repository, oid);
99 break;
100 case OBJ_BLOB:
101 obj = (struct object *)lookup_blob(the_repository, oid);
102 break;
103 default:
104 die("unknown object type for %s: %s",
105 oid_to_hex(oid), type_name(type));
108 if (!obj)
109 die("unable to lookup %s", oid_to_hex(oid));
111 add_pending_object(data->revs, obj, "");
112 if (data->cb)
113 data->cb(obj, pack, offset, mtime);
116 static int want_recent_object(struct recent_data *data,
117 const struct object_id *oid)
119 if (data->ignore_in_core_kept_packs &&
120 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
121 return 0;
122 return 1;
125 static int add_recent_loose(const struct object_id *oid,
126 const char *path, void *data)
128 struct stat st;
129 struct object *obj;
131 if (!want_recent_object(data, oid))
132 return 0;
134 obj = lookup_object(the_repository, oid);
136 if (obj && obj->flags & SEEN)
137 return 0;
139 if (stat(path, &st) < 0) {
141 * It's OK if an object went away during our iteration; this
142 * could be due to a simultaneous repack. But anything else
143 * we should abort, since we might then fail to mark objects
144 * which should not be pruned.
146 if (errno == ENOENT)
147 return 0;
148 return error_errno("unable to stat %s", oid_to_hex(oid));
151 add_recent_object(oid, NULL, 0, st.st_mtime, data);
152 return 0;
155 static int add_recent_packed(const struct object_id *oid,
156 struct packed_git *p, uint32_t pos,
157 void *data)
159 struct object *obj;
160 timestamp_t mtime = p->mtime;
162 if (!want_recent_object(data, oid))
163 return 0;
165 obj = lookup_object(the_repository, oid);
167 if (obj && obj->flags & SEEN)
168 return 0;
169 if (p->is_cruft) {
170 if (load_pack_mtimes(p) < 0)
171 die(_("could not load cruft pack .mtimes"));
172 mtime = nth_packed_mtime(p, pos);
174 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
175 return 0;
178 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
179 timestamp_t timestamp,
180 report_recent_object_fn *cb,
181 int ignore_in_core_kept_packs)
183 struct recent_data data;
184 enum for_each_object_flags flags;
185 int r;
187 data.revs = revs;
188 data.timestamp = timestamp;
189 data.cb = cb;
190 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
192 r = for_each_loose_object(add_recent_loose, &data,
193 FOR_EACH_OBJECT_LOCAL_ONLY);
194 if (r)
195 return r;
197 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
198 if (ignore_in_core_kept_packs)
199 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
201 return for_each_packed_object(add_recent_packed, &data, flags);
204 static int mark_object_seen(const struct object_id *oid,
205 enum object_type type,
206 int exclude,
207 uint32_t name_hash,
208 struct packed_git *found_pack,
209 off_t found_offset)
211 struct object *obj = lookup_object_by_type(the_repository, oid, type);
212 if (!obj)
213 die("unable to create object '%s'", oid_to_hex(oid));
215 obj->flags |= SEEN;
216 return 0;
219 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
220 timestamp_t mark_recent, struct progress *progress)
222 struct connectivity_progress cp;
223 struct bitmap_index *bitmap_git;
226 * Set up revision parsing, and mark us as being interested
227 * in all object types, not just commits.
229 revs->tag_objects = 1;
230 revs->blob_objects = 1;
231 revs->tree_objects = 1;
233 /* Add all refs from the index file */
234 add_index_objects_to_pending(revs, 0);
236 /* Add all external refs */
237 for_each_ref(add_one_ref, revs);
239 /* detached HEAD is not included in the list above */
240 head_ref(add_one_ref, revs);
241 other_head_refs(add_one_ref, revs);
243 /* Add all reflog info */
244 if (mark_reflog)
245 add_reflogs_to_pending(revs, 0);
247 cp.progress = progress;
248 cp.count = 0;
250 bitmap_git = prepare_bitmap_walk(revs, 0);
251 if (bitmap_git) {
252 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
253 free_bitmap_index(bitmap_git);
254 } else {
255 if (prepare_revision_walk(revs))
256 die("revision walk setup failed");
257 traverse_commit_list(revs, mark_commit, mark_object, &cp);
260 if (mark_recent) {
261 revs->ignore_missing_links = 1;
262 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
263 NULL, 0))
264 die("unable to mark recent objects");
265 if (prepare_revision_walk(revs))
266 die("revision walk setup failed");
267 traverse_commit_list(revs, mark_commit, mark_object, &cp);
270 display_progress(cp.progress, cp.count);