blob: 23faf3129fb65ec5592c5021d661498143d4f608 [file] [log] [blame]
Matthias Kestenholz25f38f02006-08-02 23:52:00 +02001#include "builtin.h"
Linus Torvalds2396ec82005-07-03 14:27:34 -07002#include "cache.h"
Shawn O. Pearceb5d72f02007-10-19 00:08:37 -04003#include "progress.h"
Linus Torvalds2396ec82005-07-03 14:27:34 -07004
Junio C Hamano51890a62005-08-19 21:38:36 -07005static const char prune_packed_usage[] =
Matthias Lederhoferbd67f092007-01-18 17:11:13 +01006"git-prune-packed [-n] [-q]";
Junio C Hamano51890a62005-08-19 21:38:36 -07007
Junio C Hamanob60daf02007-01-12 15:00:13 -08008#define DRY_RUN 01
9#define VERBOSE 02
10
Nicolas Pitredc6a0752007-10-30 14:57:32 -040011static struct progress *progress;
Shawn O. Pearceb5d72f02007-10-19 00:08:37 -040012
Junio C Hamanob60daf02007-01-12 15:00:13 -080013static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
Linus Torvalds2396ec82005-07-03 14:27:34 -070014{
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;
Junio C Hamano106d7102006-09-06 02:12:09 -070026 if (!has_sha1_pack(sha1, NULL))
Linus Torvalds2396ec82005-07-03 14:27:34 -070027 continue;
28 memcpy(pathname + len, de->d_name, 38);
Junio C Hamanob60daf02007-01-12 15:00:13 -080029 if (opts & DRY_RUN)
Junio C Hamano51890a62005-08-19 21:38:36 -070030 printf("rm -f %s\n", pathname);
31 else if (unlink(pathname) < 0)
Linus Torvalds2396ec82005-07-03 14:27:34 -070032 error("unable to unlink %s", pathname);
Nicolas Pitre93ff3f62007-11-01 16:59:55 -040033 display_progress(progress, i + 1);
Linus Torvalds2396ec82005-07-03 14:27:34 -070034 }
Linus Torvalds230f1322005-10-08 15:54:01 -070035 pathname[len] = 0;
Linus Torvalds9106c092005-10-09 02:30:17 -070036 rmdir(pathname);
Linus Torvalds2396ec82005-07-03 14:27:34 -070037}
38
Junio C Hamanob60daf02007-01-12 15:00:13 -080039void prune_packed_objects(int opts)
Linus Torvalds2396ec82005-07-03 14:27:34 -070040{
41 int i;
42 static char pathname[PATH_MAX];
43 const char *dir = get_object_directory();
44 int len = strlen(dir);
45
Shawn O. Pearceb5d72f02007-10-19 00:08:37 -040046 if (opts == VERBOSE)
Nicolas Pitredc6a0752007-10-30 14:57:32 -040047 progress = start_progress_delay("Removing duplicate objects",
Shawn O. Pearceb5d72f02007-10-19 00:08:37 -040048 256, 95, 2);
49
Linus Torvalds2396ec82005-07-03 14:27:34 -070050 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
58 sprintf(pathname + len, "%02x/", i);
59 d = opendir(pathname);
60 if (!d)
Linus Torvalds230f1322005-10-08 15:54:01 -070061 continue;
Junio C Hamanob60daf02007-01-12 15:00:13 -080062 prune_dir(i, d, pathname, len + 3, opts);
Linus Torvalds2396ec82005-07-03 14:27:34 -070063 closedir(d);
64 }
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -040065 stop_progress(&progress);
Linus Torvalds2396ec82005-07-03 14:27:34 -070066}
67
Matthias Kestenholz25f38f02006-08-02 23:52:00 +020068int cmd_prune_packed(int argc, const char **argv, const char *prefix)
Linus Torvalds2396ec82005-07-03 14:27:34 -070069{
70 int i;
Junio C Hamanob60daf02007-01-12 15:00:13 -080071 int opts = VERBOSE;
Linus Torvalds2396ec82005-07-03 14:27:34 -070072
73 for (i = 1; i < argc; i++) {
74 const char *arg = argv[i];
75
76 if (*arg == '-') {
Junio C Hamano51890a62005-08-19 21:38:36 -070077 if (!strcmp(arg, "-n"))
Junio C Hamanob60daf02007-01-12 15:00:13 -080078 opts |= DRY_RUN;
79 else if (!strcmp(arg, "-q"))
80 opts &= ~VERBOSE;
Junio C Hamano51890a62005-08-19 21:38:36 -070081 else
82 usage(prune_packed_usage);
83 continue;
Linus Torvalds2396ec82005-07-03 14:27:34 -070084 }
85 /* Handle arguments here .. */
86 usage(prune_packed_usage);
87 }
Linus Torvalds41f222e2005-10-28 09:45:53 -070088 sync();
Junio C Hamanob60daf02007-01-12 15:00:13 -080089 prune_packed_objects(opts);
Linus Torvalds2396ec82005-07-03 14:27:34 -070090 return 0;
91}