From: Caleb White <cdwhite3@pm.me>
To: git@vger.kernel.org
Cc: Taylor Blau <me@ttaylorr.com>,
Phillip Wood <phillip.wood123@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
Eric Sunshine <sunshine@sunshineco.com>,
Caleb White <cdwhite3@pm.me>
Subject: [PATCH v3 4/8] worktree: add `write_worktree_linking_files()` function
Date: Thu, 31 Oct 2024 05:05:50 +0000 [thread overview]
Message-ID: <20241031-wt_relative_options-v3-4-3e44ccdf64e6@pm.me> (raw)
In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me>
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>
---
worktree.c | 36 ++++++++++++++++++++++++++++++++++++
worktree.h | 14 ++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/worktree.c b/worktree.c
index 9346d51c438cbd029e9e57591edd8c9f30cc7638..ab6f1e036fa019285a1a5152decb9aed5202909f 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1032,3 +1032,39 @@ int init_worktree_config(struct repository *r)
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);
+}
diff --git a/worktree.h b/worktree.h
index e96118621638667d87c5d7e0452ed10bd1ddf606..4786aa545ca1fa12bb16a55ddf5a59c06503b2c5 100644
--- a/worktree.h
+++ b/worktree.h
@@ -215,4 +215,18 @@ void strbuf_worktree_ref(const struct worktree *wt,
*/
int init_worktree_config(struct repository *r);
+/**
+ * Write the .git file and gitdir file that links the worktree to the repository.
+ *
+ * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
+ * is the path to the repository's `gitdir` file.
+ *
+ * Example
+ * dotgit: "/path/to/foo/.git"
+ * gitdir: "/path/to/repo/worktrees/foo/gitdir"
+ */
+void write_worktree_linking_files(struct strbuf dotgit,
+ struct strbuf gitdir,
+ int use_relative_paths);
+
#endif
--
2.47.0
next prev parent reply other threads:[~2024-10-31 5:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-31 5:05 [PATCH v3 0/8] Allow relative worktree linking to be configured by the user Caleb White
2024-10-31 5:05 ` [PATCH v3 1/8] setup: correctly reinitialize repository version Caleb White
2024-10-31 5:05 ` [PATCH v3 2/8] worktree: add `relativeWorktrees` extension Caleb White
2024-10-31 5:05 ` [PATCH v3 3/8] worktree: refactor infer_backlink return Caleb White
2024-10-31 5:05 ` Caleb White [this message]
2024-10-31 5:05 ` [PATCH v3 5/8] worktree: add relative cli/config options to `add` command Caleb White
2024-10-31 5:06 ` [PATCH v3 6/8] worktree: add relative cli/config options to `move` command Caleb White
2024-10-31 5:06 ` [PATCH v3 7/8] worktree: add relative cli/config options to `repair` command Caleb White
2024-10-31 5:06 ` [PATCH v3 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
2024-10-31 19:20 ` [PATCH v3 0/8] Allow relative worktree linking to be configured by the user Taylor Blau
2024-10-31 19:52 ` Caleb White
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241031-wt_relative_options-v3-4-3e44ccdf64e6@pm.me \
--to=cdwhite3@pm.me \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=me@ttaylorr.com \
--cc=phillip.wood123@gmail.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).