THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

595 lines
14KB

  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "refs.h"
  4. #include "strbuf.h"
  5. #include "worktree.h"
  6. #include "dir.h"
  7. #include "wt-status.h"
  8. void free_worktrees(struct worktree **worktrees)
  9. {
  10. int i = 0;
  11. for (i = 0; worktrees[i]; i++) {
  12. free(worktrees[i]->path);
  13. free(worktrees[i]->id);
  14. free(worktrees[i]->head_ref);
  15. free(worktrees[i]->lock_reason);
  16. free(worktrees[i]);
  17. }
  18. free (worktrees);
  19. }
  20. /**
  21. * Update head_sha1, head_ref and is_detached of the given worktree
  22. */
  23. static void add_head_info(struct worktree *wt)
  24. {
  25. int flags;
  26. const char *target;
  27. target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
  28. "HEAD",
  29. 0,
  30. &wt->head_oid, &flags);
  31. if (!target)
  32. return;
  33. if (flags & REF_ISSYMREF)
  34. wt->head_ref = xstrdup(target);
  35. else
  36. wt->is_detached = 1;
  37. }
  38. /**
  39. * get the main worktree
  40. */
  41. static struct worktree *get_main_worktree(void)
  42. {
  43. struct worktree *worktree = NULL;
  44. struct strbuf path = STRBUF_INIT;
  45. struct strbuf worktree_path = STRBUF_INIT;
  46. strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
  47. if (!strbuf_strip_suffix(&worktree_path, "/.git"))
  48. strbuf_strip_suffix(&worktree_path, "/.");
  49. strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
  50. worktree = xcalloc(1, sizeof(*worktree));
  51. worktree->path = strbuf_detach(&worktree_path, NULL);
  52. /*
  53. * NEEDSWORK: If this function is called from a secondary worktree and
  54. * config.worktree is present, is_bare_repository_cfg will reflect the
  55. * contents of config.worktree, not the contents of the main worktree.
  56. * This means that worktree->is_bare may be set to 0 even if the main
  57. * worktree is configured to be bare.
  58. */
  59. worktree->is_bare = (is_bare_repository_cfg == 1) ||
  60. is_bare_repository();
  61. add_head_info(worktree);
  62. strbuf_release(&path);
  63. strbuf_release(&worktree_path);
  64. return worktree;
  65. }
  66. static struct worktree *get_linked_worktree(const char *id)
  67. {
  68. struct worktree *worktree = NULL;
  69. struct strbuf path = STRBUF_INIT;
  70. struct strbuf worktree_path = STRBUF_INIT;
  71. if (!id)
  72. die("Missing linked worktree name");
  73. strbuf_git_common_path(&path, the_repository, "worktrees/%s/gitdir", id);
  74. if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)
  75. /* invalid gitdir file */
  76. goto done;
  77. strbuf_rtrim(&worktree_path);
  78. if (!strbuf_strip_suffix(&worktree_path, "/.git")) {
  79. strbuf_reset(&worktree_path);
  80. strbuf_add_absolute_path(&worktree_path, ".");
  81. strbuf_strip_suffix(&worktree_path, "/.");
  82. }
  83. strbuf_reset(&path);
  84. strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
  85. worktree = xcalloc(1, sizeof(*worktree));
  86. worktree->path = strbuf_detach(&worktree_path, NULL);
  87. worktree->id = xstrdup(id);
  88. add_head_info(worktree);
  89. done:
  90. strbuf_release(&path);
  91. strbuf_release(&worktree_path);
  92. return worktree;
  93. }
  94. static void mark_current_worktree(struct worktree **worktrees)
  95. {
  96. char *git_dir = absolute_pathdup(get_git_dir());
  97. int i;
  98. for (i = 0; worktrees[i]; i++) {
  99. struct worktree *wt = worktrees[i];
  100. const char *wt_git_dir = get_worktree_git_dir(wt);
  101. if (!fspathcmp(git_dir, absolute_path(wt_git_dir))) {
  102. wt->is_current = 1;
  103. break;
  104. }
  105. }
  106. free(git_dir);
  107. }
  108. static int compare_worktree(const void *a_, const void *b_)
  109. {
  110. const struct worktree *const *a = a_;
  111. const struct worktree *const *b = b_;
  112. return fspathcmp((*a)->path, (*b)->path);
  113. }
  114. struct worktree **get_worktrees(unsigned flags)
  115. {
  116. struct worktree **list = NULL;
  117. struct strbuf path = STRBUF_INIT;
  118. DIR *dir;
  119. struct dirent *d;
  120. int counter = 0, alloc = 2;
  121. ALLOC_ARRAY(list, alloc);
  122. list[counter++] = get_main_worktree();
  123. strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
  124. dir = opendir(path.buf);
  125. strbuf_release(&path);
  126. if (dir) {
  127. while ((d = readdir(dir)) != NULL) {
  128. struct worktree *linked = NULL;
  129. if (is_dot_or_dotdot(d->d_name))
  130. continue;
  131. if ((linked = get_linked_worktree(d->d_name))) {
  132. ALLOC_GROW(list, counter + 1, alloc);
  133. list[counter++] = linked;
  134. }
  135. }
  136. closedir(dir);
  137. }
  138. ALLOC_GROW(list, counter + 1, alloc);
  139. list[counter] = NULL;
  140. if (flags & GWT_SORT_LINKED)
  141. /*
  142. * don't sort the first item (main worktree), which will
  143. * always be the first
  144. */
  145. QSORT(list + 1, counter - 1, compare_worktree);
  146. mark_current_worktree(list);
  147. return list;
  148. }
  149. const char *get_worktree_git_dir(const struct worktree *wt)
  150. {
  151. if (!wt)
  152. return get_git_dir();
  153. else if (!wt->id)
  154. return get_git_common_dir();
  155. else
  156. return git_common_path("worktrees/%s", wt->id);
  157. }
  158. static struct worktree *find_worktree_by_suffix(struct worktree **list,
  159. const char *suffix)
  160. {
  161. struct worktree *found = NULL;
  162. int nr_found = 0, suffixlen;
  163. suffixlen = strlen(suffix);
  164. if (!suffixlen)
  165. return NULL;
  166. for (; *list && nr_found < 2; list++) {
  167. const char *path = (*list)->path;
  168. int pathlen = strlen(path);
  169. int start = pathlen - suffixlen;
  170. /* suffix must start at directory boundary */
  171. if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) &&
  172. !fspathcmp(suffix, path + start)) {
  173. found = *list;
  174. nr_found++;
  175. }
  176. }
  177. return nr_found == 1 ? found : NULL;
  178. }
  179. struct worktree *find_worktree(struct worktree **list,
  180. const char *prefix,
  181. const char *arg)
  182. {
  183. struct worktree *wt;
  184. char *path;
  185. char *to_free = NULL;
  186. if ((wt = find_worktree_by_suffix(list, arg)))
  187. return wt;
  188. if (prefix)
  189. arg = to_free = prefix_filename(prefix, arg);
  190. path = real_pathdup(arg, 0);
  191. if (!path) {
  192. free(to_free);
  193. return NULL;
  194. }
  195. for (; *list; list++) {
  196. const char *wt_path = real_path_if_valid((*list)->path);
  197. if (wt_path && !fspathcmp(path, wt_path))
  198. break;
  199. }
  200. free(path);
  201. free(to_free);
  202. return *list;
  203. }
  204. int is_main_worktree(const struct worktree *wt)
  205. {
  206. return !wt->id;
  207. }
  208. const char *worktree_lock_reason(struct worktree *wt)
  209. {
  210. assert(!is_main_worktree(wt));
  211. if (!wt->lock_reason_valid) {
  212. struct strbuf path = STRBUF_INIT;
  213. strbuf_addstr(&path, worktree_git_path(wt, "locked"));
  214. if (file_exists(path.buf)) {
  215. struct strbuf lock_reason = STRBUF_INIT;
  216. if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
  217. die_errno(_("failed to read '%s'"), path.buf);
  218. strbuf_trim(&lock_reason);
  219. wt->lock_reason = strbuf_detach(&lock_reason, NULL);
  220. } else
  221. wt->lock_reason = NULL;
  222. wt->lock_reason_valid = 1;
  223. strbuf_release(&path);
  224. }
  225. return wt->lock_reason;
  226. }
  227. /* convenient wrapper to deal with NULL strbuf */
  228. static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
  229. {
  230. va_list params;
  231. if (!buf)
  232. return;
  233. va_start(params, fmt);
  234. strbuf_vaddf(buf, fmt, params);
  235. va_end(params);
  236. }
  237. int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
  238. unsigned flags)
  239. {
  240. struct strbuf wt_path = STRBUF_INIT;
  241. char *path = NULL;
  242. int err, ret = -1;
  243. strbuf_addf(&wt_path, "%s/.git", wt->path);
  244. if (is_main_worktree(wt)) {
  245. if (is_directory(wt_path.buf)) {
  246. ret = 0;
  247. goto done;
  248. }
  249. /*
  250. * Main worktree using .git file to point to the
  251. * repository would make it impossible to know where
  252. * the actual worktree is if this function is executed
  253. * from another worktree. No .git file support for now.
  254. */
  255. strbuf_addf_gently(errmsg,
  256. _("'%s' at main working tree is not the repository directory"),
  257. wt_path.buf);
  258. goto done;
  259. }
  260. /*
  261. * Make sure "gitdir" file points to a real .git file and that
  262. * file points back here.
  263. */
  264. if (!is_absolute_path(wt->path)) {
  265. strbuf_addf_gently(errmsg,
  266. _("'%s' file does not contain absolute path to the working tree location"),
  267. git_common_path("worktrees/%s/gitdir", wt->id));
  268. goto done;
  269. }
  270. if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
  271. !file_exists(wt->path)) {
  272. ret = 0;
  273. goto done;
  274. }
  275. if (!file_exists(wt_path.buf)) {
  276. strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
  277. goto done;
  278. }
  279. path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
  280. if (!path) {
  281. strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
  282. wt_path.buf, err);
  283. goto done;
  284. }
  285. ret = fspathcmp(path, real_path(git_common_path("worktrees/%s", wt->id)));
  286. if (ret)
  287. strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
  288. wt->path, git_common_path("worktrees/%s", wt->id));
  289. done:
  290. free(path);
  291. strbuf_release(&wt_path);
  292. return ret;
  293. }
  294. void update_worktree_location(struct worktree *wt, const char *path_)
  295. {
  296. struct strbuf path = STRBUF_INIT;
  297. if (is_main_worktree(wt))
  298. BUG("can't relocate main worktree");
  299. strbuf_realpath(&path, path_, 1);
  300. if (fspathcmp(wt->path, path.buf)) {
  301. write_file(git_common_path("worktrees/%s/gitdir", wt->id),
  302. "%s/.git", path.buf);
  303. free(wt->path);
  304. wt->path = strbuf_detach(&path, NULL);
  305. }
  306. strbuf_release(&path);
  307. }
  308. int is_worktree_being_rebased(const struct worktree *wt,
  309. const char *target)
  310. {
  311. struct wt_status_state state;
  312. int found_rebase;
  313. memset(&state, 0, sizeof(state));
  314. found_rebase = wt_status_check_rebase(wt, &state) &&
  315. ((state.rebase_in_progress ||
  316. state.rebase_interactive_in_progress) &&
  317. state.branch &&
  318. starts_with(target, "refs/heads/") &&
  319. !strcmp(state.branch, target + strlen("refs/heads/")));
  320. free(state.branch);
  321. free(state.onto);
  322. return found_rebase;
  323. }
  324. int is_worktree_being_bisected(const struct worktree *wt,
  325. const char *target)
  326. {
  327. struct wt_status_state state;
  328. int found_rebase;
  329. memset(&state, 0, sizeof(state));
  330. found_rebase = wt_status_check_bisect(wt, &state) &&
  331. state.branch &&
  332. starts_with(target, "refs/heads/") &&
  333. !strcmp(state.branch, target + strlen("refs/heads/"));
  334. free(state.branch);
  335. return found_rebase;
  336. }
  337. /*
  338. * note: this function should be able to detect shared symref even if
  339. * HEAD is temporarily detached (e.g. in the middle of rebase or
  340. * bisect). New commands that do similar things should update this
  341. * function as well.
  342. */
  343. const struct worktree *find_shared_symref(const char *symref,
  344. const char *target)
  345. {
  346. const struct worktree *existing = NULL;
  347. static struct worktree **worktrees;
  348. int i = 0;
  349. if (worktrees)
  350. free_worktrees(worktrees);
  351. worktrees = get_worktrees(0);
  352. for (i = 0; worktrees[i]; i++) {
  353. struct worktree *wt = worktrees[i];
  354. const char *symref_target;
  355. struct ref_store *refs;
  356. int flags;
  357. if (wt->is_bare)
  358. continue;
  359. if (wt->is_detached && !strcmp(symref, "HEAD")) {
  360. if (is_worktree_being_rebased(wt, target)) {
  361. existing = wt;
  362. break;
  363. }
  364. if (is_worktree_being_bisected(wt, target)) {
  365. existing = wt;
  366. break;
  367. }
  368. }
  369. refs = get_worktree_ref_store(wt);
  370. symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
  371. NULL, &flags);
  372. if ((flags & REF_ISSYMREF) &&
  373. symref_target && !strcmp(symref_target, target)) {
  374. existing = wt;
  375. break;
  376. }
  377. }
  378. return existing;
  379. }
  380. int submodule_uses_worktrees(const char *path)
  381. {
  382. char *submodule_gitdir;
  383. struct strbuf sb = STRBUF_INIT;
  384. DIR *dir;
  385. struct dirent *d;
  386. int ret = 0;
  387. struct repository_format format = REPOSITORY_FORMAT_INIT;
  388. submodule_gitdir = git_pathdup_submodule(path, "%s", "");
  389. if (!submodule_gitdir)
  390. return 0;
  391. /* The env would be set for the superproject. */
  392. get_common_dir_noenv(&sb, submodule_gitdir);
  393. free(submodule_gitdir);
  394. /*
  395. * The check below is only known to be good for repository format
  396. * version 0 at the time of writing this code.
  397. */
  398. strbuf_addstr(&sb, "/config");
  399. read_repository_format(&format, sb.buf);
  400. if (format.version != 0) {
  401. strbuf_release(&sb);
  402. clear_repository_format(&format);
  403. return 1;
  404. }
  405. clear_repository_format(&format);
  406. /* Replace config by worktrees. */
  407. strbuf_setlen(&sb, sb.len - strlen("config"));
  408. strbuf_addstr(&sb, "worktrees");
  409. /* See if there is any file inside the worktrees directory. */
  410. dir = opendir(sb.buf);
  411. strbuf_release(&sb);
  412. if (!dir)
  413. return 0;
  414. while ((d = readdir(dir)) != NULL) {
  415. if (is_dot_or_dotdot(d->d_name))
  416. continue;
  417. ret = 1;
  418. break;
  419. }
  420. closedir(dir);
  421. return ret;
  422. }
  423. int parse_worktree_ref(const char *worktree_ref, const char **name,
  424. int *name_length, const char **ref)
  425. {
  426. if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
  427. if (!*worktree_ref)
  428. return -1;
  429. if (name)
  430. *name = NULL;
  431. if (name_length)
  432. *name_length = 0;
  433. if (ref)
  434. *ref = worktree_ref;
  435. return 0;
  436. }
  437. if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
  438. const char *slash = strchr(worktree_ref, '/');
  439. if (!slash || slash == worktree_ref || !slash[1])
  440. return -1;
  441. if (name)
  442. *name = worktree_ref;
  443. if (name_length)
  444. *name_length = slash - worktree_ref;
  445. if (ref)
  446. *ref = slash + 1;
  447. return 0;
  448. }
  449. return -1;
  450. }
  451. void strbuf_worktree_ref(const struct worktree *wt,
  452. struct strbuf *sb,
  453. const char *refname)
  454. {
  455. switch (ref_type(refname)) {
  456. case REF_TYPE_PSEUDOREF:
  457. case REF_TYPE_PER_WORKTREE:
  458. if (wt && !wt->is_current) {
  459. if (is_main_worktree(wt))
  460. strbuf_addstr(sb, "main-worktree/");
  461. else
  462. strbuf_addf(sb, "worktrees/%s/", wt->id);
  463. }
  464. break;
  465. case REF_TYPE_MAIN_PSEUDOREF:
  466. case REF_TYPE_OTHER_PSEUDOREF:
  467. break;
  468. case REF_TYPE_NORMAL:
  469. /*
  470. * For shared refs, don't prefix worktrees/ or
  471. * main-worktree/. It's not necessary and
  472. * files-backend.c can't handle it anyway.
  473. */
  474. break;
  475. }
  476. strbuf_addstr(sb, refname);
  477. }
  478. const char *worktree_ref(const struct worktree *wt, const char *refname)
  479. {
  480. static struct strbuf sb = STRBUF_INIT;
  481. strbuf_reset(&sb);
  482. strbuf_worktree_ref(wt, &sb, refname);
  483. return sb.buf;
  484. }
  485. int other_head_refs(each_ref_fn fn, void *cb_data)
  486. {
  487. struct worktree **worktrees, **p;
  488. int ret = 0;
  489. worktrees = get_worktrees(0);
  490. for (p = worktrees; *p; p++) {
  491. struct worktree *wt = *p;
  492. struct object_id oid;
  493. int flag;
  494. if (wt->is_current)
  495. continue;
  496. if (!refs_read_ref_full(get_main_ref_store(the_repository),
  497. worktree_ref(wt, "HEAD"),
  498. RESOLVE_REF_READING,
  499. &oid, &flag))
  500. ret = fn(worktree_ref(wt, "HEAD"), &oid, flag, cb_data);
  501. if (ret)
  502. break;
  503. }
  504. free_worktrees(worktrees);
  505. return ret;
  506. }