Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 2 | #include "commit.h" |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 3 | #include "diff.h" |
| 4 | #include "revision.h" |
| 5 | #include "builtin.h" |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 6 | #include "reachable.h" |
Michele Ballabio | 629de47 | 2008-03-23 21:50:29 +0100 | [diff] [blame] | 7 | #include "parse-options.h" |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 8 | |
Michele Ballabio | 629de47 | 2008-03-23 21:50:29 +0100 | [diff] [blame] | 9 | static const char * const prune_usage[] = { |
| 10 | "git-prune [-n] [--expire <time>] [--] [<head>...]", |
| 11 | NULL |
| 12 | }; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 13 | static int show_only; |
Johannes Schindelin | f01913e | 2007-11-29 20:59:55 +0000 | [diff] [blame] | 14 | static unsigned long expire; |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 15 | |
| 16 | static int prune_object(char *path, const char *filename, const unsigned char *sha1) |
| 17 | { |
Johannes Schindelin | f01913e | 2007-11-29 20:59:55 +0000 | [diff] [blame] | 18 | const char *fullpath = mkpath("%s/%s", path, filename); |
| 19 | if (expire) { |
| 20 | struct stat st; |
| 21 | if (lstat(fullpath, &st)) |
| 22 | return error("Could not stat '%s'", fullpath); |
| 23 | if (st.st_mtime > expire) |
| 24 | return 0; |
| 25 | } |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 26 | if (show_only) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 27 | enum object_type type = sha1_object_info(sha1, NULL); |
| 28 | printf("%s %s\n", sha1_to_hex(sha1), |
| 29 | (type > 0) ? typename(type) : "unknown"); |
Nicolas Pitre | 3254d21 | 2007-03-20 23:32:13 -0400 | [diff] [blame] | 30 | } else |
Johannes Schindelin | f01913e | 2007-11-29 20:59:55 +0000 | [diff] [blame] | 31 | unlink(fullpath); |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int prune_dir(int i, char *path) |
| 36 | { |
| 37 | DIR *dir = opendir(path); |
| 38 | struct dirent *de; |
| 39 | |
| 40 | if (!dir) |
| 41 | return 0; |
| 42 | |
| 43 | while ((de = readdir(dir)) != NULL) { |
| 44 | char name[100]; |
| 45 | unsigned char sha1[20]; |
| 46 | int len = strlen(de->d_name); |
| 47 | |
| 48 | switch (len) { |
| 49 | case 2: |
| 50 | if (de->d_name[1] != '.') |
| 51 | break; |
| 52 | case 1: |
| 53 | if (de->d_name[0] != '.') |
| 54 | break; |
| 55 | continue; |
| 56 | case 38: |
| 57 | sprintf(name, "%02x", i); |
| 58 | memcpy(name+2, de->d_name, len+1); |
| 59 | if (get_sha1_hex(name, sha1) < 0) |
| 60 | break; |
| 61 | |
| 62 | /* |
| 63 | * Do we know about this object? |
| 64 | * It must have been reachable |
| 65 | */ |
| 66 | if (lookup_object(sha1)) |
| 67 | continue; |
| 68 | |
| 69 | prune_object(path, de->d_name, sha1); |
| 70 | continue; |
| 71 | } |
| 72 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
| 73 | } |
Nicolas Pitre | 3254d21 | 2007-03-20 23:32:13 -0400 | [diff] [blame] | 74 | if (!show_only) |
| 75 | rmdir(path); |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 76 | closedir(dir); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void prune_object_dir(const char *path) |
| 81 | { |
| 82 | int i; |
| 83 | for (i = 0; i < 256; i++) { |
| 84 | static char dir[4096]; |
| 85 | sprintf(dir, "%s/%02x", path, i); |
| 86 | prune_dir(i, dir); |
| 87 | } |
| 88 | } |
| 89 | |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Write errors (particularly out of space) can result in |
| 92 | * failed temporary packs (and more rarely indexes and other |
| 93 | * files begining with "tmp_") accumulating in the |
| 94 | * object directory. |
| 95 | */ |
| 96 | static void remove_temporary_files(void) |
| 97 | { |
| 98 | DIR *dir; |
| 99 | struct dirent *de; |
| 100 | char* dirname=get_object_directory(); |
| 101 | |
| 102 | dir = opendir(dirname); |
| 103 | if (!dir) { |
| 104 | fprintf(stderr, "Unable to open object directory %s\n", |
| 105 | dirname); |
| 106 | return; |
| 107 | } |
| 108 | while ((de = readdir(dir)) != NULL) { |
| 109 | if (!prefixcmp(de->d_name, "tmp_")) { |
| 110 | char name[PATH_MAX]; |
| 111 | int c = snprintf(name, PATH_MAX, "%s/%s", |
| 112 | dirname, de->d_name); |
| 113 | if (c < 0 || c >= PATH_MAX) |
| 114 | continue; |
| 115 | if (expire) { |
| 116 | struct stat st; |
| 117 | if (stat(name, &st) != 0 || st.st_mtime >= expire) |
| 118 | continue; |
| 119 | } |
| 120 | printf("Removing stale temporary file %s\n", name); |
| 121 | if (!show_only) |
| 122 | unlink(name); |
| 123 | } |
| 124 | } |
| 125 | closedir(dir); |
| 126 | } |
| 127 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 128 | int cmd_prune(int argc, const char **argv, const char *prefix) |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 129 | { |
Junio C Hamano | 2430481 | 2007-01-06 02:16:10 -0800 | [diff] [blame] | 130 | struct rev_info revs; |
Michele Ballabio | 629de47 | 2008-03-23 21:50:29 +0100 | [diff] [blame] | 131 | const struct option options[] = { |
| 132 | OPT_BOOLEAN('n', NULL, &show_only, |
| 133 | "do not remove, show only"), |
| 134 | OPT_DATE(0, "expire", &expire, |
| 135 | "expire objects older than <time>"), |
| 136 | OPT_END() |
| 137 | }; |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 138 | |
Junio C Hamano | 16157b8 | 2007-01-05 13:31:43 -0800 | [diff] [blame] | 139 | save_commit_buffer = 0; |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 140 | init_revisions(&revs, prefix); |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 141 | |
Michele Ballabio | 629de47 | 2008-03-23 21:50:29 +0100 | [diff] [blame] | 142 | argc = parse_options(argc, argv, options, prune_usage, 0); |
Junio C Hamano | fe308f5 | 2008-03-24 23:20:51 -0700 | [diff] [blame] | 143 | while (argc--) { |
| 144 | unsigned char sha1[20]; |
| 145 | const char *name = *argv++; |
| 146 | |
| 147 | if (!get_sha1(name, sha1)) { |
| 148 | struct object *object = parse_object(sha1); |
| 149 | if (!object) |
| 150 | die("bad object: %s", name); |
| 151 | add_pending_object(&revs, object, ""); |
| 152 | } |
| 153 | else |
| 154 | die("unrecognized argument: %s", name); |
| 155 | } |
Michele Ballabio | 629de47 | 2008-03-23 21:50:29 +0100 | [diff] [blame] | 156 | mark_reachable_objects(&revs, 1); |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 157 | prune_object_dir(get_object_directory()); |
| 158 | |
J. Bruce Fields | 2eb53e6 | 2006-10-22 19:01:23 -0400 | [diff] [blame] | 159 | sync(); |
| 160 | prune_packed_objects(show_only); |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 161 | remove_temporary_files(); |
Linus Torvalds | ba84a79 | 2006-07-06 10:16:22 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | } |