Merge branch 'gt/core-bare-in-templates'
[alt-git.git] / reachable.c
blob3b85add243ba79a4ccc90c9faacbb9fee2742303
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "refs.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-ll.h"
16 #include "pack-bitmap.h"
17 #include "pack-mtimes.h"
18 #include "config.h"
19 #include "run-command.h"
20 #include "sequencer.h"
22 struct connectivity_progress {
23 struct progress *progress;
24 unsigned long count;
27 static void update_progress(struct connectivity_progress *cp)
29 cp->count++;
30 if ((cp->count & 1023) == 0)
31 display_progress(cp->progress, cp->count);
34 static void add_one_file(const char *path, struct rev_info *revs)
36 struct strbuf buf = STRBUF_INIT;
37 struct object_id oid;
38 struct object *object;
40 if (!read_oneliner(&buf, path, READ_ONELINER_SKIP_IF_EMPTY)) {
41 strbuf_release(&buf);
42 return;
44 strbuf_trim(&buf);
45 if (!get_oid_hex(buf.buf, &oid)) {
46 object = parse_object_or_die(&oid, buf.buf);
47 add_pending_object(revs, object, "");
49 strbuf_release(&buf);
52 /* Mark objects recorded in rebase state files as reachable. */
53 static void add_rebase_files(struct rev_info *revs)
55 struct strbuf buf = STRBUF_INIT;
56 size_t len;
57 const char *path[] = {
58 "rebase-apply/autostash",
59 "rebase-apply/orig-head",
60 "rebase-merge/autostash",
61 "rebase-merge/orig-head",
63 struct worktree **worktrees = get_worktrees();
65 for (struct worktree **wt = worktrees; *wt; wt++) {
66 strbuf_reset(&buf);
67 strbuf_addstr(&buf, get_worktree_git_dir(*wt));
68 strbuf_complete(&buf, '/');
69 len = buf.len;
70 for (size_t i = 0; i < ARRAY_SIZE(path); i++) {
71 strbuf_setlen(&buf, len);
72 strbuf_addstr(&buf, path[i]);
73 add_one_file(buf.buf, revs);
76 strbuf_release(&buf);
77 free_worktrees(worktrees);
80 static int add_one_ref(const char *path, const struct object_id *oid,
81 int flag, void *cb_data)
83 struct rev_info *revs = (struct rev_info *)cb_data;
84 struct object *object;
86 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
87 warning("symbolic ref is dangling: %s", path);
88 return 0;
91 object = parse_object_or_die(oid, path);
92 add_pending_object(revs, object, "");
94 return 0;
98 * The traversal will have already marked us as SEEN, so we
99 * only need to handle any progress reporting here.
101 static void mark_object(struct object *obj UNUSED,
102 const char *name UNUSED,
103 void *data)
105 update_progress(data);
108 static void mark_commit(struct commit *c, void *data)
110 mark_object(&c->object, NULL, data);
113 struct recent_data {
114 struct rev_info *revs;
115 timestamp_t timestamp;
116 report_recent_object_fn *cb;
117 int ignore_in_core_kept_packs;
119 struct oidset extra_recent_oids;
120 int extra_recent_oids_loaded;
123 static int run_one_gc_recent_objects_hook(struct oidset *set,
124 const char *args)
126 struct child_process cmd = CHILD_PROCESS_INIT;
127 struct strbuf buf = STRBUF_INIT;
128 FILE *out;
129 int ret = 0;
131 cmd.use_shell = 1;
132 cmd.out = -1;
134 strvec_push(&cmd.args, args);
136 if (start_command(&cmd))
137 return -1;
139 out = xfdopen(cmd.out, "r");
140 while (strbuf_getline(&buf, out) != EOF) {
141 struct object_id oid;
142 const char *rest;
144 if (parse_oid_hex(buf.buf, &oid, &rest) || *rest) {
145 ret = error(_("invalid extra cruft tip: '%s'"), buf.buf);
146 break;
149 oidset_insert(set, &oid);
152 fclose(out);
153 ret |= finish_command(&cmd);
155 strbuf_release(&buf);
156 return ret;
159 static void load_gc_recent_objects(struct recent_data *data)
161 const struct string_list *programs;
162 int ret = 0;
163 size_t i;
165 data->extra_recent_oids_loaded = 1;
167 if (git_config_get_string_multi("gc.recentobjectshook", &programs))
168 return;
170 for (i = 0; i < programs->nr; i++) {
171 ret = run_one_gc_recent_objects_hook(&data->extra_recent_oids,
172 programs->items[i].string);
173 if (ret)
174 die(_("unable to enumerate additional recent objects"));
178 static int obj_is_recent(const struct object_id *oid, timestamp_t mtime,
179 struct recent_data *data)
181 if (mtime > data->timestamp)
182 return 1;
184 if (!data->extra_recent_oids_loaded)
185 load_gc_recent_objects(data);
186 return oidset_contains(&data->extra_recent_oids, oid);
189 static void add_recent_object(const struct object_id *oid,
190 struct packed_git *pack,
191 off_t offset,
192 timestamp_t mtime,
193 struct recent_data *data)
195 struct object *obj;
196 enum object_type type;
198 if (!obj_is_recent(oid, mtime, data))
199 return;
202 * We do not want to call parse_object here, because
203 * inflating blobs and trees could be very expensive.
204 * However, we do need to know the correct type for
205 * later processing, and the revision machinery expects
206 * commits and tags to have been parsed.
208 type = oid_object_info(the_repository, oid, NULL);
209 if (type < 0)
210 die("unable to get object info for %s", oid_to_hex(oid));
212 switch (type) {
213 case OBJ_TAG:
214 case OBJ_COMMIT:
215 obj = parse_object_or_die(oid, NULL);
216 break;
217 case OBJ_TREE:
218 obj = (struct object *)lookup_tree(the_repository, oid);
219 break;
220 case OBJ_BLOB:
221 obj = (struct object *)lookup_blob(the_repository, oid);
222 break;
223 default:
224 die("unknown object type for %s: %s",
225 oid_to_hex(oid), type_name(type));
228 if (!obj)
229 die("unable to lookup %s", oid_to_hex(oid));
231 add_pending_object(data->revs, obj, "");
232 if (data->cb)
233 data->cb(obj, pack, offset, mtime);
236 static int want_recent_object(struct recent_data *data,
237 const struct object_id *oid)
239 if (data->ignore_in_core_kept_packs &&
240 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
241 return 0;
242 return 1;
245 static int add_recent_loose(const struct object_id *oid,
246 const char *path, void *data)
248 struct stat st;
249 struct object *obj;
251 if (!want_recent_object(data, oid))
252 return 0;
254 obj = lookup_object(the_repository, oid);
256 if (obj && obj->flags & SEEN)
257 return 0;
259 if (stat(path, &st) < 0) {
261 * It's OK if an object went away during our iteration; this
262 * could be due to a simultaneous repack. But anything else
263 * we should abort, since we might then fail to mark objects
264 * which should not be pruned.
266 if (errno == ENOENT)
267 return 0;
268 return error_errno("unable to stat %s", oid_to_hex(oid));
271 add_recent_object(oid, NULL, 0, st.st_mtime, data);
272 return 0;
275 static int add_recent_packed(const struct object_id *oid,
276 struct packed_git *p,
277 uint32_t pos,
278 void *data)
280 struct object *obj;
281 timestamp_t mtime = p->mtime;
283 if (!want_recent_object(data, oid))
284 return 0;
286 obj = lookup_object(the_repository, oid);
288 if (obj && obj->flags & SEEN)
289 return 0;
290 if (p->is_cruft) {
291 if (load_pack_mtimes(p) < 0)
292 die(_("could not load cruft pack .mtimes"));
293 mtime = nth_packed_mtime(p, pos);
295 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
296 return 0;
299 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
300 timestamp_t timestamp,
301 report_recent_object_fn *cb,
302 int ignore_in_core_kept_packs)
304 struct recent_data data;
305 enum for_each_object_flags flags;
306 int r;
308 data.revs = revs;
309 data.timestamp = timestamp;
310 data.cb = cb;
311 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
313 oidset_init(&data.extra_recent_oids, 0);
314 data.extra_recent_oids_loaded = 0;
316 r = for_each_loose_object(add_recent_loose, &data,
317 FOR_EACH_OBJECT_LOCAL_ONLY);
318 if (r)
319 goto done;
321 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
322 if (ignore_in_core_kept_packs)
323 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
325 r = for_each_packed_object(add_recent_packed, &data, flags);
327 done:
328 oidset_clear(&data.extra_recent_oids);
330 return r;
333 static int mark_object_seen(const struct object_id *oid,
334 enum object_type type,
335 int exclude UNUSED,
336 uint32_t name_hash UNUSED,
337 struct packed_git *found_pack UNUSED,
338 off_t found_offset UNUSED)
340 struct object *obj = lookup_object_by_type(the_repository, oid, type);
341 if (!obj)
342 die("unable to create object '%s'", oid_to_hex(oid));
344 obj->flags |= SEEN;
345 return 0;
348 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
349 timestamp_t mark_recent, struct progress *progress)
351 struct connectivity_progress cp;
352 struct bitmap_index *bitmap_git;
355 * Set up revision parsing, and mark us as being interested
356 * in all object types, not just commits.
358 revs->tag_objects = 1;
359 revs->blob_objects = 1;
360 revs->tree_objects = 1;
362 /* Add all refs from the index file */
363 add_index_objects_to_pending(revs, 0);
365 /* Add all external refs */
366 for_each_ref(add_one_ref, revs);
368 /* detached HEAD is not included in the list above */
369 head_ref(add_one_ref, revs);
370 other_head_refs(add_one_ref, revs);
372 /* rebase autostash and orig-head */
373 add_rebase_files(revs);
375 /* Add all reflog info */
376 if (mark_reflog)
377 add_reflogs_to_pending(revs, 0);
379 cp.progress = progress;
380 cp.count = 0;
382 bitmap_git = prepare_bitmap_walk(revs, 0);
383 if (bitmap_git) {
384 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
385 free_bitmap_index(bitmap_git);
386 } else {
387 if (prepare_revision_walk(revs))
388 die("revision walk setup failed");
389 traverse_commit_list(revs, mark_commit, mark_object, &cp);
392 if (mark_recent) {
393 revs->ignore_missing_links = 1;
394 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
395 NULL, 0))
396 die("unable to mark recent objects");
397 if (prepare_revision_walk(revs))
398 die("revision walk setup failed");
399 traverse_commit_list(revs, mark_commit, mark_object, &cp);
402 display_progress(cp.progress, cp.count);