4 static const char prune_packed_usage[] =
5 "git-prune-packed [-n] [-q]";
10 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
15 sprintf(hex, "%02x", i);
16 while ((de = readdir(dir)) != NULL) {
17 unsigned char sha1[20];
18 if (strlen(de->d_name) != 38)
20 memcpy(hex+2, de->d_name, 38);
21 if (get_sha1_hex(hex, sha1))
23 if (!has_sha1_pack(sha1, NULL))
25 memcpy(pathname + len, de->d_name, 38);
27 printf("rm -f %s\n", pathname);
28 else if (unlink(pathname) < 0)
29 error("unable to unlink %s", pathname);
35 void prune_packed_objects(int opts)
38 static char pathname[PATH_MAX];
39 const char *dir = get_object_directory();
40 int len = strlen(dir);
42 if (len > PATH_MAX - 42)
43 die("impossible object directory");
44 memcpy(pathname, dir, len);
45 if (len && pathname[len-1] != '/')
46 pathname[len++] = '/';
47 for (i = 0; i < 256; i++) {
50 sprintf(pathname + len, "%02x/", i);
51 d = opendir(pathname);
52 if (opts == VERBOSE && (d || i == 255))
53 fprintf(stderr, "Removing unused objects %d%%...\015",
57 prune_dir(i, d, pathname, len + 3, opts);
61 fprintf(stderr, "\nDone.\n");
64 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
69 for (i = 1; i < argc; i++) {
70 const char *arg = argv[i];
73 if (!strcmp(arg, "-n"))
75 else if (!strcmp(arg, "-q"))
78 usage(prune_packed_usage);
81 /* Handle arguments here .. */
82 usage(prune_packed_usage);
85 prune_packed_objects(opts);