Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "commit.h"
8 #include "blob.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "reachable.h"
12 #include "cache-tree.h"
13 #include "progress.h"
14 #include "list-objects.h"
15 #include "packfile.h"
16 #include "worktree.h"
17 #include "object-file.h"
18 #include "pack-bitmap.h"
19 #include "pack-mtimes.h"
20 #include "config.h"
21 #include "run-command.h"
22 #include "sequencer.h"
23
24 struct connectivity_progress {
25 struct progress *progress;
26 unsigned long count;
27 };
28
29 static void update_progress(struct connectivity_progress *cp)
30 {
31 cp->count++;
32 if ((cp->count & 1023) == 0)
33 display_progress(cp->progress, cp->count);
34 }
35
36 static void add_one_file(const char *path, struct rev_info *revs)
37 {
38 struct strbuf buf = STRBUF_INIT;
39 struct object_id oid;
40 struct object *object;
41
42 if (!read_oneliner(&buf, path, READ_ONELINER_SKIP_IF_EMPTY)) {
43 strbuf_release(&buf);
44 return;
45 }
46 strbuf_trim(&buf);
47 if (!get_oid_hex(buf.buf, &oid)) {
48 object = parse_object_or_die(the_repository, &oid, buf.buf);
49 add_pending_object(revs, object, "");
50 }
51 strbuf_release(&buf);
52 }
53
54 /* Mark objects recorded in rebase state files as reachable. */
55 static void add_rebase_files(struct rev_info *revs)
56 {
57 struct strbuf buf = STRBUF_INIT;
58 size_t len;
59 const char *path[] = {
60 "rebase-apply/autostash",
61 "rebase-apply/orig-head",
62 "rebase-merge/autostash",
63 "rebase-merge/orig-head",
64 };
65 struct worktree **worktrees = get_worktrees();
66
67 for (struct worktree **wt = worktrees; *wt; wt++) {
68 char *wt_gitdir = get_worktree_git_dir(*wt);
69
70 strbuf_reset(&buf);
71 strbuf_addstr(&buf, wt_gitdir);
72 strbuf_complete(&buf, '/');
73 len = buf.len;
74 for (size_t i = 0; i < ARRAY_SIZE(path); i++) {
75 strbuf_setlen(&buf, len);
76 strbuf_addstr(&buf, path[i]);
77 add_one_file(buf.buf, revs);
78 }
79
80 free(wt_gitdir);
81 }
82 strbuf_release(&buf);
83 free_worktrees(worktrees);
84 }
85
86 static int add_one_ref(const struct reference *ref, void *cb_data)
87 {
88 struct rev_info *revs = (struct rev_info *)cb_data;
89 struct object *object;
90
91 if ((ref->flags & REF_ISSYMREF) && (ref->flags & REF_ISBROKEN)) {
92 warning("symbolic ref is dangling: %s", ref->name);
93 return 0;
94 }
95
96 object = parse_object_or_die(the_repository, ref->oid, ref->name);
97 add_pending_object(revs, object, "");
98
99 return 0;
100 }
101
102 /*
103 * The traversal will have already marked us as SEEN, so we
104 * only need to handle any progress reporting here.
105 */
106 static void mark_object(struct object *obj UNUSED,
107 const char *name UNUSED,
108 void *data)
109 {
110 update_progress(data);
111 }
112
113 static void mark_commit(struct commit *c, void *data)
114 {
115 mark_object(&c->object, NULL, data);
116 }
117
118 struct recent_data {
119 struct rev_info *revs;
120 timestamp_t timestamp;
121 report_recent_object_fn *cb;
122 int ignore_in_core_kept_packs;
123
124 struct oidset extra_recent_oids;
125 int extra_recent_oids_loaded;
126 };
127
128 static int run_one_gc_recent_objects_hook(struct oidset *set,
129 const char *args)
130 {
131 struct child_process cmd = CHILD_PROCESS_INIT;
132 struct strbuf buf = STRBUF_INIT;
133 FILE *out;
134 int ret = 0;
135
136 cmd.use_shell = 1;
137 cmd.out = -1;
138
139 strvec_push(&cmd.args, args);
140
141 if (start_command(&cmd))
142 return -1;
143
144 out = xfdopen(cmd.out, "r");
145 while (strbuf_getline(&buf, out) != EOF) {
146 struct object_id oid;
147 const char *rest;
148
149 if (parse_oid_hex(buf.buf, &oid, &rest) || *rest) {
150 ret = error(_("invalid extra cruft tip: '%s'"), buf.buf);
151 break;
152 }
153
154 oidset_insert(set, &oid);
155 }
156
157 fclose(out);
158 ret |= finish_command(&cmd);
159
160 strbuf_release(&buf);
161 return ret;
162 }
163
164 static void load_gc_recent_objects(struct recent_data *data)
165 {
166 const struct string_list *programs;
167 int ret = 0;
168 size_t i;
169
170 data->extra_recent_oids_loaded = 1;
171
172 if (repo_config_get_string_multi(the_repository, "gc.recentobjectshook", &programs))
173 return;
174
175 for (i = 0; i < programs->nr; i++) {
176 ret = run_one_gc_recent_objects_hook(&data->extra_recent_oids,
177 programs->items[i].string);
178 if (ret)
179 die(_("unable to enumerate additional recent objects"));
180 }
181 }
182
183 static int obj_is_recent(const struct object_id *oid, timestamp_t mtime,
184 struct recent_data *data)
185 {
186 if (mtime > data->timestamp)
187 return 1;
188
189 if (!data->extra_recent_oids_loaded)
190 load_gc_recent_objects(data);
191 return oidset_contains(&data->extra_recent_oids, oid);
192 }
193
194 static int want_recent_object(struct recent_data *data,
195 const struct object_id *oid)
196 {
197 if (data->ignore_in_core_kept_packs &&
198 has_object_kept_pack(data->revs->repo, oid, KEPT_PACK_IN_CORE))
199 return 0;
200 return 1;
201 }
202
203 static int add_recent_object(const struct object_id *oid,
204 struct object_info *oi,
205 void *cb_data)
206 {
207 struct recent_data *data = cb_data;
208 struct object *obj;
209
210 if (!want_recent_object(data, oid) ||
211 !obj_is_recent(oid, *oi->mtimep, data))
212 return 0;
213
214 switch (*oi->typep) {
215 case OBJ_TAG:
216 case OBJ_COMMIT:
217 obj = parse_object_or_die(the_repository, oid, NULL);
218 break;
219 case OBJ_TREE:
220 obj = (struct object *)lookup_tree(the_repository, oid);
221 break;
222 case OBJ_BLOB:
223 obj = (struct object *)lookup_blob(the_repository, oid);
224 break;
225 default:
226 die("unknown object type for %s: %s",
227 oid_to_hex(oid), type_name(*oi->typep));
228 }
229
230 if (!obj)
231 die("unable to lookup %s", oid_to_hex(oid));
232 if (obj->flags & SEEN)
233 return 0;
234
235 add_pending_object(data->revs, obj, "");
236 if (data->cb) {
237 if (oi->whence == OI_PACKED)
238 data->cb(obj, oi->u.packed.pack, oi->u.packed.offset, *oi->mtimep);
239 else
240 data->cb(obj, NULL, 0, *oi->mtimep);
241 }
242
243 return 0;
244 }
245
246 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
247 timestamp_t timestamp,
248 report_recent_object_fn *cb,
249 int ignore_in_core_kept_packs)
250 {
251 struct recent_data data;
252 unsigned flags;
253 enum object_type type;
254 time_t mtime;
255 struct object_info oi = {
256 .mtimep = &mtime,
257 .typep = &type,
258 };
259 int r;
260
261 data.revs = revs;
262 data.timestamp = timestamp;
263 data.cb = cb;
264 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
265
266 oidset_init(&data.extra_recent_oids, 0);
267 data.extra_recent_oids_loaded = 0;
268
269 flags = ODB_FOR_EACH_OBJECT_LOCAL_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER;
270 if (ignore_in_core_kept_packs)
271 flags |= ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
272
273 r = odb_for_each_object(revs->repo->objects, &oi, add_recent_object, &data, flags);
274 if (r)
275 goto done;
276
277 done:
278 oidset_clear(&data.extra_recent_oids);
279
280 return r;
281 }
282
283 static int mark_object_seen(const struct object_id *oid,
284 enum object_type type,
285 int exclude UNUSED,
286 uint32_t name_hash UNUSED,
287 struct packed_git *found_pack UNUSED,
288 off_t found_offset UNUSED,
289 void *payload UNUSED)
290 {
291 struct object *obj = lookup_object_by_type(the_repository, oid, type);
292 if (!obj)
293 die("unable to create object '%s'", oid_to_hex(oid));
294
295 obj->flags |= SEEN;
296 return 0;
297 }
298
299 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
300 timestamp_t mark_recent, struct progress *progress)
301 {
302 struct connectivity_progress cp;
303 struct bitmap_index *bitmap_git;
304
305 /*
306 * Set up revision parsing, and mark us as being interested
307 * in all object types, not just commits.
308 */
309 revs->tag_objects = 1;
310 revs->blob_objects = 1;
311 revs->tree_objects = 1;
312
313 /* Add all refs from the index file */
314 add_index_objects_to_pending(revs, 0);
315
316 /* Add all external refs */
317 refs_for_each_ref(get_main_ref_store(the_repository), add_one_ref,
318 revs);
319
320 /* detached HEAD is not included in the list above */
321 refs_head_ref(get_main_ref_store(the_repository), add_one_ref, revs);
322 other_head_refs(add_one_ref, revs);
323
324 /* rebase autostash and orig-head */
325 add_rebase_files(revs);
326
327 /* Add all reflog info */
328 if (mark_reflog)
329 add_reflogs_to_pending(revs, 0);
330
331 cp.progress = progress;
332 cp.count = 0;
333
334 bitmap_git = prepare_bitmap_walk(revs, 0);
335 if (bitmap_git) {
336 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
337 free_bitmap_index(bitmap_git);
338 } else {
339 if (prepare_revision_walk(revs))
340 die("revision walk setup failed");
341 traverse_commit_list(revs, mark_commit, mark_object, &cp);
342 }
343
344 if (mark_recent) {
345 revs->ignore_missing_links = 1;
346 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
347 NULL, 0))
348 die("unable to mark recent objects");
349 if (prepare_revision_walk(revs))
350 die("revision walk setup failed");
351 traverse_commit_list(revs, mark_commit, mark_object, &cp);
352 }
353
354 display_progress(cp.progress, cp.count);
355 }