Skip to content

Commit

Permalink
Fix repo root display in convert (#100)
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
9999years authored Nov 18, 2024
1 parent c204a0b commit 0fce93d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
26 changes: 22 additions & 4 deletions src/git/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utf8PathBuf> {
pub fn repo_root_display(&self) -> miette::Result<Utf8PathBuf> {
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: {}",
Expand Down

0 comments on commit 0fce93d

Please sign in to comment.