Commit | Line | Data |
---|---|---|
25f38f06 | 1 | #include "builtin.h" |
2396ec85 | 2 | #include "cache.h" |
b5d72f0a | 3 | #include "progress.h" |
7cfe0c98 | 4 | #include "parse-options.h" |
2396ec85 | 5 | |
7cfe0c98 | 6 | static const char * const prune_packed_usage[] = { |
5696d588 | 7 | N_("git prune-packed [-n|--dry-run] [-q|--quiet]"), |
7cfe0c98 SB |
8 | NULL |
9 | }; | |
51890a64 | 10 | |
dc6a0757 | 11 | static struct progress *progress; |
b5d72f0a | 12 | |
b60daf05 | 13 | static 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 | ||
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; | |
cd673c1f | 26 | if (!has_sha1_pack(sha1)) |
2396ec85 LT |
27 | continue; |
28 | memcpy(pathname + len, de->d_name, 38); | |
af0b4a3b | 29 | if (opts & PRUNE_PACKED_DRY_RUN) |
51890a64 | 30 | printf("rm -f %s\n", pathname); |
691f1a28 AR |
31 | else |
32 | unlink_or_warn(pathname); | |
93ff3f6a | 33 | display_progress(progress, i + 1); |
2396ec85 LT |
34 | } |
35 | } | |
36 | ||
b60daf05 | 37 | void prune_packed_objects(int opts) |
2396ec85 LT |
38 | { |
39 | int i; | |
40 | static char pathname[PATH_MAX]; | |
41 | const char *dir = get_object_directory(); | |
42 | int len = strlen(dir); | |
43 | ||
af0b4a3b | 44 | if (opts & PRUNE_PACKED_VERBOSE) |
dc6a0757 | 45 | progress = start_progress_delay("Removing duplicate objects", |
b5d72f0a SP |
46 | 256, 95, 2); |
47 | ||
2396ec85 LT |
48 | if (len > PATH_MAX - 42) |
49 | die("impossible object directory"); | |
50 | memcpy(pathname, dir, len); | |
51 | if (len && pathname[len-1] != '/') | |
52 | pathname[len++] = '/'; | |
53 | for (i = 0; i < 256; i++) { | |
54 | DIR *d; | |
55 | ||
531e6daa | 56 | display_progress(progress, i + 1); |
2396ec85 LT |
57 | sprintf(pathname + len, "%02x/", i); |
58 | d = opendir(pathname); | |
59 | if (!d) | |
230f1322 | 60 | continue; |
b60daf05 | 61 | prune_dir(i, d, pathname, len + 3, opts); |
2396ec85 | 62 | closedir(d); |
d34e70d6 KB |
63 | pathname[len + 2] = '\0'; |
64 | rmdir(pathname); | |
2396ec85 | 65 | } |
4d4fcc54 | 66 | stop_progress(&progress); |
2396ec85 LT |
67 | } |
68 | ||
25f38f06 | 69 | int cmd_prune_packed(int argc, const char **argv, const char *prefix) |
2396ec85 | 70 | { |
af0b4a3b | 71 | int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0; |
7cfe0c98 | 72 | const struct option prune_packed_options[] = { |
af0b4a3b NTND |
73 | OPT_BIT('n', "dry-run", &opts, N_("dry run"), |
74 | PRUNE_PACKED_DRY_RUN), | |
75 | OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"), | |
76 | PRUNE_PACKED_VERBOSE), | |
7cfe0c98 SB |
77 | OPT_END() |
78 | }; | |
2396ec85 | 79 | |
7cfe0c98 SB |
80 | argc = parse_options(argc, argv, prefix, prune_packed_options, |
81 | prune_packed_usage, 0); | |
2396ec85 | 82 | |
b60daf05 | 83 | prune_packed_objects(opts); |
2396ec85 LT |
84 | return 0; |
85 | } |