Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "strbuf.h" |
| 4 | #include "worktree.h" |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 5 | #include "dir.h" |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 6 | #include "wt-status.h" |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 7 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 8 | void free_worktrees(struct worktree **worktrees) |
| 9 | { |
| 10 | int i = 0; |
| 11 | |
| 12 | for (i = 0; worktrees[i]; i++) { |
| 13 | free(worktrees[i]->path); |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 14 | free(worktrees[i]->id); |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 15 | free(worktrees[i]->head_ref); |
Nguyễn Thái Ngọc Duy | 346ef53 | 2016-06-13 19:18:23 +0700 | [diff] [blame] | 16 | free(worktrees[i]->lock_reason); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 17 | free(worktrees[i]); |
| 18 | } |
| 19 | free (worktrees); |
| 20 | } |
| 21 | |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 22 | /* |
| 23 | * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL, |
Li Peng | 832c0e5 | 2016-05-06 20:36:46 +0800 | [diff] [blame] | 24 | * set is_detached to 1 (0) if the ref is detached (is not detached). |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 25 | * |
| 26 | * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so |
| 27 | * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses |
| 28 | * git_path). Parse the ref ourselves. |
| 29 | * |
| 30 | * return -1 if the ref is not a proper ref, 0 otherwise (success) |
| 31 | */ |
| 32 | static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached) |
| 33 | { |
| 34 | if (is_detached) |
| 35 | *is_detached = 0; |
| 36 | if (!strbuf_readlink(ref, path_to_ref, 0)) { |
| 37 | /* HEAD is symbolic link */ |
| 38 | if (!starts_with(ref->buf, "refs/") || |
| 39 | check_refname_format(ref->buf, 0)) |
| 40 | return -1; |
| 41 | } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) { |
| 42 | /* textual symref or detached */ |
| 43 | if (!starts_with(ref->buf, "ref:")) { |
| 44 | if (is_detached) |
| 45 | *is_detached = 1; |
| 46 | } else { |
| 47 | strbuf_remove(ref, 0, strlen("ref:")); |
| 48 | strbuf_trim(ref); |
| 49 | if (check_refname_format(ref->buf, 0)) |
| 50 | return -1; |
| 51 | } |
| 52 | } else |
| 53 | return -1; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 57 | /** |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 58 | * Add the head_sha1 and head_ref (if not detached) to the given worktree |
| 59 | */ |
| 60 | static void add_head_info(struct strbuf *head_ref, struct worktree *worktree) |
| 61 | { |
| 62 | if (head_ref->len) { |
| 63 | if (worktree->is_detached) { |
| 64 | get_sha1_hex(head_ref->buf, worktree->head_sha1); |
| 65 | } else { |
| 66 | resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL); |
| 67 | worktree->head_ref = strbuf_detach(head_ref, NULL); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 73 | * get the main worktree |
| 74 | */ |
| 75 | static struct worktree *get_main_worktree(void) |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 76 | { |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 77 | struct worktree *worktree = NULL; |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 78 | struct strbuf path = STRBUF_INIT; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 79 | struct strbuf worktree_path = STRBUF_INIT; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 80 | struct strbuf head_ref = STRBUF_INIT; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 81 | int is_bare = 0; |
| 82 | int is_detached = 0; |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 83 | |
René Scharfe | fd2e7da | 2016-07-09 17:43:59 +0200 | [diff] [blame] | 84 | strbuf_add_absolute_path(&worktree_path, get_git_common_dir()); |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 85 | is_bare = !strbuf_strip_suffix(&worktree_path, "/.git"); |
| 86 | if (is_bare) |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 87 | strbuf_strip_suffix(&worktree_path, "/."); |
| 88 | |
| 89 | strbuf_addf(&path, "%s/HEAD", get_git_common_dir()); |
| 90 | |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 91 | if (parse_ref(path.buf, &head_ref, &is_detached) < 0) |
| 92 | goto done; |
| 93 | |
| 94 | worktree = xmalloc(sizeof(struct worktree)); |
| 95 | worktree->path = strbuf_detach(&worktree_path, NULL); |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 96 | worktree->id = NULL; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 97 | worktree->is_bare = is_bare; |
| 98 | worktree->head_ref = NULL; |
| 99 | worktree->is_detached = is_detached; |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 100 | worktree->is_current = 0; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 101 | add_head_info(&head_ref, worktree); |
Nguyễn Thái Ngọc Duy | 346ef53 | 2016-06-13 19:18:23 +0700 | [diff] [blame] | 102 | worktree->lock_reason = NULL; |
| 103 | worktree->lock_reason_valid = 0; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 104 | |
| 105 | done: |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 106 | strbuf_release(&path); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 107 | strbuf_release(&worktree_path); |
| 108 | strbuf_release(&head_ref); |
| 109 | return worktree; |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 112 | static struct worktree *get_linked_worktree(const char *id) |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 113 | { |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 114 | struct worktree *worktree = NULL; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 115 | struct strbuf path = STRBUF_INIT; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 116 | struct strbuf worktree_path = STRBUF_INIT; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 117 | struct strbuf head_ref = STRBUF_INIT; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 118 | int is_detached = 0; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 119 | |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 120 | if (!id) |
| 121 | die("Missing linked worktree name"); |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 122 | |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 123 | strbuf_git_common_path(&path, "worktrees/%s/gitdir", id); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 124 | if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) |
| 125 | /* invalid gitdir file */ |
| 126 | goto done; |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 127 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 128 | strbuf_rtrim(&worktree_path); |
| 129 | if (!strbuf_strip_suffix(&worktree_path, "/.git")) { |
| 130 | strbuf_reset(&worktree_path); |
René Scharfe | fd2e7da | 2016-07-09 17:43:59 +0200 | [diff] [blame] | 131 | strbuf_add_absolute_path(&worktree_path, "."); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 132 | strbuf_strip_suffix(&worktree_path, "/."); |
| 133 | } |
| 134 | |
Michael Rappazzo | 1ceb7f9 | 2015-10-08 13:01:02 -0400 | [diff] [blame] | 135 | strbuf_reset(&path); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 136 | strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id); |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 137 | |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 138 | if (parse_ref(path.buf, &head_ref, &is_detached) < 0) |
| 139 | goto done; |
| 140 | |
| 141 | worktree = xmalloc(sizeof(struct worktree)); |
| 142 | worktree->path = strbuf_detach(&worktree_path, NULL); |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 143 | worktree->id = xstrdup(id); |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 144 | worktree->is_bare = 0; |
| 145 | worktree->head_ref = NULL; |
| 146 | worktree->is_detached = is_detached; |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 147 | worktree->is_current = 0; |
Michael Rappazzo | 92718b7 | 2015-10-08 13:01:04 -0400 | [diff] [blame] | 148 | add_head_info(&head_ref, worktree); |
Nguyễn Thái Ngọc Duy | 346ef53 | 2016-06-13 19:18:23 +0700 | [diff] [blame] | 149 | worktree->lock_reason = NULL; |
| 150 | worktree->lock_reason_valid = 0; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 151 | |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 152 | done: |
| 153 | strbuf_release(&path); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 154 | strbuf_release(&worktree_path); |
| 155 | strbuf_release(&head_ref); |
| 156 | return worktree; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 159 | static void mark_current_worktree(struct worktree **worktrees) |
| 160 | { |
Nguyễn Thái Ngọc Duy | 360af2d | 2016-05-22 16:33:52 +0700 | [diff] [blame] | 161 | char *git_dir = xstrdup(absolute_path(get_git_dir())); |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 162 | int i; |
| 163 | |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 164 | for (i = 0; worktrees[i]; i++) { |
| 165 | struct worktree *wt = worktrees[i]; |
Nguyễn Thái Ngọc Duy | 360af2d | 2016-05-22 16:33:52 +0700 | [diff] [blame] | 166 | const char *wt_git_dir = get_worktree_git_dir(wt); |
| 167 | |
| 168 | if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) { |
| 169 | wt->is_current = 1; |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 170 | break; |
Nguyễn Thái Ngọc Duy | 360af2d | 2016-05-22 16:33:52 +0700 | [diff] [blame] | 171 | } |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 172 | } |
Nguyễn Thái Ngọc Duy | 360af2d | 2016-05-22 16:33:52 +0700 | [diff] [blame] | 173 | free(git_dir); |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 174 | } |
| 175 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 176 | struct worktree **get_worktrees(void) |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 177 | { |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 178 | struct worktree **list = NULL; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 179 | struct strbuf path = STRBUF_INIT; |
| 180 | DIR *dir; |
| 181 | struct dirent *d; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 182 | int counter = 0, alloc = 2; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 183 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 184 | list = xmalloc(alloc * sizeof(struct worktree *)); |
| 185 | |
| 186 | if ((list[counter] = get_main_worktree())) |
| 187 | counter++; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 188 | |
| 189 | strbuf_addf(&path, "%s/worktrees", get_git_common_dir()); |
| 190 | dir = opendir(path.buf); |
| 191 | strbuf_release(&path); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 192 | if (dir) { |
| 193 | while ((d = readdir(dir)) != NULL) { |
| 194 | struct worktree *linked = NULL; |
Nguyễn Thái Ngọc Duy | afb9e30 | 2016-05-22 16:33:54 +0700 | [diff] [blame] | 195 | if (is_dot_or_dotdot(d->d_name)) |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 196 | continue; |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 197 | |
Nguyễn Thái Ngọc Duy | d4cddd6 | 2016-01-18 18:21:29 +0700 | [diff] [blame] | 198 | if ((linked = get_linked_worktree(d->d_name))) { |
| 199 | ALLOC_GROW(list, counter + 1, alloc); |
| 200 | list[counter++] = linked; |
| 201 | } |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 202 | } |
| 203 | closedir(dir); |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 204 | } |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 205 | ALLOC_GROW(list, counter + 1, alloc); |
| 206 | list[counter] = NULL; |
Nguyễn Thái Ngọc Duy | 750e8a6 | 2016-04-22 20:01:28 +0700 | [diff] [blame] | 207 | |
| 208 | mark_current_worktree(list); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 209 | return list; |
| 210 | } |
| 211 | |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 212 | const char *get_worktree_git_dir(const struct worktree *wt) |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 213 | { |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 214 | if (!wt) |
| 215 | return get_git_dir(); |
| 216 | else if (!wt->id) |
| 217 | return get_git_common_dir(); |
| 218 | else |
| 219 | return git_common_path("worktrees/%s", wt->id); |
| 220 | } |
| 221 | |
Nguyễn Thái Ngọc Duy | 080739b | 2016-06-13 19:18:26 +0700 | [diff] [blame] | 222 | static struct worktree *find_worktree_by_suffix(struct worktree **list, |
| 223 | const char *suffix) |
| 224 | { |
| 225 | struct worktree *found = NULL; |
| 226 | int nr_found = 0, suffixlen; |
| 227 | |
| 228 | suffixlen = strlen(suffix); |
| 229 | if (!suffixlen) |
| 230 | return NULL; |
| 231 | |
| 232 | for (; *list && nr_found < 2; list++) { |
| 233 | const char *path = (*list)->path; |
| 234 | int pathlen = strlen(path); |
| 235 | int start = pathlen - suffixlen; |
| 236 | |
| 237 | /* suffix must start at directory boundary */ |
| 238 | if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && |
| 239 | !fspathcmp(suffix, path + start)) { |
| 240 | found = *list; |
| 241 | nr_found++; |
| 242 | } |
| 243 | } |
| 244 | return nr_found == 1 ? found : NULL; |
| 245 | } |
| 246 | |
Nguyễn Thái Ngọc Duy | 6835314 | 2016-06-03 19:19:39 +0700 | [diff] [blame] | 247 | struct worktree *find_worktree(struct worktree **list, |
| 248 | const char *prefix, |
| 249 | const char *arg) |
| 250 | { |
Nguyễn Thái Ngọc Duy | 080739b | 2016-06-13 19:18:26 +0700 | [diff] [blame] | 251 | struct worktree *wt; |
Nguyễn Thái Ngọc Duy | 6835314 | 2016-06-03 19:19:39 +0700 | [diff] [blame] | 252 | char *path; |
| 253 | |
Nguyễn Thái Ngọc Duy | 080739b | 2016-06-13 19:18:26 +0700 | [diff] [blame] | 254 | if ((wt = find_worktree_by_suffix(list, arg))) |
| 255 | return wt; |
| 256 | |
Nguyễn Thái Ngọc Duy | 6835314 | 2016-06-03 19:19:39 +0700 | [diff] [blame] | 257 | arg = prefix_filename(prefix, strlen(prefix), arg); |
| 258 | path = xstrdup(real_path(arg)); |
| 259 | for (; *list; list++) |
| 260 | if (!fspathcmp(path, real_path((*list)->path))) |
| 261 | break; |
| 262 | free(path); |
| 263 | return *list; |
| 264 | } |
| 265 | |
Nguyễn Thái Ngọc Duy | 984ad9e | 2016-06-03 19:19:40 +0700 | [diff] [blame] | 266 | int is_main_worktree(const struct worktree *wt) |
| 267 | { |
| 268 | return !wt->id; |
| 269 | } |
| 270 | |
Nguyễn Thái Ngọc Duy | 346ef53 | 2016-06-13 19:18:23 +0700 | [diff] [blame] | 271 | const char *is_worktree_locked(struct worktree *wt) |
| 272 | { |
| 273 | assert(!is_main_worktree(wt)); |
| 274 | |
| 275 | if (!wt->lock_reason_valid) { |
| 276 | struct strbuf path = STRBUF_INIT; |
| 277 | |
| 278 | strbuf_addstr(&path, worktree_git_path(wt, "locked")); |
| 279 | if (file_exists(path.buf)) { |
| 280 | struct strbuf lock_reason = STRBUF_INIT; |
| 281 | if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) |
| 282 | die_errno(_("failed to read '%s'"), path.buf); |
| 283 | strbuf_trim(&lock_reason); |
| 284 | wt->lock_reason = strbuf_detach(&lock_reason, NULL); |
| 285 | } else |
| 286 | wt->lock_reason = NULL; |
| 287 | wt->lock_reason_valid = 1; |
| 288 | strbuf_release(&path); |
| 289 | } |
| 290 | |
| 291 | return wt->lock_reason; |
| 292 | } |
| 293 | |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 294 | int is_worktree_being_rebased(const struct worktree *wt, |
| 295 | const char *target) |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 296 | { |
| 297 | struct wt_status_state state; |
| 298 | int found_rebase; |
| 299 | |
| 300 | memset(&state, 0, sizeof(state)); |
| 301 | found_rebase = wt_status_check_rebase(wt, &state) && |
| 302 | ((state.rebase_in_progress || |
| 303 | state.rebase_interactive_in_progress) && |
| 304 | state.branch && |
| 305 | starts_with(target, "refs/heads/") && |
| 306 | !strcmp(state.branch, target + strlen("refs/heads/"))); |
| 307 | free(state.branch); |
| 308 | free(state.onto); |
| 309 | return found_rebase; |
| 310 | } |
| 311 | |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 312 | int is_worktree_being_bisected(const struct worktree *wt, |
| 313 | const char *target) |
Nguyễn Thái Ngọc Duy | 04a3dfb | 2016-04-22 20:01:35 +0700 | [diff] [blame] | 314 | { |
| 315 | struct wt_status_state state; |
| 316 | int found_rebase; |
| 317 | |
| 318 | memset(&state, 0, sizeof(state)); |
| 319 | found_rebase = wt_status_check_bisect(wt, &state) && |
| 320 | state.branch && |
| 321 | starts_with(target, "refs/heads/") && |
| 322 | !strcmp(state.branch, target + strlen("refs/heads/")); |
| 323 | free(state.branch); |
| 324 | return found_rebase; |
| 325 | } |
| 326 | |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 327 | /* |
| 328 | * note: this function should be able to detect shared symref even if |
| 329 | * HEAD is temporarily detached (e.g. in the middle of rebase or |
| 330 | * bisect). New commands that do similar things should update this |
| 331 | * function as well. |
| 332 | */ |
Nguyễn Thái Ngọc Duy | d3b9ac0 | 2016-04-22 20:01:27 +0700 | [diff] [blame] | 333 | const struct worktree *find_shared_symref(const char *symref, |
| 334 | const char *target) |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 335 | { |
Nguyễn Thái Ngọc Duy | d3b9ac0 | 2016-04-22 20:01:27 +0700 | [diff] [blame] | 336 | const struct worktree *existing = NULL; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 337 | struct strbuf path = STRBUF_INIT; |
| 338 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | d3b9ac0 | 2016-04-22 20:01:27 +0700 | [diff] [blame] | 339 | static struct worktree **worktrees; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 340 | int i = 0; |
| 341 | |
Nguyễn Thái Ngọc Duy | d3b9ac0 | 2016-04-22 20:01:27 +0700 | [diff] [blame] | 342 | if (worktrees) |
| 343 | free_worktrees(worktrees); |
| 344 | worktrees = get_worktrees(); |
| 345 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 346 | for (i = 0; worktrees[i]; i++) { |
Nguyễn Thái Ngọc Duy | c871714 | 2016-04-22 20:01:32 +0700 | [diff] [blame] | 347 | struct worktree *wt = worktrees[i]; |
Dennis Kaarsemaker | 171c646 | 2016-10-12 18:41:07 +0200 | [diff] [blame] | 348 | if (wt->is_bare) |
| 349 | continue; |
Nguyễn Thái Ngọc Duy | c871714 | 2016-04-22 20:01:32 +0700 | [diff] [blame] | 350 | |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 351 | if (wt->is_detached && !strcmp(symref, "HEAD")) { |
| 352 | if (is_worktree_being_rebased(wt, target)) { |
| 353 | existing = wt; |
| 354 | break; |
| 355 | } |
Nguyễn Thái Ngọc Duy | 04a3dfb | 2016-04-22 20:01:35 +0700 | [diff] [blame] | 356 | if (is_worktree_being_bisected(wt, target)) { |
| 357 | existing = wt; |
| 358 | break; |
| 359 | } |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 360 | } |
| 361 | |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 362 | strbuf_reset(&path); |
| 363 | strbuf_reset(&sb); |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 364 | strbuf_addf(&path, "%s/%s", |
Nguyễn Thái Ngọc Duy | c871714 | 2016-04-22 20:01:32 +0700 | [diff] [blame] | 365 | get_worktree_git_dir(wt), |
Nguyễn Thái Ngọc Duy | 69dfe3b | 2016-04-22 20:01:26 +0700 | [diff] [blame] | 366 | symref); |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 367 | |
| 368 | if (parse_ref(path.buf, &sb, NULL)) { |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if (!strcmp(sb.buf, target)) { |
Nguyễn Thái Ngọc Duy | c871714 | 2016-04-22 20:01:32 +0700 | [diff] [blame] | 373 | existing = wt; |
Michael Rappazzo | 5193490 | 2015-10-08 13:01:03 -0400 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | strbuf_release(&path); |
| 379 | strbuf_release(&sb); |
Michael Rappazzo | ac6c561 | 2015-10-02 07:55:31 -0400 | [diff] [blame] | 380 | |
| 381 | return existing; |
| 382 | } |