907e36828f61ee925adfe5eff111abb2fe3f0681
   1#include "builtin.h"
   2#include "cache.h"
   3#include "progress.h"
   4
   5static const char prune_packed_usage[] =
   6"git-prune-packed [-n] [-q]";
   7
   8#define DRY_RUN 01
   9#define VERBOSE 02
  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        if (opts == VERBOSE)
  19                display_progress(&progress, i + 1);
  20
  21        sprintf(hex, "%02x", i);
  22        while ((de = readdir(dir)) != NULL) {
  23                unsigned char sha1[20];
  24                if (strlen(de->d_name) != 38)
  25                        continue;
  26                memcpy(hex+2, de->d_name, 38);
  27                if (get_sha1_hex(hex, sha1))
  28                        continue;
  29                if (!has_sha1_pack(sha1, NULL))
  30                        continue;
  31                memcpy(pathname + len, de->d_name, 38);
  32                if (opts & DRY_RUN)
  33                        printf("rm -f %s\n", pathname);
  34                else if (unlink(pathname) < 0)
  35                        error("unable to unlink %s", pathname);
  36        }
  37        pathname[len] = 0;
  38        rmdir(pathname);
  39}
  40
  41void prune_packed_objects(int opts)
  42{
  43        int i;
  44        static char pathname[PATH_MAX];
  45        const char *dir = get_object_directory();
  46        int len = strlen(dir);
  47
  48        if (opts == VERBOSE)
  49                start_progress_delay(&progress,
  50                        "Removing duplicate objects",
  51                        256, 95, 2);
  52
  53        if (len > PATH_MAX - 42)
  54                die("impossible object directory");
  55        memcpy(pathname, dir, len);
  56        if (len && pathname[len-1] != '/')
  57                pathname[len++] = '/';
  58        for (i = 0; i < 256; i++) {
  59                DIR *d;
  60
  61                sprintf(pathname + len, "%02x/", i);
  62                d = opendir(pathname);
  63                if (!d)
  64                        continue;
  65                prune_dir(i, d, pathname, len + 3, opts);
  66                closedir(d);
  67        }
  68        if (opts == VERBOSE)
  69                stop_progress(&progress);
  70}
  71
  72int cmd_prune_packed(int argc, const char **argv, const char *prefix)
  73{
  74        int i;
  75        int opts = VERBOSE;
  76
  77        for (i = 1; i < argc; i++) {
  78                const char *arg = argv[i];
  79
  80                if (*arg == '-') {
  81                        if (!strcmp(arg, "-n"))
  82                                opts |= DRY_RUN;
  83                        else if (!strcmp(arg, "-q"))
  84                                opts &= ~VERBOSE;
  85                        else
  86                                usage(prune_packed_usage);
  87                        continue;
  88                }
  89                /* Handle arguments here .. */
  90                usage(prune_packed_usage);
  91        }
  92        sync();
  93        prune_packed_objects(opts);
  94        return 0;
  95}