MCPcopy Create free account
hub / github.com/git/git / files_optimize

Function files_optimize

refs/files-backend.c:1458–1524  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1456}
1457
1458static int files_optimize(struct ref_store *ref_store,
1459 struct refs_optimize_opts *opts)
1460{
1461 struct files_ref_store *refs =
1462 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1463 "pack_refs");
1464 struct ref_iterator *iter;
1465 int ok;
1466 struct ref_to_prune *refs_to_prune = NULL;
1467 struct strbuf err = STRBUF_INIT;
1468 struct ref_transaction *transaction;
1469
1470 if (!should_pack_refs(refs, opts))
1471 return 0;
1472
1473 transaction = ref_store_transaction_begin(refs->packed_ref_store,
1474 0, &err);
1475 if (!transaction)
1476 return -1;
1477
1478 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
1479
1480 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
1481 refs->base.repo, 0);
1482 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1483 /*
1484 * If the loose reference can be packed, add an entry
1485 * in the packed ref cache. If the reference should be
1486 * pruned, also add it to refs_to_prune.
1487 */
1488 if (!should_pack_ref(refs, &iter->ref, opts))
1489 continue;
1490
1491 /*
1492 * Add a reference creation for this reference to the
1493 * packed-refs transaction:
1494 */
1495 if (ref_transaction_update(transaction, iter->ref.name,
1496 iter->ref.oid, NULL, NULL, NULL,
1497 REF_NO_DEREF, NULL, &err))
1498 die("failure preparing to create packed reference %s: %s",
1499 iter->ref.name, err.buf);
1500
1501 /* Schedule the loose reference for pruning if requested. */
1502 if ((opts->flags & REFS_OPTIMIZE_PRUNE)) {
1503 struct ref_to_prune *n;
1504 FLEX_ALLOC_STR(n, name, iter->ref.name);
1505 oidcpy(&n->oid, iter->ref.oid);
1506 n->next = refs_to_prune;
1507 refs_to_prune = n;
1508 }
1509 }
1510 if (ok != ITER_DONE)
1511 die("error while iterating over references");
1512
1513 if (ref_transaction_commit(transaction, &err))
1514 die("unable to write new packed-refs: %s", err.buf);
1515

Callers

nothing calls this directly

Calls 15

files_downcastFunction · 0.85
should_pack_refsFunction · 0.85
packed_refs_lockFunction · 0.85
cache_ref_iterator_beginFunction · 0.85
get_loose_ref_cacheFunction · 0.85
ref_iterator_advanceFunction · 0.85
should_pack_refFunction · 0.85
ref_transaction_updateFunction · 0.85
oidcpyFunction · 0.85
ref_transaction_commitFunction · 0.85
ref_transaction_freeFunction · 0.85

Tested by

no test coverage detected