4 #include "parse-options.h"
6 static const char * const worktree_usage
[] = {
7 N_("git worktree prune [<options>]"),
13 static unsigned long expire
;
15 static int prune_worktree(const char *id
, struct strbuf
*reason
)
21 if (!is_directory(git_path("worktrees/%s", id
))) {
22 strbuf_addf(reason
, _("Removing worktrees/%s: not a valid directory"), id
);
25 if (file_exists(git_path("worktrees/%s/locked", id
)))
27 if (stat(git_path("worktrees/%s/gitdir", id
), &st
)) {
28 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file does not exist"), id
);
31 fd
= open(git_path("worktrees/%s/gitdir", id
), O_RDONLY
);
33 strbuf_addf(reason
, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
38 path
= xmalloc(len
+ 1);
39 read_in_full(fd
, path
, len
);
41 while (len
&& (path
[len
- 1] == '\n' || path
[len
- 1] == '\r'))
44 strbuf_addf(reason
, _("Removing worktrees/%s: invalid gitdir file"), id
);
49 if (!file_exists(path
)) {
53 * the repo is moved manually and has not been
56 if (!stat(git_path("worktrees/%s/link", id
), &st_link
) &&
59 if (st
.st_mtime
<= expire
) {
60 strbuf_addf(reason
, _("Removing worktrees/%s: gitdir file points to non-existent location"), id
);
70 static void prune_worktrees(void)
72 struct strbuf reason
= STRBUF_INIT
;
73 struct strbuf path
= STRBUF_INIT
;
74 DIR *dir
= opendir(git_path("worktrees"));
79 while ((d
= readdir(dir
)) != NULL
) {
80 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
82 strbuf_reset(&reason
);
83 if (!prune_worktree(d
->d_name
, &reason
))
85 if (show_only
|| verbose
)
86 printf("%s\n", reason
.buf
);
90 strbuf_addstr(&path
, git_path("worktrees/%s", d
->d_name
));
91 ret
= remove_dir_recursively(&path
, 0);
92 if (ret
< 0 && errno
== ENOTDIR
)
93 ret
= unlink(path
.buf
);
95 error(_("failed to remove: %s"), strerror(errno
));
99 rmdir(git_path("worktrees"));
100 strbuf_release(&reason
);
101 strbuf_release(&path
);
104 static int prune(int ac
, const char **av
, const char *prefix
)
106 struct option options
[] = {
107 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
108 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
109 OPT_EXPIRY_DATE(0, "expire", &expire
,
110 N_("expire objects older than <time>")),
115 ac
= parse_options(ac
, av
, prefix
, options
, worktree_usage
, 0);
117 usage_with_options(worktree_usage
, options
);
122 int cmd_worktree(int ac
, const char **av
, const char *prefix
)
124 struct option options
[] = {
129 usage_with_options(worktree_usage
, options
);
130 if (!strcmp(av
[1], "prune"))
131 return prune(ac
- 1, av
+ 1, prefix
);
132 usage_with_options(worktree_usage
, options
);