Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Silva <rafaeloliveira.cs@gmail.com>2021-01-20 00:27:33 +0300
committerJunio C Hamano <gitster@pobox.com>2021-01-30 20:57:08 +0300
commita29a8b7574ab34026252691933f462eddc59146a (patch)
tree63f26ea5798bce6632dff33f04f949f4f3f8fc92 /worktree.c
parent66e871b6647ffea61a77a0f82c7ef3415f1ee79c (diff)
worktree: libify should_prune_worktree()
As part of teaching "git worktree list" to annotate worktree that is a candidate for pruning, let's move should_prune_worktree() from builtin/worktree.c to worktree.c in order to make part of the worktree public API. should_prune_worktree() knows how to select the given worktree for pruning based on an expiration date, however the expiration value is stored in a static file-scope variable and it is not local to the function. In order to move the function, teach should_prune_worktree() to take the expiration date as an argument and document the new parameter that is not immediately obvious. Also, change the function comment to clearly state that the worktree's path is returned in `wtpath` argument. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c
index 821b233479..8ae019af79 100644
--- a/worktree.c
+++ b/worktree.c
@@ -741,3 +741,71 @@ done:
strbuf_release(&realdotgit);
strbuf_release(&dotgit);
}
+
+int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire)
+{
+ struct stat st;
+ char *path;
+ int fd;
+ size_t len;
+ ssize_t read_result;
+
+ *wtpath = NULL;
+ if (!is_directory(git_path("worktrees/%s", id))) {
+ strbuf_addstr(reason, _("not a valid directory"));
+ return 1;
+ }
+ if (file_exists(git_path("worktrees/%s/locked", id)))
+ return 0;
+ if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
+ strbuf_addstr(reason, _("gitdir file does not exist"));
+ return 1;
+ }
+ fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
+ if (fd < 0) {
+ strbuf_addf(reason, _("unable to read gitdir file (%s)"),
+ strerror(errno));
+ return 1;
+ }
+ len = xsize_t(st.st_size);
+ path = xmallocz(len);
+
+ read_result = read_in_full(fd, path, len);
+ if (read_result < 0) {
+ strbuf_addf(reason, _("unable to read gitdir file (%s)"),
+ strerror(errno));
+ close(fd);
+ free(path);
+ return 1;
+ }
+ close(fd);
+
+ if (read_result != len) {
+ strbuf_addf(reason,
+ _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
+ (uintmax_t)len, (uintmax_t)read_result);
+ free(path);
+ return 1;
+ }
+ while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
+ len--;
+ if (!len) {
+ strbuf_addstr(reason, _("invalid gitdir file"));
+ free(path);
+ return 1;
+ }
+ path[len] = '\0';
+ if (!file_exists(path)) {
+ if (stat(git_path("worktrees/%s/index", id), &st) ||
+ st.st_mtime <= expire) {
+ strbuf_addstr(reason, _("gitdir file points to non-existent location"));
+ free(path);
+ return 1;
+ } else {
+ *wtpath = path;
+ return 0;
+ }
+ }
+ *wtpath = path;
+ return 0;
+}