cli: print file paths as relative and using OS directory separator

This commit is contained in:
Martin von Zweigbergk 2021-05-16 11:06:44 -07:00
parent 4d51cd96a1
commit b671eca7ad
3 changed files with 57 additions and 12 deletions

View file

@ -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)
}
}

View file

@ -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(())
}

View file

@ -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();
}
}
}