relax usage of the progress API
[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         display_progress(progress, i + 1);
19
20         sprintf(hex, "%02x", i);
21         while ((de = readdir(dir)) != NULL) {
22                 unsigned char sha1[20];
23                 if (strlen(de->d_name) != 38)
24                         continue;
25                 memcpy(hex+2, de->d_name, 38);
26                 if (get_sha1_hex(hex, sha1))
27                         continue;
28                 if (!has_sha1_pack(sha1, NULL))
29                         continue;
30                 memcpy(pathname + len, de->d_name, 38);
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                 progress = start_progress_delay("Removing duplicate objects",
49                         256, 95, 2);
50
51         if (len > PATH_MAX - 42)
52                 die("impossible object directory");
53         memcpy(pathname, dir, len);
54         if (len && pathname[len-1] != '/')
55                 pathname[len++] = '/';
56         for (i = 0; i < 256; i++) {
57                 DIR *d;
58
59                 sprintf(pathname + len, "%02x/", i);
60                 d = opendir(pathname);
61                 if (!d)
62                         continue;
63                 prune_dir(i, d, pathname, len + 3, opts);
64                 closedir(d);
65         }
66         stop_progress(&progress);
67 }
68
69 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
70 {
71         int i;
72         int opts = VERBOSE;
73
74         for (i = 1; i < argc; i++) {
75                 const char *arg = argv[i];
76
77                 if (*arg == '-') {
78                         if (!strcmp(arg, "-n"))
79                                 opts |= DRY_RUN;
80                         else if (!strcmp(arg, "-q"))
81                                 opts &= ~VERBOSE;
82                         else
83                                 usage(prune_packed_usage);
84                         continue;
85                 }
86                 /* Handle arguments here .. */
87                 usage(prune_packed_usage);
88         }
89         sync();
90         prune_packed_objects(opts);
91         return 0;
92 }