4 #include "parse-options.h"
6 static const char * const prune_packed_usage[] = {
7 "git prune-packed [-n|--dry-run] [-q|--quiet]",
14 static struct progress *progress;
16 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
21 sprintf(hex, "%02x", i);
22 while ((de = readdir(dir)) != NULL) {
23 unsigned char sha1[20];
24 if (strlen(de->d_name) != 38)
26 memcpy(hex+2, de->d_name, 38);
27 if (get_sha1_hex(hex, sha1))
29 if (!has_sha1_pack(sha1))
31 memcpy(pathname + len, de->d_name, 38);
33 printf("rm -f %s\n", pathname);
35 unlink_or_warn(pathname);
36 display_progress(progress, i + 1);
40 void prune_packed_objects(int opts)
43 static char pathname[PATH_MAX];
44 const char *dir = get_object_directory();
45 int len = strlen(dir);
48 progress = start_progress_delay("Removing duplicate objects",
51 if (len > PATH_MAX - 42)
52 die("impossible object directory");
53 memcpy(pathname, dir, len);
54 if (len && pathname[len-1] != '/')
55 pathname[len++] = '/';
56 for (i = 0; i < 256; i++) {
59 display_progress(progress, i + 1);
60 sprintf(pathname + len, "%02x/", i);
61 d = opendir(pathname);
64 prune_dir(i, d, pathname, len + 3, opts);
66 pathname[len + 2] = '\0';
69 stop_progress(&progress);
72 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
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),
81 argc = parse_options(argc, argv, prefix, prune_packed_options,
82 prune_packed_usage, 0);
84 prune_packed_objects(opts);