Merge branch 'rs/ctype-test'
[git.git] / reachable.c
blobaba63ebeb3be784a1fba95e13070e8bd847315c1
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"
11 #include "list-objects.h"
12 #include "packfile.h"
13 #include "worktree.h"
14 #include "object-store.h"
15 #include "pack-bitmap.h"
16 #include "pack-mtimes.h"
18 struct connectivity_progress {
19 struct progress *progress;
20 unsigned long count;
23 static void update_progress(struct connectivity_progress *cp)
25 cp->count++;
26 if ((cp->count & 1023) == 0)
27 display_progress(cp->progress, cp->count);
30 static int add_one_ref(const char *path, const struct object_id *oid,
31 int flag, void *cb_data)
33 struct rev_info *revs = (struct rev_info *)cb_data;
34 struct object *object;
36 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
37 warning("symbolic ref is dangling: %s", path);
38 return 0;
41 object = parse_object_or_die(oid, path);
42 add_pending_object(revs, object, "");
44 return 0;
48 * The traversal will have already marked us as SEEN, so we
49 * only need to handle any progress reporting here.
51 static void mark_object(struct object *obj, const char *name, void *data)
53 update_progress(data);
56 static void mark_commit(struct commit *c, void *data)
58 mark_object(&c->object, NULL, data);
61 struct recent_data {
62 struct rev_info *revs;
63 timestamp_t timestamp;
64 report_recent_object_fn *cb;
65 int ignore_in_core_kept_packs;
68 static void add_recent_object(const struct object_id *oid,
69 struct packed_git *pack,
70 off_t offset,
71 timestamp_t mtime,
72 struct recent_data *data)
74 struct object *obj;
75 enum object_type type;
77 if (mtime <= data->timestamp)
78 return;
81 * We do not want to call parse_object here, because
82 * inflating blobs and trees could be very expensive.
83 * However, we do need to know the correct type for
84 * later processing, and the revision machinery expects
85 * commits and tags to have been parsed.
87 type = oid_object_info(the_repository, oid, NULL);
88 if (type < 0)
89 die("unable to get object info for %s", oid_to_hex(oid));
91 switch (type) {
92 case OBJ_TAG:
93 case OBJ_COMMIT:
94 obj = parse_object_or_die(oid, NULL);
95 break;
96 case OBJ_TREE:
97 obj = (struct object *)lookup_tree(the_repository, oid);
98 break;
99 case OBJ_BLOB:
100 obj = (struct object *)lookup_blob(the_repository, oid);
101 break;
102 default:
103 die("unknown object type for %s: %s",
104 oid_to_hex(oid), type_name(type));
107 if (!obj)
108 die("unable to lookup %s", oid_to_hex(oid));
110 add_pending_object(data->revs, obj, "");
111 if (data->cb)
112 data->cb(obj, pack, offset, mtime);
115 static int want_recent_object(struct recent_data *data,
116 const struct object_id *oid)
118 if (data->ignore_in_core_kept_packs &&
119 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
120 return 0;
121 return 1;
124 static int add_recent_loose(const struct object_id *oid,
125 const char *path, void *data)
127 struct stat st;
128 struct object *obj;
130 if (!want_recent_object(data, oid))
131 return 0;
133 obj = lookup_object(the_repository, oid);
135 if (obj && obj->flags & SEEN)
136 return 0;
138 if (stat(path, &st) < 0) {
140 * It's OK if an object went away during our iteration; this
141 * could be due to a simultaneous repack. But anything else
142 * we should abort, since we might then fail to mark objects
143 * which should not be pruned.
145 if (errno == ENOENT)
146 return 0;
147 return error_errno("unable to stat %s", oid_to_hex(oid));
150 add_recent_object(oid, NULL, 0, st.st_mtime, data);
151 return 0;
154 static int add_recent_packed(const struct object_id *oid,
155 struct packed_git *p, uint32_t pos,
156 void *data)
158 struct object *obj;
159 timestamp_t mtime = p->mtime;
161 if (!want_recent_object(data, oid))
162 return 0;
164 obj = lookup_object(the_repository, oid);
166 if (obj && obj->flags & SEEN)
167 return 0;
168 if (p->is_cruft) {
169 if (load_pack_mtimes(p) < 0)
170 die(_("could not load cruft pack .mtimes"));
171 mtime = nth_packed_mtime(p, pos);
173 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
174 return 0;
177 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
178 timestamp_t timestamp,
179 report_recent_object_fn *cb,
180 int ignore_in_core_kept_packs)
182 struct recent_data data;
183 enum for_each_object_flags flags;
184 int r;
186 data.revs = revs;
187 data.timestamp = timestamp;
188 data.cb = cb;
189 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
191 r = for_each_loose_object(add_recent_loose, &data,
192 FOR_EACH_OBJECT_LOCAL_ONLY);
193 if (r)
194 return r;
196 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
197 if (ignore_in_core_kept_packs)
198 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
200 return for_each_packed_object(add_recent_packed, &data, flags);
203 static int mark_object_seen(const struct object_id *oid,
204 enum object_type type,
205 int exclude,
206 uint32_t name_hash,
207 struct packed_git *found_pack,
208 off_t found_offset)
210 struct object *obj = lookup_object_by_type(the_repository, oid, type);
211 if (!obj)
212 die("unable to create object '%s'", oid_to_hex(oid));
214 obj->flags |= SEEN;
215 return 0;
218 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
219 timestamp_t mark_recent, struct progress *progress)
221 struct connectivity_progress cp;
222 struct bitmap_index *bitmap_git;
225 * Set up revision parsing, and mark us as being interested
226 * in all object types, not just commits.
228 revs->tag_objects = 1;
229 revs->blob_objects = 1;
230 revs->tree_objects = 1;
232 /* Add all refs from the index file */
233 add_index_objects_to_pending(revs, 0);
235 /* Add all external refs */
236 for_each_ref(add_one_ref, revs);
238 /* detached HEAD is not included in the list above */
239 head_ref(add_one_ref, revs);
240 other_head_refs(add_one_ref, revs);
242 /* Add all reflog info */
243 if (mark_reflog)
244 add_reflogs_to_pending(revs, 0);
246 cp.progress = progress;
247 cp.count = 0;
249 bitmap_git = prepare_bitmap_walk(revs, 0);
250 if (bitmap_git) {
251 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
252 free_bitmap_index(bitmap_git);
253 } else {
254 if (prepare_revision_walk(revs))
255 die("revision walk setup failed");
256 traverse_commit_list(revs, mark_commit, mark_object, &cp);
259 if (mark_recent) {
260 revs->ignore_missing_links = 1;
261 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
262 NULL, 0))
263 die("unable to mark recent objects");
264 if (prepare_revision_walk(revs))
265 die("revision walk setup failed");
266 traverse_commit_list(revs, mark_commit, mark_object, &cp);
269 display_progress(cp.progress, cp.count);