git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #include "git-compat-util.h" |
| 2 | #include "abspath.h" |
| 3 | #include "blob.h" |
| 4 | #include "chdir-notify.h" |
| 5 | #include "config.h" |
| 6 | #include "gettext.h" |
| 7 | #include "hex.h" |
| 8 | #include "lockfile.h" |
| 9 | #include "object-file.h" |
| 10 | #include "odb.h" |
| 11 | #include "odb/source.h" |
| 12 | #include "odb/source-files.h" |
| 13 | #include "odb/source-loose.h" |
| 14 | #include "pack-objects.h" |
| 15 | #include "packfile.h" |
| 16 | #include "path.h" |
| 17 | #include "promisor-remote.h" |
| 18 | #include "quote.h" |
| 19 | #include "repack.h" |
| 20 | #include "run-command.h" |
| 21 | #include "strbuf.h" |
| 22 | #include "string-list.h" |
| 23 | #include "strmap.h" |
| 24 | #include "strvec.h" |
| 25 | #include "tree.h" |
| 26 | #include "write-or-die.h" |
| 27 | |
| 28 | struct odb_files_dir *odb_files_dir_new(struct object_database *odb, |
| 29 | const char *path, bool local) |
| 30 | { |
| 31 | struct odb_files_dir *dir; |
| 32 | |
| 33 | CALLOC_ARRAY(dir, 1); |
| 34 | dir->abspath = absolute_pathdup(path); |
| 35 | dir->local = local; |
| 36 | dir->loose = odb_source_loose_new(odb, path); |
| 37 | dir->packed = odb_source_packed_new(odb, path, local); |
| 38 | |
| 39 | return dir; |
| 40 | } |
| 41 | |
| 42 | void odb_files_dir_free(struct odb_files_dir *dir) |
| 43 | { |
| 44 | if (!dir) |
| 45 | return; |
| 46 | odb_source_free(&dir->loose->base); |
| 47 | odb_source_free(&dir->packed->base); |
| 48 | free(dir->abspath); |
| 49 | free(dir); |
| 50 | } |
| 51 | |
| 52 | static void odb_source_files_reparent(const char *old_cwd, |
| 53 | const char *new_cwd, |
| 54 | void *cb_data) |
| 55 | { |
| 56 | struct odb_source_files *files = cb_data; |
| 57 | char *path = reparent_relative_path(old_cwd, new_cwd, |
| 58 | files->base.path); |
| 59 | |
| 60 | free(files->base.path); |
| 61 | files->base.path = path; |
| 62 | } |
| 63 | |
| 64 | static void odb_source_files_free(struct odb_source *source) |
| 65 | { |
| 66 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 67 | |
| 68 | chdir_notify_unregister(odb_source_files_reparent, files); |
| 69 | |
| 70 | while (files->dirs) { |
| 71 | struct odb_files_dir *next = files->dirs->next; |
| 72 | odb_files_dir_free(files->dirs); |
| 73 | files->dirs = next; |
| 74 | } |
| 75 | strset_clear(&files->dir_paths); |
| 76 | stat_validity_clear(&files->alternates_validity); |
| 77 | |
| 78 | odb_source_release(&files->base); |
| 79 | free(files); |
| 80 | } |
| 81 | |
| 82 | static void odb_source_files_close(struct odb_source *source) |
| 83 | { |
| 84 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 85 | |
| 86 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 87 | odb_source_close(&dir->loose->base); |
| 88 | odb_source_close(&dir->packed->base); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static int odb_source_files_create_on_disk(struct odb_source *source, |
| 93 | const struct odb_create_on_disk_options *opts) |
| 94 | { |
| 95 | struct lock_file alternates_lock = LOCK_INIT; |
| 96 | struct strbuf path = STRBUF_INIT; |
| 97 | struct strset seen = STRSET_INIT; |
| 98 | struct strbuf line = STRBUF_INIT; |
| 99 | int ret; |
| 100 | |
| 101 | safe_create_dir(source->odb->repo, source->path, 1); |
| 102 | |
| 103 | strbuf_addf(&path, "%s/pack", source->path); |
| 104 | safe_create_dir(source->odb->repo, path.buf, 1); |
| 105 | |
| 106 | strbuf_reset(&path); |
| 107 | strbuf_addf(&path, "%s/info", source->path); |
| 108 | safe_create_dir(source->odb->repo, path.buf, 1); |
| 109 | |
| 110 | if (opts->alternates && opts->alternates->nr) { |
| 111 | FILE *alternates, *orig; |
| 112 | |
| 113 | strbuf_reset(&path); |
| 114 | strbuf_addf(&path, "%s/info/alternates", source->path); |
| 115 | |
| 116 | repo_hold_lock_file_for_update(source->odb->repo, &alternates_lock, |
| 117 | path.buf, LOCK_DIE_ON_ERROR); |
| 118 | |
| 119 | alternates = fdopen_lock_file(&alternates_lock, "w"); |
| 120 | if (!alternates) { |
| 121 | ret = error_errno(_("unable to fdopen alternates lockfile")); |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * The alternates file may already exist, e.g. when it has been |
| 127 | * seeded from a template directory. Read any preexisting |
| 128 | * entries so that we don't end up writing duplicates. |
| 129 | */ |
| 130 | orig = fopen(path.buf, "r"); |
| 131 | if (orig) { |
| 132 | while (strbuf_getline(&line, orig) != EOF) { |
| 133 | strset_add(&seen, line.buf); |
| 134 | fprintf(alternates, "%s\n", line.buf); |
| 135 | } |
| 136 | |
| 137 | if (ferror(orig)) { |
| 138 | ret = error_errno(_("unable to read alternates file")); |
| 139 | fclose(orig); |
| 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | fclose(orig); |
| 144 | } else if (errno != ENOENT) { |
| 145 | ret = error_errno(_("unable to read alternates file")); |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | for (size_t i = 0; i < opts->alternates->nr; i++) { |
| 150 | const char *alternate = opts->alternates->v[i]; |
| 151 | if (!strset_add(&seen, alternate)) |
| 152 | continue; |
| 153 | fprintf(alternates, "%s\n", alternate); |
| 154 | } |
| 155 | |
| 156 | if (ferror(alternates)) { |
| 157 | ret = error_errno(_("unable to write alternates file")); |
| 158 | goto out; |
| 159 | } |
| 160 | |
| 161 | if (commit_lock_file(&alternates_lock)) { |
| 162 | ret = error_errno(_("unable to commit alternates file")); |
| 163 | goto out; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* Reprepare the object database to activate alternates. */ |
| 168 | odb_reprepare(source->odb); |
| 169 | |
| 170 | ret = 0; |
| 171 | |
| 172 | out: |
| 173 | rollback_lock_file(&alternates_lock); |
| 174 | strbuf_release(&line); |
| 175 | strbuf_release(&path); |
| 176 | strset_clear(&seen); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Return non-zero iff the path is usable as an alternate object directory. |
| 182 | */ |
| 183 | static bool odb_files_dir_is_usable(struct odb_source_files *files, |
| 184 | const char *path) |
| 185 | { |
| 186 | struct strbuf buf = STRBUF_INIT; |
| 187 | bool usable = false; |
| 188 | |
| 189 | /* |
| 190 | * Prevent the common mistake of listing the same thing twice, |
| 191 | * or object directory itself. |
| 192 | * |
| 193 | * NEEDSWORK: we're using "core.ignoreCase" to deduplicate |
| 194 | * alternates that _may_ be the same. This requires quite a bit |
| 195 | * of boilerplate for dubious benefit: |
| 196 | * |
| 197 | * - Duplicating alternates should really only lead to |
| 198 | * regressed performance. |
| 199 | * |
| 200 | * - We don't properly resolve symlinks or mointpoints, so we |
| 201 | * may still end up duplicating alternates. |
| 202 | * |
| 203 | * - The value may be lying, in which case we might |
| 204 | * deduplicate alternates that are in fact not mapping to the |
| 205 | * same directory. |
| 206 | * |
| 207 | * We should investigate whether we can remove this whole |
| 208 | * mechanism outright. |
| 209 | */ |
| 210 | if (strset_empty(&files->dir_paths)) { |
| 211 | files->dir_paths_icase = 0; |
| 212 | repo_config_get_bool(files->base.odb->repo, "core.ignorecase", |
| 213 | &files->dir_paths_icase); |
| 214 | |
| 215 | strbuf_realpath(&buf, files->base.path, 1); |
| 216 | if (files->dir_paths_icase) |
| 217 | strbuf_tolower(&buf); |
| 218 | strset_add(&files->dir_paths, buf.buf); |
| 219 | |
| 220 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 221 | strbuf_reset(&buf); |
| 222 | strbuf_addstr(&buf, dir->abspath); |
| 223 | if (files->dir_paths_icase) |
| 224 | strbuf_tolower(&buf); |
| 225 | strset_add(&files->dir_paths, buf.buf); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | strbuf_reset(&buf); |
| 230 | strbuf_addstr(&buf, path); |
| 231 | if (files->dir_paths_icase) |
| 232 | strbuf_tolower(&buf); |
| 233 | |
| 234 | if (strset_contains(&files->dir_paths, buf.buf)) |
| 235 | goto out; |
| 236 | |
| 237 | /* Detect cases where alternate disappeared */ |
| 238 | if (!is_directory(path)) { |
| 239 | error(_("object directory %s does not exist; " |
| 240 | "check .git/objects/info/alternates"), |
| 241 | path); |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | usable = true; |
| 246 | |
| 247 | out: |
| 248 | strbuf_release(&buf); |
| 249 | return usable; |
| 250 | } |
| 251 | |
| 252 | static void parse_alternates(const char *string, |
| 253 | int sep, |
| 254 | const char *relative_base, |
| 255 | struct strvec *out) |
| 256 | { |
| 257 | struct strbuf pathbuf = STRBUF_INIT; |
| 258 | struct strbuf buf = STRBUF_INIT; |
| 259 | |
| 260 | if (!string || !*string) |
| 261 | return; |
| 262 | |
| 263 | while (*string) { |
| 264 | const char *end; |
| 265 | |
| 266 | strbuf_reset(&buf); |
| 267 | strbuf_reset(&pathbuf); |
| 268 | |
| 269 | if (*string == '#') { |
| 270 | /* comment; consume up to next separator */ |
| 271 | end = strchrnul(string, sep); |
| 272 | } else if (*string == '"' && !unquote_c_style(&buf, string, &end)) { |
| 273 | /* |
| 274 | * quoted path; unquote_c_style has copied the |
| 275 | * data for us and set "end". Broken quoting (e.g., |
| 276 | * an entry that doesn't end with a quote) falls |
| 277 | * back to the unquoted case below. |
| 278 | */ |
| 279 | } else { |
| 280 | /* normal, unquoted path */ |
| 281 | end = strchrnul(string, sep); |
| 282 | strbuf_add(&buf, string, end - string); |
| 283 | } |
| 284 | |
| 285 | if (*end) |
| 286 | end++; |
| 287 | string = end; |
| 288 | |
| 289 | if (!buf.len) |
| 290 | continue; |
| 291 | |
| 292 | if (!is_absolute_path(buf.buf) && relative_base) { |
| 293 | strbuf_realpath(&pathbuf, relative_base, 1); |
| 294 | strbuf_addch(&pathbuf, '/'); |
| 295 | } |
| 296 | strbuf_addbuf(&pathbuf, &buf); |
| 297 | |
| 298 | strbuf_reset(&buf); |
| 299 | if (!strbuf_realpath(&buf, pathbuf.buf, 0)) { |
| 300 | error(_("unable to normalize alternate object path: %s"), |
| 301 | pathbuf.buf); |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * The trailing slash after the directory name is given by |
| 307 | * this function at the end. Remove duplicates. |
| 308 | */ |
| 309 | while (buf.len && buf.buf[buf.len - 1] == '/') |
| 310 | strbuf_setlen(&buf, buf.len - 1); |
| 311 | |
| 312 | strvec_push(out, buf.buf); |
| 313 | } |
| 314 | |
| 315 | strbuf_release(&pathbuf); |
| 316 | strbuf_release(&buf); |
| 317 | } |
| 318 | |
| 319 | static void read_alternates(const char *object_dir, struct strvec *out, |
| 320 | struct stat_validity *validity) |
| 321 | { |
| 322 | struct strbuf buf = STRBUF_INIT; |
| 323 | char *path; |
| 324 | |
| 325 | path = xstrfmt("%s/info/alternates", object_dir); |
| 326 | if (!validity || !stat_validity_check(validity, path)) { |
| 327 | bool successful = false; |
| 328 | int fd; |
| 329 | |
| 330 | fd = open(path, O_RDONLY); |
| 331 | if (fd < 0) { |
| 332 | warn_on_fopen_errors(path); |
| 333 | } else { |
| 334 | if (validity) |
| 335 | stat_validity_update(validity, fd); |
| 336 | if (strbuf_read(&buf, fd, 1024) < 0) { |
| 337 | warning_errno(_("unable to read alternates file '%s'"), |
| 338 | path); |
| 339 | } else { |
| 340 | parse_alternates(buf.buf, '\n', object_dir, out); |
| 341 | successful = true; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (validity && !successful) |
| 346 | stat_validity_clear(validity); |
| 347 | if (fd >= 0) |
| 348 | close(fd); |
| 349 | } |
| 350 | |
| 351 | strbuf_release(&buf); |
| 352 | free(path); |
| 353 | } |
| 354 | |
| 355 | static void add_alternate_recursively(struct odb_source_files *files, |
| 356 | const char *path, |
| 357 | int depth) |
| 358 | { |
| 359 | struct odb_files_dir *alternate; |
| 360 | struct strvec alternates = STRVEC_INIT; |
| 361 | |
| 362 | if (!odb_files_dir_is_usable(files, path)) |
| 363 | goto out; |
| 364 | |
| 365 | alternate = odb_files_dir_new(files->base.odb, path, false); |
| 366 | |
| 367 | /* add the alternate entry */ |
| 368 | *files->dirs_tail = alternate; |
| 369 | files->dirs_tail = &(alternate->next); |
| 370 | |
| 371 | if (files->dir_paths_icase) { |
| 372 | struct strbuf path = STRBUF_INIT; |
| 373 | |
| 374 | strbuf_addstr(&path, alternate->abspath); |
| 375 | strbuf_tolower(&path); |
| 376 | strset_add(&files->dir_paths, path.buf); |
| 377 | |
| 378 | strbuf_release(&path); |
| 379 | } else { |
| 380 | strset_add(&files->dir_paths, alternate->abspath); |
| 381 | } |
| 382 | |
| 383 | /* recursively add alternates */ |
| 384 | read_alternates(alternate->abspath, &alternates, NULL); |
| 385 | if (alternates.nr && depth + 1 > 5) { |
| 386 | error(_("%s: ignoring alternate object stores, nesting too deep"), |
| 387 | path); |
| 388 | } else { |
| 389 | for (size_t i = 0; i < alternates.nr; i++) |
| 390 | add_alternate_recursively(files, alternates.v[i], depth + 1); |
| 391 | } |
| 392 | |
| 393 | out: |
| 394 | strvec_clear(&alternates); |
| 395 | } |
| 396 | |
| 397 | static void prepare_alternates(struct odb_source_files *files, |
| 398 | const char *alternate_db) |
| 399 | { |
| 400 | struct strvec alternates = STRVEC_INIT; |
| 401 | |
| 402 | parse_alternates(alternate_db, PATH_SEP, NULL, &alternates); |
| 403 | read_alternates(files->base.path, &alternates, |
| 404 | &files->alternates_validity); |
| 405 | |
| 406 | for (size_t i = 0; i < alternates.nr; i++) |
| 407 | add_alternate_recursively(files, alternates.v[i], 0); |
| 408 | |
| 409 | strvec_clear(&alternates); |
| 410 | } |
| 411 | |
| 412 | static void odb_source_files_prepare(struct odb_source *source, |
| 413 | enum odb_prepare_flags flags) |
| 414 | { |
| 415 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 416 | |
| 417 | /* |
| 418 | * Reprepare alternates, in case the alternates file was modified |
| 419 | * during the course of this process. This only _adds_ directories to |
| 420 | * the linked list, so existing directories will continue to exist |
| 421 | * for the lifetime of the process. |
| 422 | */ |
| 423 | if (flags & ODB_PREPARE_FLUSH_CACHES) |
| 424 | prepare_alternates(files, NULL); |
| 425 | |
| 426 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 427 | odb_source_prepare(&dir->loose->base, flags); |
| 428 | odb_source_prepare(&dir->packed->base, flags); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static enum odb_read_status odb_source_files_read_object_info(struct odb_source *source, |
| 433 | const struct object_id *oid, |
| 434 | struct object_info *oi, |
| 435 | enum object_info_flags flags, |
| 436 | struct strbuf *errmsg) |
| 437 | { |
| 438 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 439 | struct odb_files_dir **tail = NULL; |
| 440 | enum odb_read_status status = ODB_READ_NOT_FOUND; |
| 441 | enum odb_read_status ret; |
| 442 | bool retry = false; |
| 443 | |
| 444 | retry: |
| 445 | /* |
| 446 | * Reading an object may fail even though the object exists, for |
| 447 | * example because it is corrupt. Report this failure to the caller in |
| 448 | * case none of the directories was able to read the object, and |
| 449 | * prefer the first such error in case multiple reads have failed. |
| 450 | */ |
| 451 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 452 | if (retry) |
| 453 | odb_source_prepare(&dir->packed->base, ODB_PREPARE_FLUSH_CACHES); |
| 454 | |
| 455 | ret = odb_source_read_object_info(&dir->packed->base, oid, oi, flags, |
| 456 | status == ODB_READ_NOT_FOUND ? errmsg : NULL); |
| 457 | if (!ret) |
| 458 | return 0; |
| 459 | if (ret != ODB_READ_NOT_FOUND && status == ODB_READ_NOT_FOUND) |
| 460 | status = ret; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * When we have reprepared alternates then we only re-scan directories |
| 465 | * that were newly added. |
| 466 | */ |
| 467 | for (struct odb_files_dir *dir = tail ? *tail : files->dirs; dir; dir = dir->next) { |
| 468 | ret = odb_source_read_object_info(&dir->loose->base, oid, oi, flags, |
| 469 | status == ODB_READ_NOT_FOUND ? errmsg : NULL); |
| 470 | if (!ret) |
| 471 | return 0; |
| 472 | if (ret != ODB_READ_NOT_FOUND && status == ODB_READ_NOT_FOUND) |
| 473 | status = ret; |
| 474 | } |
| 475 | |
| 476 | if (!retry && !(flags & OBJECT_INFO_QUICK)) { |
| 477 | tail = files->dirs_tail; |
| 478 | prepare_alternates(files, NULL); |
| 479 | retry = true; |
| 480 | goto retry; |
| 481 | } |
| 482 | |
| 483 | return status; |
| 484 | } |
| 485 | |
| 486 | static int odb_source_files_read_object_stream(struct odb_stream **out, |
| 487 | struct odb_source *source, |
| 488 | const struct object_id *oid) |
| 489 | { |
| 490 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 491 | |
| 492 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) |
| 493 | if (!odb_source_read_object_stream(out, &dir->packed->base, oid)) |
| 494 | return 0; |
| 495 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) |
| 496 | if (!odb_source_read_object_stream(out, &dir->loose->base, oid)) |
| 497 | return 0; |
| 498 | |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | static int odb_source_files_for_each_object(struct odb_source *source, |
| 503 | const struct object_info *request, |
| 504 | odb_for_each_object_cb cb, |
| 505 | void *cb_data, |
| 506 | const struct odb_for_each_object_options *opts) |
| 507 | { |
| 508 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 509 | int ret; |
| 510 | |
| 511 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 512 | if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !dir->local) |
| 513 | continue; |
| 514 | |
| 515 | if (!(opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) { |
| 516 | ret = odb_source_for_each_object(&dir->loose->base, request, cb, cb_data, opts); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | ret = odb_source_for_each_object(&dir->packed->base, request, cb, cb_data, opts); |
| 522 | if (ret) |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int odb_source_files_count_objects(struct odb_source *source, |
| 530 | enum odb_count_objects_flags flags, |
| 531 | unsigned long *out) |
| 532 | { |
| 533 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 534 | unsigned long count = 0; |
| 535 | int ret; |
| 536 | |
| 537 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 538 | unsigned long dir_count; |
| 539 | |
| 540 | ret = odb_source_count_objects(&dir->packed->base, flags, &dir_count); |
| 541 | if (ret < 0) |
| 542 | goto out; |
| 543 | count += dir_count; |
| 544 | |
| 545 | if (!(flags & ODB_COUNT_OBJECTS_APPROXIMATE)) { |
| 546 | ret = odb_source_count_objects(&dir->loose->base, flags, &dir_count); |
| 547 | if (ret < 0) |
| 548 | goto out; |
| 549 | count += dir_count; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | *out = count; |
| 554 | ret = 0; |
| 555 | |
| 556 | out: |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | static int odb_source_files_find_abbrev_len(struct odb_source *source, |
| 561 | const struct object_id *oid, |
| 562 | unsigned min_len, |
| 563 | unsigned *out) |
| 564 | { |
| 565 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 566 | unsigned len = min_len; |
| 567 | int ret = 0; |
| 568 | |
| 569 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 570 | ret = odb_source_find_abbrev_len(&dir->packed->base, oid, len, &len); |
| 571 | if (ret < 0) |
| 572 | goto out; |
| 573 | |
| 574 | ret = odb_source_find_abbrev_len(&dir->loose->base, oid, len, &len); |
| 575 | if (ret < 0) |
| 576 | goto out; |
| 577 | } |
| 578 | |
| 579 | *out = len; |
| 580 | ret = 0; |
| 581 | |
| 582 | out: |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static int odb_source_files_freshen_object(struct odb_source *source, |
| 587 | const struct object_id *oid, |
| 588 | const time_t *mtime) |
| 589 | { |
| 590 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 591 | |
| 592 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) |
| 593 | if (odb_source_freshen_object(&dir->packed->base, oid, mtime) || |
| 594 | odb_source_freshen_object(&dir->loose->base, oid, mtime)) |
| 595 | return 1; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int odb_source_files_write_object(struct odb_source *source, |
| 601 | const void *buf, size_t len, |
| 602 | enum object_type type, |
| 603 | const struct object_id *oid, |
| 604 | const struct object_id *compat_oid, |
| 605 | const time_t *mtime, |
| 606 | enum odb_write_object_flags flags) |
| 607 | { |
| 608 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 609 | return odb_source_write_object(&files->dirs->loose->base, buf, len, type, |
| 610 | oid, compat_oid, mtime, flags); |
| 611 | } |
| 612 | |
| 613 | static int odb_source_files_write_object_stream(struct odb_source *source, |
| 614 | struct odb_stream *stream, |
| 615 | struct object_id *oid) |
| 616 | { |
| 617 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 618 | return odb_source_write_object_stream(&files->dirs->loose->base, stream, oid); |
| 619 | } |
| 620 | |
| 621 | static int odb_source_files_begin_transaction(struct odb_source *source, |
| 622 | struct odb_transaction **out, |
| 623 | enum odb_transaction_flags flags) |
| 624 | { |
| 625 | return odb_transaction_files_begin(source, out, flags); |
| 626 | } |
| 627 | |
| 628 | static int too_many_loose_objects(struct odb_source_files *files, int limit) |
| 629 | { |
| 630 | unsigned long loose_count; |
| 631 | |
| 632 | if (limit <= 0) |
| 633 | return 0; |
| 634 | |
| 635 | if (odb_source_count_objects(&files->dirs->loose->base, ODB_COUNT_OBJECTS_APPROXIMATE, |
| 636 | &loose_count) < 0) |
| 637 | return 0; |
| 638 | |
| 639 | /* |
| 640 | * This is weird, but stems from legacy behaviour: the GC auto |
| 641 | * threshold was always essentially interpreted as if it was rounded up |
| 642 | * to the next multiple 256 of, so we retain this behaviour for now. |
| 643 | */ |
| 644 | return loose_count > (DIV_ROUND_UP(((unsigned long) limit), 256) * 256); |
| 645 | } |
| 646 | |
| 647 | static struct packed_git *find_base_packs(struct odb_source_files *files, |
| 648 | struct string_list *packs, |
| 649 | unsigned long limit) |
| 650 | { |
| 651 | struct packfile_list_entry *e; |
| 652 | struct packed_git *base = NULL; |
| 653 | |
| 654 | for (e = packfile_store_get_packs(files->dirs->packed); e; e = e->next) { |
| 655 | if (e->pack->is_cruft) |
| 656 | continue; |
| 657 | if (limit) { |
| 658 | if ((uintmax_t) e->pack->pack_size >= limit) |
| 659 | string_list_append(packs, e->pack->pack_name); |
| 660 | } else if (!base || base->pack_size < e->pack->pack_size) { |
| 661 | base = e->pack; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if (base) |
| 666 | string_list_append(packs, base->pack_name); |
| 667 | |
| 668 | return base; |
| 669 | } |
| 670 | |
| 671 | static int too_many_packs(struct odb_source_files *files, int gc_auto_pack_limit) |
| 672 | { |
| 673 | struct packfile_list_entry *e; |
| 674 | int cnt = 0; |
| 675 | |
| 676 | if (gc_auto_pack_limit <= 0) |
| 677 | return 0; |
| 678 | |
| 679 | for (e = packfile_store_get_packs(files->dirs->packed); e; e = e->next) { |
| 680 | if (e->pack->pack_keep) |
| 681 | continue; |
| 682 | /* |
| 683 | * Perhaps check the size of the pack and count only |
| 684 | * very small ones here? |
| 685 | */ |
| 686 | cnt++; |
| 687 | } |
| 688 | return gc_auto_pack_limit < cnt; |
| 689 | } |
| 690 | |
| 691 | static uint64_t total_ram(void) |
| 692 | { |
| 693 | #if defined(HAVE_SYSINFO) |
| 694 | struct sysinfo si; |
| 695 | |
| 696 | if (!sysinfo(&si)) { |
| 697 | uint64_t total = si.totalram; |
| 698 | |
| 699 | if (si.mem_unit > 1) |
| 700 | total *= (uint64_t)si.mem_unit; |
| 701 | return total; |
| 702 | } |
| 703 | #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64)) |
| 704 | uint64_t physical_memory; |
| 705 | int mib[2]; |
| 706 | size_t length; |
| 707 | |
| 708 | mib[0] = CTL_HW; |
| 709 | # if defined(HW_MEMSIZE) |
| 710 | mib[1] = HW_MEMSIZE; |
| 711 | # elif defined(HW_PHYSMEM64) |
| 712 | mib[1] = HW_PHYSMEM64; |
| 713 | # else |
| 714 | mib[1] = HW_PHYSMEM; |
| 715 | # endif |
| 716 | length = sizeof(physical_memory); |
| 717 | if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0)) { |
| 718 | if (length == 4) { |
| 719 | uint32_t mem; |
| 720 | |
| 721 | if (!sysctl(mib, 2, &mem, &length, NULL, 0)) |
| 722 | physical_memory = mem; |
| 723 | } |
| 724 | return physical_memory; |
| 725 | } |
| 726 | #elif defined(GIT_WINDOWS_NATIVE) |
| 727 | MEMORYSTATUSEX memInfo; |
| 728 | |
| 729 | memInfo.dwLength = sizeof(MEMORYSTATUSEX); |
| 730 | if (GlobalMemoryStatusEx(&memInfo)) |
| 731 | return memInfo.ullTotalPhys; |
| 732 | #endif |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static uint64_t estimate_repack_memory(struct odb_source_files *files, |
| 737 | struct packed_git *pack) |
| 738 | { |
| 739 | unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; |
| 740 | unsigned long delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT; |
| 741 | unsigned long nr_objects; |
| 742 | size_t os_cache, heap; |
| 743 | |
| 744 | if (odb_source_count_objects(&files->base, ODB_COUNT_OBJECTS_APPROXIMATE, |
| 745 | &nr_objects) < 0) |
| 746 | return 0; |
| 747 | |
| 748 | if (!pack || !nr_objects) |
| 749 | return 0; |
| 750 | |
| 751 | repo_config_get_ulong(files->base.odb->repo, "pack.deltacachesize", |
| 752 | &max_delta_cache_size); |
| 753 | repo_config_get_ulong(files->base.odb->repo, "core.deltabasecachelimit", |
| 754 | &delta_base_cache_limit); |
| 755 | |
| 756 | /* |
| 757 | * First we have to scan through at least one pack. |
| 758 | * Assume enough room in OS file cache to keep the entire pack |
| 759 | * or we may accidentally evict data of other processes from |
| 760 | * the cache. |
| 761 | */ |
| 762 | os_cache = pack->pack_size + pack->index_size; |
| 763 | /* then pack-objects needs lots more for book keeping */ |
| 764 | heap = sizeof(struct object_entry) * nr_objects; |
| 765 | /* |
| 766 | * internal rev-list --all --objects takes up some memory too, |
| 767 | * let's say half of it is for blobs |
| 768 | */ |
| 769 | heap += sizeof(struct blob) * nr_objects / 2; |
| 770 | /* |
| 771 | * and the other half is for trees (commits and tags are |
| 772 | * usually insignificant) |
| 773 | */ |
| 774 | heap += sizeof(struct tree) * nr_objects / 2; |
| 775 | /* and then obj_hash[], underestimated in fact */ |
| 776 | heap += sizeof(struct object *) * nr_objects; |
| 777 | /* revindex is used also */ |
| 778 | heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects; |
| 779 | /* |
| 780 | * read_sha1_file() (either at delta calculation phase, or |
| 781 | * writing phase) also fills up the delta base cache |
| 782 | */ |
| 783 | heap += delta_base_cache_limit; |
| 784 | /* and of course pack-objects has its own delta cache */ |
| 785 | heap += max_delta_cache_size; |
| 786 | |
| 787 | return os_cache + heap; |
| 788 | } |
| 789 | |
| 790 | static int keep_one_pack(struct string_list_item *item, void *data) |
| 791 | { |
| 792 | struct strvec *args = data; |
| 793 | strvec_pushf(args, "--keep-pack=%s", basename(item->string)); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static void add_repack_all_option(struct repository *repo, |
| 798 | const struct odb_optimize_options *opts, |
| 799 | struct string_list *keep_pack, |
| 800 | struct strvec *args) |
| 801 | { |
| 802 | char *repack_filter = NULL; |
| 803 | char *repack_filter_to = NULL; |
| 804 | |
| 805 | repo_config_get_string(repo, "gc.repackfilter", &repack_filter); |
| 806 | repo_config_get_string(repo, "gc.repackfilterto", &repack_filter_to); |
| 807 | |
| 808 | if (opts->prune_expire && !strcmp(opts->prune_expire, "now") && |
| 809 | !(opts->cruft_packs && opts->expire_to)) |
| 810 | strvec_push(args, "-a"); |
| 811 | else if (opts->cruft_packs) { |
| 812 | strvec_push(args, "--cruft"); |
| 813 | if (opts->prune_expire) |
| 814 | strvec_pushf(args, "--cruft-expiration=%s", opts->prune_expire); |
| 815 | if (opts->max_cruft_size) |
| 816 | strvec_pushf(args, "--max-cruft-size=%lu", |
| 817 | opts->max_cruft_size); |
| 818 | if (opts->expire_to) |
| 819 | strvec_pushf(args, "--expire-to=%s", opts->expire_to); |
| 820 | } else { |
| 821 | strvec_push(args, "-A"); |
| 822 | if (opts->prune_expire) |
| 823 | strvec_pushf(args, "--unpack-unreachable=%s", opts->prune_expire); |
| 824 | } |
| 825 | |
| 826 | if (keep_pack) |
| 827 | for_each_string_list(keep_pack, keep_one_pack, args); |
| 828 | |
| 829 | if (repack_filter && *repack_filter) |
| 830 | strvec_pushf(args, "--filter=%s", repack_filter); |
| 831 | if (repack_filter_to && *repack_filter_to) |
| 832 | strvec_pushf(args, "--filter-to=%s", repack_filter_to); |
| 833 | |
| 834 | free(repack_filter); |
| 835 | free(repack_filter_to); |
| 836 | } |
| 837 | |
| 838 | static void add_repack_incremental_option(struct strvec *args) |
| 839 | { |
| 840 | strvec_push(args, "--no-write-bitmap-index"); |
| 841 | } |
| 842 | |
| 843 | bool odb_source_files_optimize_required(struct odb_source *source, |
| 844 | const struct odb_optimize_options *opts) |
| 845 | { |
| 846 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 847 | struct repository *repo = source->odb->repo; |
| 848 | |
| 849 | switch (opts->strategy) { |
| 850 | case ODB_OPTIMIZE_INCREMENTAL: { |
| 851 | int gc_auto_threshold = 6700; |
| 852 | int gc_auto_pack_limit = 50; |
| 853 | |
| 854 | repo_config_get_int(repo, "gc.auto", &gc_auto_threshold); |
| 855 | repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit); |
| 856 | |
| 857 | /* |
| 858 | * Setting gc.auto to 0 or negative can disable the |
| 859 | * automatic gc. |
| 860 | */ |
| 861 | if (gc_auto_threshold <= 0) |
| 862 | return false; |
| 863 | if (!too_many_packs(files, gc_auto_pack_limit) && |
| 864 | !too_many_loose_objects(files, gc_auto_threshold)) |
| 865 | return false; |
| 866 | |
| 867 | return true; |
| 868 | } |
| 869 | case ODB_OPTIMIZE_GEOMETRIC: { |
| 870 | struct pack_geometry geometry = { |
| 871 | .split_factor = 2, |
| 872 | }; |
| 873 | struct pack_objects_args po_args = { |
| 874 | .local = 1, |
| 875 | }; |
| 876 | struct existing_packs existing_packs = EXISTING_PACKS_INIT; |
| 877 | struct string_list kept_packs = STRING_LIST_INIT_DUP; |
| 878 | int auto_value = 6700; |
| 879 | bool ret; |
| 880 | |
| 881 | repo_config_get_int(repo, "maintenance.geometric-repack.auto", |
| 882 | &auto_value); |
| 883 | if (!auto_value) |
| 884 | return false; |
| 885 | if (auto_value < 0) |
| 886 | return true; |
| 887 | |
| 888 | repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor", |
| 889 | &geometry.split_factor); |
| 890 | |
| 891 | existing_packs.repo = repo; |
| 892 | existing_packs_collect(&existing_packs, &kept_packs); |
| 893 | pack_geometry_init(&geometry, &existing_packs, &po_args); |
| 894 | pack_geometry_split(&geometry); |
| 895 | |
| 896 | /* |
| 897 | * When we'd merge at least two packs with one another we always |
| 898 | * perform the repack. |
| 899 | */ |
| 900 | if (geometry.split) { |
| 901 | ret = true; |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Otherwise, we estimate the number of loose objects to determine |
| 907 | * whether we want to create a new packfile or not. |
| 908 | */ |
| 909 | if (too_many_loose_objects(files, auto_value)) { |
| 910 | ret = true; |
| 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | ret = false; |
| 915 | |
| 916 | out: |
| 917 | existing_packs_release(&existing_packs); |
| 918 | pack_geometry_release(&geometry); |
| 919 | return ret; |
| 920 | } |
| 921 | default: |
| 922 | BUG("unknown maintenance strategy '%d'", opts->strategy); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | int odb_source_files_optimize(struct odb_source *source, |
| 927 | const struct odb_optimize_options *opts) |
| 928 | { |
| 929 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 930 | struct repository *repo = source->odb->repo; |
| 931 | struct child_process repack_cmd = CHILD_PROCESS_INIT; |
| 932 | unsigned long big_pack_threshold = 0; |
| 933 | int gc_auto_threshold = 6700; |
| 934 | int gc_auto_pack_limit = 50; |
| 935 | int ret; |
| 936 | |
| 937 | repo_config_get_int(repo, "gc.auto", &gc_auto_threshold); |
| 938 | repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit); |
| 939 | repo_config_get_ulong(repo, "gc.bigpackthreshold", &big_pack_threshold); |
| 940 | |
| 941 | if (repo->repository_format_precious_objects) |
| 942 | return 0; |
| 943 | |
| 944 | repack_cmd.git_cmd = 1; |
| 945 | repack_cmd.odb_to_close = repo->objects; |
| 946 | |
| 947 | strvec_pushl(&repack_cmd.args, "repack", "-d", "-l", NULL); |
| 948 | if (opts->flags & ODB_OPTIMIZE_NO_REUSE_DELTAS) |
| 949 | strvec_push(&repack_cmd.args, "-f"); |
| 950 | if (opts->depth > 0) |
| 951 | strvec_pushf(&repack_cmd.args, "--depth=%d", opts->depth); |
| 952 | if (opts->window > 0) |
| 953 | strvec_pushf(&repack_cmd.args, "--window=%d", opts->window); |
| 954 | if (!(opts->flags & ODB_OPTIMIZE_VERBOSE)) |
| 955 | strvec_push(&repack_cmd.args, "-q"); |
| 956 | |
| 957 | /* |
| 958 | * There's three cases we need to consider: |
| 959 | * |
| 960 | * - If we're invoked without `--auto` we'll need to perform a full |
| 961 | * repack. |
| 962 | * |
| 963 | * - If we're invoked with `--auto` and there's too many packs, then |
| 964 | * we perform a full repack, as well. |
| 965 | * |
| 966 | * - Otherwise we perform an incremental repack. |
| 967 | */ |
| 968 | switch (opts->strategy) { |
| 969 | case ODB_OPTIMIZE_INCREMENTAL: |
| 970 | if (!(opts->flags & ODB_OPTIMIZE_AUTO)) { |
| 971 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; |
| 972 | |
| 973 | if (opts->keep_largest_pack != -1) { |
| 974 | if (opts->keep_largest_pack) |
| 975 | find_base_packs(files, &keep_pack, 0); |
| 976 | } else if (big_pack_threshold) { |
| 977 | find_base_packs(files, &keep_pack, big_pack_threshold); |
| 978 | } |
| 979 | |
| 980 | add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args); |
| 981 | string_list_clear(&keep_pack, 0); |
| 982 | } else { |
| 983 | if (too_many_packs(files, gc_auto_pack_limit)) { |
| 984 | struct string_list keep_pack = STRING_LIST_INIT_NODUP; |
| 985 | |
| 986 | if (big_pack_threshold) { |
| 987 | find_base_packs(files, &keep_pack, big_pack_threshold); |
| 988 | if (keep_pack.nr >= (unsigned long) gc_auto_pack_limit) { |
| 989 | string_list_clear(&keep_pack, 0); |
| 990 | find_base_packs(files, &keep_pack, 0); |
| 991 | } |
| 992 | } else { |
| 993 | struct packed_git *p = find_base_packs(files, &keep_pack, 0); |
| 994 | uint64_t mem_have, mem_want; |
| 995 | |
| 996 | mem_have = total_ram(); |
| 997 | mem_want = estimate_repack_memory(files, p); |
| 998 | |
| 999 | /* |
| 1000 | * Only allow 1/2 of memory for pack-objects, leave |
| 1001 | * the rest for the OS and other processes in the |
| 1002 | * system. |
| 1003 | */ |
| 1004 | if (!mem_have || mem_want < mem_have / 2) |
| 1005 | string_list_clear(&keep_pack, 0); |
| 1006 | } |
| 1007 | |
| 1008 | add_repack_all_option(repo, opts, &keep_pack, &repack_cmd.args); |
| 1009 | string_list_clear(&keep_pack, 0); |
| 1010 | } else { |
| 1011 | add_repack_incremental_option(&repack_cmd.args); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | break; |
| 1016 | case ODB_OPTIMIZE_GEOMETRIC: { |
| 1017 | struct pack_geometry geometry = { |
| 1018 | .split_factor = 2, |
| 1019 | }; |
| 1020 | struct pack_objects_args po_args = { |
| 1021 | .local = 1, |
| 1022 | }; |
| 1023 | struct existing_packs existing_packs = EXISTING_PACKS_INIT; |
| 1024 | struct string_list kept_packs = STRING_LIST_INIT_DUP; |
| 1025 | |
| 1026 | repo_config_get_int(repo, "maintenance.geometric-repack.splitFactor", |
| 1027 | &geometry.split_factor); |
| 1028 | |
| 1029 | existing_packs.repo = repo; |
| 1030 | existing_packs_collect(&existing_packs, &kept_packs); |
| 1031 | pack_geometry_init(&geometry, &existing_packs, &po_args); |
| 1032 | pack_geometry_split(&geometry); |
| 1033 | |
| 1034 | if (geometry.split < geometry.pack_nr) { |
| 1035 | strvec_pushf(&repack_cmd.args, "--geometric=%d", |
| 1036 | geometry.split_factor); |
| 1037 | } else { |
| 1038 | add_repack_all_option(repo, opts, NULL, &repack_cmd.args); |
| 1039 | } |
| 1040 | if (repo->settings.core_multi_pack_index) |
| 1041 | strvec_push(&repack_cmd.args, "--write-midx"); |
| 1042 | |
| 1043 | existing_packs_release(&existing_packs); |
| 1044 | pack_geometry_release(&geometry); |
| 1045 | break; |
| 1046 | } |
| 1047 | default: |
| 1048 | die("unknown maintenance strategy '%d'", opts->strategy); |
| 1049 | } |
| 1050 | |
| 1051 | if (run_command(&repack_cmd)) { |
| 1052 | ret = error("failed to run %s", repack_cmd.args.v[0]); |
| 1053 | goto out; |
| 1054 | } |
| 1055 | |
| 1056 | /* Geometric repacking uses cruft packs, so we don't have to prune separately. */ |
| 1057 | if (opts->strategy != ODB_OPTIMIZE_GEOMETRIC && opts->prune_expire) { |
| 1058 | struct child_process prune_cmd = CHILD_PROCESS_INIT; |
| 1059 | |
| 1060 | strvec_pushl(&prune_cmd.args, "prune", "--expire", NULL); |
| 1061 | /* run `git prune` even if using cruft packs */ |
| 1062 | strvec_push(&prune_cmd.args, opts->prune_expire); |
| 1063 | if (!(opts->flags & ODB_OPTIMIZE_VERBOSE)) |
| 1064 | strvec_push(&prune_cmd.args, "--no-progress"); |
| 1065 | if (repo_has_promisor_remote(repo)) |
| 1066 | strvec_push(&prune_cmd.args, |
| 1067 | "--exclude-promisor-objects"); |
| 1068 | prune_cmd.git_cmd = 1; |
| 1069 | |
| 1070 | if (run_command(&prune_cmd)) { |
| 1071 | ret = error("failed to run %s", prune_cmd.args.v[0]); |
| 1072 | goto out; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (opts->flags & ODB_OPTIMIZE_AUTO && too_many_loose_objects(files, gc_auto_threshold)) |
| 1077 | warning(_("There are too many unreachable loose objects; " |
| 1078 | "run 'git prune' to remove them.")); |
| 1079 | |
| 1080 | ret = 0; |
| 1081 | |
| 1082 | out: |
| 1083 | return ret; |
| 1084 | } |
| 1085 | |
| 1086 | struct odb_pack_generator_files { |
| 1087 | struct odb_pack_generator base; |
| 1088 | struct child_process cp; |
| 1089 | }; |
| 1090 | |
| 1091 | static int odb_pack_generator_files_finish(struct odb_pack_generator *_generator) |
| 1092 | { |
| 1093 | struct odb_pack_generator_files *generator = |
| 1094 | (struct odb_pack_generator_files *)_generator; |
| 1095 | int ret; |
| 1096 | |
| 1097 | ret = finish_command(&generator->cp); |
| 1098 | free(generator); |
| 1099 | |
| 1100 | if (ret) { |
| 1101 | /* |
| 1102 | * On failure, pack-objects is expected to have written a |
| 1103 | * useful error message to its standard error stream already. |
| 1104 | * Death by signal is worth mentioning, though, with the |
| 1105 | * exception of SIGPIPE: that is a normal occurrence when the |
| 1106 | * consumer of the pack hangs up. |
| 1107 | */ |
| 1108 | if (ret > 128 && ret - 128 == SIGPIPE) |
| 1109 | return -1; |
| 1110 | if (ret > 128) |
| 1111 | error(_("pack-objects died of signal %d"), ret - 128); |
| 1112 | return -1; |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int odb_source_files_generate_pack(struct odb_source *source UNUSED, |
| 1119 | struct odb_pack_generator **out, |
| 1120 | const struct odb_generate_pack_options *opts) |
| 1121 | { |
| 1122 | struct odb_pack_generator_files *generator; |
| 1123 | struct child_process *cp; |
| 1124 | FILE *in; |
| 1125 | |
| 1126 | CALLOC_ARRAY(generator, 1); |
| 1127 | child_process_init(&generator->cp); |
| 1128 | cp = &generator->cp; |
| 1129 | |
| 1130 | /* |
| 1131 | * The hook is expected to spawn "$hook git pack-objects <args...>" |
| 1132 | * and to behave like git-pack-objects(1) would have. This can for |
| 1133 | * example be used to serve precomputed packfiles. |
| 1134 | */ |
| 1135 | if (opts->pack_objects_hook) { |
| 1136 | strvec_push(&cp->args, opts->pack_objects_hook); |
| 1137 | strvec_push(&cp->args, "git"); |
| 1138 | cp->use_shell = 1; |
| 1139 | } else { |
| 1140 | cp->git_cmd = 1; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * The caller-provided shallow boundary overrides any shallow state |
| 1145 | * that the repository itself may have, so the shallow file needs to |
| 1146 | * be neutralized. |
| 1147 | */ |
| 1148 | if (opts->shallows.nr) { |
| 1149 | strvec_push(&cp->args, "--shallow-file"); |
| 1150 | strvec_push(&cp->args, ""); |
| 1151 | } |
| 1152 | strvec_push(&cp->args, "pack-objects"); |
| 1153 | strvec_push(&cp->args, "--revs"); |
| 1154 | strvec_push(&cp->args, "--stdout"); |
| 1155 | if (opts->thin) |
| 1156 | strvec_push(&cp->args, "--thin"); |
| 1157 | if (opts->shallow) |
| 1158 | strvec_push(&cp->args, "--shallow"); |
| 1159 | if (opts->ofs_delta) |
| 1160 | strvec_push(&cp->args, "--delta-base-offset"); |
| 1161 | if (opts->include_tag) |
| 1162 | strvec_push(&cp->args, "--include-tag"); |
| 1163 | if (opts->missing_allow_promisor) |
| 1164 | strvec_push(&cp->args, "--missing=allow-promisor"); |
| 1165 | if (opts->disable_bitmaps) |
| 1166 | strvec_push(&cp->args, "--no-use-bitmap-index"); |
| 1167 | switch (opts->progress) { |
| 1168 | case ODB_GENERATE_PACK_PROGRESS_NONE: |
| 1169 | strvec_push(&cp->args, "--quiet"); |
| 1170 | break; |
| 1171 | case ODB_GENERATE_PACK_PROGRESS_STANDARD: |
| 1172 | strvec_push(&cp->args, "--progress"); |
| 1173 | break; |
| 1174 | case ODB_GENERATE_PACK_PROGRESS_VERBOSE: |
| 1175 | strvec_push(&cp->args, "--all-progress"); |
| 1176 | break; |
| 1177 | default: |
| 1178 | BUG("unknown progress option %d", opts->progress); |
| 1179 | } |
| 1180 | if (opts->filter_spec) |
| 1181 | strvec_pushf(&cp->args, "--filter=%s", opts->filter_spec); |
| 1182 | if (opts->uri_protocols) |
| 1183 | for (size_t i = 0; i < opts->uri_protocols->nr; i++) |
| 1184 | strvec_pushf(&cp->args, "--uri-protocol=%s", |
| 1185 | opts->uri_protocols->items[i].string); |
| 1186 | |
| 1187 | cp->in = -1; |
| 1188 | cp->out = opts->pack_fd; |
| 1189 | cp->err = opts->progress_fd; |
| 1190 | cp->clean_on_exit = 1; |
| 1191 | |
| 1192 | if (start_command(cp)) { |
| 1193 | free(generator); |
| 1194 | return error(_("could not spawn pack-objects")); |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * Feed the objects to pack-objects. This is safe to do synchronously |
| 1199 | * because pack-objects consumes all of its standard input before it |
| 1200 | * starts to generate the pack. |
| 1201 | */ |
| 1202 | in = xfdopen(cp->in, "w"); |
| 1203 | for (size_t i = 0; i < opts->shallows.nr; i++) |
| 1204 | fprintf(in, "--shallow %s\n", oid_to_hex(&opts->shallows.oid[i])); |
| 1205 | for (size_t i = 0; i < opts->wants.nr; i++) |
| 1206 | fprintf(in, "%s\n", oid_to_hex(&opts->wants.oid[i])); |
| 1207 | fprintf(in, "--not\n"); |
| 1208 | for (size_t i = 0; i < opts->haves.nr; i++) |
| 1209 | fprintf(in, "%s\n", oid_to_hex(&opts->haves.oid[i])); |
| 1210 | fprintf(in, "\n"); |
| 1211 | fflush(in); |
| 1212 | if (ferror(in)) { |
| 1213 | error(_("error writing to pack-objects")); |
| 1214 | fclose(in); |
| 1215 | if (opts->pack_fd < 0) |
| 1216 | close(cp->out); |
| 1217 | if (opts->progress_fd < 0) |
| 1218 | close(cp->err); |
| 1219 | finish_command(cp); |
| 1220 | free(generator); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | fclose(in); |
| 1224 | |
| 1225 | generator->base.out = opts->pack_fd < 0 ? cp->out : -1; |
| 1226 | generator->base.err = opts->progress_fd < 0 ? cp->err : -1; |
| 1227 | generator->base.finish = odb_pack_generator_files_finish; |
| 1228 | |
| 1229 | *out = &generator->base; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int odb_source_files_fsck(struct odb_source *source, |
| 1234 | struct odb_fsck_options *opts) |
| 1235 | { |
| 1236 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 1237 | int ret = 0; |
| 1238 | |
| 1239 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 1240 | if (!(opts->flags & ODB_FSCK_FULL) && !dir->local) |
| 1241 | continue; |
| 1242 | |
| 1243 | ret |= odb_source_fsck(&dir->loose->base, opts); |
| 1244 | ret |= odb_source_fsck(&dir->packed->base, opts); |
| 1245 | } |
| 1246 | |
| 1247 | return ret; |
| 1248 | } |
| 1249 | |
| 1250 | struct odb_files_dir *odb_source_files_find_dir(struct object_database *odb, const char *obj_dir) |
| 1251 | { |
| 1252 | struct odb_source_files *files = odb_source_files_downcast(odb->source); |
| 1253 | char *obj_dir_real = real_pathdup(obj_dir, 1); |
| 1254 | struct strbuf odb_path_real = STRBUF_INIT; |
| 1255 | struct odb_files_dir *dir; |
| 1256 | |
| 1257 | for (dir = files->dirs; dir; dir = dir->next) { |
| 1258 | strbuf_realpath(&odb_path_real, dir->abspath, 1); |
| 1259 | if (!strcmp(obj_dir_real, odb_path_real.buf)) |
| 1260 | break; |
| 1261 | } |
| 1262 | |
| 1263 | free(obj_dir_real); |
| 1264 | strbuf_release(&odb_path_real); |
| 1265 | return dir; |
| 1266 | } |
| 1267 | |
| 1268 | struct odb_source_files *odb_source_files_new(struct object_database *odb, |
| 1269 | const char *payload, |
| 1270 | enum odb_new_flags flags) |
| 1271 | { |
| 1272 | struct odb_source_files *files; |
| 1273 | char *object_dir = NULL; |
| 1274 | char *alternates = NULL; |
| 1275 | |
| 1276 | if (flags & ODB_NEW_HONOR_ENV) { |
| 1277 | object_dir = xstrdup_or_null(getenv(DB_ENVIRONMENT)); |
| 1278 | alternates = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT)); |
| 1279 | } |
| 1280 | if (!object_dir && payload) { |
| 1281 | if (is_absolute_path(payload)) |
| 1282 | object_dir = xstrdup(payload); |
| 1283 | else |
| 1284 | object_dir = xstrfmt("%s/%s", odb->repo->commondir, payload); |
| 1285 | } |
| 1286 | if (!object_dir) |
| 1287 | object_dir = xstrfmt("%s/objects", odb->repo->commondir); |
| 1288 | |
| 1289 | CALLOC_ARRAY(files, 1); |
| 1290 | odb_source_init(&files->base, odb, ODB_SOURCE_FILES, object_dir); |
| 1291 | |
| 1292 | files->dirs = odb_files_dir_new(odb, object_dir, true); |
| 1293 | files->dirs_tail = &files->dirs->next; |
| 1294 | strset_init(&files->dir_paths); |
| 1295 | |
| 1296 | files->base.free = odb_source_files_free; |
| 1297 | files->base.close = odb_source_files_close; |
| 1298 | files->base.create_on_disk = odb_source_files_create_on_disk; |
| 1299 | files->base.prepare = odb_source_files_prepare; |
| 1300 | files->base.fsck = odb_source_files_fsck; |
| 1301 | files->base.read_object_info = odb_source_files_read_object_info; |
| 1302 | files->base.read_object_stream = odb_source_files_read_object_stream; |
| 1303 | files->base.for_each_object = odb_source_files_for_each_object; |
| 1304 | files->base.count_objects = odb_source_files_count_objects; |
| 1305 | files->base.find_abbrev_len = odb_source_files_find_abbrev_len; |
| 1306 | files->base.freshen_object = odb_source_files_freshen_object; |
| 1307 | files->base.write_object = odb_source_files_write_object; |
| 1308 | files->base.write_object_stream = odb_source_files_write_object_stream; |
| 1309 | files->base.begin_transaction = odb_source_files_begin_transaction; |
| 1310 | files->base.optimize = odb_source_files_optimize; |
| 1311 | files->base.optimize_required = odb_source_files_optimize_required; |
| 1312 | files->base.generate_pack = odb_source_files_generate_pack; |
| 1313 | |
| 1314 | /* |
| 1315 | * Ideally, we would only ever store absolute paths in the source. This |
| 1316 | * is not (yet) possible though because we access and assume relative |
| 1317 | * paths in the primary ODB source in some user-facing functionality. |
| 1318 | */ |
| 1319 | if (!is_absolute_path(object_dir)) |
| 1320 | chdir_notify_register(odb_source_files_reparent, files); |
| 1321 | |
| 1322 | prepare_alternates(files, alternates); |
| 1323 | |
| 1324 | free(object_dir); |
| 1325 | free(alternates); |
| 1326 | return files; |
| 1327 | } |