Makefile: be nice when running in a path containing spaces
[git/mingw/4msysgit/wingit-dll.git] / builtin-prune-packed.c
blob6473d327e689d438e285a50c435244ffa5a0ee3d
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 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
12 struct dirent *de;
13 char hex[40];
15 sprintf(hex, "%02x", i);
16 while ((de = readdir(dir)) != NULL) {
17 unsigned char sha1[20];
18 if (strlen(de->d_name) != 38)
19 continue;
20 memcpy(hex+2, de->d_name, 38);
21 if (get_sha1_hex(hex, sha1))
22 continue;
23 if (!has_sha1_pack(sha1, NULL))
24 continue;
25 memcpy(pathname + len, de->d_name, 38);
26 if (opts & DRY_RUN)
27 printf("rm -f %s\n", pathname);
28 else {
29 if (unlink(pathname) < 0)
30 error("unable to unlink %s", pathname);
33 pathname[len] = 0;
34 rmdir(pathname);
37 void prune_packed_objects(int opts)
39 int i;
40 static char pathname[PATH_MAX];
41 const char *dir = get_object_directory();
42 int len = strlen(dir);
44 if (len > PATH_MAX - 42)
45 die("impossible object directory");
46 memcpy(pathname, dir, len);
47 if (len && pathname[len-1] != '/')
48 pathname[len++] = '/';
49 for (i = 0; i < 256; i++) {
50 DIR *d;
52 sprintf(pathname + len, "%02x/", i);
53 d = opendir(pathname);
54 if (opts == VERBOSE && (d || i == 255))
55 fprintf(stderr, "Removing unused objects %d%%...\015",
56 ((i+1) * 100) / 256);
57 if (!d)
58 continue;
59 prune_dir(i, d, pathname, len + 3, opts);
60 closedir(d);
62 if (opts == VERBOSE)
63 fprintf(stderr, "\nDone.\n");
66 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
68 int i;
69 int opts = VERBOSE;
71 for (i = 1; i < argc; i++) {
72 const char *arg = argv[i];
74 if (*arg == '-') {
75 if (!strcmp(arg, "-n"))
76 opts |= DRY_RUN;
77 else if (!strcmp(arg, "-q"))
78 opts &= ~VERBOSE;
79 else
80 usage(prune_packed_usage);
81 continue;
83 /* Handle arguments here .. */
84 usage(prune_packed_usage);
86 sync();
87 prune_packed_objects(opts);
88 return 0;