From b671eca7ade23d8f6691aab3421661fcf5567ffd Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 16 May 2021 11:06:44 -0700 Subject: [PATCH] cli: print file paths as relative and using OS directory separator --- lib/src/repo_path.rs | 14 +++++++++----- src/commands.rs | 32 +++++++++++++++++++++++++------- src/ui.rs | 23 +++++++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/lib/src/repo_path.rs b/lib/src/repo_path.rs index 7b99fed63..cfd897fc9 100644 --- a/lib/src/repo_path.rs +++ b/lib/src/repo_path.rs @@ -127,6 +127,14 @@ impl RepoPath { } } + pub fn to_fs_path(&self, base: &Path) -> PathBuf { + let mut result = base.to_owned(); + for dir in self.dir.components() { + result = result.join(&dir.value); + } + result.join(&self.basename.value) + } + pub fn is_root(&self) -> bool { self.dir.is_root() && self.basename.value.is_empty() } @@ -287,11 +295,7 @@ impl FileRepoPath { } pub fn to_fs_path(&self, base: &Path) -> PathBuf { - let mut result = base.to_owned(); - for dir in self.dir.components() { - result = result.join(&dir.value); - } - result.join(&self.basename.value) + self.to_repo_path().to_fs_path(base) } } diff --git a/src/commands.rs b/src/commands.rs index 358eb16e6..fb7b00bc5 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -21,6 +21,7 @@ use std::ffi::OsString; use std::fmt::Debug; use std::fs::OpenOptions; use std::io::{Read, Write}; +use std::path::Path; use std::process::Command; use std::sync::Arc; use std::time::Instant; @@ -824,7 +825,7 @@ fn cmd_files( let mut repo_command = command.repo_helper(ui)?; let commit = repo_command.resolve_revision_arg(sub_matches)?; for (name, _value) in commit.tree().entries() { - writeln!(ui, "{}", name.to_internal_string())?; + ui.write(&ui.format_file_path(repo_command.repo().working_copy_path(), &name))?; } Ok(()) } @@ -943,7 +944,7 @@ fn cmd_diff( } let repo = repo_command.repo(); if sub_matches.is_present("summary") { - show_diff_summary(ui, &from_tree, &to_tree)?; + show_diff_summary(ui, repo.working_copy_path(), &from_tree, &to_tree)?; } else { let mut styler = ui.styler(); styler.add_label(String::from("diff"))?; @@ -1084,16 +1085,28 @@ fn cmd_diff( Ok(()) } -fn show_diff_summary(ui: &mut Ui, from: &Tree, to: &Tree) -> io::Result<()> { +fn show_diff_summary(ui: &mut Ui, wc_path: &Path, from: &Tree, to: &Tree) -> io::Result<()> { let summary = from.diff_summary(&to); for file in summary.modified { - writeln!(ui, "M {}", file.to_internal_string())?; + writeln!( + ui, + "M {}", + ui.format_file_path(wc_path, &file.to_repo_path()) + )?; } for file in summary.added { - writeln!(ui, "A {}", file.to_internal_string())?; + writeln!( + ui, + "A {}", + ui.format_file_path(wc_path, &file.to_repo_path()) + )?; } for file in summary.removed { - writeln!(ui, "R {}", file.to_internal_string())?; + writeln!( + ui, + "R {}", + ui.format_file_path(wc_path, &file.to_repo_path()) + )?; } Ok(()) } @@ -1113,7 +1126,12 @@ fn cmd_status( ui.write_commit_summary(repo.as_repo_ref(), &commit.parents()[0])?; ui.write("\n")?; ui.write("Diff summary:\n")?; - show_diff_summary(ui, &commit.parents()[0].tree(), &commit.tree())?; + show_diff_summary( + ui, + repo.working_copy_path(), + &commit.parents()[0].tree(), + &commit.tree(), + )?; Ok(()) } diff --git a/src/ui.rs b/src/ui.rs index c18fc2166..d989804b2 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -19,6 +19,7 @@ use std::{fmt, io}; use jujutsu_lib::commit::Commit; use jujutsu_lib::repo::RepoRef; +use jujutsu_lib::repo_path::RepoPath; use jujutsu_lib::settings::UserSettings; use crate::styler::{ColorStyler, PlainTextStyler, Styler}; @@ -100,4 +101,26 @@ impl<'a> Ui<'a> { template_writer.format(commit)?; Ok(()) } + + pub fn format_file_path(&self, wc_path: &Path, file: &RepoPath) -> String { + relative_path(&self.cwd, &file.to_fs_path(wc_path)) + .to_str() + .unwrap() + .to_owned() + } +} + +fn relative_path(mut from: &Path, to: &Path) -> PathBuf { + let mut result = PathBuf::from(""); + loop { + if let Ok(suffix) = to.strip_prefix(from) { + return result.join(suffix); + } + if let Some(parent) = from.parent() { + result = result.join(".."); + from = parent; + } else { + return to.to_owned(); + } + } }