worktree.c: add find_worktree()
[gitweb.git] / worktree.c
index 360ba417e828d92d7473f6dff8b49bb61a42440a..0782e00983d5a0dbcbeefa222d21352aa54dc7cc 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)
 {
@@ -94,6 +96,7 @@ 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);
 
 done:
@@ -138,6 +141,7 @@ 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);
 
 done:
@@ -147,6 +151,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 +187,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 +199,8 @@ struct worktree **get_worktrees(void)
        }
        ALLOC_GROW(list, counter + 1, alloc);
        list[counter] = NULL;
+
+       mark_current_worktree(list);
        return list;
 }
 
@@ -191,6 +214,60 @@ const char *get_worktree_git_dir(const struct worktree *wt)
                return git_common_path("worktrees/%s", wt->id);
 }
 
+struct worktree *find_worktree(struct worktree **list,
+                              const char *prefix,
+                              const char *arg)
+{
+       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_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)
 {
@@ -205,10 +282,23 @@ const struct worktree *find_shared_symref(const char *symref,
        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)) {
@@ -216,7 +306,7 @@ const struct worktree *find_shared_symref(const char *symref,
                }
 
                if (!strcmp(sb.buf, target)) {
-                       existing = worktrees[i];
+                       existing = wt;
                        break;
                }
        }