4 #include "parse-options.h"
6 static const char * const prune_packed_usage[] = {
7 N_("git prune-packed [-n|--dry-run] [-q|--quiet]"),
11 static struct progress *progress;
13 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
18 sprintf(hex, "%02x", i);
19 while ((de = readdir(dir)) != NULL) {
20 unsigned char sha1[20];
21 if (strlen(de->d_name) != 38)
23 memcpy(hex+2, de->d_name, 38);
24 if (get_sha1_hex(hex, sha1))
26 if (!has_sha1_pack(sha1))
28 memcpy(pathname + len, de->d_name, 38);
29 if (opts & PRUNE_PACKED_DRY_RUN)
30 printf("rm -f %s\n", pathname);
32 unlink_or_warn(pathname);
33 display_progress(progress, i + 1);
37 void prune_packed_objects(int opts)
40 static char pathname[PATH_MAX];
41 const char *dir = get_object_directory();
42 int len = strlen(dir);
44 if (opts & PRUNE_PACKED_VERBOSE)
45 progress = start_progress_delay("Removing duplicate objects",
48 if (len > PATH_MAX - 42)
49 die("impossible object directory");
50 memcpy(pathname, dir, len);
51 if (len && pathname[len-1] != '/')
52 pathname[len++] = '/';
53 for (i = 0; i < 256; i++) {
56 display_progress(progress, i + 1);
57 sprintf(pathname + len, "%02x/", i);
58 d = opendir(pathname);
61 prune_dir(i, d, pathname, len + 3, opts);
63 pathname[len + 2] = '\0';
66 stop_progress(&progress);
69 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
71 int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
72 const struct option prune_packed_options[] = {
73 OPT_BIT('n', "dry-run", &opts, N_("dry run"),
74 PRUNE_PACKED_DRY_RUN),
75 OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"),
76 PRUNE_PACKED_VERBOSE),
80 argc = parse_options(argc, argv, prefix, prune_packed_options,
81 prune_packed_usage, 0);
83 prune_packed_objects(opts);