]>
Commit | Line | Data |
---|---|---|
ba84a797 | 1 | #include "cache.h" |
ba84a797 | 2 | #include "commit.h" |
ba84a797 LT |
3 | #include "diff.h" |
4 | #include "revision.h" | |
5 | #include "builtin.h" | |
94421474 | 6 | #include "reachable.h" |
629de472 | 7 | #include "parse-options.h" |
ba84a797 | 8 | |
629de472 | 9 | static const char * const prune_usage[] = { |
b35ddf41 | 10 | "git prune [-n] [-v] [--expire <time>] [--] [<head>...]", |
629de472 MB |
11 | NULL |
12 | }; | |
96f1e58f | 13 | static int show_only; |
b35ddf41 | 14 | static int verbose; |
f01913e4 | 15 | static unsigned long expire; |
ba84a797 | 16 | |
db87e396 | 17 | static int prune_tmp_object(const char *path, const char *filename) |
0e8316cc BC |
18 | { |
19 | const char *fullpath = mkpath("%s/%s", path, filename); | |
20 | if (expire) { | |
21 | struct stat st; | |
22 | if (lstat(fullpath, &st)) | |
23 | return error("Could not stat '%s'", fullpath); | |
24 | if (st.st_mtime > expire) | |
25 | return 0; | |
26 | } | |
27 | printf("Removing stale temporary file %s\n", fullpath); | |
28 | if (!show_only) | |
29 | unlink(fullpath); | |
30 | return 0; | |
31 | } | |
32 | ||
ba84a797 LT |
33 | static int prune_object(char *path, const char *filename, const unsigned char *sha1) |
34 | { | |
f01913e4 JS |
35 | const char *fullpath = mkpath("%s/%s", path, filename); |
36 | if (expire) { | |
37 | struct stat st; | |
38 | if (lstat(fullpath, &st)) | |
39 | return error("Could not stat '%s'", fullpath); | |
40 | if (st.st_mtime > expire) | |
41 | return 0; | |
42 | } | |
b35ddf41 | 43 | if (show_only || verbose) { |
21666f1a NP |
44 | enum object_type type = sha1_object_info(sha1, NULL); |
45 | printf("%s %s\n", sha1_to_hex(sha1), | |
46 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
47 | } |
48 | if (!show_only) | |
f01913e4 | 49 | unlink(fullpath); |
ba84a797 LT |
50 | return 0; |
51 | } | |
52 | ||
53 | static int prune_dir(int i, char *path) | |
54 | { | |
55 | DIR *dir = opendir(path); | |
56 | struct dirent *de; | |
57 | ||
58 | if (!dir) | |
59 | return 0; | |
60 | ||
61 | while ((de = readdir(dir)) != NULL) { | |
62 | char name[100]; | |
63 | unsigned char sha1[20]; | |
64 | int len = strlen(de->d_name); | |
65 | ||
66 | switch (len) { | |
67 | case 2: | |
68 | if (de->d_name[1] != '.') | |
69 | break; | |
70 | case 1: | |
71 | if (de->d_name[0] != '.') | |
72 | break; | |
73 | continue; | |
74 | case 38: | |
75 | sprintf(name, "%02x", i); | |
76 | memcpy(name+2, de->d_name, len+1); | |
77 | if (get_sha1_hex(name, sha1) < 0) | |
78 | break; | |
79 | ||
80 | /* | |
81 | * Do we know about this object? | |
82 | * It must have been reachable | |
83 | */ | |
84 | if (lookup_object(sha1)) | |
85 | continue; | |
86 | ||
87 | prune_object(path, de->d_name, sha1); | |
88 | continue; | |
89 | } | |
3d32a46b BC |
90 | if (!prefixcmp(de->d_name, "tmp_obj_")) { |
91 | prune_tmp_object(path, de->d_name); | |
92 | continue; | |
93 | } | |
ba84a797 LT |
94 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
95 | } | |
3254d218 NP |
96 | if (!show_only) |
97 | rmdir(path); | |
ba84a797 LT |
98 | closedir(dir); |
99 | return 0; | |
100 | } | |
101 | ||
102 | static void prune_object_dir(const char *path) | |
103 | { | |
104 | int i; | |
105 | for (i = 0; i < 256; i++) { | |
106 | static char dir[4096]; | |
107 | sprintf(dir, "%s/%02x", path, i); | |
108 | prune_dir(i, dir); | |
109 | } | |
110 | } | |
111 | ||
8464010f DST |
112 | /* |
113 | * Write errors (particularly out of space) can result in | |
114 | * failed temporary packs (and more rarely indexes and other | |
db87e396 BC |
115 | * files begining with "tmp_") accumulating in the object |
116 | * and the pack directories. | |
8464010f | 117 | */ |
db87e396 | 118 | static void remove_temporary_files(const char *path) |
8464010f DST |
119 | { |
120 | DIR *dir; | |
121 | struct dirent *de; | |
8464010f | 122 | |
db87e396 | 123 | dir = opendir(path); |
8464010f | 124 | if (!dir) { |
db87e396 | 125 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
126 | return; |
127 | } | |
0e8316cc BC |
128 | while ((de = readdir(dir)) != NULL) |
129 | if (!prefixcmp(de->d_name, "tmp_")) | |
db87e396 | 130 | prune_tmp_object(path, de->d_name); |
8464010f DST |
131 | closedir(dir); |
132 | } | |
133 | ||
a633fca0 | 134 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 135 | { |
24304816 | 136 | struct rev_info revs; |
629de472 MB |
137 | const struct option options[] = { |
138 | OPT_BOOLEAN('n', NULL, &show_only, | |
139 | "do not remove, show only"), | |
b35ddf41 MG |
140 | OPT_BOOLEAN('v', NULL, &verbose, |
141 | "report pruned objects"), | |
629de472 MB |
142 | OPT_DATE(0, "expire", &expire, |
143 | "expire objects older than <time>"), | |
144 | OPT_END() | |
145 | }; | |
db87e396 | 146 | char *s; |
ba84a797 | 147 | |
16157b80 | 148 | save_commit_buffer = 0; |
a633fca0 | 149 | init_revisions(&revs, prefix); |
ba84a797 | 150 | |
629de472 | 151 | argc = parse_options(argc, argv, options, prune_usage, 0); |
fe308f53 JH |
152 | while (argc--) { |
153 | unsigned char sha1[20]; | |
154 | const char *name = *argv++; | |
155 | ||
156 | if (!get_sha1(name, sha1)) { | |
157 | struct object *object = parse_object(sha1); | |
158 | if (!object) | |
159 | die("bad object: %s", name); | |
160 | add_pending_object(&revs, object, ""); | |
161 | } | |
162 | else | |
163 | die("unrecognized argument: %s", name); | |
164 | } | |
629de472 | 165 | mark_reachable_objects(&revs, 1); |
ba84a797 LT |
166 | prune_object_dir(get_object_directory()); |
167 | ||
2eb53e65 | 168 | prune_packed_objects(show_only); |
db87e396 BC |
169 | remove_temporary_files(get_object_directory()); |
170 | s = xstrdup(mkpath("%s/pack", get_object_directory())); | |
171 | remove_temporary_files(s); | |
172 | free(s); | |
ba84a797 LT |
173 | return 0; |
174 | } |