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