]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-prune-packed.c
Teach prune-packed to use the standard progress meter
[thirdparty/git.git] / builtin-prune-packed.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "progress.h"
4
5 static const char prune_packed_usage[] =
6 "git-prune-packed [-n] [-q]";
7
8 #define DRY_RUN 01
9 #define VERBOSE 02
10
11 static struct progress progress;
12
13 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
14 {
15 struct dirent *de;
16 char hex[40];
17
18 sprintf(hex, "%02x", i);
19 while ((de = readdir(dir)) != NULL) {
20 unsigned char sha1[20];
21 if (strlen(de->d_name) != 38)
22 continue;
23 memcpy(hex+2, de->d_name, 38);
24 if (get_sha1_hex(hex, sha1))
25 continue;
26 if (!has_sha1_pack(sha1, NULL))
27 continue;
28 memcpy(pathname + len, de->d_name, 38);
29 if (opts == VERBOSE)
30 display_progress(&progress, i + 1);
31 if (opts & DRY_RUN)
32 printf("rm -f %s\n", pathname);
33 else if (unlink(pathname) < 0)
34 error("unable to unlink %s", pathname);
35 }
36 pathname[len] = 0;
37 rmdir(pathname);
38 }
39
40 void prune_packed_objects(int opts)
41 {
42 int i;
43 static char pathname[PATH_MAX];
44 const char *dir = get_object_directory();
45 int len = strlen(dir);
46
47 if (opts == VERBOSE)
48 start_progress_delay(&progress,
49 "Removing duplicate objects",
50 256, 95, 2);
51
52 if (len > PATH_MAX - 42)
53 die("impossible object directory");
54 memcpy(pathname, dir, len);
55 if (len && pathname[len-1] != '/')
56 pathname[len++] = '/';
57 for (i = 0; i < 256; i++) {
58 DIR *d;
59
60 sprintf(pathname + len, "%02x/", i);
61 d = opendir(pathname);
62 if (!d)
63 continue;
64 prune_dir(i, d, pathname, len + 3, opts);
65 closedir(d);
66 }
67 if (opts == VERBOSE)
68 stop_progress(&progress);
69 }
70
71 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
72 {
73 int i;
74 int opts = VERBOSE;
75
76 for (i = 1; i < argc; i++) {
77 const char *arg = argv[i];
78
79 if (*arg == '-') {
80 if (!strcmp(arg, "-n"))
81 opts |= DRY_RUN;
82 else if (!strcmp(arg, "-q"))
83 opts &= ~VERBOSE;
84 else
85 usage(prune_packed_usage);
86 continue;
87 }
88 /* Handle arguments here .. */
89 usage(prune_packed_usage);
90 }
91 sync();
92 prune_packed_objects(opts);
93 return 0;
94 }