repack: simplify handling of --write-bitmap-index
[git/raj.git] / builtin / prune-packed.c
blobfa6ce42f44f82d34299f2dc0109be4d831cb3d48
1 #include "builtin.h"
2 #include "cache.h"
3 #include "progress.h"
4 #include "parse-options.h"
6 static const char * const prune_packed_usage[] = {
7 N_("git prune-packed [-n|--dry-run] [-q|--quiet]"),
8 NULL
9 };
11 static struct progress *progress;
13 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
15 struct dirent *de;
16 char hex[40];
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))
27 continue;
28 memcpy(pathname + len, de->d_name, 38);
29 if (opts & PRUNE_PACKED_DRY_RUN)
30 printf("rm -f %s\n", pathname);
31 else
32 unlink_or_warn(pathname);
33 display_progress(progress, i + 1);
37 void prune_packed_objects(int opts)
39 int i;
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",
46 256, 95, 2);
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++) {
54 DIR *d;
56 display_progress(progress, i + 1);
57 sprintf(pathname + len, "%02x/", i);
58 d = opendir(pathname);
59 if (!d)
60 continue;
61 prune_dir(i, d, pathname, len + 3, opts);
62 closedir(d);
63 pathname[len + 2] = '\0';
64 rmdir(pathname);
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),
77 OPT_END()
80 argc = parse_options(argc, argv, prefix, prune_packed_options,
81 prune_packed_usage, 0);
83 prune_packed_objects(opts);
84 return 0;