Skip to content

Commit

Permalink
Replace util::without_prefix with Path::strip_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Feb 3, 2019
1 parent 129e762 commit 092c88c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
let mut list: Vec<_> = src
.list_files(pkg)?
.iter()
.map(|file| util::without_prefix(file, root).unwrap().to_path_buf())
.map(|file| file.strip_prefix(root).unwrap().to_path_buf())
.collect();
if include_lockfile(pkg) {
list.push("Cargo.lock".into());
Expand Down Expand Up @@ -266,7 +266,7 @@ fn check_vcs_file_collision(pkg: &Package, src_files: &[PathBuf]) -> CargoResult
let vcs_info_path = Path::new(VCS_INFO_FILE);
let collision = src_files
.iter()
.find(|&p| util::without_prefix(&p, root).unwrap() == vcs_info_path);
.find(|&p| p.strip_prefix(root).unwrap() == vcs_info_path);
if collision.is_some() {
failure::bail!(
"Invalid inclusion of reserved file name \
Expand Down Expand Up @@ -296,7 +296,7 @@ fn tar(
let config = ws.config();
let root = pkg.root();
for file in src_files.iter() {
let relative = util::without_prefix(file, root).unwrap();
let relative = file.strip_prefix(root)?;
check_filename(relative)?;
let relative = relative.to_str().ok_or_else(|| {
failure::format_err!("non-utf8 path in source directory: {}", relative.display())
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

use crate::core::{nightly_features_allowed, TargetKind, Workspace};
use crate::ops;
use crate::util::{self, CargoResult, ProcessError};
use crate::util::{CargoResult, ProcessError};

pub fn run(
ws: &Workspace<'_>,
Expand Down Expand Up @@ -81,10 +81,10 @@ pub fn run(
let compile = ops::compile(ws, options)?;
assert_eq!(compile.binaries.len(), 1);
let exe = &compile.binaries[0];
let exe = match util::without_prefix(exe, config.cwd()) {
Some(path) if path.file_name() == Some(path.as_os_str()) => Path::new(".").join(path),
Some(path) => path.to_path_buf(),
None => exe.to_path_buf(),
let exe = match exe.strip_prefix(config.cwd()) {
Ok(path) if path.file_name() == Some(path.as_os_str()) => Path::new(".").join(path),
Ok(path) => path.to_path_buf(),
Err(_) => exe.to_path_buf(),
};
let pkg = bins[0].0;
let mut process = compile.target_process(exe, pkg)?;
Expand Down
11 changes: 4 additions & 7 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::compiler::{Compilation, Doctest};
use crate::core::Workspace;
use crate::ops;
use crate::util::errors::CargoResult;
use crate::util::{self, CargoTestError, ProcessError, Test};
use crate::util::{CargoTestError, ProcessError, Test};

pub struct TestOptions<'a> {
pub compile_opts: ops::CompileOptions<'a>,
Expand Down Expand Up @@ -81,18 +81,15 @@ fn run_unit_tests(
let mut errors = Vec::new();

for &(ref pkg, ref kind, ref test, ref exe) in &compilation.tests {
let to_display = match util::without_prefix(exe, cwd) {
Some(path) => path,
None => &**exe,
};
let exe_display = exe.strip_prefix(cwd).unwrap_or(exe).display();
let mut cmd = compilation.target_process(exe, pkg)?;
cmd.args(test_args);
config
.shell()
.concise(|shell| shell.status("Running", to_display.display().to_string()))?;
.concise(|shell| shell.status("Running", &exe_display))?;
config
.shell()
.verbose(|shell| shell.status("Running", cmd.to_string()))?;
.verbose(|shell| shell.status("Running", &cmd))?;

let result = cmd.exec();

Expand Down
10 changes: 5 additions & 5 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::{Dependency, Package, PackageId, Source, SourceId, Summary};
use crate::ops;
use crate::util::paths;
use crate::util::Config;
use crate::util::{self, internal, CargoResult};
use crate::util::{internal, CargoResult};

pub struct PathSource<'cfg> {
source_id: SourceId,
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<'cfg> PathSource<'cfg> {
// matching to paths

let mut filter = |path: &Path| -> CargoResult<bool> {
let relative_path = util::without_prefix(path, root).unwrap();
let relative_path = path.strip_prefix(root)?;
let glob_should_package = glob_should_package(relative_path);
let ignore_should_package = ignore_should_package(relative_path)?;

Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'cfg> PathSource<'cfg> {
Ok(index) => index,
Err(err) => return Some(Err(err.into())),
};
let path = util::without_prefix(root, cur).unwrap().join("Cargo.toml");
let path = root.strip_prefix(cur).unwrap().join("Cargo.toml");
if index.get_path(&path, 0).is_some() {
return Some(self.list_files_git(pkg, &repo, filter));
}
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'cfg> PathSource<'cfg> {
});
let mut opts = git2::StatusOptions::new();
opts.include_untracked(true);
if let Some(suffix) = util::without_prefix(pkg_path, root) {
if let Ok(suffix) = pkg_path.strip_prefix(root) {
opts.pathspec(suffix);
}
let statuses = repo.statuses(Some(&mut opts))?;
Expand Down Expand Up @@ -376,7 +376,7 @@ impl<'cfg> PathSource<'cfg> {

if is_dir.unwrap_or_else(|| file_path.is_dir()) {
warn!(" found submodule {}", file_path.display());
let rel = util::without_prefix(&file_path, root).unwrap();
let rel = file_path.strip_prefix(root)?;
let rel = rel.to_str().ok_or_else(|| {
failure::format_err!("invalid utf-8 filename: {}", rel.display())
})?;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::lev_distance::lev_distance;
pub use self::lockserver::{LockServer, LockServerClient, LockServerStarted};
pub use self::paths::{bytes2path, dylib_path, join_paths, path2bytes};
pub use self::paths::{dylib_path_envvar, normalize_path, without_prefix};
pub use self::paths::{dylib_path_envvar, normalize_path};
pub use self::process_builder::{process, ProcessBuilder};
pub use self::progress::{Progress, ProgressStyle};
pub use self::read2::read2;
Expand Down
14 changes: 0 additions & 14 deletions src/cargo/util/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,6 @@ pub fn normalize_path(path: &Path) -> PathBuf {
ret
}

pub fn without_prefix<'a>(long_path: &'a Path, prefix: &'a Path) -> Option<&'a Path> {
let mut a = long_path.components();
let mut b = prefix.components();
loop {
match b.next() {
Some(y) => match a.next() {
Some(x) if x == y => continue,
_ => return None,
},
None => return Some(a.as_path()),
}
}
}

pub fn resolve_executable(exec: &Path) -> CargoResult<PathBuf> {
if exec.components().count() == 1 {
let paths = env::var_os("PATH").ok_or_else(|| failure::format_err!("no PATH"))?;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn do_read_manifest(

let toml = {
let pretty_filename =
util::without_prefix(manifest_file, config.cwd()).unwrap_or(manifest_file);
manifest_file.strip_prefix(config.cwd()).unwrap_or(manifest_file);
parse(contents, pretty_filename, config)?
};

Expand Down

0 comments on commit 092c88c

Please sign in to comment.