Skip to content

Commit 0fce93d

Browse files
authored
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!
1 parent c204a0b commit 0fce93d

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/convert.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ where
217217
// - `convert_bare_dot_git`
218218
// - `convert_bare_dot_git_from_parent`
219219
// - `convert_common_parent`
220-
let repo = git.path().repo_root_or_git_common_dir_if_bare()?;
221-
let repo = repo
222-
.parent()
223-
.ok_or_else(|| miette!("Repository path has no parent: {repo}"))?;
220+
let repo = git.path().repo_root_display()?;
224221
let worktrees = git.worktree().list()?;
225222

226223
let destination = Self::destination_plan(&worktrees, &opts)?;

src/git/path.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,32 @@ where
3232
Self(git)
3333
}
3434

35-
/// If in a working tree, get the repository root (`git rev-parse --show-toplevel`). If the
36-
/// repository is bare, get the `.git` directory (`git rev-parse --git-dir`). Otherwise, error.
35+
/// Get the path of the repository root, for display purposes only.
36+
///
37+
/// If in a working tree, get the repository root (`git rev-parse --show-toplevel`).
38+
///
39+
/// If the repository is bare, get the `.git` directory (`git rev-parse --git-dir`):
40+
/// - If it's named `.git`, get its parent.
41+
/// - Otherwise, return it directly.
42+
///
43+
/// Otherwise, error.
3744
#[instrument(level = "trace")]
38-
pub fn repo_root_or_git_common_dir_if_bare(&self) -> miette::Result<Utf8PathBuf> {
45+
pub fn repo_root_display(&self) -> miette::Result<Utf8PathBuf> {
3946
if self.0.worktree().is_inside()? {
4047
self.0.worktree().root()
4148
} else if self.0.config().is_bare()? {
42-
self.git_common_dir()
49+
let git_dir = self.git_common_dir()?;
50+
let git_dir_basename = git_dir
51+
.file_name()
52+
.ok_or_else(|| miette!("Git directory has no basename: {git_dir}"))?;
53+
if git_dir_basename == ".git" {
54+
Ok(git_dir
55+
.parent()
56+
.ok_or_else(|| miette!("Git directory has no parent: {git_dir}"))?
57+
.to_owned())
58+
} else {
59+
Ok(git_dir)
60+
}
4361
} else {
4462
Err(miette!(
4563
"Path is not in a working tree or a bare repository: {}",

0 commit comments

Comments
 (0)