builtin-prune-packed.con commit git-p4: fix bug in symlink handling (b39c361)
   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        "git prune-packed [-n|--dry-run] [-q|--quiet]",
   8        NULL
   9};
  10
  11#define DRY_RUN 01
  12#define VERBOSE 02
  13
  14static struct progress *progress;
  15
  16static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
  17{
  18        struct dirent *de;
  19        char hex[40];
  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))
  30                        continue;
  31                memcpy(pathname + len, de->d_name, 38);
  32                if (opts & DRY_RUN)
  33                        printf("rm -f %s\n", pathname);
  34                else
  35                        unlink_or_warn(pathname);
  36                display_progress(progress, i + 1);
  37        }
  38        pathname[len] = 0;
  39        rmdir(pathname);
  40}
  41
  42void prune_packed_objects(int opts)
  43{
  44        int i;
  45        static char pathname[PATH_MAX];
  46        const char *dir = get_object_directory();
  47        int len = strlen(dir);
  48
  49        if (opts == VERBOSE)
  50                progress = start_progress_delay("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                display_progress(progress, i + 1);
  62                sprintf(pathname + len, "%02x/", i);
  63                d = opendir(pathname);
  64                if (!d)
  65                        continue;
  66                prune_dir(i, d, pathname, len + 3, opts);
  67                closedir(d);
  68        }
  69        stop_progress(&progress);
  70}
  71
  72int cmd_prune_packed(int argc, const char **argv, const char *prefix)
  73{
  74        int opts = isatty(2) ? VERBOSE : 0;
  75        const struct option prune_packed_options[] = {
  76                OPT_BIT('n', "dry-run", &opts, "dry run", DRY_RUN),
  77                OPT_NEGBIT('q', "quiet", &opts, "be quiet", VERBOSE),
  78                OPT_END()
  79        };
  80
  81        argc = parse_options(argc, argv, prefix, prune_packed_options,
  82                             prune_packed_usage, 0);
  83
  84        prune_packed_objects(opts);
  85        return 0;
  86}