1 #include "git-compat-util.h"
10 #include "cache-tree.h"
12 #include "list-objects.h"
15 #include "object-store-ll.h"
16 #include "pack-bitmap.h"
17 #include "pack-mtimes.h"
19 #include "run-command.h"
21 struct connectivity_progress
{
22 struct progress
*progress
;
26 static void update_progress(struct connectivity_progress
*cp
)
29 if ((cp
->count
& 1023) == 0)
30 display_progress(cp
->progress
, cp
->count
);
33 static int add_one_ref(const char *path
, const struct object_id
*oid
,
34 int flag
, void *cb_data
)
36 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
37 struct object
*object
;
39 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
40 warning("symbolic ref is dangling: %s", path
);
44 object
= parse_object_or_die(oid
, path
);
45 add_pending_object(revs
, object
, "");
51 * The traversal will have already marked us as SEEN, so we
52 * only need to handle any progress reporting here.
54 static void mark_object(struct object
*obj UNUSED
,
55 const char *name UNUSED
,
58 update_progress(data
);
61 static void mark_commit(struct commit
*c
, void *data
)
63 mark_object(&c
->object
, NULL
, data
);
67 struct rev_info
*revs
;
68 timestamp_t timestamp
;
69 report_recent_object_fn
*cb
;
70 int ignore_in_core_kept_packs
;
72 struct oidset extra_recent_oids
;
73 int extra_recent_oids_loaded
;
76 static int run_one_gc_recent_objects_hook(struct oidset
*set
,
79 struct child_process cmd
= CHILD_PROCESS_INIT
;
80 struct strbuf buf
= STRBUF_INIT
;
87 strvec_push(&cmd
.args
, args
);
89 if (start_command(&cmd
))
92 out
= xfdopen(cmd
.out
, "r");
93 while (strbuf_getline(&buf
, out
) != EOF
) {
97 if (parse_oid_hex(buf
.buf
, &oid
, &rest
) || *rest
) {
98 ret
= error(_("invalid extra cruft tip: '%s'"), buf
.buf
);
102 oidset_insert(set
, &oid
);
106 ret
|= finish_command(&cmd
);
108 strbuf_release(&buf
);
112 static void load_gc_recent_objects(struct recent_data
*data
)
114 const struct string_list
*programs
;
118 data
->extra_recent_oids_loaded
= 1;
120 if (git_config_get_string_multi("gc.recentobjectshook", &programs
))
123 for (i
= 0; i
< programs
->nr
; i
++) {
124 ret
= run_one_gc_recent_objects_hook(&data
->extra_recent_oids
,
125 programs
->items
[i
].string
);
127 die(_("unable to enumerate additional recent objects"));
131 static int obj_is_recent(const struct object_id
*oid
, timestamp_t mtime
,
132 struct recent_data
*data
)
134 if (mtime
> data
->timestamp
)
137 if (!data
->extra_recent_oids_loaded
)
138 load_gc_recent_objects(data
);
139 return oidset_contains(&data
->extra_recent_oids
, oid
);
142 static void add_recent_object(const struct object_id
*oid
,
143 struct packed_git
*pack
,
146 struct recent_data
*data
)
149 enum object_type type
;
151 if (!obj_is_recent(oid
, mtime
, data
))
155 * We do not want to call parse_object here, because
156 * inflating blobs and trees could be very expensive.
157 * However, we do need to know the correct type for
158 * later processing, and the revision machinery expects
159 * commits and tags to have been parsed.
161 type
= oid_object_info(the_repository
, oid
, NULL
);
163 die("unable to get object info for %s", oid_to_hex(oid
));
168 obj
= parse_object_or_die(oid
, NULL
);
171 obj
= (struct object
*)lookup_tree(the_repository
, oid
);
174 obj
= (struct object
*)lookup_blob(the_repository
, oid
);
177 die("unknown object type for %s: %s",
178 oid_to_hex(oid
), type_name(type
));
182 die("unable to lookup %s", oid_to_hex(oid
));
184 add_pending_object(data
->revs
, obj
, "");
186 data
->cb(obj
, pack
, offset
, mtime
);
189 static int want_recent_object(struct recent_data
*data
,
190 const struct object_id
*oid
)
192 if (data
->ignore_in_core_kept_packs
&&
193 has_object_kept_pack(oid
, IN_CORE_KEEP_PACKS
))
198 static int add_recent_loose(const struct object_id
*oid
,
199 const char *path
, void *data
)
204 if (!want_recent_object(data
, oid
))
207 obj
= lookup_object(the_repository
, oid
);
209 if (obj
&& obj
->flags
& SEEN
)
212 if (stat(path
, &st
) < 0) {
214 * It's OK if an object went away during our iteration; this
215 * could be due to a simultaneous repack. But anything else
216 * we should abort, since we might then fail to mark objects
217 * which should not be pruned.
221 return error_errno("unable to stat %s", oid_to_hex(oid
));
224 add_recent_object(oid
, NULL
, 0, st
.st_mtime
, data
);
228 static int add_recent_packed(const struct object_id
*oid
,
229 struct packed_git
*p
,
234 timestamp_t mtime
= p
->mtime
;
236 if (!want_recent_object(data
, oid
))
239 obj
= lookup_object(the_repository
, oid
);
241 if (obj
&& obj
->flags
& SEEN
)
244 if (load_pack_mtimes(p
) < 0)
245 die(_("could not load cruft pack .mtimes"));
246 mtime
= nth_packed_mtime(p
, pos
);
248 add_recent_object(oid
, p
, nth_packed_object_offset(p
, pos
), mtime
, data
);
252 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
253 timestamp_t timestamp
,
254 report_recent_object_fn
*cb
,
255 int ignore_in_core_kept_packs
)
257 struct recent_data data
;
258 enum for_each_object_flags flags
;
262 data
.timestamp
= timestamp
;
264 data
.ignore_in_core_kept_packs
= ignore_in_core_kept_packs
;
266 oidset_init(&data
.extra_recent_oids
, 0);
267 data
.extra_recent_oids_loaded
= 0;
269 r
= for_each_loose_object(add_recent_loose
, &data
,
270 FOR_EACH_OBJECT_LOCAL_ONLY
);
274 flags
= FOR_EACH_OBJECT_LOCAL_ONLY
| FOR_EACH_OBJECT_PACK_ORDER
;
275 if (ignore_in_core_kept_packs
)
276 flags
|= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
;
278 r
= for_each_packed_object(add_recent_packed
, &data
, flags
);
281 oidset_clear(&data
.extra_recent_oids
);
286 static int mark_object_seen(const struct object_id
*oid
,
287 enum object_type type
,
289 uint32_t name_hash UNUSED
,
290 struct packed_git
*found_pack UNUSED
,
291 off_t found_offset UNUSED
)
293 struct object
*obj
= lookup_object_by_type(the_repository
, oid
, type
);
295 die("unable to create object '%s'", oid_to_hex(oid
));
301 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
302 timestamp_t mark_recent
, struct progress
*progress
)
304 struct connectivity_progress cp
;
305 struct bitmap_index
*bitmap_git
;
308 * Set up revision parsing, and mark us as being interested
309 * in all object types, not just commits.
311 revs
->tag_objects
= 1;
312 revs
->blob_objects
= 1;
313 revs
->tree_objects
= 1;
315 /* Add all refs from the index file */
316 add_index_objects_to_pending(revs
, 0);
318 /* Add all external refs */
319 for_each_ref(add_one_ref
, revs
);
321 /* detached HEAD is not included in the list above */
322 head_ref(add_one_ref
, revs
);
323 other_head_refs(add_one_ref
, revs
);
325 /* Add all reflog info */
327 add_reflogs_to_pending(revs
, 0);
329 cp
.progress
= progress
;
332 bitmap_git
= prepare_bitmap_walk(revs
, 0);
334 traverse_bitmap_commit_list(bitmap_git
, revs
, mark_object_seen
);
335 free_bitmap_index(bitmap_git
);
337 if (prepare_revision_walk(revs
))
338 die("revision walk setup failed");
339 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
343 revs
->ignore_missing_links
= 1;
344 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
,
346 die("unable to mark recent objects");
347 if (prepare_revision_walk(revs
))
348 die("revision walk setup failed");
349 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
352 display_progress(cp
.progress
, cp
.count
);