9 #include "cache-tree.h"
11 #include "list-objects.h"
14 #include "object-store.h"
16 struct connectivity_progress
{
17 struct progress
*progress
;
21 static void update_progress(struct connectivity_progress
*cp
)
24 if ((cp
->count
& 1023) == 0)
25 display_progress(cp
->progress
, cp
->count
);
28 static int add_one_ref(const char *path
, const struct object_id
*oid
,
29 int flag
, void *cb_data
)
31 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
32 struct object
*object
;
34 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
35 warning("symbolic ref is dangling: %s", path
);
39 object
= parse_object_or_die(oid
, path
);
40 add_pending_object(revs
, object
, "");
46 * The traversal will have already marked us as SEEN, so we
47 * only need to handle any progress reporting here.
49 static void mark_object(struct object
*obj
, const char *name
, void *data
)
51 update_progress(data
);
54 static void mark_commit(struct commit
*c
, void *data
)
56 mark_object(&c
->object
, NULL
, data
);
60 struct rev_info
*revs
;
61 timestamp_t timestamp
;
64 static void add_recent_object(const struct object_id
*oid
,
66 struct recent_data
*data
)
69 enum object_type type
;
71 if (mtime
<= data
->timestamp
)
75 * We do not want to call parse_object here, because
76 * inflating blobs and trees could be very expensive.
77 * However, we do need to know the correct type for
78 * later processing, and the revision machinery expects
79 * commits and tags to have been parsed.
81 type
= oid_object_info(the_repository
, oid
, NULL
);
83 die("unable to get object info for %s", oid_to_hex(oid
));
88 obj
= parse_object_or_die(oid
, NULL
);
91 obj
= (struct object
*)lookup_tree(oid
);
94 obj
= (struct object
*)lookup_blob(oid
);
97 die("unknown object type for %s: %s",
98 oid_to_hex(oid
), type_name(type
));
102 die("unable to lookup %s", oid_to_hex(oid
));
104 add_pending_object(data
->revs
, obj
, "");
107 static int add_recent_loose(const struct object_id
*oid
,
108 const char *path
, void *data
)
111 struct object
*obj
= lookup_object(oid
->hash
);
113 if (obj
&& obj
->flags
& SEEN
)
116 if (stat(path
, &st
) < 0) {
118 * It's OK if an object went away during our iteration; this
119 * could be due to a simultaneous repack. But anything else
120 * we should abort, since we might then fail to mark objects
121 * which should not be pruned.
125 return error_errno("unable to stat %s", oid_to_hex(oid
));
128 add_recent_object(oid
, st
.st_mtime
, data
);
132 static int add_recent_packed(const struct object_id
*oid
,
133 struct packed_git
*p
, uint32_t pos
,
136 struct object
*obj
= lookup_object(oid
->hash
);
138 if (obj
&& obj
->flags
& SEEN
)
140 add_recent_object(oid
, p
->mtime
, data
);
144 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
145 timestamp_t timestamp
)
147 struct recent_data data
;
151 data
.timestamp
= timestamp
;
153 r
= for_each_loose_object(add_recent_loose
, &data
,
154 FOR_EACH_OBJECT_LOCAL_ONLY
);
157 return for_each_packed_object(add_recent_packed
, &data
,
158 FOR_EACH_OBJECT_LOCAL_ONLY
);
161 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
162 timestamp_t mark_recent
, struct progress
*progress
)
164 struct connectivity_progress cp
;
167 * Set up revision parsing, and mark us as being interested
168 * in all object types, not just commits.
170 revs
->tag_objects
= 1;
171 revs
->blob_objects
= 1;
172 revs
->tree_objects
= 1;
174 /* Add all refs from the index file */
175 add_index_objects_to_pending(revs
, 0);
177 /* Add all external refs */
178 for_each_ref(add_one_ref
, revs
);
180 /* detached HEAD is not included in the list above */
181 head_ref(add_one_ref
, revs
);
182 other_head_refs(add_one_ref
, revs
);
184 /* Add all reflog info */
186 add_reflogs_to_pending(revs
, 0);
188 cp
.progress
= progress
;
192 * Set up the revision walk - this will move all commits
193 * from the pending list to the commit walking list.
195 if (prepare_revision_walk(revs
))
196 die("revision walk setup failed");
197 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
200 revs
->ignore_missing_links
= 1;
201 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
))
202 die("unable to mark recent objects");
203 if (prepare_revision_walk(revs
))
204 die("revision walk setup failed");
205 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
208 display_progress(cp
.progress
, cp
.count
);