Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve path display #50

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ clap_mangen = { version = "0.2.20", optional = true }
command-error = { version = "0.4.0", features = [ "tracing" ] }
common-path = "1.0.0"
derive_more = { version = "1.0.0", features = ["as_ref", "constructor", "deref", "deref_mut", "display", "from", "into"] }
dirs = "5.0.1"
fs-err = "2.11.0"
itertools = "0.13.0"
miette = { version = "7.2.0", default-features = false, features = ["fancy-no-backtrace"] }
Expand Down
33 changes: 22 additions & 11 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::borrow::Cow;
use std::fmt::Display;
use std::process::Command;

use camino::Utf8Path;
use camino::Utf8PathBuf;
use command_error::CommandExt;
use command_error::Utf8ProgramAndArgs;
Expand All @@ -17,16 +19,17 @@ use crate::format_bulleted_list::format_bulleted_list;
use crate::git::BranchRef;
use crate::git::Git;
use crate::git::LocalBranchRef;
use crate::normal_path::NormalPath;
use crate::AddWorktreeOpts;
use crate::PathDisplay;
use crate::Utf8Absolutize;

/// A plan for creating a new `git worktree`.
#[derive(Debug, Clone)]
pub struct WorktreePlan<'a> {
git: AppGit<'a>,
/// The directory to run commands from.
worktree: Utf8PathBuf,
destination: NormalPath,
destination: Utf8PathBuf,
branch: BranchStartPointPlan,
/// Relative paths to copy to the new worktree, if any.
copy_untracked: Vec<Utf8PathBuf>,
Expand Down Expand Up @@ -58,24 +61,31 @@ impl<'a> WorktreePlan<'a> {
git: &AppGit<'_>,
args: &AddArgs,
new_branch: &LocalBranchRef,
) -> miette::Result<NormalPath> {
) -> miette::Result<Utf8PathBuf> {
match &args.inner.name_or_path {
Some(name_or_path) => {
if name_or_path.contains('/') {
// Test case: `add_by_path`.
NormalPath::from_cwd(name_or_path)
Utf8Path::new(name_or_path)
.absolutize()
.map(Cow::into_owned)
} else {
// Test case: `add_by_name_new_local`.
NormalPath::from_cwd(
git.worktree()
.container()?
.tap_mut(|p| p.push(name_or_path)),
)
git.worktree()
.container()?
.tap_mut(|p| p.push(name_or_path))
.absolutize()
.map(Cow::into_owned)
}
}
// Test case: `add_branch_new_local`.
None => NormalPath::from_cwd(git.worktree().path_for(new_branch.branch_name())?),
None => git
.worktree()
.path_for(new_branch.branch_name())?
.absolutize()
.map(Cow::into_owned),
}
.into_diagnostic()
}

