c1ec334b06f8b8d55b4087d1208d85731fd46543
   1#include "cache.h"
   2#include "refs.h"
   3#include "strbuf.h"
   4#include "worktree.h"
   5#include "dir.h"
   6#include "wt-status.h"
   7
   8void free_worktrees(struct worktree **worktrees)
   9{
  10        int i = 0;
  11
  12        for (i = 0; worktrees[i]; i++) {
  13                free(worktrees[i]->path);
  14                free(worktrees[i]->id);
  15                free(worktrees[i]->head_ref);
  16                free(worktrees[i]->lock_reason);
  17                free(worktrees[i]);
  18        }
  19        free (worktrees);
  20}
  21
  22/**
  23 * Update head_sha1, head_ref and is_detached of the given worktree
  24 */
  25static void add_head_info(struct worktree *wt)
  26{
  27        int flags;
  28        const char *target;
  29
  30        target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
  31                                         "HEAD",
  32                                         RESOLVE_REF_READING,
  33                                         wt->head_sha1, &flags);
  34        if (!target)
  35                return;
  36
  37        if (flags & REF_ISSYMREF)
  38                wt->head_ref = xstrdup(target);
  39        else
  40                wt->is_detached = 1;
  41}
  42
  43/**
  44 * get the main worktree
  45 */
  46static struct worktree *get_main_worktree(void)
  47{
  48        struct worktree *worktree = NULL;
  49        struct strbuf path = STRBUF_INIT;
  50        struct strbuf worktree_path = STRBUF_INIT;
  51        int is_bare = 0;
  52
  53        strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
  54        is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
  55        if (is_bare)
  56                strbuf_strip_suffix(&worktree_path, "/.");
  57
  58        strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
  59
  60        worktree = xcalloc(1, sizeof(*worktree));
  61        worktree->path = strbuf_detach(&worktree_path, NULL);
  62        worktree->is_bare = is_bare;
  63        add_head_info(worktree);
  64
  65        strbuf_release(&path);
  66        strbuf_release(&worktree_path);
  67        return worktree;
  68}
  69
  70static struct worktree *get_linked_worktree(const char *id)
  71{
  72        struct worktree *worktree = NULL;
  73        struct strbuf path = STRBUF_INIT;
  74        struct strbuf worktree_path = STRBUF_INIT;
  75
  76        if (!id)
  77                die("Missing linked worktree name");
  78
  79        strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
  80        if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
  81                /* invalid gitdir file */
  82                goto done;
  83
  84        strbuf_rtrim(&worktree_path);
  85        if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
  86                strbuf_reset(&worktree_path);
  87                strbuf_add_absolute_path(&worktree_path, ".");
  88                strbuf_strip_suffix(&worktree_path, "/.");
  89        }
  90
  91        strbuf_reset(&path);
  92        strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
  93
  94        worktree = xcalloc(1, sizeof(*worktree));
  95        worktree->path = strbuf_detach(&worktree_path, NULL);
  96        worktree->id = xstrdup(id);
  97        add_head_info(worktree);
  98
  99done:
 100        strbuf_release(&path);
 101        strbuf_release(&worktree_path);
 102        return worktree;
 103}
 104
 105static void mark_current_worktree(struct worktree **worktrees)
 106{
 107        char *git_dir = absolute_pathdup(get_git_dir());
 108        int i;
 109
 110        for (i = 0; worktrees[i]; i++) {
 111                struct worktree *wt = worktrees[i];
 112                const char *wt_git_dir = get_worktree_git_dir(wt);
 113
 114                if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
 115                        wt->is_current = 1;
 116                        break;
 117                }
 118        }
 119        free(git_dir);
 120}
 121
 122static int compare_worktree(const void *a_, const void *b_)
 123{
 124        const struct worktree *const *a = a_;
 125        const struct worktree *const *b = b_;
 126        return fspathcmp((*a)->path, (*b)->path);
 127}
 128
 129struct worktree **get_worktrees(unsigned flags)
 130{
 131        struct worktree **list = NULL;
 132        struct strbuf path = STRBUF_INIT;
 133        DIR *dir;
 134        struct dirent *d;
 135        int counter = 0, alloc = 2;
 136
 137        ALLOC_ARRAY(list, alloc);
 138
 139        list[counter++] = get_main_worktree();
 140
 141        strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
 142        dir = opendir(path.buf);
 143        strbuf_release(&path);
 144        if (dir) {
 145                while ((d = readdir(dir)) != NULL) {
 146                        struct worktree *linked = NULL;
 147                        if (is_dot_or_dotdot(d->d_name))
 148                                continue;
 149
 150                        if ((linked = get_linked_worktree(d->d_name))) {
 151                                ALLOC_GROW(list, counter + 1, alloc);
 152                                list[counter++] = linked;
 153                        }
 154                }
 155                closedir(dir);
 156        }
 157        ALLOC_GROW(list, counter + 1, alloc);
 158        list[counter] = NULL;
 159
 160        if (flags & GWT_SORT_LINKED)
 161                /*
 162                 * don't sort the first item (main worktree), which will
 163                 * always be the first
 164                 */
 165                QSORT(list + 1, counter - 1, compare_worktree);
 166
 167        mark_current_worktree(list);
 168        return list;
 169}
 170
 171const char *get_worktree_git_dir(const struct worktree *wt)
 172{
 173        if (!wt)
 174                return get_git_dir();
 175        else if (!wt->id)
 176                return get_git_common_dir();
 177        else
 178                return git_common_path("worktrees/%s", wt->id);
 179}
 180
 181static struct worktree *find_worktree_by_suffix(struct worktree **list,
 182                                                const char *suffix)
 183{
 184        struct worktree *found = NULL;
 185        int nr_found = 0, suffixlen;
 186
 187        suffixlen = strlen(suffix);
 188        if (!suffixlen)
 189                return NULL;
 190
 191        for (; *list && nr_found < 2; list++) {
 192                const char      *path    = (*list)->path;
 193                int              pathlen = strlen(path);
 194                int              start   = pathlen - suffixlen;
 195
 196                /* suffix must start at directory boundary */
 197                if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
 198                    !fspathcmp(suffix, path + start)) {
 199                        found = *list;
 200                        nr_found++;
 201                }
 202        }
 203        return nr_found == 1 ? found : NULL;
 204}
 205
 206struct worktree *find_worktree(struct worktree **list,
 207                               const char *prefix,
 208                               const char *arg)
 209{
 210        struct worktree *wt;
 211        char *path;
 212
 213        if ((wt = find_worktree_by_suffix(list, arg)))
 214                return wt;
 215
 216        arg = prefix_filename(prefix, strlen(prefix), arg);
 217        path = real_pathdup(arg, 1);
 218        for (; *list; list++)
 219                if (!fspathcmp(path, real_path((*list)->path)))
 220                        break;
 221        free(path);
 222        return *list;
 223}
 224
 225int is_main_worktree(const struct worktree *wt)
 226{
 227        return !wt->id;
 228}
 229
 230const char *is_worktree_locked(struct worktree *wt)
 231{
 232        assert(!is_main_worktree(wt));
 233
 234        if (!wt->lock_reason_valid) {
 235                struct strbuf path = STRBUF_INIT;
 236
 237                strbuf_addstr(&path, worktree_git_path(wt, "locked"));
 238                if (file_exists(path.buf)) {
 239                        struct strbuf lock_reason = STRBUF_INIT;
 240                        if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
 241                                die_errno(_("failed to read '%s'"), path.buf);
 242                        strbuf_trim(&lock_reason);
 243                        wt->lock_reason = strbuf_detach(&lock_reason, NULL);
 244                } else
 245                        wt->lock_reason = NULL;
 246                wt->lock_reason_valid = 1;
 247                strbuf_release(&path);
 248        }
 249
 250        return wt->lock_reason;
 251}
 252
 253int is_worktree_being_rebased(const struct worktree *wt,
 254                              const char *target)
 255{
 256        struct wt_status_state state;
 257        int found_rebase;
 258
 259        memset(&state, 0, sizeof(state));
 260        found_rebase = wt_status_check_rebase(wt, &state) &&
 261                ((state.rebase_in_progress ||
 262                  state.rebase_interactive_in_progress) &&
 263                 state.branch &&
 264                 starts_with(target, "refs/heads/") &&
 265                 !strcmp(state.branch, target + strlen("refs/heads/")));
 266        free(state.branch);
 267        free(state.onto);
 268        return found_rebase;
 269}
 270
 271int is_worktree_being_bisected(const struct worktree *wt,
 272                               const char *target)
 273{
 274        struct wt_status_state state;
 275        int found_rebase;
 276
 277        memset(&state, 0, sizeof(state));
 278        found_rebase = wt_status_check_bisect(wt, &state) &&
 279                state.branch &&
 280                starts_with(target, "refs/heads/") &&
 281                !strcmp(state.branch, target + strlen("refs/heads/"));
 282        free(state.branch);
 283        return found_rebase;
 284}
 285
 286/*
 287 * note: this function should be able to detect shared symref even if
 288 * HEAD is temporarily detached (e.g. in the middle of rebase or
 289 * bisect). New commands that do similar things should update this
 290 * function as well.
 291 */
 292const struct worktree *find_shared_symref(const char *symref,
 293                                          const char *target)
 294{
 295        const struct worktree *existing = NULL;
 296        static struct worktree **worktrees;
 297        int i = 0;
 298
 299        if (worktrees)
 300                free_worktrees(worktrees);
 301        worktrees = get_worktrees(0);
 302
 303        for (i = 0; worktrees[i]; i++) {
 304                struct worktree *wt = worktrees[i];
 305                const char *symref_target;
 306                unsigned char sha1[20];
 307                struct ref_store *refs;
 308                int flags;
 309
 310                if (wt->is_bare)
 311                        continue;
 312
 313                if (wt->is_detached && !strcmp(symref, "HEAD")) {
 314                        if (is_worktree_being_rebased(wt, target)) {
 315                                existing = wt;
 316                                break;
 317                        }
 318                        if (is_worktree_being_bisected(wt, target)) {
 319                                existing = wt;
 320                                break;
 321                        }
 322                }
 323
 324                refs = get_worktree_ref_store(wt);
 325                symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
 326                                                        sha1, &flags);
 327                if ((flags & REF_ISSYMREF) && !strcmp(symref_target, target)) {
 328                        existing = wt;
 329                        break;
 330                }
 331        }
 332
 333        return existing;
 334}
 335
 336int submodule_uses_worktrees(const char *path)
 337{
 338        char *submodule_gitdir;
 339        struct strbuf sb = STRBUF_INIT;
 340        DIR *dir;
 341        struct dirent *d;
 342        int ret = 0;
 343        struct repository_format format;
 344
 345        submodule_gitdir = git_pathdup_submodule(path, "%s", "");
 346        if (!submodule_gitdir)
 347                return 0;
 348
 349        /* The env would be set for the superproject. */
 350        get_common_dir_noenv(&sb, submodule_gitdir);
 351
 352        /*
 353         * The check below is only known to be good for repository format
 354         * version 0 at the time of writing this code.
 355         */
 356        strbuf_addstr(&sb, "/config");
 357        read_repository_format(&format, sb.buf);
 358        if (format.version != 0) {
 359                strbuf_release(&sb);
 360                return 1;
 361        }
 362
 363        /* Replace config by worktrees. */
 364        strbuf_setlen(&sb, sb.len - strlen("config"));
 365        strbuf_addstr(&sb, "worktrees");
 366
 367        /* See if there is any file inside the worktrees directory. */
 368        dir = opendir(sb.buf);
 369        strbuf_release(&sb);
 370        free(submodule_gitdir);
 371
 372        if (!dir)
 373                return 0;
 374
 375        while ((d = readdir(dir)) != NULL) {
 376                if (is_dot_or_dotdot(d->d_name))
 377                        continue;
 378
 379                ret = 1;
 380                break;
 381        }
 382        closedir(dir);
 383        return ret;
 384}