10 #include "cache-tree.h"
12 #include "list-objects.h"
15 #include "object-store.h"
16 #include "pack-bitmap.h"
17 #include "pack-mtimes.h"
19 struct connectivity_progress
{
20 struct progress
*progress
;
24 static void update_progress(struct connectivity_progress
*cp
)
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
);
42 object
= parse_object_or_die(oid
, path
);
43 add_pending_object(revs
, object
, "");
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
);
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
,
73 struct recent_data
*data
)
76 enum object_type type
;
78 if (mtime
<= data
->timestamp
)
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
);
90 die("unable to get object info for %s", oid_to_hex(oid
));
95 obj
= parse_object_or_die(oid
, NULL
);
98 obj
= (struct object
*)lookup_tree(the_repository
, oid
);
101 obj
= (struct object
*)lookup_blob(the_repository
, oid
);
104 die("unknown object type for %s: %s",
105 oid_to_hex(oid
), type_name(type
));
109 die("unable to lookup %s", oid_to_hex(oid
));
111 add_pending_object(data
->revs
, obj
, "");
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
))
125 static int add_recent_loose(const struct object_id
*oid
,
126 const char *path
, void *data
)
131 if (!want_recent_object(data
, oid
))
134 obj
= lookup_object(the_repository
, oid
);
136 if (obj
&& obj
->flags
& SEEN
)
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.
148 return error_errno("unable to stat %s", oid_to_hex(oid
));
151 add_recent_object(oid
, NULL
, 0, st
.st_mtime
, data
);
155 static int add_recent_packed(const struct object_id
*oid
,
156 struct packed_git
*p
, uint32_t pos
,
160 timestamp_t mtime
= p
->mtime
;
162 if (!want_recent_object(data
, oid
))
165 obj
= lookup_object(the_repository
, oid
);
167 if (obj
&& obj
->flags
& SEEN
)
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
);
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
;
188 data
.timestamp
= timestamp
;
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
);
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
,
208 struct packed_git
*found_pack
,
211 struct object
*obj
= lookup_object_by_type(the_repository
, oid
, type
);
213 die("unable to create object '%s'", oid_to_hex(oid
));
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 */
245 add_reflogs_to_pending(revs
, 0);
247 cp
.progress
= progress
;
250 bitmap_git
= prepare_bitmap_walk(revs
, 0);
252 traverse_bitmap_commit_list(bitmap_git
, revs
, mark_object_seen
);
253 free_bitmap_index(bitmap_git
);
255 if (prepare_revision_walk(revs
))
256 die("revision walk setup failed");
257 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
261 revs
->ignore_missing_links
= 1;
262 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
,
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
);