debian: new upstream release
[git/debian.git] / reachable.c
blob0ce8f83e56a9b42400b8ec792b19deb3a69ddb6e
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-ll.h"
17 #include "pack-bitmap.h"
18 #include "pack-mtimes.h"
19 #include "config.h"
20 #include "run-command.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 int add_one_ref(const char *path, const struct object_id *oid,
35 int flag, void *cb_data)
37 struct rev_info *revs = (struct rev_info *)cb_data;
38 struct object *object;
40 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
41 warning("symbolic ref is dangling: %s", path);
42 return 0;
45 object = parse_object_or_die(oid, path);
46 add_pending_object(revs, object, "");
48 return 0;
52 * The traversal will have already marked us as SEEN, so we
53 * only need to handle any progress reporting here.
55 static void mark_object(struct object *obj UNUSED,
56 const char *name UNUSED,
57 void *data)
59 update_progress(data);
62 static void mark_commit(struct commit *c, void *data)
64 mark_object(&c->object, NULL, data);
67 struct recent_data {
68 struct rev_info *revs;
69 timestamp_t timestamp;
70 report_recent_object_fn *cb;
71 int ignore_in_core_kept_packs;
73 struct oidset extra_recent_oids;
74 int extra_recent_oids_loaded;
77 static int run_one_gc_recent_objects_hook(struct oidset *set,
78 const char *args)
80 struct child_process cmd = CHILD_PROCESS_INIT;
81 struct strbuf buf = STRBUF_INIT;
82 FILE *out;
83 int ret = 0;
85 cmd.use_shell = 1;
86 cmd.out = -1;
88 strvec_push(&cmd.args, args);
90 if (start_command(&cmd))
91 return -1;
93 out = xfdopen(cmd.out, "r");
94 while (strbuf_getline(&buf, out) != EOF) {
95 struct object_id oid;
96 const char *rest;
98 if (parse_oid_hex(buf.buf, &oid, &rest) || *rest) {
99 ret = error(_("invalid extra cruft tip: '%s'"), buf.buf);
100 break;
103 oidset_insert(set, &oid);
106 fclose(out);
107 ret |= finish_command(&cmd);
109 strbuf_release(&buf);
110 return ret;
113 static void load_gc_recent_objects(struct recent_data *data)
115 const struct string_list *programs;
116 int ret = 0;
117 size_t i;
119 data->extra_recent_oids_loaded = 1;
121 if (git_config_get_string_multi("gc.recentobjectshook", &programs))
122 return;
124 for (i = 0; i < programs->nr; i++) {
125 ret = run_one_gc_recent_objects_hook(&data->extra_recent_oids,
126 programs->items[i].string);
127 if (ret)
128 die(_("unable to enumerate additional recent objects"));
132 static int obj_is_recent(const struct object_id *oid, timestamp_t mtime,
133 struct recent_data *data)
135 if (mtime > data->timestamp)
136 return 1;
138 if (!data->extra_recent_oids_loaded)
139 load_gc_recent_objects(data);
140 return oidset_contains(&data->extra_recent_oids, oid);
143 static void add_recent_object(const struct object_id *oid,
144 struct packed_git *pack,
145 off_t offset,
146 timestamp_t mtime,
147 struct recent_data *data)
149 struct object *obj;
150 enum object_type type;
152 if (!obj_is_recent(oid, mtime, data))
153 return;
156 * We do not want to call parse_object here, because
157 * inflating blobs and trees could be very expensive.
158 * However, we do need to know the correct type for
159 * later processing, and the revision machinery expects
160 * commits and tags to have been parsed.
162 type = oid_object_info(the_repository, oid, NULL);
163 if (type < 0)
164 die("unable to get object info for %s", oid_to_hex(oid));
166 switch (type) {
167 case OBJ_TAG:
168 case OBJ_COMMIT:
169 obj = parse_object_or_die(oid, NULL);
170 break;
171 case OBJ_TREE:
172 obj = (struct object *)lookup_tree(the_repository, oid);
173 break;
174 case OBJ_BLOB:
175 obj = (struct object *)lookup_blob(the_repository, oid);
176 break;
177 default:
178 die("unknown object type for %s: %s",
179 oid_to_hex(oid), type_name(type));
182 if (!obj)
183 die("unable to lookup %s", oid_to_hex(oid));
185 add_pending_object(data->revs, obj, "");
186 if (data->cb)
187 data->cb(obj, pack, offset, mtime);
190 static int want_recent_object(struct recent_data *data,
191 const struct object_id *oid)
193 if (data->ignore_in_core_kept_packs &&
194 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
195 return 0;
196 return 1;
199 static int add_recent_loose(const struct object_id *oid,
200 const char *path, void *data)
202 struct stat st;
203 struct object *obj;
205 if (!want_recent_object(data, oid))
206 return 0;
208 obj = lookup_object(the_repository, oid);
210 if (obj && obj->flags & SEEN)
211 return 0;
213 if (stat(path, &st) < 0) {
215 * It's OK if an object went away during our iteration; this
216 * could be due to a simultaneous repack. But anything else
217 * we should abort, since we might then fail to mark objects
218 * which should not be pruned.
220 if (errno == ENOENT)
221 return 0;
222 return error_errno("unable to stat %s", oid_to_hex(oid));
225 add_recent_object(oid, NULL, 0, st.st_mtime, data);
226 return 0;
229 static int add_recent_packed(const struct object_id *oid,
230 struct packed_git *p,
231 uint32_t pos,
232 void *data)
234 struct object *obj;
235 timestamp_t mtime = p->mtime;
237 if (!want_recent_object(data, oid))
238 return 0;
240 obj = lookup_object(the_repository, oid);
242 if (obj && obj->flags & SEEN)
243 return 0;
244 if (p->is_cruft) {
245 if (load_pack_mtimes(p) < 0)
246 die(_("could not load cruft pack .mtimes"));
247 mtime = nth_packed_mtime(p, pos);
249 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
250 return 0;
253 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
254 timestamp_t timestamp,
255 report_recent_object_fn *cb,
256 int ignore_in_core_kept_packs)
258 struct recent_data data;
259 enum for_each_object_flags flags;
260 int r;
262 data.revs = revs;
263 data.timestamp = timestamp;
264 data.cb = cb;
265 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
267 oidset_init(&data.extra_recent_oids, 0);
268 data.extra_recent_oids_loaded = 0;
270 r = for_each_loose_object(add_recent_loose, &data,
271 FOR_EACH_OBJECT_LOCAL_ONLY);
272 if (r)
273 goto done;
275 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
276 if (ignore_in_core_kept_packs)
277 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
279 r = for_each_packed_object(add_recent_packed, &data, flags);
281 done:
282 oidset_clear(&data.extra_recent_oids);
284 return r;
287 static int mark_object_seen(const struct object_id *oid,
288 enum object_type type,
289 int exclude UNUSED,
290 uint32_t name_hash UNUSED,
291 struct packed_git *found_pack UNUSED,
292 off_t found_offset UNUSED)
294 struct object *obj = lookup_object_by_type(the_repository, oid, type);
295 if (!obj)
296 die("unable to create object '%s'", oid_to_hex(oid));
298 obj->flags |= SEEN;
299 return 0;
302 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
303 timestamp_t mark_recent, struct progress *progress)
305 struct connectivity_progress cp;
306 struct bitmap_index *bitmap_git;
309 * Set up revision parsing, and mark us as being interested
310 * in all object types, not just commits.
312 revs->tag_objects = 1;
313 revs->blob_objects = 1;
314 revs->tree_objects = 1;
316 /* Add all refs from the index file */
317 add_index_objects_to_pending(revs, 0);
319 /* Add all external refs */
320 for_each_ref(add_one_ref, revs);
322 /* detached HEAD is not included in the list above */
323 head_ref(add_one_ref, revs);
324 other_head_refs(add_one_ref, revs);
326 /* Add all reflog info */
327 if (mark_reflog)
328 add_reflogs_to_pending(revs, 0);
330 cp.progress = progress;
331 cp.count = 0;
333 bitmap_git = prepare_bitmap_walk(revs, 0);
334 if (bitmap_git) {
335 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
336 free_bitmap_index(bitmap_git);
337 } else {
338 if (prepare_revision_walk(revs))
339 die("revision walk setup failed");
340 traverse_commit_list(revs, mark_commit, mark_object, &cp);
343 if (mark_recent) {
344 revs->ignore_missing_links = 1;
345 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
346 NULL, 0))
347 die("unable to mark recent objects");
348 if (prepare_revision_walk(revs))
349 die("revision walk setup failed");
350 traverse_commit_list(revs, mark_commit, mark_object, &cp);
353 display_progress(cp.progress, cp.count);