1 #include "git-compat-util.h"
10 #include "reachable.h"
11 #include "cache-tree.h"
13 #include "list-objects.h"
16 #include "object-store.h"
17 #include "pack-bitmap.h"
18 #include "pack-mtimes.h"
20 struct connectivity_progress
{
21 struct progress
*progress
;
25 static void update_progress(struct connectivity_progress
*cp
)
28 if ((cp
->count
& 1023) == 0)
29 display_progress(cp
->progress
, cp
->count
);
32 static int add_one_ref(const char *path
, const struct object_id
*oid
,
33 int flag
, void *cb_data
)
35 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
36 struct object
*object
;
38 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
39 warning("symbolic ref is dangling: %s", path
);
43 object
= parse_object_or_die(oid
, path
);
44 add_pending_object(revs
, object
, "");
50 * The traversal will have already marked us as SEEN, so we
51 * only need to handle any progress reporting here.
53 static void mark_object(struct object
*obj UNUSED
,
54 const char *name UNUSED
,
57 update_progress(data
);
60 static void mark_commit(struct commit
*c
, void *data
)
62 mark_object(&c
->object
, NULL
, data
);
66 struct rev_info
*revs
;
67 timestamp_t timestamp
;
68 report_recent_object_fn
*cb
;
69 int ignore_in_core_kept_packs
;
72 static void add_recent_object(const struct object_id
*oid
,
73 struct packed_git
*pack
,
76 struct recent_data
*data
)
79 enum object_type type
;
81 if (mtime
<= data
->timestamp
)
85 * We do not want to call parse_object here, because
86 * inflating blobs and trees could be very expensive.
87 * However, we do need to know the correct type for
88 * later processing, and the revision machinery expects
89 * commits and tags to have been parsed.
91 type
= oid_object_info(the_repository
, oid
, NULL
);
93 die("unable to get object info for %s", oid_to_hex(oid
));
98 obj
= parse_object_or_die(oid
, NULL
);
101 obj
= (struct object
*)lookup_tree(the_repository
, oid
);
104 obj
= (struct object
*)lookup_blob(the_repository
, oid
);
107 die("unknown object type for %s: %s",
108 oid_to_hex(oid
), type_name(type
));
112 die("unable to lookup %s", oid_to_hex(oid
));
114 add_pending_object(data
->revs
, obj
, "");
116 data
->cb(obj
, pack
, offset
, mtime
);
119 static int want_recent_object(struct recent_data
*data
,
120 const struct object_id
*oid
)
122 if (data
->ignore_in_core_kept_packs
&&
123 has_object_kept_pack(oid
, IN_CORE_KEEP_PACKS
))
128 static int add_recent_loose(const struct object_id
*oid
,
129 const char *path
, void *data
)
134 if (!want_recent_object(data
, oid
))
137 obj
= lookup_object(the_repository
, oid
);
139 if (obj
&& obj
->flags
& SEEN
)
142 if (stat(path
, &st
) < 0) {
144 * It's OK if an object went away during our iteration; this
145 * could be due to a simultaneous repack. But anything else
146 * we should abort, since we might then fail to mark objects
147 * which should not be pruned.
151 return error_errno("unable to stat %s", oid_to_hex(oid
));
154 add_recent_object(oid
, NULL
, 0, st
.st_mtime
, data
);
158 static int add_recent_packed(const struct object_id
*oid
,
159 struct packed_git
*p
,
164 timestamp_t mtime
= p
->mtime
;
166 if (!want_recent_object(data
, oid
))
169 obj
= lookup_object(the_repository
, oid
);
171 if (obj
&& obj
->flags
& SEEN
)
174 if (load_pack_mtimes(p
) < 0)
175 die(_("could not load cruft pack .mtimes"));
176 mtime
= nth_packed_mtime(p
, pos
);
178 add_recent_object(oid
, p
, nth_packed_object_offset(p
, pos
), mtime
, data
);
182 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
183 timestamp_t timestamp
,
184 report_recent_object_fn
*cb
,
185 int ignore_in_core_kept_packs
)
187 struct recent_data data
;
188 enum for_each_object_flags flags
;
192 data
.timestamp
= timestamp
;
194 data
.ignore_in_core_kept_packs
= ignore_in_core_kept_packs
;
196 r
= for_each_loose_object(add_recent_loose
, &data
,
197 FOR_EACH_OBJECT_LOCAL_ONLY
);
201 flags
= FOR_EACH_OBJECT_LOCAL_ONLY
| FOR_EACH_OBJECT_PACK_ORDER
;
202 if (ignore_in_core_kept_packs
)
203 flags
|= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
;
205 return for_each_packed_object(add_recent_packed
, &data
, flags
);
208 static int mark_object_seen(const struct object_id
*oid
,
209 enum object_type type
,
211 uint32_t name_hash UNUSED
,
212 struct packed_git
*found_pack UNUSED
,
213 off_t found_offset UNUSED
)
215 struct object
*obj
= lookup_object_by_type(the_repository
, oid
, type
);
217 die("unable to create object '%s'", oid_to_hex(oid
));
223 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
224 timestamp_t mark_recent
, struct progress
*progress
)
226 struct connectivity_progress cp
;
227 struct bitmap_index
*bitmap_git
;
230 * Set up revision parsing, and mark us as being interested
231 * in all object types, not just commits.
233 revs
->tag_objects
= 1;
234 revs
->blob_objects
= 1;
235 revs
->tree_objects
= 1;
237 /* Add all refs from the index file */
238 add_index_objects_to_pending(revs
, 0);
240 /* Add all external refs */
241 for_each_ref(add_one_ref
, revs
);
243 /* detached HEAD is not included in the list above */
244 head_ref(add_one_ref
, revs
);
245 other_head_refs(add_one_ref
, revs
);
247 /* Add all reflog info */
249 add_reflogs_to_pending(revs
, 0);
251 cp
.progress
= progress
;
254 bitmap_git
= prepare_bitmap_walk(revs
, 0);
256 traverse_bitmap_commit_list(bitmap_git
, revs
, mark_object_seen
);
257 free_bitmap_index(bitmap_git
);
259 if (prepare_revision_walk(revs
))
260 die("revision walk setup failed");
261 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
265 revs
->ignore_missing_links
= 1;
266 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
,
268 die("unable to mark recent objects");
269 if (prepare_revision_walk(revs
))
270 die("revision walk setup failed");
271 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
274 display_progress(cp
.progress
, cp
.count
);