]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repack: remove 'generated_pack' API from the builtin
authorTaylor Blau <me@ttaylorr.com>
Wed, 15 Oct 2025 22:28:29 +0000 (18:28 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Oct 2025 17:08:55 +0000 (10:08 -0700)
Now that we have factored the "generated_pack" API, we can move it to
repack.ch, further slimming down builtin/repack.c.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repack.c
repack.c
repack.h

index 966db27613e37d11503f164b8f9644caf99af529..0e11c3b2c96f5c78980372edcc0557f7ca00cdf4 100644 (file)
@@ -134,89 +134,6 @@ static int write_oid(const struct object_id *oid,
        return 0;
 }
 
-static struct {
-       const char *name;
-       unsigned optional:1;
-} exts[] = {
-       {".pack"},
-       {".rev", 1},
-       {".mtimes", 1},
-       {".bitmap", 1},
-       {".promisor", 1},
-       {".idx"},
-};
-
-struct generated_pack {
-       struct tempfile *tempfiles[ARRAY_SIZE(exts)];
-};
-
-static struct generated_pack *generated_pack_populate(const char *name,
-                                                     const char *packtmp)
-{
-       struct stat statbuf;
-       struct strbuf path = STRBUF_INIT;
-       struct generated_pack *pack = xcalloc(1, sizeof(*pack));
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(exts); i++) {
-               strbuf_reset(&path);
-               strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
-
-               if (stat(path.buf, &statbuf))
-                       continue;
-
-               pack->tempfiles[i] = register_tempfile(path.buf);
-       }
-
-       strbuf_release(&path);
-       return pack;
-}
-
-static int generated_pack_has_ext(const struct generated_pack *pack,
-                                 const char *ext)
-{
-       int i;
-       for (i = 0; i < ARRAY_SIZE(exts); i++) {
-               if (strcmp(exts[i].name, ext))
-                       continue;
-               return !!pack->tempfiles[i];
-       }
-       BUG("unknown pack extension: '%s'", ext);
-}
-
-static void generated_pack_install(struct generated_pack *pack,
-                                  const char *name,
-                                  const char *packdir, const char *packtmp)
-{
-       int ext;
-       for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
-               char *fname;
-
-               fname = mkpathdup("%s/pack-%s%s", packdir, name,
-                                 exts[ext].name);
-
-               if (pack->tempfiles[ext]) {
-                       const char *fname_old = get_tempfile_path(pack->tempfiles[ext]);
-                       struct stat statbuffer;
-
-                       if (!stat(fname_old, &statbuffer)) {
-                               statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
-                               chmod(fname_old, statbuffer.st_mode);
-                       }
-
-                       if (rename_tempfile(&pack->tempfiles[ext], fname))
-                               die_errno(_("renaming pack to '%s' failed"),
-                                         fname);
-               } else if (!exts[ext].optional)
-                       die(_("pack-objects did not write a '%s' file for pack %s-%s"),
-                           exts[ext].name, packtmp, name);
-               else if (unlink(fname) < 0 && errno != ENOENT)
-                       die_errno(_("could not unlink: %s"), fname);
-
-               free(fname);
-       }
-}
-
 static void repack_promisor_objects(struct repository *repo,
                                    const struct pack_objects_args *args,
                                    struct string_list *names)
index 9182e1c50bc8fc5d6e31a5a0a27049522c072bd1..d8afdd352d4ed9556dfaf6d9b59808d12360432d 100644 (file)
--- a/repack.c
+++ b/repack.c
@@ -3,9 +3,11 @@
 #include "midx.h"
 #include "odb.h"
 #include "packfile.h"
+#include "path.h"
 #include "repack.h"
 #include "repository.h"
 #include "run-command.h"
+#include "tempfile.h"
 
 void prepare_pack_objects(struct child_process *cmd,
                          const struct pack_objects_args *args,
@@ -219,3 +221,84 @@ void existing_packs_release(struct existing_packs *existing)
        string_list_clear(&existing->non_kept_packs, 0);
        string_list_clear(&existing->cruft_packs, 0);
 }
+
+static struct {
+       const char *name;
+       unsigned optional:1;
+} exts[] = {
+       {".pack"},
+       {".rev", 1},
+       {".mtimes", 1},
+       {".bitmap", 1},
+       {".promisor", 1},
+       {".idx"},
+};
+
+struct generated_pack {
+       struct tempfile *tempfiles[ARRAY_SIZE(exts)];
+};
+
+struct generated_pack *generated_pack_populate(const char *name,
+                                              const char *packtmp)
+{
+       struct stat statbuf;
+       struct strbuf path = STRBUF_INIT;
+       struct generated_pack *pack = xcalloc(1, sizeof(*pack));
+       size_t i;
+
+       for (i = 0; i < ARRAY_SIZE(exts); i++) {
+               strbuf_reset(&path);
+               strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
+
+               if (stat(path.buf, &statbuf))
+                       continue;
+
+               pack->tempfiles[i] = register_tempfile(path.buf);
+       }
+
+       strbuf_release(&path);
+       return pack;
+}
+
+int generated_pack_has_ext(const struct generated_pack *pack, const char *ext)
+{
+       size_t i;
+       for (i = 0; i < ARRAY_SIZE(exts); i++) {
+               if (strcmp(exts[i].name, ext))
+                       continue;
+               return !!pack->tempfiles[i];
+       }
+       BUG("unknown pack extension: '%s'", ext);
+}
+
+void generated_pack_install(struct generated_pack *pack, const char *name,
+                           const char *packdir, const char *packtmp)
+{
+       size_t ext;
+       for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
+               char *fname;
+
+               fname = mkpathdup("%s/pack-%s%s", packdir, name,
+                                 exts[ext].name);
+
+               if (pack->tempfiles[ext]) {
+                       const char *fname_old = get_tempfile_path(pack->tempfiles[ext]);
+                       struct stat statbuffer;
+
+                       if (!stat(fname_old, &statbuffer)) {
+                               statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
+                               chmod(fname_old, statbuffer.st_mode);
+                       }
+
+                       if (rename_tempfile(&pack->tempfiles[ext], fname))
+                               die_errno(_("renaming pack to '%s' failed"),
+                                         fname);
+               } else if (!exts[ext].optional)
+                       die(_("pack-objects did not write a '%s' file for pack %s-%s"),
+                           exts[ext].name, packtmp, name);
+               else if (unlink(fname) < 0 && errno != ENOENT)
+                       die_errno(_("could not unlink: %s"), fname);
+
+               free(fname);
+       }
+}
index 19796e22432136ddd1c097fa57a5889159604688..f37eb4952495a1e108c8876aa981bc7165b5a8ca 100644 (file)
--- a/repack.h
+++ b/repack.h
@@ -66,4 +66,12 @@ void existing_packs_remove_redundant(struct existing_packs *existing,
                                     const char *packdir);
 void existing_packs_release(struct existing_packs *existing);
 
+struct generated_pack;
+
+struct generated_pack *generated_pack_populate(const char *name,
+                                              const char *packtmp);
+int generated_pack_has_ext(const struct generated_pack *pack, const char *ext);
+void generated_pack_install(struct generated_pack *pack, const char *name,
+                           const char *packdir, const char *packtmp);
+
 #endif /* REPACK_H */