]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-prune-packed.c
make struct progress an opaque type
[thirdparty/git.git] / builtin-prune-packed.c
CommitLineData
25f38f06 1#include "builtin.h"
2396ec85 2#include "cache.h"
b5d72f0a 3#include "progress.h"
2396ec85 4
51890a64 5static const char prune_packed_usage[] =
bd67f09f 6"git-prune-packed [-n] [-q]";
51890a64 7
b60daf05
JH
8#define DRY_RUN 01
9#define VERBOSE 02
10
dc6a0757 11static struct progress *progress;
b5d72f0a 12
b60daf05 13static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
2396ec85
LT
14{
15 struct dirent *de;
16 char hex[40];
17
0e549137 18 if (opts == VERBOSE)
dc6a0757 19 display_progress(progress, i + 1);
0e549137 20
2396ec85
LT
21 sprintf(hex, "%02x", i);
22 while ((de = readdir(dir)) != NULL) {
23 unsigned char sha1[20];
24 if (strlen(de->d_name) != 38)
25 continue;
26 memcpy(hex+2, de->d_name, 38);
27 if (get_sha1_hex(hex, sha1))
28 continue;
106d710b 29 if (!has_sha1_pack(sha1, NULL))
2396ec85
LT
30 continue;
31 memcpy(pathname + len, de->d_name, 38);
b60daf05 32 if (opts & DRY_RUN)
51890a64
JH
33 printf("rm -f %s\n", pathname);
34 else if (unlink(pathname) < 0)
2396ec85
LT
35 error("unable to unlink %s", pathname);
36 }
230f1322 37 pathname[len] = 0;
9106c097 38 rmdir(pathname);
2396ec85
LT
39}
40
b60daf05 41void prune_packed_objects(int opts)
2396ec85
LT
42{
43 int i;
44 static char pathname[PATH_MAX];
45 const char *dir = get_object_directory();
46 int len = strlen(dir);
47
b5d72f0a 48 if (opts == VERBOSE)
dc6a0757 49 progress = start_progress_delay("Removing duplicate objects",
b5d72f0a
SP
50 256, 95, 2);
51
2396ec85
LT
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)
230f1322 63 continue;
b60daf05 64 prune_dir(i, d, pathname, len + 3, opts);
2396ec85
LT
65 closedir(d);
66 }
b60daf05 67 if (opts == VERBOSE)
b5d72f0a 68 stop_progress(&progress);
2396ec85
LT
69}
70
25f38f06 71int cmd_prune_packed(int argc, const char **argv, const char *prefix)
2396ec85
LT
72{
73 int i;
b60daf05 74 int opts = VERBOSE;
2396ec85
LT
75
76 for (i = 1; i < argc; i++) {
77 const char *arg = argv[i];
78
79 if (*arg == '-') {
51890a64 80 if (!strcmp(arg, "-n"))
b60daf05
JH
81 opts |= DRY_RUN;
82 else if (!strcmp(arg, "-q"))
83 opts &= ~VERBOSE;
51890a64
JH
84 else
85 usage(prune_packed_usage);
86 continue;
2396ec85
LT
87 }
88 /* Handle arguments here .. */
89 usage(prune_packed_usage);
90 }
41f222e8 91 sync();
b60daf05 92 prune_packed_objects(opts);
2396ec85
LT
93 return 0;
94}