worktree.c: add is_worktree_locked()
[gitweb.git] / worktree.c
index 5ae54f009151939879d4891e2fa95f59110fb6ff..2bcfff3850203dbb283ff40455f0c97694fff7e5 100644 (file)
@@ -2,6 +2,8 @@
 #include "refs.h"
 #include "strbuf.h"
 #include "worktree.h"
+#include "dir.h"
+#include "wt-status.h"
 
 void free_worktrees(struct worktree **worktrees)
 {
@@ -11,6 +13,7 @@ void free_worktrees(struct worktree **worktrees)
                free(worktrees[i]->path);
                free(worktrees[i]->id);
                free(worktrees[i]->head_ref);
+               free(worktrees[i]->lock_reason);
                free(worktrees[i]);
        }
        free (worktrees);
@@ -94,7 +97,10 @@ static struct worktree *get_main_worktree(void)
        worktree->is_bare = is_bare;
        worktree->head_ref = NULL;
        worktree->is_detached = is_detached;
+       worktree->is_current = 0;
        add_head_info(&head_ref, worktree);
+       worktree->lock_reason = NULL;
+       worktree->lock_reason_valid = 0;
 
 done:
        strbuf_release(&path);
@@ -138,7 +144,10 @@ static struct worktree *get_linked_worktree(const char *id)
        worktree->is_bare = 0;
        worktree->head_ref = NULL;
        worktree->is_detached = is_detached;
+       worktree->is_current = 0;
        add_head_info(&head_ref, worktree);
+       worktree->lock_reason = NULL;
+       worktree->lock_reason_valid = 0;
 
 done:
        strbuf_release(&path);
@@ -147,6 +156,23 @@ static struct worktree *get_linked_worktree(const char *id)
        return worktree;
 }
 
+static void mark_current_worktree(struct worktree **worktrees)
+{
+       char *git_dir = xstrdup(absolute_path(get_git_dir()));
+       int i;
+
+       for (i = 0; worktrees[i]; i++) {
+               struct worktree *wt = worktrees[i];
+               const char *wt_git_dir = get_worktree_git_dir(wt);
+
+               if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
+                       wt->is_current = 1;
+                       break;
+               }
+       }
+       free(git_dir);
+}
+
 struct worktree **get_worktrees(void)
 {
        struct worktree **list = NULL;
@@ -166,7 +192,7 @@ struct worktree **get_worktrees(void)
        if (dir) {
                while ((d = readdir(dir)) != NULL) {
                        struct worktree *linked = NULL;
-                       if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+                       if (is_dot_or_dotdot(d->d_name))
                                continue;
 
                        if ((linked = get_linked_worktree(d->d_name))) {
@@ -178,6 +204,8 @@ struct worktree **get_worktrees(void)
        }
        ALLOC_GROW(list, counter + 1, alloc);
        list[counter] = NULL;
+
+       mark_current_worktree(list);
        return list;
 }
 
@@ -191,19 +219,119 @@ const char *get_worktree_git_dir(const struct worktree *wt)
                return git_common_path("worktrees/%s", wt->id);
 }
 
-char *find_shared_symref(const char *symref, const char *target)
+struct worktree *find_worktree(struct worktree **list,
+                              const char *prefix,
+                              const char *arg)
 {
-       char *existing = NULL;
+       char *path;
+
+       arg = prefix_filename(prefix, strlen(prefix), arg);
+       path = xstrdup(real_path(arg));
+       for (; *list; list++)
+               if (!fspathcmp(path, real_path((*list)->path)))
+                       break;
+       free(path);
+       return *list;
+}
+
+int is_main_worktree(const struct worktree *wt)
+{
+       return !wt->id;
+}
+
+const char *is_worktree_locked(struct worktree *wt)
+{
+       assert(!is_main_worktree(wt));
+
+       if (!wt->lock_reason_valid) {
+               struct strbuf path = STRBUF_INIT;
+
+               strbuf_addstr(&path, worktree_git_path(wt, "locked"));
+               if (file_exists(path.buf)) {
+                       struct strbuf lock_reason = STRBUF_INIT;
+                       if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
+                               die_errno(_("failed to read '%s'"), path.buf);
+                       strbuf_trim(&lock_reason);
+                       wt->lock_reason = strbuf_detach(&lock_reason, NULL);
+               } else
+                       wt->lock_reason = NULL;
+               wt->lock_reason_valid = 1;
+               strbuf_release(&path);
+       }
+
+       return wt->lock_reason;
+}
+
+int is_worktree_being_rebased(const struct worktree *wt,
+                             const char *target)
+{
+       struct wt_status_state state;
+       int found_rebase;
+
+       memset(&state, 0, sizeof(state));
+       found_rebase = wt_status_check_rebase(wt, &state) &&
+               ((state.rebase_in_progress ||
+                 state.rebase_interactive_in_progress) &&
+                state.branch &&
+                starts_with(target, "refs/heads/") &&
+                !strcmp(state.branch, target + strlen("refs/heads/")));
+       free(state.branch);
+       free(state.onto);
+       return found_rebase;
+}
+
+int is_worktree_being_bisected(const struct worktree *wt,
+                              const char *target)
+{
+       struct wt_status_state state;
+       int found_rebase;
+
+       memset(&state, 0, sizeof(state));
+       found_rebase = wt_status_check_bisect(wt, &state) &&
+               state.branch &&
+               starts_with(target, "refs/heads/") &&
+               !strcmp(state.branch, target + strlen("refs/heads/"));
+       free(state.branch);
+       return found_rebase;
+}
+
+/*
+ * note: this function should be able to detect shared symref even if
+ * HEAD is temporarily detached (e.g. in the middle of rebase or
+ * bisect). New commands that do similar things should update this
+ * function as well.
+ */
+const struct worktree *find_shared_symref(const char *symref,
+                                         const char *target)
+{
+       const struct worktree *existing = NULL;
        struct strbuf path = STRBUF_INIT;
        struct strbuf sb = STRBUF_INIT;
-       struct worktree **worktrees = get_worktrees();
+       static struct worktree **worktrees;
        int i = 0;
 
+       if (worktrees)
+               free_worktrees(worktrees);
+       worktrees = get_worktrees();
+
        for (i = 0; worktrees[i]; i++) {
+               struct worktree *wt = worktrees[i];
+
+               if (wt->is_detached && !strcmp(symref, "HEAD")) {
+                       if (is_worktree_being_rebased(wt, target)) {
+                               existing = wt;
+                               break;
+                       }
+                       if (is_worktree_being_bisected(wt, target)) {
+                               existing = wt;
+                               break;
+                       }
+               }
+
                strbuf_reset(&path);
                strbuf_reset(&sb);
                strbuf_addf(&path, "%s/%s",
-                           get_worktree_git_dir(worktrees[i]),
+                           get_worktree_git_dir(wt),
                            symref);
 
                if (parse_ref(path.buf, &sb, NULL)) {
@@ -211,14 +339,13 @@ char *find_shared_symref(const char *symref, const char *target)
                }
 
                if (!strcmp(sb.buf, target)) {
-                       existing = xstrdup(worktrees[i]->path);
+                       existing = wt;
                        break;
                }
        }
 
        strbuf_release(&path);
        strbuf_release(&sb);
-       free_worktrees(worktrees);
 
        return existing;
 }