]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune-packed.c
Merge branch 'nd/am-i18n-fix'
[thirdparty/git.git] / builtin / prune-packed.c
CommitLineData
25f38f06 1#include "builtin.h"
2396ec85 2#include "cache.h"
b5d72f0a 3#include "progress.h"
7cfe0c98 4#include "parse-options.h"
2396ec85 5
7cfe0c98
SB
6static const char * const prune_packed_usage[] = {
7 "git prune-packed [-n|--dry-run] [-q|--quiet]",
8 NULL
9};
51890a64 10
b60daf05
JH
11#define DRY_RUN 01
12#define VERBOSE 02
13
dc6a0757 14static struct progress *progress;
b5d72f0a 15
b60daf05 16static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
2396ec85
LT
17{
18 struct dirent *de;
19 char hex[40];
20
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;
cd673c1f 29 if (!has_sha1_pack(sha1))
2396ec85
LT
30 continue;
31 memcpy(pathname + len, de->d_name, 38);
b60daf05 32 if (opts & DRY_RUN)
51890a64 33 printf("rm -f %s\n", pathname);
691f1a28
AR
34 else
35 unlink_or_warn(pathname);
93ff3f6a 36 display_progress(progress, i + 1);
2396ec85
LT
37 }
38}
39
b60daf05 40void prune_packed_objects(int opts)
2396ec85
LT
41{
42 int i;
43 static char pathname[PATH_MAX];
44 const char *dir = get_object_directory();
45 int len = strlen(dir);
46
b5d72f0a 47 if (opts == VERBOSE)
dc6a0757 48 progress = start_progress_delay("Removing duplicate objects",
b5d72f0a
SP
49 256, 95, 2);
50
2396ec85
LT
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
531e6daa 59 display_progress(progress, i + 1);
2396ec85
LT
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 65 closedir(d);
d34e70d6
KB
66 pathname[len + 2] = '\0';
67 rmdir(pathname);
2396ec85 68 }
4d4fcc54 69 stop_progress(&progress);
2396ec85
LT
70}
71
25f38f06 72int cmd_prune_packed(int argc, const char **argv, const char *prefix)
2396ec85 73{
1ddf5efc 74 int opts = isatty(2) ? VERBOSE : 0;
7cfe0c98
SB
75 const struct option prune_packed_options[] = {
76 OPT_BIT('n', "dry-run", &opts, "dry run", DRY_RUN),
77 OPT_NEGBIT('q', "quiet", &opts, "be quiet", VERBOSE),
78 OPT_END()
79 };
2396ec85 80
7cfe0c98
SB
81 argc = parse_options(argc, argv, prefix, prune_packed_options,
82 prune_packed_usage, 0);
2396ec85 83
b60daf05 84 prune_packed_objects(opts);
2396ec85
LT
85 return 0;
86}