blob: 0ce8f83e56a9b42400b8ec792b19deb3a69ddb6e [file] [log] [blame]
Elijah Newren98750582023-03-21 06:26:04 +00001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00003#include "hex.h"
Junio C Hamano94421472007-01-06 02:16:17 -08004#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"
Nguyễn Thái Ngọc Duydc347192011-11-05 19:00:08 +070012#include "progress.h"
Jeff King5f78a432014-10-15 18:37:28 -040013#include "list-objects.h"
Jonathan Tan7709f462017-08-18 15:20:38 -070014#include "packfile.h"
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +070015#include "worktree.h"
Elijah Newrena034e912023-05-16 06:34:06 +000016#include "object-store-ll.h"
Jeff Kingfde67d62019-02-13 23:37:43 -050017#include "pack-bitmap.h"
Taylor Blaufb546d62022-05-20 19:17:57 -040018#include "pack-mtimes.h"
Taylor Blau4dc16e22023-06-07 18:58:17 -040019#include "config.h"
20#include "run-command.h"
Junio C Hamano94421472007-01-06 02:16:17 -080021
Jeff King0b26abc2011-11-08 00:37:00 -050022struct connectivity_progress {
23 struct progress *progress;
24 unsigned long count;
25};
26
27static void update_progress(struct connectivity_progress *cp)
28{
29 cp->count++;
30 if ((cp->count & 1023) == 0)
31 display_progress(cp->progress, cp->count);
32}
33
Michael Haggerty635170f2015-05-25 18:39:00 +000034static int add_one_ref(const char *path, const struct object_id *oid,
35 int flag, void *cb_data)
Junio C Hamano94421472007-01-06 02:16:17 -080036{
Junio C Hamano94421472007-01-06 02:16:17 -080037 struct rev_info *revs = (struct rev_info *)cb_data;
Johannes Schindelin14886b42015-09-28 16:01:25 +020038 struct object *object;
Junio C Hamano94421472007-01-06 02:16:17 -080039
Johannes Schindelin14886b42015-09-28 16:01:25 +020040 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
41 warning("symbolic ref is dangling: %s", path);
42 return 0;
43 }
44
brian m. carlsonc251c832017-05-06 22:10:38 +000045 object = parse_object_or_die(oid, path);
Junio C Hamano94421472007-01-06 02:16:17 -080046 add_pending_object(revs, object, "");
47
48 return 0;
49}
50
Jeff King5f78a432014-10-15 18:37:28 -040051/*
52 * The traversal will have already marked us as SEEN, so we
53 * only need to handle any progress reporting here.
54 */
Jeff Kingc50dca22023-02-24 01:39:22 -050055static void mark_object(struct object *obj UNUSED,
56 const char *name UNUSED,
57 void *data)
Jeff King5f78a432014-10-15 18:37:28 -040058{
59 update_progress(data);
60}
61
62static void mark_commit(struct commit *c, void *data)
63{
Jeff Kingde1e67d2016-02-11 17:28:36 -050064 mark_object(&c->object, NULL, data);
Jeff King5f78a432014-10-15 18:37:28 -040065}
66
Jeff Kingd3038d22014-10-15 18:41:35 -040067struct recent_data {
68 struct rev_info *revs;
Johannes Schindelindddbad72017-04-26 21:29:31 +020069 timestamp_t timestamp;
Taylor Blau2fb90402022-05-20 19:17:54 -040070 report_recent_object_fn *cb;
71 int ignore_in_core_kept_packs;
Taylor Blau4dc16e22023-06-07 18:58:17 -040072
73 struct oidset extra_recent_oids;
74 int extra_recent_oids_loaded;
Jeff Kingd3038d22014-10-15 18:41:35 -040075};
76
Taylor Blau4dc16e22023-06-07 18:58:17 -040077static int run_one_gc_recent_objects_hook(struct oidset *set,
78 const char *args)
79{
80 struct child_process cmd = CHILD_PROCESS_INIT;
81 struct strbuf buf = STRBUF_INIT;
82 FILE *out;
83 int ret = 0;
84
85 cmd.use_shell = 1;
86 cmd.out = -1;
87
88 strvec_push(&cmd.args, args);
89
90 if (start_command(&cmd))
91 return -1;
92
93 out = xfdopen(cmd.out, "r");
94 while (strbuf_getline(&buf, out) != EOF) {
95 struct object_id oid;
96 const char *rest;
97
98 if (parse_oid_hex(buf.buf, &oid, &rest) || *rest) {
99 ret = error(_("invalid extra cruft tip: '%s'"), buf.buf);
100 break;
101 }
102
103 oidset_insert(set, &oid);
104 }
105
106 fclose(out);
107 ret |= finish_command(&cmd);
108
109 strbuf_release(&buf);
110 return ret;
111}
112
113static void load_gc_recent_objects(struct recent_data *data)
114{
115 const struct string_list *programs;
116 int ret = 0;
117 size_t i;
118
119 data->extra_recent_oids_loaded = 1;
120
121 if (git_config_get_string_multi("gc.recentobjectshook", &programs))
122 return;
123
124 for (i = 0; i < programs->nr; i++) {
125 ret = run_one_gc_recent_objects_hook(&data->extra_recent_oids,
126 programs->items[i].string);
127 if (ret)
128 die(_("unable to enumerate additional recent objects"));
129 }
130}
131
Taylor Blau01e9ca42023-06-07 18:58:12 -0400132static int obj_is_recent(const struct object_id *oid, timestamp_t mtime,
133 struct recent_data *data)
134{
Taylor Blau4dc16e22023-06-07 18:58:17 -0400135 if (mtime > data->timestamp)
136 return 1;
137
138 if (!data->extra_recent_oids_loaded)
139 load_gc_recent_objects(data);
140 return oidset_contains(&data->extra_recent_oids, oid);
Taylor Blau01e9ca42023-06-07 18:58:12 -0400141}
142
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000143static void add_recent_object(const struct object_id *oid,
Taylor Blau2fb90402022-05-20 19:17:54 -0400144 struct packed_git *pack,
145 off_t offset,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200146 timestamp_t mtime,
Jeff Kingd3038d22014-10-15 18:41:35 -0400147 struct recent_data *data)
148{
149 struct object *obj;
150 enum object_type type;
151
Taylor Blau01e9ca42023-06-07 18:58:12 -0400152 if (!obj_is_recent(oid, mtime, data))
Jeff Kingd3038d22014-10-15 18:41:35 -0400153 return;
154
155 /*
156 * We do not want to call parse_object here, because
157 * inflating blobs and trees could be very expensive.
158 * However, we do need to know the correct type for
159 * later processing, and the revision machinery expects
160 * commits and tags to have been parsed.
161 */
Stefan Beller0df8e962018-04-25 11:20:59 -0700162 type = oid_object_info(the_repository, oid, NULL);
Jeff Kingd3038d22014-10-15 18:41:35 -0400163 if (type < 0)
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000164 die("unable to get object info for %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -0400165
166 switch (type) {
167 case OBJ_TAG:
168 case OBJ_COMMIT:
brian m. carlsonc251c832017-05-06 22:10:38 +0000169 obj = parse_object_or_die(oid, NULL);
Jeff Kingd3038d22014-10-15 18:41:35 -0400170 break;
171 case OBJ_TREE:
Stefan Bellerf86bcc72018-06-28 18:21:56 -0700172 obj = (struct object *)lookup_tree(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -0400173 break;
174 case OBJ_BLOB:
Stefan Bellerda14a7f2018-06-28 18:21:55 -0700175 obj = (struct object *)lookup_blob(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -0400176 break;
177 default:
178 die("unknown object type for %s: %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800179 oid_to_hex(oid), type_name(type));
Jeff Kingd3038d22014-10-15 18:41:35 -0400180 }
181
182 if (!obj)
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000183 die("unable to lookup %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -0400184
185 add_pending_object(data->revs, obj, "");
Taylor Blau2fb90402022-05-20 19:17:54 -0400186 if (data->cb)
187 data->cb(obj, pack, offset, mtime);
188}
189
190static int want_recent_object(struct recent_data *data,
191 const struct object_id *oid)
192{
193 if (data->ignore_in_core_kept_packs &&
194 has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
195 return 0;
196 return 1;
Jeff Kingd3038d22014-10-15 18:41:35 -0400197}
198
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000199static int add_recent_loose(const struct object_id *oid,
Jeff Kingd3038d22014-10-15 18:41:35 -0400200 const char *path, void *data)
201{
202 struct stat st;
Taylor Blau2fb90402022-05-20 19:17:54 -0400203 struct object *obj;
204
205 if (!want_recent_object(data, oid))
206 return 0;
207
208 obj = lookup_object(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -0400209
210 if (obj && obj->flags & SEEN)
211 return 0;
212
213 if (stat(path, &st) < 0) {
214 /*
215 * It's OK if an object went away during our iteration; this
216 * could be due to a simultaneous repack. But anything else
217 * we should abort, since we might then fail to mark objects
218 * which should not be pruned.
219 */
220 if (errno == ENOENT)
221 return 0;
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000222 return error_errno("unable to stat %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -0400223 }
224
Taylor Blau2fb90402022-05-20 19:17:54 -0400225 add_recent_object(oid, NULL, 0, st.st_mtime, data);
Jeff Kingd3038d22014-10-15 18:41:35 -0400226 return 0;
227}
228
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000229static int add_recent_packed(const struct object_id *oid,
Jeff Kingbe252d32023-02-24 01:39:24 -0500230 struct packed_git *p,
231 uint32_t pos,
Jeff Kingd3038d22014-10-15 18:41:35 -0400232 void *data)
233{
Taylor Blau2fb90402022-05-20 19:17:54 -0400234 struct object *obj;
Taylor Blaufb546d62022-05-20 19:17:57 -0400235 timestamp_t mtime = p->mtime;
Taylor Blau2fb90402022-05-20 19:17:54 -0400236
237 if (!want_recent_object(data, oid))
238 return 0;
239
240 obj = lookup_object(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -0400241
242 if (obj && obj->flags & SEEN)
243 return 0;
Taylor Blaufb546d62022-05-20 19:17:57 -0400244 if (p->is_cruft) {
245 if (load_pack_mtimes(p) < 0)
246 die(_("could not load cruft pack .mtimes"));
247 mtime = nth_packed_mtime(p, pos);
248 }
249 add_recent_object(oid, p, nth_packed_object_offset(p, pos), mtime, data);
Jeff Kingd3038d22014-10-15 18:41:35 -0400250 return 0;
251}
252
Jeff Kingabcb8652014-10-15 18:42:09 -0400253int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
Taylor Blau2fb90402022-05-20 19:17:54 -0400254 timestamp_t timestamp,
255 report_recent_object_fn *cb,
256 int ignore_in_core_kept_packs)
Jeff Kingd3038d22014-10-15 18:41:35 -0400257{
258 struct recent_data data;
Taylor Blau2fb90402022-05-20 19:17:54 -0400259 enum for_each_object_flags flags;
Jeff Kingd3038d22014-10-15 18:41:35 -0400260 int r;
261
262 data.revs = revs;
263 data.timestamp = timestamp;
Taylor Blau2fb90402022-05-20 19:17:54 -0400264 data.cb = cb;
265 data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;
Jeff Kingd3038d22014-10-15 18:41:35 -0400266
Taylor Blau4dc16e22023-06-07 18:58:17 -0400267 oidset_init(&data.extra_recent_oids, 0);
268 data.extra_recent_oids_loaded = 0;
269
Jeff King1385bb72015-03-27 07:32:41 -0400270 r = for_each_loose_object(add_recent_loose, &data,
271 FOR_EACH_OBJECT_LOCAL_ONLY);
Jeff Kingd3038d22014-10-15 18:41:35 -0400272 if (r)
Taylor Blau4dc16e22023-06-07 18:58:17 -0400273 goto done;
Taylor Blau2fb90402022-05-20 19:17:54 -0400274
275 flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
276 if (ignore_in_core_kept_packs)
277 flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;
278
Taylor Blau4dc16e22023-06-07 18:58:17 -0400279 r = for_each_packed_object(add_recent_packed, &data, flags);
280
281done:
282 oidset_clear(&data.extra_recent_oids);
283
284 return r;
Jeff Kingd3038d22014-10-15 18:41:35 -0400285}
286
Jeff Kingfde67d62019-02-13 23:37:43 -0500287static int mark_object_seen(const struct object_id *oid,
288 enum object_type type,
Jeff Kingc50dca22023-02-24 01:39:22 -0500289 int exclude UNUSED,
290 uint32_t name_hash UNUSED,
291 struct packed_git *found_pack UNUSED,
292 off_t found_offset UNUSED)
Jeff Kingfde67d62019-02-13 23:37:43 -0500293{
294 struct object *obj = lookup_object_by_type(the_repository, oid, type);
295 if (!obj)
296 die("unable to create object '%s'", oid_to_hex(oid));
297
298 obj->flags |= SEEN;
299 return 0;
300}
301
Nguyễn Thái Ngọc Duydc347192011-11-05 19:00:08 +0700302void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200303 timestamp_t mark_recent, struct progress *progress)
Junio C Hamano94421472007-01-06 02:16:17 -0800304{
Jeff King0b26abc2011-11-08 00:37:00 -0500305 struct connectivity_progress cp;
Jeff Kingfde67d62019-02-13 23:37:43 -0500306 struct bitmap_index *bitmap_git;
Jeff King0b26abc2011-11-08 00:37:00 -0500307
Junio C Hamano94421472007-01-06 02:16:17 -0800308 /*
309 * Set up revision parsing, and mark us as being interested
310 * in all object types, not just commits.
311 */
312 revs->tag_objects = 1;
313 revs->blob_objects = 1;
314 revs->tree_objects = 1;
315
316 /* Add all refs from the index file */
Jeff King1be111d2014-10-16 20:44:30 -0400317 add_index_objects_to_pending(revs, 0);
Junio C Hamano94421472007-01-06 02:16:17 -0800318
319 /* Add all external refs */
Michael Haggerty635170f2015-05-25 18:39:00 +0000320 for_each_ref(add_one_ref, revs);
Junio C Hamano94421472007-01-06 02:16:17 -0800321
Max Kirillovc40fdd02014-09-03 19:14:10 +0300322 /* detached HEAD is not included in the list above */
Michael Haggerty635170f2015-05-25 18:39:00 +0000323 head_ref(add_one_ref, revs);
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700324 other_head_refs(add_one_ref, revs);
Max Kirillovc40fdd02014-09-03 19:14:10 +0300325
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500326 /* Add all reflog info */
Junio C Hamano94421472007-01-06 02:16:17 -0800327 if (mark_reflog)
Jeff King718ccc92014-10-15 18:38:31 -0400328 add_reflogs_to_pending(revs, 0);
Junio C Hamano94421472007-01-06 02:16:17 -0800329
Jeff King0b26abc2011-11-08 00:37:00 -0500330 cp.progress = progress;
331 cp.count = 0;
332
Derrick Stolee09d4a792022-03-09 16:01:35 +0000333 bitmap_git = prepare_bitmap_walk(revs, 0);
Jeff Kingfde67d62019-02-13 23:37:43 -0500334 if (bitmap_git) {
Jeff King4eb707e2020-02-14 13:22:27 -0500335 traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
Jeff Kingfde67d62019-02-13 23:37:43 -0500336 free_bitmap_index(bitmap_git);
Jeff King2ba582b2021-04-28 11:42:43 -0400337 } else {
338 if (prepare_revision_walk(revs))
339 die("revision walk setup failed");
340 traverse_commit_list(revs, mark_commit, mark_object, &cp);
Jeff Kingfde67d62019-02-13 23:37:43 -0500341 }
342
Jeff Kingd3038d22014-10-15 18:41:35 -0400343 if (mark_recent) {
344 revs->ignore_missing_links = 1;
Taylor Blau2fb90402022-05-20 19:17:54 -0400345 if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
346 NULL, 0))
Jeff Kingd3038d22014-10-15 18:41:35 -0400347 die("unable to mark recent objects");
348 if (prepare_revision_walk(revs))
349 die("revision walk setup failed");
350 traverse_commit_list(revs, mark_commit, mark_object, &cp);
351 }
352
Jeff King0b26abc2011-11-08 00:37:00 -0500353 display_progress(cp.progress, cp.count);
Junio C Hamano94421472007-01-06 02:16:17 -0800354}