9 #include "cache-tree.h"
11 #include "list-objects.h"
14 #include "object-store.h"
15 #include "pack-bitmap.h"
16 #include "pack-mtimes.h"
18 struct connectivity_progress
{
19 struct progress
*progress
;
23 static void update_progress(struct connectivity_progress
*cp
)
26 if ((cp
->count
& 1023) == 0)
27 display_progress(cp
->progress
, cp
->count
);
30 static int add_one_ref(const char *path
, const struct object_id
*oid
,
31 int flag
, void *cb_data
)
33 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
34 struct object
*object
;
36 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
37 warning("symbolic ref is dangling: %s", path
);
41 object
= parse_object_or_die(oid
, path
);
42 add_pending_object(revs
, object
, "");
48 * The traversal will have already marked us as SEEN, so we
49 * only need to handle any progress reporting here.
51 static void mark_object(struct object
*obj
, const char *name
, void *data
)
53 update_progress(data
);
56 static void mark_commit(struct commit
*c
, void *data
)
58 mark_object(&c
->object
, NULL
, data
);
62 struct rev_info
*revs
;
63 timestamp_t timestamp
;
64 report_recent_object_fn
*cb
;
65 int ignore_in_core_kept_packs
;
68 static void add_recent_object(const struct object_id
*oid
,
69 struct packed_git
*pack
,
72 struct recent_data
*data
)
75 enum object_type type
;
77 if (mtime
<= data
->timestamp
)
81 * We do not want to call parse_object here, because
82 * inflating blobs and trees could be very expensive.
83 * However, we do need to know the correct type for
84 * later processing, and the revision machinery expects
85 * commits and tags to have been parsed.
87 type
= oid_object_info(the_repository
, oid
, NULL
);
89 die("unable to get object info for %s", oid_to_hex(oid
));
94 obj
= parse_object_or_die(oid
, NULL
);
97 obj
= (struct object
*)lookup_tree(the_repository
, oid
);
100 obj
= (struct object
*)lookup_blob(the_repository
, oid
);
103 die("unknown object type for %s: %s",
104 oid_to_hex(oid
), type_name(type
));
108 die("unable to lookup %s", oid_to_hex(oid
));
110 add_pending_object(data
->revs
, obj
, "");
112 data
->cb(obj
, pack
, offset
, mtime
);
115 static int want_recent_object(struct recent_data
*data
,
116 const struct object_id
*oid
)
118 if (data
->ignore_in_core_kept_packs
&&
119 has_object_kept_pack(oid
, IN_CORE_KEEP_PACKS
))
124 static int add_recent_loose(const struct object_id
*oid
,
125 const char *path
, void *data
)
130 if (!want_recent_object(data
, oid
))
133 obj
= lookup_object(the_repository
, oid
);
135 if (obj
&& obj
->flags
& SEEN
)
138 if (stat(path
, &st
) < 0) {
140 * It's OK if an object went away during our iteration; this
141 * could be due to a simultaneous repack. But anything else
142 * we should abort, since we might then fail to mark objects
143 * which should not be pruned.
147 return error_errno("unable to stat %s", oid_to_hex(oid
));
150 add_recent_object(oid
, NULL
, 0, st
.st_mtime
, data
);
154 static int add_recent_packed(const struct object_id
*oid
,
155 struct packed_git
*p
, uint32_t pos
,
159 timestamp_t mtime
= p
->mtime
;
161 if (!want_recent_object(data
, oid
))
164 obj
= lookup_object(the_repository
, oid
);
166 if (obj
&& obj
->flags
& SEEN
)
169 if (load_pack_mtimes(p
) < 0)
170 die(_("could not load cruft pack .mtimes"));
171 mtime
= nth_packed_mtime(p
, pos
);
173 add_recent_object(oid
, p
, nth_packed_object_offset(p
, pos
), mtime
, data
);
177 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
178 timestamp_t timestamp
,
179 report_recent_object_fn
*cb
,
180 int ignore_in_core_kept_packs
)
182 struct recent_data data
;
183 enum for_each_object_flags flags
;
187 data
.timestamp
= timestamp
;
189 data
.ignore_in_core_kept_packs
= ignore_in_core_kept_packs
;
191 r
= for_each_loose_object(add_recent_loose
, &data
,
192 FOR_EACH_OBJECT_LOCAL_ONLY
);
196 flags
= FOR_EACH_OBJECT_LOCAL_ONLY
| FOR_EACH_OBJECT_PACK_ORDER
;
197 if (ignore_in_core_kept_packs
)
198 flags
|= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
;
200 return for_each_packed_object(add_recent_packed
, &data
, flags
);
203 static int mark_object_seen(const struct object_id
*oid
,
204 enum object_type type
,
207 struct packed_git
*found_pack
,
210 struct object
*obj
= lookup_object_by_type(the_repository
, oid
, type
);
212 die("unable to create object '%s'", oid_to_hex(oid
));
218 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
219 timestamp_t mark_recent
, struct progress
*progress
)
221 struct connectivity_progress cp
;
222 struct bitmap_index
*bitmap_git
;
225 * Set up revision parsing, and mark us as being interested
226 * in all object types, not just commits.
228 revs
->tag_objects
= 1;
229 revs
->blob_objects
= 1;
230 revs
->tree_objects
= 1;
232 /* Add all refs from the index file */
233 add_index_objects_to_pending(revs
, 0);
235 /* Add all external refs */
236 for_each_ref(add_one_ref
, revs
);
238 /* detached HEAD is not included in the list above */
239 head_ref(add_one_ref
, revs
);
240 other_head_refs(add_one_ref
, revs
);
242 /* Add all reflog info */
244 add_reflogs_to_pending(revs
, 0);
246 cp
.progress
= progress
;
249 bitmap_git
= prepare_bitmap_walk(revs
, 0);
251 traverse_bitmap_commit_list(bitmap_git
, revs
, mark_object_seen
);
252 free_bitmap_index(bitmap_git
);
254 if (prepare_revision_walk(revs
))
255 die("revision walk setup failed");
256 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
260 revs
->ignore_missing_links
= 1;
261 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
,
263 die("unable to mark recent objects");
264 if (prepare_revision_walk(revs
))
265 die("revision walk setup failed");
266 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
269 display_progress(cp
.progress
, cp
.count
);