diff options
| author | m0wer <m0wer@sgn.space> | 2026-03-29 16:45:52 +0200 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-03-30 16:29:23 +0000 |
| commit | 820fd706a24be7a58554a27e411e120cfa28d9a6 (patch) | |
| tree | 2b95e4f835122dabfcd1f72f637a12368a4c18f9 /test_utils/src | |
| parent | e3276e74bc45cb4fb8f158b8249bee3d12a0805f (diff) | |
feat: git worktree support
Git worktrees don't have a .git directory with a parent, so we need to
look for the git dir via git2's Repository::discover() and then look for
the cache database there. This allows the client to work correctly when
run from a worktree, and also allows the cache database to be shared
between the main repo and its worktrees (since they share the same git
dir and thus the same cache path).
Diffstat (limited to 'test_utils/src')
| -rw-r--r-- | test_utils/src/git.rs | 43 | ||||
| -rw-r--r-- | test_utils/src/lib.rs | 8 |
2 files changed, 49 insertions, 2 deletions
diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs index a18f81c..2668b3f 100644 --- a/test_utils/src/git.rs +++ b/test_utils/src/git.rs | |||
| @@ -276,6 +276,49 @@ impl GitTestRepo { | |||
| 276 | Ok(()) | 276 | Ok(()) |
| 277 | } | 277 | } |
| 278 | 278 | ||
| 279 | /// Creates a git worktree linked to this repository. | ||
| 280 | /// Returns a `GitTestRepo` whose `dir` points to the worktree working | ||
| 281 | /// directory. | ||
| 282 | pub fn create_worktree(&self, branch_name: &str) -> Result<GitTestRepo> { | ||
| 283 | let worktree_path = self | ||
| 284 | .dir | ||
| 285 | .parent() | ||
| 286 | .unwrap() | ||
| 287 | .join(format!("tmpgit-worktree-{}", rand::random::<u64>())); | ||
| 288 | |||
| 289 | // Create the branch at the current HEAD | ||
| 290 | let head_commit = self.git_repo.head()?.peel_to_commit()?; | ||
| 291 | self.git_repo | ||
| 292 | .branch(branch_name, &head_commit, false) | ||
| 293 | .context("failed to create branch for worktree")?; | ||
| 294 | |||
| 295 | // Add worktree via git2 | ||
| 296 | let worktree = self | ||
| 297 | .git_repo | ||
| 298 | .worktree( | ||
| 299 | branch_name, | ||
| 300 | &worktree_path, | ||
| 301 | Some( | ||
| 302 | git2::WorktreeAddOptions::new().reference(Some( | ||
| 303 | &self | ||
| 304 | .git_repo | ||
| 305 | .find_branch(branch_name, git2::BranchType::Local)? | ||
| 306 | .into_reference(), | ||
| 307 | )), | ||
| 308 | ), | ||
| 309 | ) | ||
| 310 | .context("failed to create worktree")?; | ||
| 311 | |||
| 312 | let worktree_repo = git2::Repository::open_from_worktree(&worktree) | ||
| 313 | .context("failed to open repo from worktree")?; | ||
| 314 | |||
| 315 | Ok(GitTestRepo { | ||
| 316 | dir: worktree_path, | ||
| 317 | git_repo: worktree_repo, | ||
| 318 | delete_dir_on_drop: true, | ||
| 319 | }) | ||
| 320 | } | ||
| 321 | |||
| 279 | pub fn checkout_remote_branch(&self, branch_name: &str) -> Result<Oid> { | 322 | pub fn checkout_remote_branch(&self, branch_name: &str) -> Result<Oid> { |
| 280 | self.checkout(&format!("remotes/origin/{branch_name}"))?; | 323 | self.checkout(&format!("remotes/origin/{branch_name}"))?; |
| 281 | let mut branch = self.create_branch(branch_name)?; | 324 | let mut branch = self.create_branch(branch_name)?; |
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index 48273e8..89cbaa9 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs | |||
| @@ -1208,8 +1208,12 @@ where | |||
| 1208 | 1208 | ||
| 1209 | /** copied from client.rs */ | 1209 | /** copied from client.rs */ |
| 1210 | async fn get_local_cache_database(git_repo_path: &Path) -> Result<NostrLMDB> { | 1210 | async fn get_local_cache_database(git_repo_path: &Path) -> Result<NostrLMDB> { |
| 1211 | NostrLMDB::open(git_repo_path.join(".git/nostr-cache.lmdb")) | 1211 | let git_dir = git2::Repository::discover(git_repo_path) |
| 1212 | .context("failed to open or create nostr cache database at .git/nostr-cache.lmdb") | 1212 | .context("failed to discover git repository")? |
| 1213 | .commondir() | ||
| 1214 | .to_path_buf(); | ||
| 1215 | NostrLMDB::open(git_dir.join("nostr-cache.lmdb")) | ||
| 1216 | .context("failed to open or create nostr cache database at <git-dir>/nostr-cache.lmdb") | ||
| 1213 | } | 1217 | } |
| 1214 | 1218 | ||
| 1215 | /** copied from client.rs */ | 1219 | /** copied from client.rs */ |