]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-prune-packed.c
t4202-log.sh: Test git log --no-walk sort order
[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[] =
1b1dd23f 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
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);
b60daf05 29 if (opts & 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 34 }
230f1322 35 pathname[len] = 0;
9106c097 36 rmdir(pathname);
2396ec85
LT
37}
38
b60daf05 39void prune_packed_objects(int opts)
2396ec85
LT
40{
41 int i;
42 static char pathname[PATH_MAX];
43 const char *dir = get_object_directory();
44 int len = strlen(dir);
45
b5d72f0a 46 if (opts == VERBOSE)
dc6a0757 47 progress = start_progress_delay("Removing duplicate objects",
b5d72f0a
SP
48 256, 95, 2);
49
2396ec85
LT
50 if (len > PATH_MAX - 42)
51 die("impossible object directory");
52 memcpy(pathname, dir, len);
53 if (len && pathname[len-1] != '/')
54 pathname[len++] = '/';
55 for (i = 0; i < 256; i++) {
56 DIR *d;
57
531e6daa 58 display_progress(progress, i + 1);
2396ec85
LT
59 sprintf(pathname + len, "%02x/", i);
60 d = opendir(pathname);
61 if (!d)
230f1322 62 continue;
b60daf05 63 prune_dir(i, d, pathname, len + 3, opts);
2396ec85
LT
64 closedir(d);
65 }
4d4fcc54 66 stop_progress(&progress);
2396ec85
LT
67}
68
25f38f06 69int cmd_prune_packed(int argc, const char **argv, const char *prefix)
2396ec85
LT
70{
71 int i;
b60daf05 72 int opts = VERBOSE;
2396ec85
LT
73
74 for (i = 1; i < argc; i++) {
75 const char *arg = argv[i];
76
77 if (*arg == '-') {
51890a64 78 if (!strcmp(arg, "-n"))
b60daf05
JH
79 opts |= DRY_RUN;
80 else if (!strcmp(arg, "-q"))
81 opts &= ~VERBOSE;
51890a64
JH
82 else
83 usage(prune_packed_usage);
84 continue;
2396ec85
LT
85 }
86 /* Handle arguments here .. */
87 usage(prune_packed_usage);
88 }
b60daf05 89 prune_packed_objects(opts);
2396ec85
LT
90 return 0;
91}