repack: refactor pack deletion for future use
authorDerrick Stolee <dstolee@microsoft.com>
Mon, 10 Jun 2019 23:35:22 +0000 (16:35 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Jun 2019 17:34:40 +0000 (10:34 -0700)
The repack builtin deletes redundant pack-files and their
associated .idx, .promisor, .bitmap, and .keep files. We will want
to re-use this logic in the future for other types of repack, so
pull the logic into 'unlink_pack_path()' in packfile.c.

The 'ignore_keep' parameter is enabled for the use in repack, but
will be important for a future caller.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repack.c
packfile.c
packfile.h
index caca11392713eb92816d1e503f286bfe0d9be78a..3ea0583d0244ba98faf158fdc7ed22c006519d2b 100644 (file)
@@ -129,19 +129,9 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list,
 
 static void remove_redundant_pack(const char *dir_name, const char *base_name)
 {
-       const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
-       int i;
        struct strbuf buf = STRBUF_INIT;
-       size_t plen;
-
-       strbuf_addf(&buf, "%s/%s", dir_name, base_name);
-       plen = buf.len;
-
-       for (i = 0; i < ARRAY_SIZE(exts); i++) {
-               strbuf_setlen(&buf, plen);
-               strbuf_addstr(&buf, exts[i]);
-               unlink(buf.buf);
-       }
+       strbuf_addf(&buf, "%s/%s.pack", dir_name, base_name);
+       unlink_pack_path(buf.buf, 1);
        strbuf_release(&buf);
 }
 
index 49c8544ff4591e71e1845cd1b275d5fdb898b400..020e61a3b6b470a71536d9fefa1f2ba1294a4b17 100644 (file)
@@ -352,6 +352,34 @@ void close_all_packs(struct raw_object_store *o)
        }
 }
 
+void unlink_pack_path(const char *pack_name, int force_delete)
+{
+       static const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
+       int i;
+       struct strbuf buf = STRBUF_INIT;
+       size_t plen;
+
+       strbuf_addstr(&buf, pack_name);
+       strip_suffix_mem(buf.buf, &buf.len, ".pack");
+       plen = buf.len;
+
+       if (!force_delete) {
+               strbuf_addstr(&buf, ".keep");
+               if (!access(buf.buf, F_OK)) {
+                       strbuf_release(&buf);
+                       return;
+               }
+       }
+
+       for (i = 0; i < ARRAY_SIZE(exts); i++) {
+               strbuf_setlen(&buf, plen);
+               strbuf_addstr(&buf, exts[i]);
+               unlink(buf.buf);
+       }
+
+       strbuf_release(&buf);
+}
+
 /*
  * The LRU pack is the one with the oldest MRU window, preferring packs
  * with no used windows, or the oldest mtime if it has no windows allocated.
index b678d35c0b6df11623f2d29f7b7ea7dfe0e1bea1..3c436c124f2801caa42584be6eb82c24e1f22278 100644 (file)
@@ -95,6 +95,13 @@ void unuse_pack(struct pack_window **);
 void clear_delta_base_cache(void);
 struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
 
+/*
+ * Unlink the .pack and associated extension files.
+ * Does not unlink if 'force_delete' is false and the pack-file is
+ * marked as ".keep".
+ */
+extern void unlink_pack_path(const char *pack_name, int force_delete);
+
 /*
  * Make sure that a pointer access into an mmap'd index file is within bounds,
  * and can provide at least 8 bytes of data.