Skip to content

Commit 8c1ce22

Browse files
KarthikNayakgitster
authored andcommitted
maintenance: add checking logic in pack_refs_condition()
The 'git-maintenance(1)' command supports an '--auto' flag. Usage of the flag ensures to run maintenance tasks only if certain thresholds are met. The heuristic is defined on a task level, wherein each task defines an 'auto_condition', which states if the task should be run. The 'pack-refs' task is hard-coded to return 1 as: 1. There was never a way to check if the reference backend needs to be optimized without actually performing the optimization. 2. We can pass in the '--auto' flag to 'git-pack-refs(1)' which would optimize based on heuristics. The previous commit added a `refs_optimize_required()` function, which can be used to check if a reference backend required optimization. Use this within `pack_refs_condition()`. This allows us to add a 'git maintenance is-needed' subcommand which can notify the user if maintenance is needed without actually performing the optimization. Without this change, the reference backend would always state that optimization is needed. Since we import 'revision.h', we need to remove the definition for 'SEEN' which is duplicated in the included header. 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 f6c5ca3 commit 8c1ce22

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

builtin/gc.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "path.h"
3636
#include "reflog.h"
3737
#include "rerere.h"
38+
#include "revision.h"
3839
#include "blob.h"
3940
#include "tree.h"
4041
#include "promisor-remote.h"
@@ -285,12 +286,26 @@ static void maintenance_run_opts_release(struct maintenance_run_opts *opts)
285286

286287
static int pack_refs_condition(UNUSED struct gc_config *cfg)
287288
{
288-
/*
289-
* The auto-repacking logic for refs is handled by the ref backends and
290-
* exposed via `git pack-refs --auto`. We thus always return truish
291-
* here and let the backend decide for us.
292-
*/
293-
return 1;
289+
struct string_list included_refs = STRING_LIST_INIT_NODUP;
290+
struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
291+
struct refs_optimize_opts optimize_opts = {
292+
.exclusions = &excludes,
293+
.includes = &included_refs,
294+
.flags = REFS_OPTIMIZE_PRUNE | REFS_OPTIMIZE_AUTO,
295+
};
296+
bool required;
297+
298+
/* Check for all refs, similar to 'git refs optimize --all'. */
299+
string_list_append(optimize_opts.includes, "*");
300+
301+
if (refs_optimize_required(get_main_ref_store(the_repository),
302+
&optimize_opts, &required))
303+
return 0;
304+
305+
clear_ref_exclusions(&excludes);
306+
string_list_clear(&included_refs, 0);
307+
308+
return required;
294309
}
295310

296311
static int maintenance_task_pack_refs(struct maintenance_run_opts *opts,
@@ -1090,9 +1105,6 @@ static int maintenance_opt_schedule(const struct option *opt, const char *arg,
10901105
return 0;
10911106
}
10921107

1093-
/* Remember to update object flag allocation in object.h */
1094-
#define SEEN (1u<<0)
1095-
10961108
struct cg_auto_data {
10971109
int num_not_in_graph;
10981110
int limit;

object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ void object_array_init(struct object_array *array);
7979
* list-objects-filter.c: 21
8080
* bloom.c: 2122
8181
* builtin/fsck.c: 0--3
82-
* builtin/gc.c: 0
8382
* builtin/index-pack.c: 2021
8483
* reflog.c: 10--12
8584
* builtin/show-branch.c: 0-------------------------------------------26

0 commit comments

Comments
 (0)