blob: d24b097114ef2610416b106ea9aa2000873924f3 [file] [log] [blame]
Linus Torvalds2396ec82005-07-03 14:27:34 -07001#include "cache.h"
2
Junio C Hamano51890a62005-08-19 21:38:36 -07003static const char prune_packed_usage[] =
4"git-prune-packed [-n]";
5
6static int dryrun;
Linus Torvalds2396ec82005-07-03 14:27:34 -07007
8static void prune_dir(int i, DIR *dir, char *pathname, int len)
9{
10 struct dirent *de;
11 char hex[40];
12
13 sprintf(hex, "%02x", i);
14 while ((de = readdir(dir)) != NULL) {
15 unsigned char sha1[20];
16 if (strlen(de->d_name) != 38)
17 continue;
18 memcpy(hex+2, de->d_name, 38);
19 if (get_sha1_hex(hex, sha1))
20 continue;
21 if (!has_sha1_pack(sha1))
22 continue;
23 memcpy(pathname + len, de->d_name, 38);
Junio C Hamano51890a62005-08-19 21:38:36 -070024 if (dryrun)
25 printf("rm -f %s\n", pathname);
26 else if (unlink(pathname) < 0)
Linus Torvalds2396ec82005-07-03 14:27:34 -070027 error("unable to unlink %s", pathname);
28 }
Linus Torvalds230f1322005-10-08 15:54:01 -070029 pathname[len] = 0;
Linus Torvalds9106c092005-10-09 02:30:17 -070030 rmdir(pathname);
Linus Torvalds2396ec82005-07-03 14:27:34 -070031}
32
33static void prune_packed_objects(void)
34{
35 int i;
36 static char pathname[PATH_MAX];
37 const char *dir = get_object_directory();
38 int len = strlen(dir);
39
40 if (len > PATH_MAX - 42)
41 die("impossible object directory");
42 memcpy(pathname, dir, len);
43 if (len && pathname[len-1] != '/')
44 pathname[len++] = '/';
45 for (i = 0; i < 256; i++) {
46 DIR *d;
47
48 sprintf(pathname + len, "%02x/", i);
49 d = opendir(pathname);
50 if (!d)
Linus Torvalds230f1322005-10-08 15:54:01 -070051 continue;
Linus Torvalds2396ec82005-07-03 14:27:34 -070052 prune_dir(i, d, pathname, len + 3);
53 closedir(d);
54 }
55}
56
57int main(int argc, char **argv)
58{
59 int i;
60
Junio C Hamano53228a52005-11-26 00:50:02 -080061 setup_git_directory();
62
Linus Torvalds2396ec82005-07-03 14:27:34 -070063 for (i = 1; i < argc; i++) {
64 const char *arg = argv[i];
65
66 if (*arg == '-') {
Junio C Hamano51890a62005-08-19 21:38:36 -070067 if (!strcmp(arg, "-n"))
68 dryrun = 1;
69 else
70 usage(prune_packed_usage);
71 continue;
Linus Torvalds2396ec82005-07-03 14:27:34 -070072 }
73 /* Handle arguments here .. */
74 usage(prune_packed_usage);
75 }
Linus Torvalds41f222e2005-10-28 09:45:53 -070076 sync();
Linus Torvalds2396ec82005-07-03 14:27:34 -070077 prune_packed_objects();
78 return 0;
79}