/worktree.c

https://gitlab.com/181gaming/git · C · 303 lines · 237 code · 41 blank · 25 comment · 38 complexity · d0114c22c2498ddcc91903a2ed973ec8 MD5 · raw file

  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. void free_worktrees(struct worktree **worktrees)
  8. {
  9. int i = 0;
  10. for (i = 0; worktrees[i]; i++) {
  11. free(worktrees[i]->path);
  12. free(worktrees[i]->id);
  13. free(worktrees[i]->head_ref);
  14. free(worktrees[i]);
  15. }
  16. free (worktrees);
  17. }
  18. /*
  19. * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
  20. * set is_detached to 1 (0) if the ref is detached (is not detached).
  21. *
  22. * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
  23. * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
  24. * git_path). Parse the ref ourselves.
  25. *
  26. * return -1 if the ref is not a proper ref, 0 otherwise (success)
  27. */
  28. static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
  29. {
  30. if (is_detached)
  31. *is_detached = 0;
  32. if (!strbuf_readlink(ref, path_to_ref, 0)) {
  33. /* HEAD is symbolic link */
  34. if (!starts_with(ref->buf, "refs/") ||
  35. check_refname_format(ref->buf, 0))
  36. return -1;
  37. } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
  38. /* textual symref or detached */
  39. if (!starts_with(ref->buf, "ref:")) {
  40. if (is_detached)
  41. *is_detached = 1;
  42. } else {
  43. strbuf_remove(ref, 0, strlen("ref:"));
  44. strbuf_trim(ref);
  45. if (check_refname_format(ref->buf, 0))
  46. return -1;
  47. }
  48. } else
  49. return -1;
  50. return 0;
  51. }
  52. /**
  53. * Add the head_sha1 and head_ref (if not detached) to the given worktree
  54. */
  55. static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
  56. {
  57. if (head_ref->len) {
  58. if (worktree->is_detached) {
  59. get_sha1_hex(head_ref->buf, worktree->head_sha1);
  60. } else {
  61. resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
  62. worktree->head_ref = strbuf_detach(head_ref, NULL);
  63. }
  64. }
  65. }
  66. /**
  67. * get the main worktree
  68. */
  69. static struct worktree *get_main_worktree(void)
  70. {
  71. struct worktree *worktree = NULL;
  72. struct strbuf path = STRBUF_INIT;
  73. struct strbuf worktree_path = STRBUF_INIT;
  74. struct strbuf head_ref = STRBUF_INIT;
  75. int is_bare = 0;
  76. int is_detached = 0;
  77. strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
  78. is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
  79. if (is_bare)
  80. strbuf_strip_suffix(&worktree_path, "/.");
  81. strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
  82. if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
  83. goto done;
  84. worktree = xmalloc(sizeof(struct worktree));
  85. worktree->path = strbuf_detach(&worktree_path, NULL);
  86. worktree->id = NULL;
  87. worktree->is_bare = is_bare;
  88. worktree->head_ref = NULL;
  89. worktree->is_detached = is_detached;
  90. worktree->is_current = 0;
  91. add_head_info(&head_ref, worktree);
  92. done:
  93. strbuf_release(&path);
  94. strbuf_release(&worktree_path);
  95. strbuf_release(&head_ref);
  96. return worktree;
  97. }
  98. static struct worktree *get_linked_worktree(const char *id)
  99. {
  100. struct worktree *worktree = NULL;
  101. struct strbuf path = STRBUF_INIT;
  102. struct strbuf worktree_path = STRBUF_INIT;
  103. struct strbuf head_ref = STRBUF_INIT;
  104. int is_detached = 0;
  105. if (!id)
  106. die("Missing linked worktree name");
  107. strbuf_git_common_path(&path, "worktrees/%s/gitdir", id);
  108. if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
  109. /* invalid gitdir file */
  110. goto done;
  111. strbuf_rtrim(&worktree_path);
  112. if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
  113. strbuf_reset(&worktree_path);
  114. strbuf_add_absolute_path(&worktree_path, ".");
  115. strbuf_strip_suffix(&worktree_path, "/.");
  116. }
  117. strbuf_reset(&path);
  118. strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
  119. if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
  120. goto done;
  121. worktree = xmalloc(sizeof(struct worktree));
  122. worktree->path = strbuf_detach(&worktree_path, NULL);
  123. worktree->id = xstrdup(id);
  124. worktree->is_bare = 0;
  125. worktree->head_ref = NULL;
  126. worktree->is_detached = is_detached;
  127. worktree->is_current = 0;
  128. add_head_info(&head_ref, worktree);
  129. done:
  130. strbuf_release(&path);
  131. strbuf_release(&worktree_path);
  132. strbuf_release(&head_ref);
  133. return worktree;
  134. }
  135. static void mark_current_worktree(struct worktree **worktrees)
  136. {
  137. char *git_dir = xstrdup(absolute_path(get_git_dir()));
  138. int i;
  139. for (i = 0; worktrees[i]; i++) {
  140. struct worktree *wt = worktrees[i];
  141. const char *wt_git_dir = get_worktree_git_dir(wt);
  142. if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
  143. wt->is_current = 1;
  144. break;
  145. }
  146. }
  147. free(git_dir);
  148. }
  149. struct worktree **get_worktrees(void)
  150. {
  151. struct worktree **list = NULL;
  152. struct strbuf path = STRBUF_INIT;
  153. DIR *dir;
  154. struct dirent *d;
  155. int counter = 0, alloc = 2;
  156. list = xmalloc(alloc * sizeof(struct worktree *));
  157. if ((list[counter] = get_main_worktree()))
  158. counter++;
  159. strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
  160. dir = opendir(path.buf);
  161. strbuf_release(&path);
  162. if (dir) {
  163. while ((d = readdir(dir)) != NULL) {
  164. struct worktree *linked = NULL;
  165. if (is_dot_or_dotdot(d->d_name))
  166. continue;
  167. if ((linked = get_linked_worktree(d->d_name))) {
  168. ALLOC_GROW(list, counter + 1, alloc);
  169. list[counter++] = linked;
  170. }
  171. }
  172. closedir(dir);
  173. }
  174. ALLOC_GROW(list, counter + 1, alloc);
  175. list[counter] = NULL;
  176. mark_current_worktree(list);
  177. return list;
  178. }
  179. const char *get_worktree_git_dir(const struct worktree *wt)
  180. {
  181. if (!wt)
  182. return get_git_dir();
  183. else if (!wt->id)
  184. return get_git_common_dir();
  185. else
  186. return git_common_path("worktrees/%s", wt->id);
  187. }
  188. int is_worktree_being_rebased(const struct worktree *wt,
  189. const char *target)
  190. {
  191. struct wt_status_state state;
  192. int found_rebase;
  193. memset(&state, 0, sizeof(state));
  194. found_rebase = wt_status_check_rebase(wt, &state) &&
  195. ((state.rebase_in_progress ||
  196. state.rebase_interactive_in_progress) &&
  197. state.branch &&
  198. starts_with(target, "refs/heads/") &&
  199. !strcmp(state.branch, target + strlen("refs/heads/")));
  200. free(state.branch);
  201. free(state.onto);
  202. return found_rebase;
  203. }
  204. int is_worktree_being_bisected(const struct worktree *wt,
  205. const char *target)
  206. {
  207. struct wt_status_state state;
  208. int found_rebase;
  209. memset(&state, 0, sizeof(state));
  210. found_rebase = wt_status_check_bisect(wt, &state) &&
  211. state.branch &&
  212. starts_with(target, "refs/heads/") &&
  213. !strcmp(state.branch, target + strlen("refs/heads/"));
  214. free(state.branch);
  215. return found_rebase;
  216. }
  217. /*
  218. * note: this function should be able to detect shared symref even if
  219. * HEAD is temporarily detached (e.g. in the middle of rebase or
  220. * bisect). New commands that do similar things should update this
  221. * function as well.
  222. */
  223. const struct worktree *find_shared_symref(const char *symref,
  224. const char *target)
  225. {
  226. const struct worktree *existing = NULL;
  227. struct strbuf path = STRBUF_INIT;
  228. struct strbuf sb = STRBUF_INIT;
  229. static struct worktree **worktrees;
  230. int i = 0;
  231. if (worktrees)
  232. free_worktrees(worktrees);
  233. worktrees = get_worktrees();
  234. for (i = 0; worktrees[i]; i++) {
  235. struct worktree *wt = worktrees[i];
  236. if (wt->is_detached && !strcmp(symref, "HEAD")) {
  237. if (is_worktree_being_rebased(wt, target)) {
  238. existing = wt;
  239. break;
  240. }
  241. if (is_worktree_being_bisected(wt, target)) {
  242. existing = wt;
  243. break;
  244. }
  245. }
  246. strbuf_reset(&path);
  247. strbuf_reset(&sb);
  248. strbuf_addf(&path, "%s/%s",
  249. get_worktree_git_dir(wt),
  250. symref);
  251. if (parse_ref(path.buf, &sb, NULL)) {
  252. continue;
  253. }
  254. if (!strcmp(sb.buf, target)) {
  255. existing = wt;
  256. break;
  257. }
  258. }
  259. strbuf_release(&path);
  260. strbuf_release(&sb);
  261. return existing;
  262. }