pack-objects --unpacked=<existing pack> option.
[git/git.git] / builtin-prune-packed.c
CommitLineData
25f38f06 1#include "builtin.h"
2396ec85
LT
2#include "cache.h"
3
51890a64
JH
4static const char prune_packed_usage[] =
5"git-prune-packed [-n]";
6
7static int dryrun;
2396ec85
LT
8
9static void prune_dir(int i, DIR *dir, char *pathname, int len)
10{
11 struct dirent *de;
12 char hex[40];
13
14 sprintf(hex, "%02x", i);
15 while ((de = readdir(dir)) != NULL) {
16 unsigned char sha1[20];
17 if (strlen(de->d_name) != 38)
18 continue;
19 memcpy(hex+2, de->d_name, 38);
20 if (get_sha1_hex(hex, sha1))
21 continue;
106d710b 22 if (!has_sha1_pack(sha1, NULL))
2396ec85
LT
23 continue;
24 memcpy(pathname + len, de->d_name, 38);
51890a64
JH
25 if (dryrun)
26 printf("rm -f %s\n", pathname);
27 else if (unlink(pathname) < 0)
2396ec85
LT
28 error("unable to unlink %s", pathname);
29 }
230f1322 30 pathname[len] = 0;
9106c097 31 rmdir(pathname);
2396ec85
LT
32}
33
34static void prune_packed_objects(void)
35{
36 int i;
37 static char pathname[PATH_MAX];
38 const char *dir = get_object_directory();
39 int len = strlen(dir);
40
41 if (len > PATH_MAX - 42)
42 die("impossible object directory");
43 memcpy(pathname, dir, len);
44 if (len && pathname[len-1] != '/')
45 pathname[len++] = '/';
46 for (i = 0; i < 256; i++) {
47 DIR *d;
48
49 sprintf(pathname + len, "%02x/", i);
50 d = opendir(pathname);
51 if (!d)
230f1322 52 continue;
2396ec85
LT
53 prune_dir(i, d, pathname, len + 3);
54 closedir(d);
55 }
56}
57
25f38f06 58int cmd_prune_packed(int argc, const char **argv, const char *prefix)
2396ec85
LT
59{
60 int i;
61
62 for (i = 1; i < argc; i++) {
63 const char *arg = argv[i];
64
65 if (*arg == '-') {
51890a64
JH
66 if (!strcmp(arg, "-n"))
67 dryrun = 1;
68 else
69 usage(prune_packed_usage);
70 continue;
2396ec85
LT
71 }
72 /* Handle arguments here .. */
73 usage(prune_packed_usage);
74 }
41f222e8 75 sync();
2396ec85
LT
76 prune_packed_objects();
77 return 0;
78}