diff options
| author | Caleb White <cdwhite3@pm.me> | 2024-11-29 22:22:47 +0000 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2024-12-02 09:36:17 +0900 |
| commit | 4dac9e3c01cf056edd315e0ed26e6df1c4d94571 (patch) | |
| tree | 090dffaff7ae9cd4aca2f4ec64d48b855f704690 /worktree.c | |
| parent | 5976310916458868b5cbd9d8c7cc7de5af418230 (diff) | |
| download | git-4dac9e3c01cf056edd315e0ed26e6df1c4d94571.tar.xz | |
worktree: add `write_worktree_linking_files()` function
A new helper function, `write_worktree_linking_files()`, centralizes
the logic for computing and writing either relative or absolute
paths, based on the provided configuration. This function accepts
`strbuf` pointers to both the worktree’s `.git` link and the
repository’s `gitdir`, and then writes the appropriate path to each.
The `relativeWorktrees` extension is automatically set when a worktree
is linked with relative paths.
Signed-off-by: Caleb White <cdwhite3@pm.me>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
| -rw-r--r-- | worktree.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c index afde5394c8..cf05045cc9 100644 --- a/worktree.c +++ b/worktree.c @@ -1032,3 +1032,38 @@ cleanup: free(main_worktree_file); return res; } + +void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir, + int use_relative_paths) +{ + struct strbuf path = STRBUF_INIT; + struct strbuf repo = STRBUF_INIT; + struct strbuf tmp = STRBUF_INIT; + + strbuf_addbuf(&path, &dotgit); + strbuf_strip_suffix(&path, "/.git"); + strbuf_realpath(&path, path.buf, 1); + strbuf_addbuf(&repo, &gitdir); + strbuf_strip_suffix(&repo, "/gitdir"); + strbuf_realpath(&repo, repo.buf, 1); + + if (use_relative_paths && !the_repository->repository_format_relative_worktrees) { + if (upgrade_repository_format(1) < 0) + die(_("unable to upgrade repository format to support relative worktrees")); + if (git_config_set_gently("extensions.relativeWorktrees", "true")) + die(_("unable to set extensions.relativeWorktrees setting")); + the_repository->repository_format_relative_worktrees = 1; + } + + if (use_relative_paths) { + write_file(gitdir.buf, "%s/.git", relative_path(path.buf, repo.buf, &tmp)); + write_file(dotgit.buf, "gitdir: %s", relative_path(repo.buf, path.buf, &tmp)); + } else { + write_file(gitdir.buf, "%s/.git", path.buf); + write_file(dotgit.buf, "gitdir: %s", repo.buf); + } + + strbuf_release(&path); + strbuf_release(&repo); + strbuf_release(&tmp); +} |
