]>
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" |
dc347195 | 8 | #include "progress.h" |
8ca12c0d | 9 | #include "dir.h" |
ba84a797 | 10 | |
629de472 | 11 | static const char * const prune_usage[] = { |
8f5b7281 | 12 | N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"), |
629de472 MB |
13 | NULL |
14 | }; | |
96f1e58f | 15 | static int show_only; |
b35ddf41 | 16 | static int verbose; |
f01913e4 | 17 | static unsigned long expire; |
bf0a59b3 | 18 | static int show_progress = -1; |
ba84a797 | 19 | |
4454e9cb | 20 | static int prune_tmp_file(const char *fullpath) |
0e8316cc | 21 | { |
cbf731ed AS |
22 | struct stat st; |
23 | if (lstat(fullpath, &st)) | |
24 | return error("Could not stat '%s'", fullpath); | |
25 | if (st.st_mtime > expire) | |
26 | return 0; | |
90b29cb7 BC |
27 | if (show_only || verbose) |
28 | printf("Removing stale temporary file %s\n", fullpath); | |
0e8316cc | 29 | if (!show_only) |
691f1a28 | 30 | unlink_or_warn(fullpath); |
0e8316cc BC |
31 | return 0; |
32 | } | |
33 | ||
27e1e22d JK |
34 | static int prune_object(const unsigned char *sha1, const char *fullpath, |
35 | void *data) | |
ba84a797 | 36 | { |
cbf731ed | 37 | struct stat st; |
27e1e22d JK |
38 | |
39 | /* | |
40 | * Do we know about this object? | |
41 | * It must have been reachable | |
42 | */ | |
43 | if (lookup_object(sha1)) | |
44 | return 0; | |
45 | ||
46 | if (lstat(fullpath, &st)) { | |
47 | /* report errors, but do not stop pruning */ | |
48 | error("Could not stat '%s'", fullpath); | |
49 | return 0; | |
50 | } | |
cbf731ed AS |
51 | if (st.st_mtime > expire) |
52 | return 0; | |
b35ddf41 | 53 | if (show_only || verbose) { |
21666f1a NP |
54 | enum object_type type = sha1_object_info(sha1, NULL); |
55 | printf("%s %s\n", sha1_to_hex(sha1), | |
56 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
57 | } |
58 | if (!show_only) | |
691f1a28 | 59 | unlink_or_warn(fullpath); |
ba84a797 LT |
60 | return 0; |
61 | } | |
62 | ||
27e1e22d | 63 | static int prune_cruft(const char *basename, const char *path, void *data) |
ba84a797 | 64 | { |
27e1e22d JK |
65 | if (starts_with(basename, "tmp_obj_")) |
66 | prune_tmp_file(path); | |
67 | else | |
68 | fprintf(stderr, "bad sha1 file: %s\n", path); | |
ba84a797 LT |
69 | return 0; |
70 | } | |
71 | ||
27e1e22d | 72 | static int prune_subdir(int nr, const char *path, void *data) |
ba84a797 | 73 | { |
27e1e22d JK |
74 | if (!show_only) |
75 | rmdir(path); | |
76 | return 0; | |
ba84a797 LT |
77 | } |
78 | ||
23af91d1 NTND |
79 | static int prune_worktree(const char *id, struct strbuf *reason) |
80 | { | |
81 | struct stat st; | |
82 | char *path; | |
83 | int fd, len; | |
84 | ||
85 | if (!is_directory(git_path("worktrees/%s", id))) { | |
86 | strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id); | |
87 | return 1; | |
88 | } | |
89 | if (file_exists(git_path("worktrees/%s/locked", id))) | |
90 | return 0; | |
91 | if (stat(git_path("worktrees/%s/gitdir", id), &st)) { | |
92 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id); | |
93 | return 1; | |
94 | } | |
95 | fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY); | |
96 | if (fd < 0) { | |
97 | strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"), | |
98 | id, strerror(errno)); | |
99 | return 1; | |
100 | } | |
101 | len = st.st_size; | |
102 | path = xmalloc(len + 1); | |
103 | read_in_full(fd, path, len); | |
104 | close(fd); | |
105 | while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) | |
106 | len--; | |
107 | if (!len) { | |
108 | strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id); | |
109 | free(path); | |
110 | return 1; | |
111 | } | |
112 | path[len] = '\0'; | |
113 | if (!file_exists(path)) { | |
114 | struct stat st_link; | |
115 | free(path); | |
116 | /* | |
117 | * the repo is moved manually and has not been | |
118 | * accessed since? | |
119 | */ | |
120 | if (!stat(git_path("worktrees/%s/link", id), &st_link) && | |
121 | st_link.st_nlink > 1) | |
122 | return 0; | |
562bc080 MK |
123 | if (st.st_mtime <= expire) { |
124 | strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id); | |
125 | return 1; | |
126 | } else { | |
127 | return 0; | |
128 | } | |
23af91d1 NTND |
129 | } |
130 | free(path); | |
562bc080 | 131 | return 0; |
23af91d1 NTND |
132 | } |
133 | ||
134 | static void prune_worktrees(void) | |
135 | { | |
136 | struct strbuf reason = STRBUF_INIT; | |
137 | struct strbuf path = STRBUF_INIT; | |
138 | DIR *dir = opendir(git_path("worktrees")); | |
139 | struct dirent *d; | |
140 | int ret; | |
141 | if (!dir) | |
142 | return; | |
143 | while ((d = readdir(dir)) != NULL) { | |
144 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) | |
145 | continue; | |
146 | strbuf_reset(&reason); | |
147 | if (!prune_worktree(d->d_name, &reason)) | |
148 | continue; | |
149 | if (show_only || verbose) | |
150 | printf("%s\n", reason.buf); | |
151 | if (show_only) | |
152 | continue; | |
153 | strbuf_reset(&path); | |
154 | strbuf_addstr(&path, git_path("worktrees/%s", d->d_name)); | |
155 | ret = remove_dir_recursively(&path, 0); | |
156 | if (ret < 0 && errno == ENOTDIR) | |
157 | ret = unlink(path.buf); | |
158 | if (ret) | |
159 | error(_("failed to remove: %s"), strerror(errno)); | |
160 | } | |
161 | closedir(dir); | |
162 | if (!show_only) | |
163 | rmdir(git_path("worktrees")); | |
164 | strbuf_release(&reason); | |
165 | strbuf_release(&path); | |
166 | } | |
167 | ||
8464010f DST |
168 | /* |
169 | * Write errors (particularly out of space) can result in | |
170 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 171 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 172 | * and the pack directories. |
8464010f | 173 | */ |
db87e396 | 174 | static void remove_temporary_files(const char *path) |
8464010f DST |
175 | { |
176 | DIR *dir; | |
177 | struct dirent *de; | |
8464010f | 178 | |
db87e396 | 179 | dir = opendir(path); |
8464010f | 180 | if (!dir) { |
db87e396 | 181 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
182 | return; |
183 | } | |
0e8316cc | 184 | while ((de = readdir(dir)) != NULL) |
59556548 | 185 | if (starts_with(de->d_name, "tmp_")) |
4454e9cb | 186 | prune_tmp_file(mkpath("%s/%s", path, de->d_name)); |
8464010f DST |
187 | closedir(dir); |
188 | } | |
189 | ||
a633fca0 | 190 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 191 | { |
24304816 | 192 | struct rev_info revs; |
bf0a59b3 | 193 | struct progress *progress = NULL; |
23af91d1 | 194 | int do_prune_worktrees = 0; |
629de472 | 195 | const struct option options[] = { |
8f5b7281 NTND |
196 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
197 | OPT__VERBOSE(&verbose, N_("report pruned objects")), | |
198 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
23af91d1 | 199 | OPT_BOOL(0, "worktrees", &do_prune_worktrees, N_("prune .git/worktrees")), |
27ec394a JH |
200 | OPT_EXPIRY_DATE(0, "expire", &expire, |
201 | N_("expire objects older than <time>")), | |
629de472 MB |
202 | OPT_END() |
203 | }; | |
db87e396 | 204 | char *s; |
ba84a797 | 205 | |
cbf731ed | 206 | expire = ULONG_MAX; |
16157b80 | 207 | save_commit_buffer = 0; |
afc711b8 | 208 | check_replace_refs = 0; |
ff4056bb | 209 | ref_paranoia = 1; |
a633fca0 | 210 | init_revisions(&revs, prefix); |
ba84a797 | 211 | |
37782920 | 212 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
23af91d1 NTND |
213 | |
214 | if (do_prune_worktrees) { | |
215 | if (argc) | |
216 | die(_("--worktrees does not take extra arguments")); | |
217 | prune_worktrees(); | |
218 | return 0; | |
219 | } | |
220 | ||
fe308f53 JH |
221 | while (argc--) { |
222 | unsigned char sha1[20]; | |
223 | const char *name = *argv++; | |
224 | ||
225 | if (!get_sha1(name, sha1)) { | |
f7892d18 | 226 | struct object *object = parse_object_or_die(sha1, name); |
fe308f53 JH |
227 | add_pending_object(&revs, object, ""); |
228 | } | |
229 | else | |
230 | die("unrecognized argument: %s", name); | |
231 | } | |
bf0a59b3 JK |
232 | |
233 | if (show_progress == -1) | |
234 | show_progress = isatty(2); | |
235 | if (show_progress) | |
754dbc43 | 236 | progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2); |
bf0a59b3 | 237 | |
d3038d22 | 238 | mark_reachable_objects(&revs, 1, expire, progress); |
dc347195 | 239 | stop_progress(&progress); |
27e1e22d JK |
240 | for_each_loose_file_in_objdir(get_object_directory(), prune_object, |
241 | prune_cruft, prune_subdir, NULL); | |
ba84a797 | 242 | |
af0b4a3b | 243 | prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0); |
db87e396 | 244 | remove_temporary_files(get_object_directory()); |
4e2d094d | 245 | s = mkpathdup("%s/pack", get_object_directory()); |
db87e396 BC |
246 | remove_temporary_files(s); |
247 | free(s); | |
eab3296c NTND |
248 | |
249 | if (is_repository_shallow()) | |
250 | prune_shallow(show_only); | |
251 | ||
ba84a797 LT |
252 | return 0; |
253 | } |