fn command(&self, git: &Git) -> Command {
Expand Down Expand Up @@ -159,7 +169,8 @@ impl Display for WorktreePlan<'_> {
write!(
f,
"Creating worktree in {} for {}",
self.destination, self.branch,
self.destination.display_path_cwd(),
self.branch,
)?;

if !self.copy_untracked.is_empty() {
Expand Down
55 changes: 31 additions & 24 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::borrow::Cow;
use std::fmt::Display;

use camino::Utf8PathBuf;
use miette::miette;
use miette::IntoDiagnostic;
use owo_colors::OwoColorize;
use owo_colors::Stream;
use rustc_hash::FxHashSet as HashSet;
Expand All @@ -13,11 +15,12 @@ use crate::format_bulleted_list_multiline;
use crate::fs;
use crate::git::BranchRef;
use crate::git::LocalBranchRef;
use crate::normal_path::NormalPath;
use crate::only_paths_in_parent_directory;
use crate::topological_sort::topological_sort;
use crate::utf8absolutize::Utf8Absolutize;
use crate::utf8tempdir::Utf8TempDir;
use crate::AddWorktreeOpts;
use crate::PathDisplay;
use crate::RenamedWorktree;
use crate::ResolveUniqueNameOpts;
use crate::Worktree;
Expand Down Expand Up @@ -64,11 +67,11 @@ impl Display for ConvertPlan<'_> {
write!(
f,
"Converting {} to a worktree repository{}.",
NormalPath::try_display_cwd(&self.repo),
self.repo.display_path_cwd(),
if self.repo == self.destination {
String::new()
} else {
format!(" at {}", NormalPath::try_display_cwd(&self.destination))
format!(" at {}", self.destination.display_path_cwd())
},
)?;

Expand All @@ -79,8 +82,8 @@ impl Display for ConvertPlan<'_> {
.map(|worktree| {
format!(
"{} -> {}",
NormalPath::try_display_cwd(&worktree.worktree.path),
NormalPath::try_display_cwd(worktree.destination(self)),
worktree.worktree.path.display_path_cwd(),
worktree.destination(self).display_path_cwd(),
)
})
.collect::<Vec<_>>();
Expand All @@ -97,8 +100,8 @@ impl Display for ConvertPlan<'_> {
.map(|worktree| {
format!(
"{} -> {}",
NormalPath::try_display_cwd(&worktree.worktree.path),
NormalPath::try_display_cwd(worktree.destination(self)),
worktree.worktree.path.display_path_cwd(),
worktree.destination(self).display_path_cwd(),
)
})
)
Expand All @@ -118,7 +121,7 @@ impl Display for ConvertPlan<'_> {
.start_point
.qualified_branch_name()
.if_supports_color(Stream::Stdout, |text| text.cyan()),
NormalPath::try_display_cwd(worktree.destination(self)),
worktree.destination(self).display_path_cwd(),
)
}))
)?;
Expand All @@ -138,8 +141,8 @@ impl Display for ConvertPlan<'_> {
format_bulleted_list_multiline([
format!(
"{} -> {}",
NormalPath::try_display_cwd(main_plan.git_dir()),
NormalPath::try_display_cwd(main_plan.git_destination(self)),
main_plan.git_dir().display_path_cwd(),
main_plan.git_destination(self).display_path_cwd(),
)
])
)?;
Expand Down Expand Up @@ -279,7 +282,7 @@ impl<'a> ConvertPlan<'a> {
tracing::debug!(
"Worktree names resolved:\n{}",
format_bulleted_list(worktrees.iter().map(|(path, worktree)| {
format!("{} → {}", NormalPath::try_display_cwd(path), &worktree.name)
format!("{} → {}", path.display_path_cwd(), &worktree.name)
}))
);

Expand Down Expand Up @@ -324,26 +327,26 @@ impl<'a> ConvertPlan<'a> {
format_bulleted_list(ret.worktrees.iter().map(|plan| {
format!(
"{} → {} → {}",
NormalPath::try_display_cwd(&plan.worktree.path),
NormalPath::try_display_cwd(plan.temp_destination(&ret)),
NormalPath::try_display_cwd(plan.destination(&ret)),
plan.worktree.path.display_path_cwd(),
plan.temp_destination(&ret).display_path_cwd(),
plan.destination(&ret).display_path_cwd(),
)
}))
);

match &ret.make_bare {
Some(make_bare) => {
tracing::debug!(
git_dir=%NormalPath::try_display_cwd(make_bare.git_dir()),
temp_git_destination=%NormalPath::try_display_cwd(make_bare.temp_git_destination(&ret)),
git_destination=%NormalPath::try_display_cwd(make_bare.git_destination(&ret)),
worktree_temp_git_destination=%NormalPath::try_display_cwd(make_bare.worktree_temp_git_destination(&ret)),
worktree_git_destination=%NormalPath::try_display_cwd(make_bare.worktree_git_destination(&ret)),
git_dir=%make_bare.git_dir().display_path_cwd(),
temp_git_destination=%make_bare.temp_git_destination(&ret).display_path_cwd(),
git_destination=%make_bare.git_destination(&ret).display_path_cwd(),
worktree_temp_git_destination=%make_bare.worktree_temp_git_destination(&ret).display_path_cwd(),
worktree_git_destination=%make_bare.worktree_git_destination(&ret).display_path_cwd(),
worktree_plan=%format!(
"{} → {} → {}",
NormalPath::try_display_cwd(&make_bare.inner.worktree.path),
NormalPath::try_display_cwd(make_bare.inner.temp_destination(&ret)),
NormalPath::try_display_cwd(make_bare.inner.destination(&ret)),
make_bare.inner.worktree.path.display_path_cwd(),
make_bare.inner.temp_destination(&ret).display_path_cwd(),
make_bare.inner.destination(&ret).display_path_cwd(),
),
"Plan for converting to a bare repository determined",
);
Expand All @@ -363,7 +366,11 @@ impl<'a> ConvertPlan<'a> {
) -> miette::Result<Utf8PathBuf> {
if let Some(destination) = &opts.destination {
// `convert_destination_explicit`
return Ok(NormalPath::from_cwd(destination.clone())?.into());
return destination
.clone()
.absolutize()
.map(Cow::into_owned)
.into_diagnostic();
}

let main = worktrees.main();
Expand Down Expand Up @@ -513,7 +520,7 @@ impl<'a> ConvertPlan<'a> {

tracing::info!(
"{} has been converted to a worktree checkout",
NormalPath::from_cwd(&self.destination)?
self.destination.display_path_cwd()
);
tracing::info!("You may need to `cd .` to refresh your shell");

Expand Down
4 changes: 2 additions & 2 deletions src/git/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miette::IntoDiagnostic;
use tracing::instrument;
use utf8_command::Utf8Output;

use crate::NormalPath;
use crate::PathDisplay;

use super::Git;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl<'a> GitPath<'a> {
} else {
Err(miette!(
"Path is not in a working tree or a bare repository: {}",
NormalPath::try_display_cwd(self.0.get_directory())
self.0.get_directory().display_path_cwd()
))
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/git/worktree/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::parse::till_null;
use crate::CommitHash;
use crate::Git;
use crate::LocalBranchRef;
use crate::NormalPath;
use crate::PathDisplay;
use crate::Ref;
use crate::ResolvedCommitish;

Expand Down Expand Up @@ -256,12 +256,7 @@ pub struct Worktree {

impl Display for Worktree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {}",
NormalPath::try_display_cwd(&self.path),
self.head
)?;
write!(f, "{} {}", self.path.display_path_cwd(), self.head)?;

if self.is_main {
write!(
Expand Down
Loading
Loading