builtin / prune-packed.con commit Merge branch 'tr/fd-gotcha-fixes' (1d1934c)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "progress.h"
   4#include "parse-options.h"
   5
   6static const char * const prune_packed_usage[] = {
   7        N_("git prune-packed [-n|--dry-run] [-q|--quiet]"),
   8        NULL
   9};
  10
  11static struct progress *progress;
  12
  13static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
  14{
  15        struct dirent *de;
  16        char hex[40];
  17
  18        sprintf(hex, "%02x", i);
  19        while ((de = readdir(dir)) != NULL) {
  20                unsigned char sha1[20];
  21                if (strlen(de->d_name) != 38)
  22                        continue;
  23                memcpy(hex+2, de->d_name, 38);
  24                if (get_sha1_hex(hex, sha1))
  25                        continue;
  26                if (!has_sha1_pack(sha1))
  27                        continue;
  28                memcpy(pathname + len, de->d_name, 38);
  29                if (opts & PRUNE_PACKED_DRY_RUN)
  30                        printf("rm -f %s\n", pathname);
  31                else
  32                        unlink_or_warn(pathname);
  33                display_progress(progress, i + 1);
  34        }
  35}
  36
  37void prune_packed_objects(int opts)
  38{
  39        int i;
  40        static char pathname[PATH_MAX];
  41        const char *dir = get_object_directory();
  42        int len = strlen(dir);
  43
  44        if (opts & PRUNE_PACKED_VERBOSE)
  45                progress = start_progress_delay("Removing duplicate objects",
  46                        256, 95, 2);
  47
  48        if (len > PATH_MAX - 42)
  49                die("impossible object directory");
  50        memcpy(pathname, dir, len);
  51        if (len && pathname[len-1] != '/')
  52                pathname[len++] = '/';
  53        for (i = 0; i < 256; i++) {
  54                DIR *d;
  55
  56                display_progress(progress, i + 1);
  57                sprintf(pathname + len, "%02x/", i);
  58                d = opendir(pathname);
  59                if (!d)
  60                        continue;
  61                prune_dir(i, d, pathname, len + 3, opts);
  62                closedir(d);
  63                pathname[len + 2] = '\0';
  64                rmdir(pathname);
  65        }
  66        stop_progress(&progress);
  67}
  68
  69int cmd_prune_packed(int argc, const char **argv, const char *prefix)
  70{
  71        int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
  72        const struct option prune_packed_options[] = {
  73                OPT_BIT('n', "dry-run", &opts, N_("dry run"),
  74                        PRUNE_PACKED_DRY_RUN),
  75                OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"),
  76                           PRUNE_PACKED_VERBOSE),
  77                OPT_END()
  78        };
  79
  80        argc = parse_options(argc, argv, prefix, prune_packed_options,
  81                             prune_packed_usage, 0);
  82
  83        prune_packed_objects(opts);
  84        return 0;
  85}