7 #include "parse-options.h"
11 static const char * const prune_usage
[] = {
12 N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
17 static unsigned long expire
;
18 static int show_progress
= -1;
20 static int prune_tmp_file(const char *fullpath
)
23 if (lstat(fullpath
, &st
))
24 return error("Could not stat '%s'", fullpath
);
25 if (st
.st_mtime
> expire
)
27 if (show_only
|| verbose
)
28 printf("Removing stale temporary file %s\n", fullpath
);
30 unlink_or_warn(fullpath
);
34 static int prune_object(const unsigned char *sha1
, const char *fullpath
,
40 * Do we know about this object?
41 * It must have been reachable
43 if (lookup_object(sha1
))
46 if (lstat(fullpath
, &st
)) {
47 /* report errors, but do not stop pruning */
48 error("Could not stat '%s'", fullpath
);
51 if (st
.st_mtime
> expire
)
53 if (show_only
|| verbose
) {
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");
59 unlink_or_warn(fullpath
);
63 static int prune_cruft(const char *basename
, const char *path
, void *data
)
65 if (starts_with(basename
, "tmp_obj_"))
68 fprintf(stderr
, "bad sha1 file: %s\n", path
);
72 static int prune_subdir(int nr
, const char *path
, void *data
)
79 static int prune_worktree(const char *id
, struct strbuf
*reason
)
85 if (!is_directory(git_path("worktrees/%s", id
))) {
86 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
89 if (file_exists(git_path("worktrees/%s/locked", id
)))
91 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
92 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
95 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
97 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
102 path
= xmalloc(len
+ 1);
103 read_in_full(fd
, path
, len
);
105 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
108 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
113 if (!file_exists(path
)) {
117 * the repo is moved manually and has not been
120 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
121 st_link
.st_nlink
> 1)
123 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
127 return st
.st_mtime
<= expire
;
130 static void prune_worktrees(void)
132 struct strbuf reason
= STRBUF_INIT
;
133 struct strbuf path
= STRBUF_INIT
;
134 DIR *dir
= opendir(git_path("worktrees"));
139 while ((d
= readdir(dir
)) != NULL
) {
140 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
142 strbuf_reset(&reason
);
143 if (!prune_worktree(d
->d_name
, &reason
))
145 if (show_only
|| verbose
)
146 printf("%s\n", reason
.buf
);
150 strbuf_addstr(&path
, git_path("worktrees/%s", d
->d_name
));
151 ret
= remove_dir_recursively(&path
, 0);
152 if (ret
< 0 && errno
== ENOTDIR
)
153 ret
= unlink(path
.buf
);
155 error(_("failed to remove: %s"), strerror(errno
));
159 rmdir(git_path("worktrees"));
160 strbuf_release(&reason
);
161 strbuf_release(&path
);
165 * Write errors (particularly out of space) can result in
166 * failed temporary packs (and more rarely indexes and other
167 * files beginning with "tmp_") accumulating in the object
168 * and the pack directories.
170 static void remove_temporary_files(const char *path
)
177 fprintf(stderr
, "Unable to open directory %s\n", path
);
180 while ((de
= readdir(dir
)) != NULL
)
181 if (starts_with(de
->d_name
, "tmp_"))
182 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
186 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
188 struct rev_info revs
;
189 struct progress
*progress
= NULL
;
190 int do_prune_worktrees
= 0;
191 const struct option options
[] = {
192 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
193 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
194 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
195 OPT_BOOL(0, "worktrees", &do_prune_worktrees
, N_("prune .git/worktrees")),
196 OPT_EXPIRY_DATE(0, "expire", &expire
,
197 N_("expire objects older than <time>")),
203 save_commit_buffer
= 0;
204 check_replace_refs
= 0;
205 init_revisions(&revs
, prefix
);
207 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
209 if (do_prune_worktrees
) {
211 die(_("--worktrees does not take extra arguments"));
217 unsigned char sha1
[20];
218 const char *name
= *argv
++;
220 if (!get_sha1(name
, sha1
)) {
221 struct object
*object
= parse_object_or_die(sha1
, name
);
222 add_pending_object(&revs
, object
, "");
225 die("unrecognized argument: %s", name
);
228 if (show_progress
== -1)
229 show_progress
= isatty(2);
231 progress
= start_progress_delay(_("Checking connectivity"), 0, 0, 2);
233 mark_reachable_objects(&revs
, 1, expire
, progress
);
234 stop_progress(&progress
);
235 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
236 prune_cruft
, prune_subdir
, NULL
);
238 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
239 remove_temporary_files(get_object_directory());
240 s
= mkpathdup("%s/pack", get_object_directory());
241 remove_temporary_files(s
);
244 if (is_repository_shallow())
245 prune_shallow(show_only
);