9 #include "cache-tree.h"
11 #include "list-objects.h"
13 struct connectivity_progress
{
14 struct progress
*progress
;
18 static void update_progress(struct connectivity_progress
*cp
)
21 if ((cp
->count
& 1023) == 0)
22 display_progress(cp
->progress
, cp
->count
);
25 static int add_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
27 struct object
*object
= parse_object_or_die(sha1
, path
);
28 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
30 add_pending_object(revs
, object
, "");
36 * The traversal will have already marked us as SEEN, so we
37 * only need to handle any progress reporting here.
39 static void mark_object(struct object
*obj
, const char *name
, void *data
)
41 update_progress(data
);
44 static void mark_commit(struct commit
*c
, void *data
)
46 mark_object(&c
->object
, NULL
, data
);
50 struct rev_info
*revs
;
51 unsigned long timestamp
;
54 static void add_recent_object(const unsigned char *sha1
,
56 struct recent_data
*data
)
59 enum object_type type
;
61 if (mtime
<= data
->timestamp
)
65 * We do not want to call parse_object here, because
66 * inflating blobs and trees could be very expensive.
67 * However, we do need to know the correct type for
68 * later processing, and the revision machinery expects
69 * commits and tags to have been parsed.
71 type
= sha1_object_info(sha1
, NULL
);
73 die("unable to get object info for %s", sha1_to_hex(sha1
));
78 obj
= parse_object_or_die(sha1
, NULL
);
81 obj
= (struct object
*)lookup_tree(sha1
);
84 obj
= (struct object
*)lookup_blob(sha1
);
87 die("unknown object type for %s: %s",
88 sha1_to_hex(sha1
), typename(type
));
92 die("unable to lookup %s", sha1_to_hex(sha1
));
94 add_pending_object(data
->revs
, obj
, "");
97 static int add_recent_loose(const unsigned char *sha1
,
98 const char *path
, void *data
)
101 struct object
*obj
= lookup_object(sha1
);
103 if (obj
&& obj
->flags
& SEEN
)
106 if (stat(path
, &st
) < 0) {
108 * It's OK if an object went away during our iteration; this
109 * could be due to a simultaneous repack. But anything else
110 * we should abort, since we might then fail to mark objects
111 * which should not be pruned.
115 return error("unable to stat %s: %s",
116 sha1_to_hex(sha1
), strerror(errno
));
119 add_recent_object(sha1
, st
.st_mtime
, data
);
123 static int add_recent_packed(const unsigned char *sha1
,
124 struct packed_git
*p
, uint32_t pos
,
127 struct object
*obj
= lookup_object(sha1
);
129 if (obj
&& obj
->flags
& SEEN
)
131 add_recent_object(sha1
, p
->mtime
, data
);
135 int add_unseen_recent_objects_to_traversal(struct rev_info
*revs
,
136 unsigned long timestamp
)
138 struct recent_data data
;
142 data
.timestamp
= timestamp
;
144 r
= for_each_loose_object(add_recent_loose
, &data
,
145 FOR_EACH_OBJECT_LOCAL_ONLY
);
148 return for_each_packed_object(add_recent_packed
, &data
,
149 FOR_EACH_OBJECT_LOCAL_ONLY
);
152 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
153 unsigned long mark_recent
,
154 struct progress
*progress
)
156 struct connectivity_progress cp
;
159 * Set up revision parsing, and mark us as being interested
160 * in all object types, not just commits.
162 revs
->tag_objects
= 1;
163 revs
->blob_objects
= 1;
164 revs
->tree_objects
= 1;
166 /* Add all refs from the index file */
167 add_index_objects_to_pending(revs
, 0);
169 /* Add all external refs */
170 for_each_ref(add_one_ref
, revs
);
172 /* detached HEAD is not included in the list above */
173 head_ref(add_one_ref
, revs
);
175 /* Add all reflog info */
177 add_reflogs_to_pending(revs
, 0);
179 cp
.progress
= progress
;
183 * Set up the revision walk - this will move all commits
184 * from the pending list to the commit walking list.
186 if (prepare_revision_walk(revs
))
187 die("revision walk setup failed");
188 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
191 revs
->ignore_missing_links
= 1;
192 if (add_unseen_recent_objects_to_traversal(revs
, mark_recent
))
193 die("unable to mark recent objects");
194 if (prepare_revision_walk(revs
))
195 die("revision walk setup failed");
196 traverse_commit_list(revs
, mark_commit
, mark_object
, &cp
);
199 display_progress(cp
.progress
, cp
.count
);