builtin/repack.c: implement support for `--max-cruft-size`

Cruft packs are an alternative mechanism for storing a collection of unreachable objects whose mtimes are recent enough to avoid being pruned out of the repository. When cruft packs were first introduced back in b757353676 (builtin/pack-objects.c: --cruft without expiration, 2022-05-20) and a7d493833f (builtin/pack-objects.c: --cruft with expiration, 2022-05-20), the recommended workflow consisted of: - Repacking periodically, either by packing anything loose in the repository (via `git repack -d`) or producing a geometric sequence of packs (via `git repack --geometric=<d> -d`). - Every so often, splitting the repository into two packs, one cruft to store the unreachable objects, and another non-cruft pack to store the reachable objects. Repositories may (out of band with the above) choose periodically to prune out some unreachable objects which have aged out of the grace period by generating a pack with `--cruft-expiration=<approxidate>`. This allowed repositories to maintain relatively few packs on average, and quarantine unreachable objects together in a cruft pack, avoiding the pitfalls of holding unreachable objects as loose while they age out (for more, see some of the details in 3d89a8c118 (Documentation/technical: add cruft-packs.txt, 2022-05-20)). This all works, but can be costly from an I/O-perspective when frequently repacking a repository that has many unreachable objects. This problem is exacerbated when those unreachable objects are rarely (if every) pruned. Since there is at most one cruft pack in the above scheme, each time we update the cruft pack it must be rewritten from scratch. Because much of the pack is reused, this is a relatively inexpensive operation from a CPU-perspective, but is very costly in terms of I/O since we end up rewriting basically the same pack (plus any new unreachable objects that have entered the repository since the last time a cruft pack was generated). At the time, we decided against implementing more robust support for multiple cruft packs. This patch implements that support which we were lacking. Introduce a new option `--max-cruft-size` which allows repositories to accumulate cruft packs up to a given size, after which point a new generation of cruft packs can accumulate until it reaches the maximum size, and so on. To generate a new cruft pack, the process works like so: - Sort a list of any existing cruft packs in ascending order of pack size. - Starting from the beginning of the list, group cruft packs together while the accumulated size is smaller than the maximum specified pack size. - Combine the objects in these cruft packs together into a new cruft pack, along with any other unreachable objects which have since entered the repository. Once a cruft pack grows beyond the size specified via `--max-cruft-size` the pack is effectively frozen. This limits the I/O churn up to a quadratic function of the value specified by the `--max-cruft-size` option, instead of behaving quadratically in the number of total unreachable objects. When pruning unreachable objects, we bypass the new code paths which combine small cruft packs together, and instead start from scratch, passing in the appropriate `--max-pack-size` down to `pack-objects`, putting it in charge of keeping the resulting set of cruft packs sized correctly. This may seem like further I/O churn, but in practice it isn't so bad. We could prune old cruft packs for whom all or most objects are removed, and then generate a new cruft pack with just the remaining set of objects. But this additional complexity buys us relatively little, because most objects end up being pruned anyway, so the I/O churn is well contained. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Oct 2, 2023 at 20:44 UTC 37dc6d81042ac41437163264e7a29d3bf50c8d90
7 files changed +426 -11
Documentation/config/gc.txt
+6
@@ -86,6 +86,12 @@ gc.cruftPacks::
86 linkgit:git-repack[1]) instead of as loose objects. The default
87 is `true`.
88
89 +gc.maxCruftSize::
90 + Limit the size of new cruft packs when repacking. When
91 + specified in addition to `--max-cruft-size`, the command line
92 + option takes priority. See the `--max-cruft-size` option of
93 + linkgit:git-repack[1].
94 +
95 gc.pruneExpire::
96 When 'git gc' is run, it will call 'prune --expire 2.weeks.ago'
97 (and 'repack --cruft --cruft-expiration 2.weeks.ago' if using
Documentation/git-gc.txt
+7
@@ -59,6 +59,13 @@ be performed as well.
59 cruft pack instead of storing them as loose objects. `--cruft`
60 is on by default.
61
62 +--max-cruft-size=<n>::
63 + When packing unreachable objects into a cruft pack, limit the
64 + size of new cruft packs to be at most `<n>` bytes. Overrides any
65 + value specified via the `gc.maxCruftSize` configuration. See
66 + the `--max-cruft-size` option of linkgit:git-repack[1] for
67 + more.
68 +
69 --prune=<date>::
70 Prune loose objects older than date (default is 2 weeks ago,
71 overridable by the config variable `gc.pruneExpire`).
Documentation/git-repack.txt
+11
@@ -74,6 +74,17 @@ to the new separate pack will be written.
74 immediately instead of waiting for the next `git gc` invocation.
75 Only useful with `--cruft -d`.
76
77 +--max-cruft-size=<n>::
78 + Repack cruft objects into packs as large as `<n>` bytes before
79 + creating new packs. As long as there are enough cruft packs
80 + smaller than `<n>`, repacking will cause a new cruft pack to
81 + be created containing objects from any combined cruft packs,
82 + along with any new unreachable objects. Cruft packs larger than
83 + `<n>` will not be modified. When the new cruft pack is larger
84 + than `<n>` bytes, it will be split into multiple packs, all of
85 + which are guaranteed to be at most `<n>` bytes in size. Only
86 + useful with `--cruft -d`.
87 +
88 --expire-to=<dir>::
89 Write a cruft pack containing pruned objects (if any) to the
90 directory `<dir>`. This option is useful for keeping a copy of
builtin/gc.c
+7
@@ -52,6 +52,7 @@ static const char * const builtin_gc_usage[] = {
52 static int pack_refs = 1;
53 static int prune_reflogs = 1;
54 static int cruft_packs = 1;
55 +static unsigned long max_cruft_size;
56 static int aggressive_depth = 50;
57 static int aggressive_window = 250;
58 static int gc_auto_threshold = 6700;
@@ -163,6 +164,7 @@ static void gc_config(void)
164 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
165 git_config_get_bool("gc.autodetach", &detach_auto);
166 git_config_get_bool("gc.cruftpacks", &cruft_packs);
167 + git_config_get_ulong("gc.maxcruftsize", &max_cruft_size);
168 git_config_get_expiry("gc.pruneexpire", &prune_expire);
169 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
170 git_config_get_expiry("gc.logexpiry", &gc_log_expire);
@@ -347,6 +349,9 @@ static void add_repack_all_option(struct string_list *keep_pack)
349 strvec_push(&repack, "--cruft");
350 if (prune_expire)
351 strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
352 + if (max_cruft_size)
353 + strvec_pushf(&repack, "--max-cruft-size=%lu",
354 + max_cruft_size);
355 } else {
356 strvec_push(&repack, "-A");
357 if (prune_expire)
@@ -575,6 +580,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
580 N_("prune unreferenced objects"),
581 PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
582 OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
583 + OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
584 + N_("with --cruft, limit the size of new cruft packs")),
585 OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
586 OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
587 PARSE_OPT_NOCOMPLETE),
builtin/repack.c
+123 -11
@@ -27,6 +27,7 @@
27 #define PACK_CRUFT 4
28
29 #define DELETE_PACK 1
30 +#define RETAIN_PACK 2
31
32 static int pack_everything;
33 static int delta_base_offset = 1;
@@ -116,11 +117,26 @@ static void pack_mark_for_deletion(struct string_list_item *item)
117 item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
118 }
119
120 +static void pack_unmark_for_deletion(struct string_list_item *item)
121 +{
122 + item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
123 +}
124 +
125 static int pack_is_marked_for_deletion(struct string_list_item *item)
126 {
127 return (uintptr_t)item->util & DELETE_PACK;
128 }
129
130 +static void pack_mark_retained(struct string_list_item *item)
131 +{
132 + item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
133 +}
134 +
135 +static int pack_is_retained(struct string_list_item *item)
136 +{
137 + return (uintptr_t)item->util & RETAIN_PACK;
138 +}
139 +
140 static void mark_packs_for_deletion_1(struct string_list *names,
141 struct string_list *list)
142 {
@@ -133,17 +149,39 @@ static void mark_packs_for_deletion_1(struct string_list *names,
149 if (len < hexsz)
150 continue;
151 sha1 = item->string + len - hexsz;
136 - /*
137 - * Mark this pack for deletion, which ensures that this
138 - * pack won't be included in a MIDX (if `--write-midx`
139 - * was given) and that we will actually delete this pack
140 - * (if `-d` was given).
141 - */
142 - if (!string_list_has_string(names, sha1))
152 +
153 + if (pack_is_retained(item)) {
154 + pack_unmark_for_deletion(item);
155 + } else if (!string_list_has_string(names, sha1)) {
156 + /*
157 + * Mark this pack for deletion, which ensures
158 + * that this pack won't be included in a MIDX
159 + * (if `--write-midx` was given) and that we
160 + * will actually delete this pack (if `-d` was
161 + * given).
162 + */
163 pack_mark_for_deletion(item);
164 + }
165 }
166 }
167
168 +static void retain_cruft_pack(struct existing_packs *existing,
169 + struct packed_git *cruft)
170 +{
171 + struct strbuf buf = STRBUF_INIT;
172 + struct string_list_item *item;
173 +
174 + strbuf_addstr(&buf, pack_basename(cruft));
175 + strbuf_strip_suffix(&buf, ".pack");
176 +
177 + item = string_list_lookup(&existing->cruft_packs, buf.buf);
178 + if (!item)
179 + BUG("could not find cruft pack '%s'", pack_basename(cruft));
180 +
181 + pack_mark_retained(item);
182 + strbuf_release(&buf);
183 +}
184 +
185 static void mark_packs_for_deletion(struct existing_packs *existing,
186 struct string_list *names)
187
@@ -225,6 +263,8 @@ static void collect_pack_filenames(struct existing_packs *existing,
263 }
264
265 string_list_sort(&existing->kept_packs);
266 + string_list_sort(&existing->non_kept_packs);
267 + string_list_sort(&existing->cruft_packs);
268 strbuf_release(&buf);
269 }
270
@@ -806,6 +846,72 @@ static void remove_redundant_bitmaps(struct string_list *include,
846 strbuf_release(&path);
847 }
848
849 +static int existing_cruft_pack_cmp(const void *va, const void *vb)
850 +{
851 + struct packed_git *a = *(struct packed_git **)va;
852 + struct packed_git *b = *(struct packed_git **)vb;
853 +
854 + if (a->pack_size < b->pack_size)
855 + return -1;
856 + if (a->pack_size > b->pack_size)
857 + return 1;
858 + return 0;
859 +}
860 +
861 +static void collapse_small_cruft_packs(FILE *in, size_t max_size,
862 + struct existing_packs *existing)
863 +{
864 + struct packed_git **existing_cruft, *p;
865 + struct strbuf buf = STRBUF_INIT;
866 + size_t total_size = 0;
867 + size_t existing_cruft_nr = 0;
868 + size_t i;
869 +
870 + ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
871 +
872 + for (p = get_all_packs(the_repository); p; p = p->next) {
873 + if (!(p->is_cruft && p->pack_local))
874 + continue;
875 +
876 + strbuf_reset(&buf);
877 + strbuf_addstr(&buf, pack_basename(p));
878 + strbuf_strip_suffix(&buf, ".pack");
879 +
880 + if (!string_list_has_string(&existing->cruft_packs, buf.buf))
881 + continue;
882 +
883 + if (existing_cruft_nr >= existing->cruft_packs.nr)
884 + BUG("too many cruft packs (found %"PRIuMAX", but knew "
885 + "of %"PRIuMAX")",
886 + (uintmax_t)existing_cruft_nr + 1,
887 + (uintmax_t)existing->cruft_packs.nr);
888 + existing_cruft[existing_cruft_nr++] = p;
889 + }
890 +
891 + QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
892 +
893 + for (i = 0; i < existing_cruft_nr; i++) {
894 + size_t proposed;
895 +
896 + p = existing_cruft[i];
897 + proposed = st_add(total_size, p->pack_size);
898 +
899 + if (proposed <= max_size) {
900 + total_size = proposed;
901 + fprintf(in, "-%s\n", pack_basename(p));
902 + } else {
903 + retain_cruft_pack(existing, p);
904 + fprintf(in, "%s\n", pack_basename(p));
905 + }
906 + }
907 +
908 + for (i = 0; i < existing->non_kept_packs.nr; i++)
909 + fprintf(in, "-%s.pack\n",
910 + existing->non_kept_packs.items[i].string);
911 +
912 + strbuf_release(&buf);
913 +}
914 +
915 static int write_cruft_pack(const struct pack_objects_args *args,
916 const char *destination,
917 const char *pack_prefix,
@@ -853,10 +959,14 @@ static int write_cruft_pack(const struct pack_objects_args *args,
959 in = xfdopen(cmd.in, "w");
960 for_each_string_list_item(item, names)
961 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
856 - for_each_string_list_item(item, &existing->non_kept_packs)
857 - fprintf(in, "-%s.pack\n", item->string);
858 - for_each_string_list_item(item, &existing->cruft_packs)
859 - fprintf(in, "-%s.pack\n", item->string);
962 + if (args->max_pack_size && !cruft_expiration) {
963 + collapse_small_cruft_packs(in, args->max_pack_size, existing);
964 + } else {
965 + for_each_string_list_item(item, &existing->non_kept_packs)
966 + fprintf(in, "-%s.pack\n", item->string);
967 + for_each_string_list_item(item, &existing->cruft_packs)
968 + fprintf(in, "-%s.pack\n", item->string);
969 + }
970 for_each_string_list_item(item, &existing->kept_packs)
971 fprintf(in, "%s.pack\n", item->string);
972 fclose(in);
@@ -919,6 +1029,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1029 PACK_CRUFT),
1030 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1031 N_("with --cruft, expire objects older than this")),
1032 + OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1033 + N_("with --cruft, limit the size of new cruft packs")),
1034 OPT_BOOL('d', NULL, &delete_redundant,
1035 N_("remove redundant packs, and run git-prune-packed")),
1036 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
t/t6500-gc.sh
+27
@@ -303,6 +303,33 @@ test_expect_success 'gc.bigPackThreshold ignores cruft packs' '
303 )
304 '
305
306 +cruft_max_size_opts="git repack -d -l --cruft --cruft-expiration=2.weeks.ago"
307 +
308 +test_expect_success 'setup for --max-cruft-size tests' '
309 + git init cruft--max-size &&
310 + (
311 + cd cruft--max-size &&
312 + prepare_cruft_history
313 + )
314 +'
315 +
316 +test_expect_success '--max-cruft-size sets appropriate repack options' '
317 + GIT_TRACE2_EVENT=$(pwd)/trace2.txt git -C cruft--max-size \
318 + gc --cruft --max-cruft-size=1M &&
319 + test_subcommand $cruft_max_size_opts --max-cruft-size=1048576 <trace2.txt
320 +'
321 +
322 +test_expect_success 'gc.maxCruftSize sets appropriate repack options' '
323 + GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
324 + git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft &&
325 + test_subcommand $cruft_max_size_opts --max-cruft-size=2097152 <trace2.txt &&
326 +
327 + GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
328 + git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft \
329 + --max-cruft-size=3M &&
330 + test_subcommand $cruft_max_size_opts --max-cruft-size=3145728 <trace2.txt
331 +'
332 +
333 run_and_wait_for_auto_gc () {
334 # We read stdout from gc for the side effect of waiting until the
335 # background gc process exits, closing its fd 9. Furthermore, the
t/t7704-repack-cruft.sh
+245
@@ -5,6 +5,7 @@ test_description='git repack works correctly'
5 . ./test-lib.sh
6
7 objdir=.git/objects
8 +packdir=$objdir/pack
9
10 test_expect_success '--expire-to stores pruned objects (now)' '
11 git init expire-to-now &&
@@ -127,4 +128,248 @@ test_expect_success '--expire-to stores pruned objects (5.minutes.ago)' '
128 )
129 '
130
131 +generate_random_blob() {
132 + test-tool genrandom "$@" >blob &&
133 + git hash-object -w -t blob blob &&
134 + rm blob
135 +}
136 +
137 +pack_random_blob () {
138 + generate_random_blob "$@" &&
139 + git repack -d -q >/dev/null
140 +}
141 +
142 +generate_cruft_pack () {
143 + pack_random_blob "$@" >/dev/null &&
144 +
145 + ls $packdir/pack-*.pack | xargs -n 1 basename >in &&
146 + pack="$(git pack-objects --cruft $packdir/pack <in)" &&
147 + git prune-packed &&
148 +
149 + echo "$packdir/pack-$pack.mtimes"
150 +}
151 +
152 +test_expect_success '--max-cruft-size creates new packs when above threshold' '
153 + git init max-cruft-size-large &&
154 + (
155 + cd max-cruft-size-large &&
156 + test_commit base &&
157 +
158 + foo="$(pack_random_blob foo $((1*1024*1024)))" &&
159 + git repack --cruft -d &&
160 + cruft_foo="$(ls $packdir/pack-*.mtimes)" &&
161 +
162 + bar="$(pack_random_blob bar $((1*1024*1024)))" &&
163 + git repack --cruft -d --max-cruft-size=1M &&
164 + cruft_bar="$(ls $packdir/pack-*.mtimes | grep -v $cruft_foo)" &&
165 +
166 + test-tool pack-mtimes $(basename "$cruft_foo") >foo.objects &&
167 + test-tool pack-mtimes $(basename "$cruft_bar") >bar.objects &&
168 +
169 + grep "^$foo" foo.objects &&
170 + test_line_count = 1 foo.objects &&
171 + grep "^$bar" bar.objects &&
172 + test_line_count = 1 bar.objects
173 + )
174 +'
175 +
176 +test_expect_success '--max-cruft-size combines existing packs when below threshold' '
177 + git init max-cruft-size-small &&
178 + (
179 + cd max-cruft-size-small &&
180 + test_commit base &&
181 +
182 + foo="$(pack_random_blob foo $((1*1024*1024)))" &&
183 + git repack --cruft -d &&
184 +
185 + bar="$(pack_random_blob bar $((1*1024*1024)))" &&
186 + git repack --cruft -d --max-cruft-size=10M &&
187 +
188 + cruft=$(ls $packdir/pack-*.mtimes) &&
189 + test-tool pack-mtimes $(basename "$cruft") >cruft.objects &&
190 +
191 + grep "^$foo" cruft.objects &&
192 + grep "^$bar" cruft.objects &&
193 + test_line_count = 2 cruft.objects
194 + )
195 +'
196 +
197 +test_expect_success '--max-cruft-size combines smaller packs first' '
198 + git init max-cruft-size-consume-small &&
199 + (
200 + cd max-cruft-size-consume-small &&
201 +
202 + test_commit base &&
203 + git repack -ad &&
204 +
205 + cruft_foo="$(generate_cruft_pack foo 524288)" && # 0.5 MiB
206 + cruft_bar="$(generate_cruft_pack bar 524288)" && # 0.5 MiB
207 + cruft_baz="$(generate_cruft_pack baz 1048576)" && # 1.0 MiB
208 + cruft_quux="$(generate_cruft_pack quux 1572864)" && # 1.5 MiB
209 +
210 + test-tool pack-mtimes "$(basename $cruft_foo)" >expect.raw &&
211 + test-tool pack-mtimes "$(basename $cruft_bar)" >>expect.raw &&
212 + sort expect.raw >expect.objects &&
213 +
214 + # repacking with `--max-cruft-size=2M` should combine
215 + # both 0.5 MiB packs together, instead of, say, one of
216 + # the 0.5 MiB packs with the 1.0 MiB pack
217 + ls $packdir/pack-*.mtimes | sort >cruft.before &&
218 + git repack -d --cruft --max-cruft-size=2M &&
219 + ls $packdir/pack-*.mtimes | sort >cruft.after &&
220 +
221 + comm -13 cruft.before cruft.after >cruft.new &&
222 + comm -23 cruft.before cruft.after >cruft.removed &&
223 +
224 + test_line_count = 1 cruft.new &&
225 + test_line_count = 2 cruft.removed &&
226 +
227 + # the two smaller packs should be rolled up first
228 + printf "%s\n" $cruft_foo $cruft_bar | sort >expect.removed &&
229 + test_cmp expect.removed cruft.removed &&
230 +
231 + # ...and contain the set of objects rolled up
232 + test-tool pack-mtimes "$(basename $(cat cruft.new))" >actual.raw &&
233 + sort actual.raw >actual.objects &&
234 +
235 + test_cmp expect.objects actual.objects
236 + )
237 +'
238 +
239 +test_expect_success 'setup --max-cruft-size with freshened objects' '
240 + git init max-cruft-size-freshen &&
241 + (
242 + cd max-cruft-size-freshen &&
243 +
244 + test_commit base &&
245 + git repack -ad &&
246 +
247 + foo="$(generate_random_blob foo 64)" &&
248 + test-tool chmtime --get -10000 \
249 + "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
250 +
251 + git repack --cruft -d &&
252 +
253 + cruft="$(ls $packdir/pack-*.mtimes)" &&
254 + test-tool pack-mtimes "$(basename $cruft)" >actual &&
255 + echo "$foo $(cat foo.mtime)" >expect &&
256 + test_cmp expect actual
257 + )
258 +'
259 +
260 +test_expect_success '--max-cruft-size with freshened objects (loose)' '
261 + (
262 + cd max-cruft-size-freshen &&
263 +
264 + # regenerate the object, setting its mtime to be more recent
265 + foo="$(generate_random_blob foo 64)" &&
266 + test-tool chmtime --get -100 \
267 + "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
268 +
269 + git repack --cruft -d &&
270 +
271 + cruft="$(ls $packdir/pack-*.mtimes)" &&
272 + test-tool pack-mtimes "$(basename $cruft)" >actual &&
273 + echo "$foo $(cat foo.mtime)" >expect &&
274 + test_cmp expect actual
275 + )
276 +'
277 +
278 +test_expect_success '--max-cruft-size with freshened objects (packed)' '
279 + (
280 + cd max-cruft-size-freshen &&
281 +
282 + # regenerate the object and store it in a packfile,
283 + # setting its mtime to be more recent
284 + #
285 + # store it alongside another cruft object so that we
286 + # do not create an identical copy of the existing
287 + # cruft pack (which contains $foo).
288 + foo="$(generate_random_blob foo 64)" &&
289 + bar="$(generate_random_blob bar 64)" &&
290 + foo_pack="$(printf "%s\n" $foo $bar | git pack-objects $packdir/pack)" &&
291 + git prune-packed &&
292 +
293 + test-tool chmtime --get -10 \
294 + "$packdir/pack-$foo_pack.pack" >foo.mtime &&
295 +
296 + git repack --cruft -d &&
297 +
298 + cruft="$(ls $packdir/pack-*.mtimes)" &&
299 + test-tool pack-mtimes "$(basename $cruft)" >actual &&
300 + echo "$foo $(cat foo.mtime)" >expect.raw &&
301 + echo "$bar $(cat foo.mtime)" >>expect.raw &&
302 + sort expect.raw >expect &&
303 + test_cmp expect actual
304 + )
305 +'
306 +
307 +test_expect_success '--max-cruft-size with pruning' '
308 + git init max-cruft-size-prune &&
309 + (
310 + cd max-cruft-size-prune &&
311 +
312 + test_commit base &&
313 + foo="$(generate_random_blob foo $((1024*1024)))" &&
314 + bar="$(generate_random_blob bar $((1024*1024)))" &&
315 + baz="$(generate_random_blob baz $((1024*1024)))" &&
316 +
317 + test-tool chmtime -10000 "$objdir/$(test_oid_to_path "$foo")" &&
318 +
319 + git repack -d --cruft --max-cruft-size=1M &&
320 +
321 + # backdate the mtimes of all cruft packs to validate
322 + # that they were rewritten as a result of pruning
323 + ls $packdir/pack-*.mtimes | sort >cruft.before &&
324 + for cruft in $(cat cruft.before)
325 + do
326 + mtime="$(test-tool chmtime --get -10000 "$cruft")" &&
327 + echo $cruft $mtime >>mtimes || return 1
328 + done &&
329 +
330 + # repack (and prune) with a --max-cruft-size to ensure
331 + # that we appropriately split the resulting set of packs
332 + git repack -d --cruft --max-cruft-size=1M \
333 + --cruft-expiration=10.seconds.ago &&
334 + ls $packdir/pack-*.mtimes | sort >cruft.after &&
335 +
336 + for cruft in $(cat cruft.after)
337 + do
338 + old_mtime="$(grep $cruft mtimes | cut -d" " -f2)" &&
339 + new_mtime="$(test-tool chmtime --get $cruft)" &&
340 + test $old_mtime -lt $new_mtime || return 1
341 + done &&
342 +
343 + test_line_count = 3 cruft.before &&
344 + test_line_count = 2 cruft.after &&
345 + test_must_fail git cat-file -e $foo &&
346 + git cat-file -e $bar &&
347 + git cat-file -e $baz
348 + )
349 +'
350 +
351 +test_expect_success '--max-cruft-size ignores non-local packs' '
352 + repo="max-cruft-size-non-local" &&
353 + git init $repo &&
354 + (
355 + cd $repo &&
356 + test_commit base &&
357 + generate_random_blob foo 64 &&
358 + git repack --cruft -d
359 + ) &&
360 +
361 + git clone --reference=$repo $repo $repo-alt &&
362 + (
363 + cd $repo-alt &&
364 +
365 + test_commit other &&
366 + generate_random_blob bar 64 &&
367 +
368 + # ensure that we do not attempt to pick up packs from
369 + # the non-alternated repository, which would result in a
370 + # crash
371 + git repack --cruft --max-cruft-size=1M -d
372 + )
373 +'
374 +
375 test_done