]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repack: move `write_cruft_pack()` out of the builtin
authorTaylor Blau <me@ttaylorr.com>
Sun, 28 Sep 2025 22:10:34 +0000 (18:10 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 29 Sep 2025 16:31:25 +0000 (09:31 -0700)
In an identical fashion as the previous commit, move the function
`write_cruft_pack()` into its own compilation unit, and make the
function visible through the repack.h API.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
builtin/repack.c
meson.build
repack-cruft.c [new file with mode: 0644]
repack.h

index ba4f3bbfa27684942be0731d0d172b4754e6788d..e3c4bf1b4aaaaf58740ee6716d11a70505b921c4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1137,6 +1137,7 @@ LIB_OBJS += refs/ref-cache.o
 LIB_OBJS += refspec.o
 LIB_OBJS += remote.o
 LIB_OBJS += repack.o
+LIB_OBJS += repack-cruft.o
 LIB_OBJS += repack-filtered.o
 LIB_OBJS += repack-geometry.o
 LIB_OBJS += repack-midx.o
index a9fc09a24d07b5a2efac5330b44080c2bb1330a8..9171ca66a734860c1f48a4b1cbbb0af5ede9b7c5 100644 (file)
@@ -106,100 +106,6 @@ static int repack_config(const char *var, const char *value,
        return git_default_config(var, value, ctx, cb);
 }
 
-static void combine_small_cruft_packs(FILE *in, size_t combine_cruft_below_size,
-                                     struct existing_packs *existing)
-{
-       struct packfile_store *packs = existing->repo->objects->packfiles;
-       struct packed_git *p;
-       struct strbuf buf = STRBUF_INIT;
-       size_t i;
-
-       for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
-               if (!(p->is_cruft && p->pack_local))
-                       continue;
-
-               strbuf_reset(&buf);
-               strbuf_addstr(&buf, pack_basename(p));
-               strbuf_strip_suffix(&buf, ".pack");
-
-               if (!string_list_has_string(&existing->cruft_packs, buf.buf))
-                       continue;
-
-               if (p->pack_size < combine_cruft_below_size) {
-                       fprintf(in, "-%s\n", pack_basename(p));
-               } else {
-                       existing_packs_retain_cruft(existing, p);
-                       fprintf(in, "%s\n", pack_basename(p));
-               }
-       }
-
-       for (i = 0; i < existing->non_kept_packs.nr; i++)
-               fprintf(in, "-%s.pack\n",
-                       existing->non_kept_packs.items[i].string);
-
-       strbuf_release(&buf);
-}
-
-static int write_cruft_pack(struct write_pack_opts *opts,
-                           const char *cruft_expiration,
-                           unsigned long combine_cruft_below_size,
-                           struct string_list *names,
-                           struct existing_packs *existing)
-{
-       struct child_process cmd = CHILD_PROCESS_INIT;
-       struct string_list_item *item;
-       FILE *in;
-       int ret;
-       const char *pack_prefix = write_pack_opts_pack_prefix(opts);
-
-       prepare_pack_objects(&cmd, opts->po_args, opts->destination);
-
-       strvec_push(&cmd.args, "--cruft");
-       if (cruft_expiration)
-               strvec_pushf(&cmd.args, "--cruft-expiration=%s",
-                            cruft_expiration);
-
-       strvec_push(&cmd.args, "--non-empty");
-
-       cmd.in = -1;
-
-       ret = start_command(&cmd);
-       if (ret)
-               return ret;
-
-       /*
-        * names has a confusing double use: it both provides the list
-        * of just-written new packs, and accepts the name of the cruft
-        * pack we are writing.
-        *
-        * By the time it is read here, it contains only the pack(s)
-        * that were just written, which is exactly the set of packs we
-        * want to consider kept.
-        *
-        * If `--expire-to` is given, the double-use served by `names`
-        * ensures that the pack written to `--expire-to` excludes any
-        * objects contained in the cruft pack.
-        */
-       in = xfdopen(cmd.in, "w");
-       for_each_string_list_item(item, names)
-               fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
-       if (combine_cruft_below_size && !cruft_expiration) {
-               combine_small_cruft_packs(in, combine_cruft_below_size,
-                                         existing);
-       } else {
-               for_each_string_list_item(item, &existing->non_kept_packs)
-                       fprintf(in, "-%s.pack\n", item->string);
-               for_each_string_list_item(item, &existing->cruft_packs)
-                       fprintf(in, "-%s.pack\n", item->string);
-       }
-       for_each_string_list_item(item, &existing->kept_packs)
-               fprintf(in, "%s.pack\n", item->string);
-       fclose(in);
-
-       return finish_pack_objects_cmd(existing->repo->hash_algo, opts, &cmd,
-                                      names);
-}
-
 int cmd_repack(int argc,
               const char **argv,
               const char *prefix,
index 7124b158ae8724eecd154ad542ae07bb8e240049..39152b37ba10a646a7980f81de496aba716c61a2 100644 (file)
@@ -463,6 +463,7 @@ libgit_sources = [
   'reftable/writer.c',
   'remote.c',
   'repack.c',
+  'repack-cruft.c',
   'repack-filtered.c',
   'repack-geometry.c',
   'repack-midx.c',
diff --git a/repack-cruft.c b/repack-cruft.c
new file mode 100644 (file)
index 0000000..accb98b
--- /dev/null
@@ -0,0 +1,99 @@
+#include "git-compat-util.h"
+#include "repack.h"
+#include "packfile.h"
+#include "repository.h"
+#include "run-command.h"
+
+static void combine_small_cruft_packs(FILE *in, off_t combine_cruft_below_size,
+                                     struct existing_packs *existing)
+{
+       struct packfile_store *packs = existing->repo->objects->packfiles;
+       struct packed_git *p;
+       struct strbuf buf = STRBUF_INIT;
+       size_t i;
+
+       for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
+               if (!(p->is_cruft && p->pack_local))
+                       continue;
+
+               strbuf_reset(&buf);
+               strbuf_addstr(&buf, pack_basename(p));
+               strbuf_strip_suffix(&buf, ".pack");
+
+               if (!string_list_has_string(&existing->cruft_packs, buf.buf))
+                       continue;
+
+               if (p->pack_size < combine_cruft_below_size) {
+                       fprintf(in, "-%s\n", pack_basename(p));
+               } else {
+                       existing_packs_retain_cruft(existing, p);
+                       fprintf(in, "%s\n", pack_basename(p));
+               }
+       }
+
+       for (i = 0; i < existing->non_kept_packs.nr; i++)
+               fprintf(in, "-%s.pack\n",
+                       existing->non_kept_packs.items[i].string);
+
+       strbuf_release(&buf);
+}
+
+int write_cruft_pack(struct write_pack_opts *opts,
+                    const char *cruft_expiration,
+                    unsigned long combine_cruft_below_size,
+                    struct string_list *names,
+                    struct existing_packs *existing)
+{
+       struct child_process cmd = CHILD_PROCESS_INIT;
+       struct string_list_item *item;
+       FILE *in;
+       int ret;
+       const char *pack_prefix = write_pack_opts_pack_prefix(opts);
+
+       prepare_pack_objects(&cmd, opts->po_args, opts->destination);
+
+       strvec_push(&cmd.args, "--cruft");
+       if (cruft_expiration)
+               strvec_pushf(&cmd.args, "--cruft-expiration=%s",
+                            cruft_expiration);
+
+       strvec_push(&cmd.args, "--non-empty");
+
+       cmd.in = -1;
+
+       ret = start_command(&cmd);
+       if (ret)
+               return ret;
+
+       /*
+        * names has a confusing double use: it both provides the list
+        * of just-written new packs, and accepts the name of the cruft
+        * pack we are writing.
+        *
+        * By the time it is read here, it contains only the pack(s)
+        * that were just written, which is exactly the set of packs we
+        * want to consider kept.
+        *
+        * If `--expire-to` is given, the double-use served by `names`
+        * ensures that the pack written to `--expire-to` excludes any
+        * objects contained in the cruft pack.
+        */
+       in = xfdopen(cmd.in, "w");
+       for_each_string_list_item(item, names)
+               fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
+       if (combine_cruft_below_size && !cruft_expiration) {
+               combine_small_cruft_packs(in, combine_cruft_below_size,
+                                         existing);
+       } else {
+               for_each_string_list_item(item, &existing->non_kept_packs)
+                       fprintf(in, "-%s.pack\n", item->string);
+               for_each_string_list_item(item, &existing->cruft_packs)
+                       fprintf(in, "-%s.pack\n", item->string);
+       }
+       for_each_string_list_item(item, &existing->kept_packs)
+               fprintf(in, "%s.pack\n", item->string);
+       fclose(in);
+
+       return finish_pack_objects_cmd(existing->repo->hash_algo, opts, &cmd,
+                                      names);
+}
index a7ddbe784b9d997fdcd80df06a40d14a601c4588..15886c348862d52a36552806e9c69f00e28d5885 100644 (file)
--- a/repack.h
+++ b/repack.h
@@ -137,4 +137,10 @@ int write_filtered_pack(struct write_pack_opts *opts,
                        struct existing_packs *existing,
                        struct string_list *names);
 
+int write_cruft_pack(struct write_pack_opts *opts,
+                    const char *cruft_expiration,
+                    unsigned long combine_cruft_below_size,
+                    struct string_list *names,
+                    struct existing_packs *existing);
+
 #endif /* REPACK_H */