Skip to content

Commit f6c5ca3

Browse files
KarthikNayakgitster
authored andcommitted
refs: add a optimize_required field to struct ref_storage_be
To allow users of the refs namespace to check if the reference backend requires optimization, add a new field `optimize_required` field to `struct ref_storage_be`. This field is of type `optimize_required_fn` which is also introduced in this commit. Modify the debug, files, packed and reftable backend to implement this field. A following commit will expose this via 'git pack-refs' and 'git refs optimize'. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e351555 commit f6c5ca3

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

refs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,13 @@ int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts)
23182318
return refs->be->optimize(refs, opts);
23192319
}
23202320

2321+
int refs_optimize_required(struct ref_store *refs,
2322+
struct refs_optimize_opts *opts,
2323+
bool *required)
2324+
{
2325+
return refs->be->optimize_required(refs, opts, required);
2326+
}
2327+
23212328
int reference_get_peeled_oid(struct repository *repo,
23222329
const struct reference *ref,
23232330
struct object_id *peeled_oid)

refs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ struct refs_optimize_opts {
520520
*/
521521
int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);
522522

523+
/*
524+
* Check if refs backend can be optimized by calling 'refs_optimize'.
525+
*/
526+
int refs_optimize_required(struct ref_store *ref_store,
527+
struct refs_optimize_opts *opts,
528+
bool *required);
529+
523530
/*
524531
* Setup reflog before using. Fill in err and return -1 on failure.
525532
*/

refs/debug.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ static int debug_optimize(struct ref_store *ref_store, struct refs_optimize_opts
124124
return res;
125125
}
126126

127+
static int debug_optimize_required(struct ref_store *ref_store,
128+
struct refs_optimize_opts *opts,
129+
bool *required)
130+
{
131+
struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
132+
int res = drefs->refs->be->optimize_required(drefs->refs, opts, required);
133+
trace_printf_key(&trace_refs, "optimize_required: %s, res: %d\n",
134+
required ? "yes" : "no", res);
135+
return res;
136+
}
137+
127138
static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
128139
const char *newref, const char *logmsg)
129140
{
@@ -431,6 +442,8 @@ struct ref_storage_be refs_be_debug = {
431442
.transaction_abort = debug_transaction_abort,
432443

433444
.optimize = debug_optimize,
445+
.optimize_required = debug_optimize_required,
446+
434447
.rename_ref = debug_rename_ref,
435448
.copy_ref = debug_copy_ref,
436449

refs/files-backend.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,16 @@ static int files_optimize(struct ref_store *ref_store,
15121512
return 0;
15131513
}
15141514

1515+
static int files_optimize_required(struct ref_store *ref_store,
1516+
struct refs_optimize_opts *opts,
1517+
bool *required)
1518+
{
1519+
struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_READ,
1520+
"optimize_required");
1521+
*required = should_pack_refs(refs, opts);
1522+
return 0;
1523+
}
1524+
15151525
/*
15161526
* People using contrib's git-new-workdir have .git/logs/refs ->
15171527
* /some/other/path/.git/logs/refs, and that may live on another device.
@@ -3982,6 +3992,7 @@ struct ref_storage_be refs_be_files = {
39823992
.transaction_abort = files_transaction_abort,
39833993

39843994
.optimize = files_optimize,
3995+
.optimize_required = files_optimize_required,
39853996
.rename_ref = files_rename_ref,
39863997
.copy_ref = files_copy_ref,
39873998

refs/packed-backend.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,17 @@ static int packed_optimize(struct ref_store *ref_store UNUSED,
17841784
return 0;
17851785
}
17861786

1787+
static int packed_optimize_required(struct ref_store *ref_store UNUSED,
1788+
struct refs_optimize_opts *opts UNUSED,
1789+
bool *required)
1790+
{
1791+
/*
1792+
* Packed refs are already optimized.
1793+
*/
1794+
*required = false;
1795+
return 0;
1796+
}
1797+
17871798
static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store UNUSED)
17881799
{
17891800
return empty_ref_iterator_begin();
@@ -2130,6 +2141,8 @@ struct ref_storage_be refs_be_packed = {
21302141
.transaction_abort = packed_transaction_abort,
21312142

21322143
.optimize = packed_optimize,
2144+
.optimize_required = packed_optimize_required,
2145+
21332146
.rename_ref = NULL,
21342147
.copy_ref = NULL,
21352148

refs/refs-internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
424424

425425
typedef int optimize_fn(struct ref_store *ref_store,
426426
struct refs_optimize_opts *opts);
427+
428+
typedef int optimize_required_fn(struct ref_store *ref_store,
429+
struct refs_optimize_opts *opts,
430+
bool *required);
431+
427432
typedef int rename_ref_fn(struct ref_store *ref_store,
428433
const char *oldref, const char *newref,
429434
const char *logmsg);
@@ -549,6 +554,7 @@ struct ref_storage_be {
549554
ref_transaction_abort_fn *transaction_abort;
550555

551556
optimize_fn *optimize;
557+
optimize_required_fn *optimize_required;
552558
rename_ref_fn *rename_ref;
553559
copy_ref_fn *copy_ref;
554560

refs/reftable-backend.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,29 @@ static int reftable_be_optimize(struct ref_store *ref_store,
17331733
return ret;
17341734
}
17351735

1736+
static int reftable_be_optimize_required(struct ref_store *ref_store,
1737+
struct refs_optimize_opts *opts,
1738+
bool *required)
1739+
{
1740+
struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_READ,
1741+
"optimize_refs_required");
1742+
struct reftable_stack *stack;
1743+
bool use_heuristics = false;
1744+
1745+
if (refs->err)
1746+
return refs->err;
1747+
1748+
stack = refs->worktree_backend.stack;
1749+
if (!stack)
1750+
stack = refs->main_backend.stack;
1751+
1752+
if (opts->flags & REFS_OPTIMIZE_AUTO)
1753+
use_heuristics = true;
1754+
1755+
return reftable_stack_compaction_required(stack, use_heuristics,
1756+
required);
1757+
}
1758+
17361759
struct write_create_symref_arg {
17371760
struct reftable_ref_store *refs;
17381761
struct reftable_stack *stack;
@@ -2756,6 +2779,8 @@ struct ref_storage_be refs_be_reftable = {
27562779
.transaction_abort = reftable_be_transaction_abort,
27572780

27582781
.optimize = reftable_be_optimize,
2782+
.optimize_required = reftable_be_optimize_required,
2783+
27592784
.rename_ref = reftable_be_rename_ref,
27602785
.copy_ref = reftable_be_copy_ref,
27612786

0 commit comments

Comments
 (0)