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 UNUSED
,
53 const char *name UNUSED
,
56 update_progress(data
);
59 static void mark_commit(struct commit
*c
, void *data
)
61 mark_object(&c
->object
, NULL
, data
);
65 struct rev_info
*revs
;
66 timestamp_t timestamp
;
67 report_recent_object_fn
*cb
;
68 int ignore_in_core_kept_packs
;
71 static void add_recent_object(const struct object_id
*oid
,
72 struct packed_git
*pack
,
75 struct recent_data
*data
)
78 enum object_type type
;
80 if (mtime
<= data
->timestamp
)
84 * We do not want to call parse_object here, because
85 * inflating blobs and trees could be very expensive.
86 * However, we do need to know the correct type for
87 * later processing, and the revision machinery expects
88 * commits and tags to have been parsed.
90 type
= oid_object_info(the_repository
, oid
, NULL
);
92 die("unable to get object info for %s", oid_to_hex(oid
));
97 obj
= parse_object_or_die(oid
, NULL
);
100 obj
= (struct object
*)lookup_tree(the_repository
, oid
);
103 obj
= (struct object
*)lookup_blob(the_repository
, oid
);
106 die("unknown object type for %s: %s",
107 oid_to_hex(oid
), type_name(type
));
111 die("unable to lookup %s", oid_to_hex(oid
));
113 add_pending_object(data
->revs
, obj
, "");
115 data
->cb(obj
, pack
, offset
, mtime
);
118 static int want_recent_object(struct recent_data
*data
,
119 const struct object_id
*oid
)
121 if (data
->ignore_in_core_kept_packs
&&
122 has_object_kept_pack(oid
, IN_CORE_KEEP_PACKS
))
127 static int add_recent_loose(const struct object_id
*oid
,
128 const char *path
, void *data
)
133 if (!want_recent_object(data
, oid
))
136 obj
= lookup_object(the_repository
, oid
);
138 if (obj
&& obj
->flags
& SEEN
)
141 if (stat(path
, &st
) < 0) {
143 * It's OK if an object went away during our iteration; this
144 * could be due to a simultaneous repack. But anything else
145 * we should abort, since we might then fail to mark objects
146 * which should not be pruned.
150 return error_errno("unable to stat %s", oid_to_hex(oid
));
153 add_recent_object(oid
, NULL
, 0, st
.st_mtime
, data
);
157 static int add_recent_packed(const struct object_id
*oid
,
158 struct packed_git
*p
,
163 timestamp_t mtime
= p
->mtime
;
165 if (!want_recent_object(data
, oid
))
168 obj
= lookup_object(the_repository
, oid
);
170 if (obj
&& obj
->flags
& SEEN
)
173 if (load_pack_mtimes(p
) < 0)
174 die(_("could not load cruft pack .mtimes"));
175 mtime
= nth_packed_mtime(p
, pos
);
177 add_recent_object(oid
, p
, nth_packed_object_offset(p
, pos
), mtime
, data
);
181 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
182 timestamp_t timestamp
,
183 report_recent_object_fn
*cb
,
184 int ignore_in_core_kept_packs
)
186 struct recent_data data
;
187 enum for_each_object_flags flags
;
191 data
.timestamp
= timestamp
;
193 data
.ignore_in_core_kept_packs
= ignore_in_core_kept_packs
;
195 r
= for_each_loose_object(add_recent_loose
, &data
,
196 FOR_EACH_OBJECT_LOCAL_ONLY
);
200 flags
= FOR_EACH_OBJECT_LOCAL_ONLY
| FOR_EACH_OBJECT_PACK_ORDER
;
201 if (ignore_in_core_kept_packs
)
202 flags
|= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
;
204 return for_each_packed_object(add_recent_packed
, &data
, flags
);
207 static int mark_object_seen(const struct object_id
*oid
,
208 enum object_type type
,
210 uint32_t name_hash UNUSED
,
211 struct packed_git
*found_pack UNUSED
,
212 off_t found_offset UNUSED
)
214 struct object
*obj
= lookup_object_by_type(the_repository
, oid
, type
);
216 die("unable to create object '%s'", oid_to_hex(oid
));
222 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
223 timestamp_t mark_recent
, struct progress
*progress
)
225 struct connectivity_progress cp
;
226 struct bitmap_index
*bitmap_git
;
229 * Set up revision parsing, and mark us as being interested
230 * in all object types, not just commits.
232 revs
->tag_objects
= 1;
233 revs
->blob_objects
= 1;
234 revs
->tree_objects
= 1;
236 /* Add all refs from the index file */
237 add_index_objects_to_pending(revs
, 0);
239 /* Add all external refs */
240 for_each_ref(add_one_ref
, revs
);
242 /* detached HEAD is not included in the list above */
243 head_ref(add_one_ref
, revs
);
244 other_head_refs(add_one_ref
, revs
);
246 /* Add all reflog info */
248 add_reflogs_to_pending(revs
, 0);
250 cp
.progress
= progress
;
253 bitmap_git
= prepare_bitmap_walk(revs
, 0);
255 traverse_bitmap_commit_list(bitmap_git
, revs
, mark_object_seen
);
256 free_bitmap_index(bitmap_git
);
258 if (prepare_revision_walk(revs
))
259 die("revision walk setup failed");
260 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
264 revs
->ignore_missing_links
= 1;
265 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
,
267 die("unable to mark recent objects");
268 if (prepare_revision_walk(revs
))
269 die("revision walk setup failed");
270 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
273 display_progress(cp
.progress
, cp
.count
);