| 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(the_repository); |
| 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->source_infop->source->type == ODB_SOURCE_PACKED) |
| 238 | data->cb(obj, oi->source_infop->u.packed.pack, |
| 239 | oi->source_infop->u.packed.offset, *oi->mtimep); |
| 240 | else |
| 241 | data->cb(obj, NULL, 0, *oi->mtimep); |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int add_unseen_recent_objects_to_traversal(struct rev_info *revs, |
| 248 | timestamp_t timestamp, |
| 249 | report_recent_object_fn *cb, |
| 250 | int ignore_in_core_kept_packs) |
| 251 | { |
| 252 | struct recent_data data; |
| 253 | unsigned flags; |
| 254 | enum object_type type; |
| 255 | time_t mtime; |
| 256 | struct odb_source_info source_info; |
| 257 | struct object_info oi = { |
| 258 | .mtimep = &mtime, |
| 259 | .typep = &type, |
| 260 | .source_infop = &source_info, |
| 261 | }; |
| 262 | int r; |
| 263 | |
| 264 | data.revs = revs; |
| 265 | data.timestamp = timestamp; |
| 266 | data.cb = cb; |
| 267 | data.ignore_in_core_kept_packs = ignore_in_core_kept_packs; |
| 268 | |
| 269 | oidset_init(&data.extra_recent_oids, 0); |
| 270 | data.extra_recent_oids_loaded = 0; |
| 271 | |
| 272 | flags = ODB_FOR_EACH_OBJECT_LOCAL_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER; |
| 273 | if (ignore_in_core_kept_packs) |
| 274 | flags |= ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS; |
| 275 | |
| 276 | r = odb_for_each_object(revs->repo->objects, &oi, add_recent_object, &data, flags); |
| 277 | if (r) |
| 278 | goto done; |
| 279 | |
| 280 | done: |
| 281 | oidset_clear(&data.extra_recent_oids); |
| 282 | |
| 283 | return r; |
| 284 | } |
| 285 | |
| 286 | static int mark_object_seen(const struct object_id *oid, |
| 287 | enum object_type type, |
| 288 | int exclude UNUSED, |
| 289 | uint32_t name_hash UNUSED, |
| 290 | struct packed_git *found_pack UNUSED, |
| 291 | off_t found_offset UNUSED, |
| 292 | void *payload UNUSED) |
| 293 | { |
| 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 | |
| 302 | void mark_reachable_objects(struct rev_info *revs, int mark_reflog, |
| 303 | timestamp_t mark_recent, struct progress *progress) |
| 304 | { |
| 305 | struct connectivity_progress cp; |
| 306 | struct bitmap_index *bitmap_git; |
| 307 | |
| 308 | /* |
| 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 */ |
| 317 | add_index_objects_to_pending(revs, 0); |
| 318 | |
| 319 | /* Add all external refs */ |
| 320 | refs_for_each_ref(get_main_ref_store(the_repository), add_one_ref, |
| 321 | revs); |
| 322 | |
| 323 | /* detached HEAD is not included in the list above */ |
| 324 | refs_head_ref(get_main_ref_store(the_repository), add_one_ref, revs); |
| 325 | other_head_refs(the_repository, add_one_ref, revs); |
| 326 | |
| 327 | /* rebase autostash and orig-head */ |
| 328 | add_rebase_files(revs); |
| 329 | |
| 330 | /* Add all reflog info */ |
| 331 | if (mark_reflog) |
| 332 | add_reflogs_to_pending(revs, 0); |
| 333 | |
| 334 | cp.progress = progress; |
| 335 | cp.count = 0; |
| 336 | |
| 337 | bitmap_git = prepare_bitmap_walk(revs, 0); |
| 338 | if (bitmap_git) { |
| 339 | traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen); |
| 340 | free_bitmap_index(bitmap_git); |
| 341 | } else { |
| 342 | if (prepare_revision_walk(revs)) |
| 343 | die("revision walk setup failed"); |
| 344 | traverse_commit_list(revs, mark_commit, mark_object, &cp); |
| 345 | } |
| 346 | |
| 347 | if (mark_recent) { |
| 348 | revs->ignore_missing_links = 1; |
| 349 | if (add_unseen_recent_objects_to_traversal(revs, mark_recent, |
| 350 | NULL, 0)) |
| 351 | die("unable to mark recent objects"); |
| 352 | if (prepare_revision_walk(revs)) |
| 353 | die("revision walk setup failed"); |
| 354 | traverse_commit_list(revs, mark_commit, mark_object, &cp); |
| 355 | } |
| 356 | |
| 357 | display_progress(cp.progress, cp.count); |
| 358 | } |