t0001: fix test on MinGW
[4msysgit-hv.git] / builtin-prune-packed.c
blobfa9ad7bfdf6d839a72ef7d051fdc34afe38f5672
1 #include "builtin.h"
2 #include "cache.h"
4 static const char prune_packed_usage[] =
5 "git-prune-packed [-n] [-q]";
7 #define DRY_RUN 01
8 #define VERBOSE 02
10 void sync(void);
12 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
14 struct dirent *de;
15 char hex[40];
17 sprintf(hex, "%02x", i);
18 while ((de = readdir(dir)) != NULL) {
19 unsigned char sha1[20];
20 if (strlen(de->d_name) != 38)
21 continue;
22 memcpy(hex+2, de->d_name, 38);
23 if (get_sha1_hex(hex, sha1))
24 continue;
25 if (!has_sha1_pack(sha1, NULL))
26 continue;
27 memcpy(pathname + len, de->d_name, 38);
28 if (opts & DRY_RUN)
29 printf("rm -f %s\n", pathname);
30 else {
31 if (unlink(pathname) < 0)
32 error("unable to unlink %s", pathname);
35 pathname[len] = 0;
36 rmdir(pathname);
39 void prune_packed_objects(int opts)
41 int i;
42 static char pathname[PATH_MAX];
43 const char *dir = get_object_directory();
44 int len = strlen(dir);
46 if (len > PATH_MAX - 42)
47 die("impossible object directory");
48 memcpy(pathname, dir, len);
49 if (len && pathname[len-1] != '/')
50 pathname[len++] = '/';
51 for (i = 0; i < 256; i++) {
52 DIR *d;
54 sprintf(pathname + len, "%02x/", i);
55 d = opendir(pathname);
56 if (opts == VERBOSE && (d || i == 255))
57 fprintf(stderr, "Removing unused objects %d%%...\015",
58 ((i+1) * 100) / 256);
59 if (!d)
60 continue;
61 prune_dir(i, d, pathname, len + 3, opts);
62 closedir(d);
64 if (opts == VERBOSE)
65 fprintf(stderr, "\nDone.\n");
68 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
70 int i;
71 int opts = VERBOSE;
73 for (i = 1; i < argc; i++) {
74 const char *arg = argv[i];
76 if (*arg == '-') {
77 if (!strcmp(arg, "-n"))
78 opts |= DRY_RUN;
79 else if (!strcmp(arg, "-q"))
80 opts &= ~VERBOSE;
81 else
82 usage(prune_packed_usage);
83 continue;
85 /* Handle arguments here .. */
86 usage(prune_packed_usage);
88 sync();
89 prune_packed_objects(opts);
90 return 0;