builtin-prune-packed.con commit branch -m/-M: remove undocumented RENAMED-REF (b0eab01)
   1#include "builtin.h"
   2#include "cache.h"
   3
   4static const char prune_packed_usage[] =
   5"git-prune-packed [-n]";
   6
   7static void prune_dir(int i, DIR *dir, char *pathname, int len, int dryrun)
   8{
   9        struct dirent *de;
  10        char hex[40];
  11
  12        sprintf(hex, "%02x", i);
  13        while ((de = readdir(dir)) != NULL) {
  14                unsigned char sha1[20];
  15                if (strlen(de->d_name) != 38)
  16                        continue;
  17                memcpy(hex+2, de->d_name, 38);
  18                if (get_sha1_hex(hex, sha1))
  19                        continue;
  20                if (!has_sha1_pack(sha1, NULL))
  21                        continue;
  22                memcpy(pathname + len, de->d_name, 38);
  23                if (dryrun)
  24                        printf("rm -f %s\n", pathname);
  25                else if (unlink(pathname) < 0)
  26                        error("unable to unlink %s", pathname);
  27        }
  28        pathname[len] = 0;
  29        rmdir(pathname);
  30}
  31
  32void prune_packed_objects(int dryrun)
  33{
  34        int i;
  35        static char pathname[PATH_MAX];
  36        const char *dir = get_object_directory();
  37        int len = strlen(dir);
  38
  39        if (len > PATH_MAX - 42)
  40                die("impossible object directory");
  41        memcpy(pathname, dir, len);
  42        if (len && pathname[len-1] != '/')
  43                pathname[len++] = '/';
  44        for (i = 0; i < 256; i++) {
  45                DIR *d;
  46
  47                sprintf(pathname + len, "%02x/", i);
  48                d = opendir(pathname);
  49                if (!d)
  50                        continue;
  51                prune_dir(i, d, pathname, len + 3, dryrun);
  52                closedir(d);
  53        }
  54}
  55
  56int cmd_prune_packed(int argc, const char **argv, const char *prefix)
  57{
  58        int i;
  59        int dryrun = 0;
  60
  61        for (i = 1; i < argc; i++) {
  62                const char *arg = argv[i];
  63
  64                if (*arg == '-') {
  65                        if (!strcmp(arg, "-n"))
  66                                dryrun = 1;
  67                        else
  68                                usage(prune_packed_usage);
  69                        continue;
  70                }
  71                /* Handle arguments here .. */
  72                usage(prune_packed_usage);
  73        }
  74        sync();
  75        prune_packed_objects(dryrun);
  76        return 0;
  77}