From b6f8bc8ff87dbdeca666fe7fc84705e228b1d79f Mon Sep 17 00:00:00 2001 From: Jonah Caplan Date: Fri, 15 Oct 2021 23:53:39 -0400 Subject: [PATCH] also handle -X and -x with no_strip --- src/exec/job.rs | 18 +++++++++++++++++- src/exec/mod.rs | 4 +--- src/walk.rs | 5 +++-- tests/tests.rs | 11 +++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/exec/job.rs b/src/exec/job.rs index 83abf1a..19c8bb0 100644 --- a/src/exec/job.rs +++ b/src/exec/job.rs @@ -5,6 +5,8 @@ use std::sync::{Arc, Mutex}; use crate::error::print_error; use crate::exit_codes::{merge_exitcodes, ExitCode}; use crate::walk::WorkerResult; +use crate::options::Options; +use crate::filesystem::strip_current_dir; use super::CommandTemplate; @@ -17,6 +19,7 @@ pub fn job( out_perm: Arc>, show_filesystem_errors: bool, buffer_output: bool, + config: &Arc, ) -> ExitCode { let mut results: Vec = Vec::new(); loop { @@ -36,10 +39,15 @@ pub fn job( Err(_) => break, }; + let path = if config.no_strip { + value + } else { + strip_current_dir(&value).to_path_buf() + }; // Drop the lock so that other threads can read from the receiver. drop(lock); // Generate a command, execute it and store its exit code. - results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), buffer_output)) + results.push(cmd.generate_and_execute(&path, Arc::clone(&out_perm), buffer_output)) } // Returns error in case of any error. merge_exitcodes(results) @@ -50,6 +58,7 @@ pub fn batch( cmd: &CommandTemplate, show_filesystem_errors: bool, buffer_output: bool, + config: &Arc, ) -> ExitCode { let paths = rx.iter().filter_map(|value| match value { WorkerResult::Entry(val) => Some(val), @@ -59,6 +68,13 @@ pub fn batch( } None } + }) + .map(|m| { + if config.no_strip { + m + } else { + strip_current_dir(&m).to_path_buf() + } }); cmd.generate_and_execute_batch(paths, buffer_output) } diff --git a/src/exec/mod.rs b/src/exec/mod.rs index a364e86..ad3b4c5 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -14,7 +14,6 @@ use lazy_static::lazy_static; use regex::Regex; use crate::exit_codes::ExitCode; -use crate::filesystem::strip_current_dir; use self::command::execute_command; use self::input::{basename, dirname, remove_extension}; @@ -145,7 +144,6 @@ impl CommandTemplate { out_perm: Arc>, buffer_output: bool, ) -> ExitCode { - let input = strip_current_dir(input); let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref())); for arg in &self.args[1..] { @@ -178,7 +176,7 @@ impl CommandTemplate { // A single `Tokens` is expected // So we can directly consume the iterator once and for all for path in &mut paths { - cmd.arg(arg.generate(strip_current_dir(path), self.path_separator.as_deref())); + cmd.arg(arg.generate(path, self.path_separator.as_deref())); has_path = true; } } else { diff --git a/src/walk.rs b/src/walk.rs index 7850ad7..a94ff3a 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -179,7 +179,7 @@ fn spawn_receiver( // This will be set to `Some` if the `--exec` argument was supplied. if let Some(ref cmd) = config.command { if cmd.in_batch_mode() { - exec::batch(rx, cmd, show_filesystem_errors, enable_output_buffering) + exec::batch(rx, cmd, show_filesystem_errors, enable_output_buffering, &config) } else { let shared_rx = Arc::new(Mutex::new(rx)); @@ -191,7 +191,7 @@ fn spawn_receiver( let rx = Arc::clone(&shared_rx); let cmd = Arc::clone(cmd); let out_perm = Arc::clone(&out_perm); - + let config = Arc::clone(&config); // Spawn a job thread that will listen for and execute inputs. let handle = thread::spawn(move || { exec::job( @@ -200,6 +200,7 @@ fn spawn_receiver( out_perm, show_filesystem_errors, enable_output_buffering, + &config, ) }); diff --git a/tests/tests.rs b/tests/tests.rs index d873f84..9d5087e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1935,4 +1935,15 @@ fn test_no_strip() { ./one/two/three/directory_foo ./symlink", ); + + te.assert_output( + &["c.foo", "./", "-x", "echo"], + "./one/two/c.foo + ./one/two/C.Foo2", + ); + + te.assert_output( + &["c.foo", "./", "-X", "echo"], + "./one/two/C.Foo2 ./one/two/c.foo" + ) }