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