]> git.ipfire.org Git - thirdparty/git.git/blob - reachable.c
Merge branch 'ow/ref-filter-omit-empty'
[thirdparty/git.git] / reachable.c
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "refs.h"
5 #include "tag.h"
6 #include "commit.h"
7 #include "blob.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "reachable.h"
11 #include "cache-tree.h"
12 #include "progress.h"
13 #include "list-objects.h"
14 #include "packfile.h"
15 #include "worktree.h"
16 #include "object-store.h"
17 #include "pack-bitmap.h"
18 #include "pack-mtimes.h"
19
20 struct connectivity_progress {
21 struct progress *progress;
22 unsigned long count;
23 };
24
25 static void update_progress(struct connectivity_progress *cp)
26 {
27 cp->count++;
28 if ((cp->count & 1023) == 0)
29 display_progress(cp->progress, cp->count);
30 }
31
32 static int add_one_ref(const char *path, const struct object_id *oid,
33 int flag, void *cb_data)
34 {
35 struct rev_info *revs = (struct rev_info *)cb_data;
36 struct object *object;
37
38 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
39 warning("symbolic ref is dangling: %s", path);
40 return 0;
41 }
42
43 object = parse_object_or_die(oid, path);
44 add_pending_object(revs, object, "");
45
46 return 0;
47 }
48
49 /*
50 * The traversal will have already marked us as SEEN, so we
51 * only need to handle any progress reporting here.
52 */
53 static void mark_object(struct object *obj UNUSED,
54 const char *name UNUSED,
55 void *data)
56 {
57 update_progress(data);
58 }
59
60 static void mark_commit(struct commit *c, void *data)
61 {
62 mark_object(&c->object, NULL, data);
63 }
64
65 struct recent_data {
66 struct rev_info *revs;
67 timestamp_t timestamp;
68 report_recent_object_fn *cb;
69 int ignore_in_core_kept_packs;
70 };
71
72 static void add_recent_object(const struct object_id *oid,
73 struct packed_git *pack,
74 off_t offset,
75 timestamp_t mtime,
76 struct recent_data *data)
77 {
78 struct object *obj;
79 enum object_type type;
80
81 if (mtime <= data->timestamp)
82 return;
83
84 /*
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.
90 */
91 type = oid_object_info(the_repository, oid, NULL);
92 if (type < 0)
93 die("unable to get object info for %s", oid_to_hex(oid));
94
95 switch (type) {
96 case OBJ_TAG:
97 case OBJ_COMMIT:
98 obj = parse_object_or_die(oid, NULL);
99 break;
100 case OBJ_TREE:
101 obj = (struct object *)lookup_tree(the_repository, oid);
102 break;
103 case OBJ_BLOB:
104 obj = (struct object *)lookup_blob(the_repository, oid);
105 break;
106 default:
107 die("unknown object type for %s: %s",
108 oid_to_hex(oid), type_name(type));
109 }
110
111 if (!obj)
112 die("unable to lookup %s", oid_to_hex(oid));
113
114 add_pending_object(data->revs, obj, "");
115 if (data->cb)
116 data->cb(obj, pack, offset, mtime);
117 }
118
119 static int want_recent_object(struct recent_data *data,
120 const struct object_id *oid)
121 {
122 if (data->ignore_in_core_kept_packs &&
123 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
124 return 0;
125 return 1;
126 }
127
128 static int add_recent_loose(const struct object_id *oid,
129 const char *path, void *data)
130 {
131 struct stat st;
132 struct object *obj;
133
134 if (!want_recent_object(data, oid))
135 return 0;
136
137 obj = lookup_object(the_repository, oid);
138
139 if (obj && obj->flags & SEEN)
140 return 0;
141
142 if (stat(path, &st) < 0) {
143 /*
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.
148 */
149 if (errno == ENOENT)
150 return 0;
151 return error_errno("unable to stat %s", oid_to_hex(oid));
152 }
153
154 add_recent_object(oid, NULL, 0, st.st_mtime, data);
155 return 0;
156 }
157
158 static int add_recent_packed(const struct object_id *oid,
159 struct packed_git *p,
160 uint32_t pos,
161 void *data)
162 {
163 struct object *obj;
164 timestamp_t mtime = p->mtime;
165
166 if (!want_recent_object(data, oid))
167 return 0;
168
169 obj = lookup_object(the_repository, oid);
170
171 if (obj && obj->flags & SEEN)
172 return 0;
173 if (p->is_cruft) {
174 if (load_pack_mtimes(p) < 0)
175 die(_("could not load cruft pack .mtimes"));
176 mtime = nth_packed_mtime(p, pos);
177 }
178 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
179 return 0;
180 }
181
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)
186 {
187 struct recent_data data;
188 enum for_each_object_flags flags;
189 int r;
190
191 data.revs = revs;
192 data.timestamp = timestamp;
193 data.cb = cb;
194 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
195
196 r = for_each_loose_object(add_recent_loose, &data,
197 FOR_EACH_OBJECT_LOCAL_ONLY);
198 if (r)
199 return r;
200
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;
204
205 return for_each_packed_object(add_recent_packed, &data, flags);
206 }
207
208 static int mark_object_seen(const struct object_id *oid,
209 enum object_type type,
210 int exclude UNUSED,
211 uint32_t name_hash UNUSED,
212 struct packed_git *found_pack UNUSED,
213 off_t found_offset UNUSED)
214 {
215 struct object *obj = lookup_object_by_type(the_repository, oid, type);
216 if (!obj)
217 die("unable to create object '%s'", oid_to_hex(oid));
218
219 obj->flags |= SEEN;
220 return 0;
221 }
222
223 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
224 timestamp_t mark_recent, struct progress *progress)
225 {
226 struct connectivity_progress cp;
227 struct bitmap_index *bitmap_git;
228
229 /*
230 * Set up revision parsing, and mark us as being interested
231 * in all object types, not just commits.
232 */
233 revs->tag_objects = 1;
234 revs->blob_objects = 1;
235 revs->tree_objects = 1;
236
237 /* Add all refs from the index file */
238 add_index_objects_to_pending(revs, 0);
239
240 /* Add all external refs */
241 for_each_ref(add_one_ref, revs);
242
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);
246
247 /* Add all reflog info */
248 if (mark_reflog)
249 add_reflogs_to_pending(revs, 0);
250
251 cp.progress = progress;
252 cp.count = 0;
253
254 bitmap_git = prepare_bitmap_walk(revs, 0);
255 if (bitmap_git) {
256 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
257 free_bitmap_index(bitmap_git);
258 } else {
259 if (prepare_revision_walk(revs))
260 die("revision walk setup failed");
261 traverse_commit_list(revs, mark_commit, mark_object, &cp);
262 }
263
264 if (mark_recent) {
265 revs->ignore_missing_links = 1;
266 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
267 NULL, 0))
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);
272 }
273
274 display_progress(cp.progress, cp.count);
275 }