9 #include "cache-tree.h"
11 #include "list-objects.h"
15 struct connectivity_progress
{
16 struct progress
*progress
;
20 static void update_progress(struct connectivity_progress
*cp
)
23 if ((cp
->count
& 1023) == 0)
24 display_progress(cp
->progress
, cp
->count
);
27 static int add_one_ref(const char *path
, const struct object_id
*oid
,
28 int flag
, void *cb_data
)
30 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
31 struct object
*object
;
33 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
34 warning("symbolic ref is dangling: %s", path
);
38 object
= parse_object_or_die(oid
, path
);
39 add_pending_object(revs
, object
, "");
45 * The traversal will have already marked us as SEEN, so we
46 * only need to handle any progress reporting here.
48 static void mark_object(struct object
*obj
, const char *name
, void *data
)
50 update_progress(data
);
53 static void mark_commit(struct commit
*c
, void *data
)
55 mark_object(&c
->object
, NULL
, data
);
59 struct rev_info
*revs
;
60 timestamp_t timestamp
;
63 static void add_recent_object(const struct object_id
*oid
,
65 struct recent_data
*data
)
68 enum object_type type
;
70 if (mtime
<= data
->timestamp
)
74 * We do not want to call parse_object here, because
75 * inflating blobs and trees could be very expensive.
76 * However, we do need to know the correct type for
77 * later processing, and the revision machinery expects
78 * commits and tags to have been parsed.
80 type
= sha1_object_info(oid
->hash
, NULL
);
82 die("unable to get object info for %s", oid_to_hex(oid
));
87 obj
= parse_object_or_die(oid
, NULL
);
90 obj
= (struct object
*)lookup_tree(oid
);
93 obj
= (struct object
*)lookup_blob(oid
);
96 die("unknown object type for %s: %s",
97 oid_to_hex(oid
), typename(type
));
101 die("unable to lookup %s", oid_to_hex(oid
));
103 add_pending_object(data
->revs
, obj
, "");
106 static int add_recent_loose(const struct object_id
*oid
,
107 const char *path
, void *data
)
110 struct object
*obj
= lookup_object(oid
->hash
);
112 if (obj
&& obj
->flags
& SEEN
)
115 if (stat(path
, &st
) < 0) {
117 * It's OK if an object went away during our iteration; this
118 * could be due to a simultaneous repack. But anything else
119 * we should abort, since we might then fail to mark objects
120 * which should not be pruned.
124 return error_errno("unable to stat %s", oid_to_hex(oid
));
127 add_recent_object(oid
, st
.st_mtime
, data
);
131 static int add_recent_packed(const struct object_id
*oid
,
132 struct packed_git
*p
, uint32_t pos
,
135 struct object
*obj
= lookup_object(oid
->hash
);
137 if (obj
&& obj
->flags
& SEEN
)
139 add_recent_object(oid
, p
->mtime
, data
);
143 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
144 timestamp_t timestamp
)
146 struct recent_data data
;
150 data
.timestamp
= timestamp
;
152 r
= for_each_loose_object(add_recent_loose
, &data
,
153 FOR_EACH_OBJECT_LOCAL_ONLY
);
156 return for_each_packed_object(add_recent_packed
, &data
,
157 FOR_EACH_OBJECT_LOCAL_ONLY
);
160 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
161 timestamp_t mark_recent
, struct progress
*progress
)
163 struct connectivity_progress cp
;
166 * Set up revision parsing, and mark us as being interested
167 * in all object types, not just commits.
169 revs
->tag_objects
= 1;
170 revs
->blob_objects
= 1;
171 revs
->tree_objects
= 1;
173 /* Add all refs from the index file */
174 add_index_objects_to_pending(revs
, 0);
176 /* Add all external refs */
177 for_each_ref(add_one_ref
, revs
);
179 /* detached HEAD is not included in the list above */
180 head_ref(add_one_ref
, revs
);
181 other_head_refs(add_one_ref
, revs
);
183 /* Add all reflog info */
185 add_reflogs_to_pending(revs
, 0);
187 cp
.progress
= progress
;
191 * Set up the revision walk - this will move all commits
192 * from the pending list to the commit walking list.
194 if (prepare_revision_walk(revs
))
195 die("revision walk setup failed");
196 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
199 revs
->ignore_missing_links
= 1;
200 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
))
201 die("unable to mark recent objects");
202 if (prepare_revision_walk(revs
))
203 die("revision walk setup failed");
204 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
207 display_progress(cp
.progress
, cp
.count
);