Matthias Kestenholz | 25f38f0 | 2006-08-02 23:52:00 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 2 | #include "cache.h" |
Shawn O. Pearce | b5d72f0 | 2007-10-19 00:08:37 -0400 | [diff] [blame] | 3 | #include "progress.h" |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 4 | |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 5 | static const char prune_packed_usage[] = |
Matthias Lederhofer | bd67f09 | 2007-01-18 17:11:13 +0100 | [diff] [blame] | 6 | "git-prune-packed [-n] [-q]"; |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 7 | |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 8 | #define DRY_RUN 01 |
| 9 | #define VERBOSE 02 |
| 10 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 11 | static struct progress *progress; |
Shawn O. Pearce | b5d72f0 | 2007-10-19 00:08:37 -0400 | [diff] [blame] | 12 | |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 13 | static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 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; |
Junio C Hamano | 106d710 | 2006-09-06 02:12:09 -0700 | [diff] [blame] | 26 | if (!has_sha1_pack(sha1, NULL)) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 27 | continue; |
| 28 | memcpy(pathname + len, de->d_name, 38); |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 29 | if (opts & DRY_RUN) |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 30 | printf("rm -f %s\n", pathname); |
| 31 | else if (unlink(pathname) < 0) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 32 | error("unable to unlink %s", pathname); |
Nicolas Pitre | 93ff3f6 | 2007-11-01 16:59:55 -0400 | [diff] [blame] | 33 | display_progress(progress, i + 1); |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 34 | } |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 35 | pathname[len] = 0; |
Linus Torvalds | 9106c09 | 2005-10-09 02:30:17 -0700 | [diff] [blame] | 36 | rmdir(pathname); |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 39 | void prune_packed_objects(int opts) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 40 | { |
| 41 | int i; |
| 42 | static char pathname[PATH_MAX]; |
| 43 | const char *dir = get_object_directory(); |
| 44 | int len = strlen(dir); |
| 45 | |
Shawn O. Pearce | b5d72f0 | 2007-10-19 00:08:37 -0400 | [diff] [blame] | 46 | if (opts == VERBOSE) |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 47 | progress = start_progress_delay("Removing duplicate objects", |
Shawn O. Pearce | b5d72f0 | 2007-10-19 00:08:37 -0400 | [diff] [blame] | 48 | 256, 95, 2); |
| 49 | |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 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 | |
| 58 | sprintf(pathname + len, "%02x/", i); |
| 59 | d = opendir(pathname); |
| 60 | if (!d) |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 61 | continue; |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 62 | prune_dir(i, d, pathname, len + 3, opts); |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 63 | closedir(d); |
| 64 | } |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 65 | stop_progress(&progress); |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Matthias Kestenholz | 25f38f0 | 2006-08-02 23:52:00 +0200 | [diff] [blame] | 68 | int cmd_prune_packed(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 69 | { |
| 70 | int i; |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 71 | int opts = VERBOSE; |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 72 | |
| 73 | for (i = 1; i < argc; i++) { |
| 74 | const char *arg = argv[i]; |
| 75 | |
| 76 | if (*arg == '-') { |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 77 | if (!strcmp(arg, "-n")) |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 78 | opts |= DRY_RUN; |
| 79 | else if (!strcmp(arg, "-q")) |
| 80 | opts &= ~VERBOSE; |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 81 | else |
| 82 | usage(prune_packed_usage); |
| 83 | continue; |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 84 | } |
| 85 | /* Handle arguments here .. */ |
| 86 | usage(prune_packed_usage); |
| 87 | } |
Linus Torvalds | 41f222e | 2005-10-28 09:45:53 -0700 | [diff] [blame] | 88 | sync(); |
Junio C Hamano | b60daf0 | 2007-01-12 15:00:13 -0800 | [diff] [blame] | 89 | prune_packed_objects(opts); |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |