]> git.ipfire.org Git - thirdparty/git.git/blame - prune-packed.c
Use symbolic name SHORT_NAME_AMBIGUOUS as error return value
[thirdparty/git.git] / prune-packed.c
CommitLineData
2396ec85
LT
1#include "cache.h"
2
51890a64
JH
3static const char prune_packed_usage[] =
4"git-prune-packed [-n]";
5
6static int dryrun;
2396ec85
LT
7
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);
51890a64
JH
24 if (dryrun)
25 printf("rm -f %s\n", pathname);
26 else if (unlink(pathname) < 0)
2396ec85
LT
27 error("unable to unlink %s", pathname);
28 }
230f1322 29 pathname[len] = 0;
9106c097 30 rmdir(pathname);
2396ec85
LT
31}
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)
230f1322 51 continue;
2396ec85
LT
52 prune_dir(i, d, pathname, len + 3);
53 closedir(d);
54 }
55}
56
57int main(int argc, char **argv)
58{
59 int i;
60
53228a5f
JH
61 setup_git_directory();
62
2396ec85
LT
63 for (i = 1; i < argc; i++) {
64 const char *arg = argv[i];
65
66 if (*arg == '-') {
51890a64
JH
67 if (!strcmp(arg, "-n"))
68 dryrun = 1;
69 else
70 usage(prune_packed_usage);
71 continue;
2396ec85
LT
72 }
73 /* Handle arguments here .. */
74 usage(prune_packed_usage);
75 }
41f222e8 76 sync();
2396ec85
LT
77 prune_packed_objects();
78 return 0;
79}