From 0fce93de9bd85bf546c66c1b3ebd3cd1d9ef9c1b Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 18 Nov 2024 10:21:11 -0800 Subject: [PATCH] Fix repo root display in `convert` (#100) Previously, `git prole` would say things like `Converting ~/projects to a worktree repository at ~/projects/my-repo`, which is misleading. Now it will say `Converting ~/projects/my-repo to a worktree repository`. Thanks to @ktang-hg for the report! --- src/convert.rs | 5 +---- src/git/path.rs | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 0cfdee1..8e01208 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -217,10 +217,7 @@ where // - `convert_bare_dot_git` // - `convert_bare_dot_git_from_parent` // - `convert_common_parent` - let repo = git.path().repo_root_or_git_common_dir_if_bare()?; - let repo = repo - .parent() - .ok_or_else(|| miette!("Repository path has no parent: {repo}"))?; + let repo = git.path().repo_root_display()?; let worktrees = git.worktree().list()?; let destination = Self::destination_plan(&worktrees, &opts)?; diff --git a/src/git/path.rs b/src/git/path.rs index 28883f5..d37e950 100644 --- a/src/git/path.rs +++ b/src/git/path.rs @@ -32,14 +32,32 @@ where Self(git) } - /// If in a working tree, get the repository root (`git rev-parse --show-toplevel`). If the - /// repository is bare, get the `.git` directory (`git rev-parse --git-dir`). Otherwise, error. + /// Get the path of the repository root, for display purposes only. + /// + /// If in a working tree, get the repository root (`git rev-parse --show-toplevel`). + /// + /// If the repository is bare, get the `.git` directory (`git rev-parse --git-dir`): + /// - If it's named `.git`, get its parent. + /// - Otherwise, return it directly. + /// + /// Otherwise, error. #[instrument(level = "trace")] - pub fn repo_root_or_git_common_dir_if_bare(&self) -> miette::Result { + pub fn repo_root_display(&self) -> miette::Result { if self.0.worktree().is_inside()? { self.0.worktree().root() } else if self.0.config().is_bare()? { - self.git_common_dir() + let git_dir = self.git_common_dir()?; + let git_dir_basename = git_dir + .file_name() + .ok_or_else(|| miette!("Git directory has no basename: {git_dir}"))?; + if git_dir_basename == ".git" { + Ok(git_dir + .parent() + .ok_or_else(|| miette!("Git directory has no parent: {git_dir}"))? + .to_owned()) + } else { + Ok(git_dir) + } } else { Err(miette!( "Path is not in a working tree or a bare repository: {}",