67
rmdir(git_path("worktrees")); /* ignore failed removal */
68
}
69
70
-/*
71
- * Return true if worktree entry should be pruned, along with the reason for
72
- * pruning. Otherwise, return false and the worktree's path, or NULL if it
73
- * cannot be determined. Caller is responsible for freeing returned path.
74
- */
75
-static int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath)
76
-{
77
- struct stat st;
78
- char *path;
79
- int fd;
80
- size_t len;
81
- ssize_t read_result;
82
-
83
- *wtpath = NULL;
84
- if (!is_directory(git_path("worktrees/%s", id))) {
85
- strbuf_addstr(reason, _("not a valid directory"));
86
- return 1;
87
- }
88
- if (file_exists(git_path("worktrees/%s/locked", id)))
89
- return 0;
90
- if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
91
- strbuf_addstr(reason, _("gitdir file does not exist"));
92
- return 1;
93
- }
94
- fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
95
- if (fd < 0) {
96
- strbuf_addf(reason, _("unable to read gitdir file (%s)"),
97
- strerror(errno));
98
- return 1;
99
- }
100
- len = xsize_t(st.st_size);
101
- path = xmallocz(len);
102
-
103
- read_result = read_in_full(fd, path, len);
104
- if (read_result < 0) {
105
- strbuf_addf(reason, _("unable to read gitdir file (%s)"),
106
- strerror(errno));
107
- close(fd);
108
- free(path);
109
- return 1;
110
- }
111
- close(fd);
112
-
113
- if (read_result != len) {
114
- strbuf_addf(reason,
115
- _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
116
- (uintmax_t)len, (uintmax_t)read_result);
117
- free(path);
118
- return 1;
119
- }
120
- while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
121
- len--;
122
- if (!len) {
123
- strbuf_addstr(reason, _("invalid gitdir file"));
124
- free(path);
125
- return 1;
126
- }
127
- path[len] = '\0';
128
- if (!file_exists(path)) {
129
- if (stat(git_path("worktrees/%s/index", id), &st) ||
130
- st.st_mtime <= expire) {
131
- strbuf_addstr(reason, _("gitdir file points to non-existent location"));
132
- free(path);
133
- return 1;
134
- } else {
135
- *wtpath = path;
136
- return 0;
137
- }
138
- }
139
- *wtpath = path;
140
- return 0;
141
-}
142
-
70
static void prune_worktree(const char *id, const char *reason)
71
{
72
if (show_only || verbose)
122
if (is_dot_or_dotdot(d->d_name))
123
continue;
124
strbuf_reset(&reason);
198
- if (should_prune_worktree(d->d_name, &reason, &path))
125
+ if (should_prune_worktree(d->d_name, &reason, &path, expire))
126
prune_worktree(d->d_name, reason.buf);
127
else if (path)
128
string_list_append(&kept, path)->util = xstrdup(d->d_name);