Skip to content

Commit 3e220e6

Browse files
derrickstoleegitster
authored andcommitted
maintenance: create auto condition for loose-objects
The loose-objects task deletes loose objects that already exist in a pack-file, then place the remaining loose objects into a new pack-file. If this step runs all the time, then we risk creating pack-files with very few objects with every 'git commit' process. To prevent overwhelming the packs directory with small pack-files, place a minimum number of objects to justify the task. The 'maintenance.loose-objects.auto' config option specifies a minimum number of loose objects to justify the task to run under the '--auto' option. This defaults to 100 loose objects. Setting the value to zero will prevent the step from running under '--auto' while a negative value will force it to run every time. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 252cfb7 commit 3e220e6

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Documentation/config/maintenance.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ maintenance.commit-graph.auto::
1414
reachable commits that are not in the commit-graph file is at least
1515
the value of `maintenance.commit-graph.auto`. The default value is
1616
100.
17+
18+
maintenance.loose-objects.auto::
19+
This integer config option controls how often the `loose-objects` task
20+
should be run as part of `git maintenance run --auto`. If zero, then
21+
the `loose-objects` task will not run with the `--auto` option. A
22+
negative value will force the task to run every time. Otherwise, a
23+
positive value implies the command should run when the number of
24+
loose objects is at least the value of `maintenance.loose-objects.auto`.
25+
The default value is 100.

builtin/gc.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,35 @@ struct write_loose_object_data {
899899
int batch_size;
900900
};
901901

902+
static int loose_object_auto_limit = 100;
903+
904+
static int loose_object_count(const struct object_id *oid,
905+
const char *path,
906+
void *data)
907+
{
908+
int *count = (int*)data;
909+
if (++(*count) >= loose_object_auto_limit)
910+
return 1;
911+
return 0;
912+
}
913+
914+
static int loose_object_auto_condition(void)
915+
{
916+
int count = 0;
917+
918+
git_config_get_int("maintenance.loose-objects.auto",
919+
&loose_object_auto_limit);
920+
921+
if (!loose_object_auto_limit)
922+
return 0;
923+
if (loose_object_auto_limit < 0)
924+
return 1;
925+
926+
return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
927+
loose_object_count,
928+
NULL, NULL, &count);
929+
}
930+
902931
static int bail_on_loose(const struct object_id *oid,
903932
const char *path,
904933
void *data)
@@ -1009,6 +1038,7 @@ static struct maintenance_task tasks[] = {
10091038
[TASK_LOOSE_OBJECTS] = {
10101039
"loose-objects",
10111040
maintenance_task_loose_objects,
1041+
loose_object_auto_condition,
10121042
},
10131043
[TASK_GC] = {
10141044
"gc",

t/t7900-maintenance.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,26 @@ test_expect_success 'loose-objects task' '
127127
test_cmp packs-between packs-after
128128
'
129129

130+
test_expect_success 'maintenance.loose-objects.auto' '
131+
git repack -adk &&
132+
GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
133+
git -c maintenance.loose-objects.auto=1 maintenance \
134+
run --auto --task=loose-objects 2>/dev/null &&
135+
test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
136+
printf data-A | git hash-object -t blob --stdin -w &&
137+
GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
138+
git -c maintenance.loose-objects.auto=2 \
139+
maintenance run --auto --task=loose-objects 2>/dev/null &&
140+
test_subcommand ! git prune-packed --quiet <trace-loA &&
141+
printf data-B | git hash-object -t blob --stdin -w &&
142+
GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
143+
git -c maintenance.loose-objects.auto=2 \
144+
maintenance run --auto --task=loose-objects 2>/dev/null &&
145+
test_subcommand git prune-packed --quiet <trace-loB &&
146+
GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
147+
git -c maintenance.loose-objects.auto=2 \
148+
maintenance run --auto --task=loose-objects 2>/dev/null &&
149+
test_subcommand git prune-packed --quiet <trace-loC
150+
'
151+
130152
test_done

0 commit comments

Comments
 (0)