@matheusbsbs / matheus-bagliano / commits / 3e220e6069
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 25, 2020 at 12:33 UTC 3e220e60696ebc27c719b83adc6f734d6857521f
3 files changed +61
Documentation/config/maintenance.txt
+9
@@ -14,3 +14,12 @@ maintenance.commit-graph.auto::
14 reachable commits that are not in the commit-graph file is at least
15 the value of `maintenance.commit-graph.auto`. The default value is
16 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
+30
@@ -899,6 +899,35 @@ struct write_loose_object_data {
899 int batch_size;
900 };
901
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 +
931 static int bail_on_loose(const struct object_id *oid,
932 const char *path,
933 void *data)
@@ -1009,6 +1038,7 @@ static struct maintenance_task tasks[] = {
1038 [TASK_LOOSE_OBJECTS] = {
1039 "loose-objects",
1040 maintenance_task_loose_objects,
1041 + loose_object_auto_condition,
1042 },
1043 [TASK_GC] = {
1044 "gc",
t/t7900-maintenance.sh
+22
@@ -127,4 +127,26 @@ test_expect_success 'loose-objects task' '
127 test_cmp packs-between packs-after
128 '
129
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 +
152 test_done