Merge with gitk
[git] / prune-packed.c
1 #include "cache.h"
2
3 static const char prune_packed_usage[] = "git-prune-packed (no arguments)";
4
5 static void prune_dir(int i, DIR *dir, char *pathname, int len)
6 {
7         struct dirent *de;
8         char hex[40];
9
10         sprintf(hex, "%02x", i);
11         while ((de = readdir(dir)) != NULL) {
12                 unsigned char sha1[20];
13                 if (strlen(de->d_name) != 38)
14                         continue;
15                 memcpy(hex+2, de->d_name, 38);
16                 if (get_sha1_hex(hex, sha1))
17                         continue;
18                 if (!has_sha1_pack(sha1))
19                         continue;
20                 memcpy(pathname + len, de->d_name, 38);
21                 if (unlink(pathname) < 0)
22                         error("unable to unlink %s", pathname);
23         }
24 }
25
26 static void prune_packed_objects(void)
27 {
28         int i;
29         static char pathname[PATH_MAX];
30         const char *dir = get_object_directory();
31         int len = strlen(dir);
32
33         if (len > PATH_MAX - 42)
34                 die("impossible object directory");
35         memcpy(pathname, dir, len);
36         if (len && pathname[len-1] != '/')
37                 pathname[len++] = '/';
38         for (i = 0; i < 256; i++) {
39                 DIR *d;
40
41                 sprintf(pathname + len, "%02x/", i);
42                 d = opendir(pathname);
43                 if (!d)
44                         die("unable to open %s", pathname);
45                 prune_dir(i, d, pathname, len + 3);
46                 closedir(d);
47         }
48 }
49
50 int main(int argc, char **argv)
51 {
52         int i;
53
54         for (i = 1; i < argc; i++) {
55                 const char *arg = argv[i];
56
57                 if (*arg == '-') {
58                         /* Handle flags here .. */
59                         usage(prune_packed_usage);
60                 }
61                 /* Handle arguments here .. */
62                 usage(prune_packed_usage);
63         }
64         prune_packed_objects();
65         return 0;
66 }