builtin-prune-packed.con commit http-push: do not get confused by submodules (1bbeb4c)
   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        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, NULL))
  27                        continue;
  28                memcpy(pathname + len, de->d_name, 38);
  29                if (opts & DRY_RUN)
  30                        printf("rm -f %s\n", pathname);
  31                else if (unlink(pathname) < 0)
  32                        error("unable to unlink %s", pathname);
  33                display_progress(progress, i + 1);
  34        }
  35        pathname[len] = 0;
  36        rmdir(pathname);
  37}
  38
  39void prune_packed_objects(int opts)
  40{
  41        int i;
  42        static char pathname[PATH_MAX];
  43        const char *dir = get_object_directory();
  44        int len = strlen(dir);
  45
  46        if (opts == VERBOSE)
  47                progress = start_progress_delay("Removing duplicate objects",
  48                        256, 95, 2);
  49
  50        if (len > PATH_MAX - 42)
  51                die("impossible object directory");
  52        memcpy(pathname, dir, len);
  53        if (len && pathname[len-1] != '/')
  54                pathname[len++] = '/';
  55        for (i = 0; i < 256; i++) {
  56                DIR *d;
  57
  58                sprintf(pathname + len, "%02x/", i);
  59                d = opendir(pathname);
  60                if (!d)
  61                        continue;
  62                prune_dir(i, d, pathname, len + 3, opts);
  63                closedir(d);
  64        }
  65        stop_progress(&progress);
  66}
  67
  68int cmd_prune_packed(int argc, const char **argv, const char *prefix)
  69{
  70        int i;
  71        int opts = VERBOSE;
  72
  73        for (i = 1; i < argc; i++) {
  74                const char *arg = argv[i];
  75
  76                if (*arg == '-') {
  77                        if (!strcmp(arg, "-n"))
  78                                opts |= DRY_RUN;
  79                        else if (!strcmp(arg, "-q"))
  80                                opts &= ~VERBOSE;
  81                        else
  82                                usage(prune_packed_usage);
  83                        continue;
  84                }
  85                /* Handle arguments here .. */
  86                usage(prune_packed_usage);
  87        }
  88        sync();
  89        prune_packed_objects(opts);
  90        return 0;
  91}