Skip to content

Commit 916d062

Browse files
derrickstoleegitster
authored andcommitted
maintenance: use pointers to check --auto
The 'git maintenance run' command has an '--auto' option. This is used by other Git commands such as 'git commit' or 'git fetch' to check if maintenance should be run after adding data to the repository. Previously, this --auto option was only used to add the argument to the 'git gc' command as part of the 'gc' task. We will be expanding the other tasks to perform a check to see if they should do work as part of the --auto flag, when they are enabled by config. First, update the 'gc' task to perform the auto check inside the maintenance process. This prevents running an extra 'git gc --auto' command when not needed. It also shows a model for other tasks. Second, use the 'auto_condition' function pointer as a signal for whether we enable the maintenance task under '--auto'. For instance, we do not want to enable the 'fetch' task in '--auto' mode, so that function pointer will remain NULL. Now that we are not automatically calling 'git gc', a test in t5514-fetch-multiple.sh must be changed to watch for 'git maintenance' instead. We continue to pass the '--auto' option to the 'git gc' command when necessary, because of the gc.autoDetach config option changes behavior. Likely, we will want to absorb the daemonizing behavior implied by gc.autoDetach as a maintenance.autoDetach config option. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 65d655b commit 916d062

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

builtin/gc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,17 @@ static int maintenance_task_gc(struct maintenance_run_opts *opts)
755755

756756
typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
757757

758+
/*
759+
* An auto condition function returns 1 if the task should run
760+
* and 0 if the task should NOT run. See needs_to_gc() for an
761+
* example.
762+
*/
763+
typedef int maintenance_auto_fn(void);
764+
758765
struct maintenance_task {
759766
const char *name;
760767
maintenance_task_fn *fn;
768+
maintenance_auto_fn *auto_condition;
761769
unsigned enabled:1;
762770

763771
/* -1 if not selected. */
@@ -776,6 +784,7 @@ static struct maintenance_task tasks[] = {
776784
[TASK_GC] = {
777785
"gc",
778786
maintenance_task_gc,
787+
need_to_gc,
779788
1,
780789
},
781790
[TASK_COMMIT_GRAPH] = {
@@ -831,6 +840,11 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
831840
if (!found_selected && !tasks[i].enabled)
832841
continue;
833842

843+
if (opts->auto_flag &&
844+
(!tasks[i].auto_condition ||
845+
!tasks[i].auto_condition()))
846+
continue;
847+
834848
if (tasks[i].fn(opts)) {
835849
error(_("task '%s' failed"), tasks[i].name);
836850
result = 1;
@@ -845,6 +859,8 @@ static void initialize_task_config(void)
845859
{
846860
int i;
847861
struct strbuf config_name = STRBUF_INIT;
862+
gc_config();
863+
848864
for (i = 0; i < TASK__COUNT; i++) {
849865
int config_value;
850866

t/t5514-fetch-multiple.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test_expect_success 'git fetch --multiple (two remotes)' '
108108
GIT_TRACE=1 git fetch --multiple one two 2>trace &&
109109
git branch -r > output &&
110110
test_cmp ../expect output &&
111-
grep "built-in: git gc" trace >gc &&
111+
grep "built-in: git maintenance" trace >gc &&
112112
test_line_count = 1 gc
113113
)
114114
'

t/t7900-maintenance.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_expect_success 'run [--auto|--quiet]' '
2323
GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
2424
git maintenance run --no-quiet 2>/dev/null &&
2525
test_subcommand git gc --quiet <run-no-auto.txt &&
26-
test_subcommand git gc --auto --quiet <run-auto.txt &&
26+
test_subcommand ! git gc --auto --quiet <run-auto.txt &&
2727
test_subcommand git gc --no-quiet <run-no-quiet.txt
2828
'
2929

0 commit comments

Comments
 (0)