also handle -X and -x with no_strip

This commit is contained in:
Jonah Caplan 2021-10-15 23:53:39 -04:00
parent 782fc278aa
commit b6f8bc8ff8
4 changed files with 32 additions and 6 deletions

View file

@ -5,6 +5,8 @@ use std::sync::{Arc, Mutex};
use crate::error::print_error; use crate::error::print_error;
use crate::exit_codes::{merge_exitcodes, ExitCode}; use crate::exit_codes::{merge_exitcodes, ExitCode};
use crate::walk::WorkerResult; use crate::walk::WorkerResult;
use crate::options::Options;
use crate::filesystem::strip_current_dir;
use super::CommandTemplate; use super::CommandTemplate;
@ -17,6 +19,7 @@ pub fn job(
out_perm: Arc<Mutex<()>>, out_perm: Arc<Mutex<()>>,
show_filesystem_errors: bool, show_filesystem_errors: bool,
buffer_output: bool, buffer_output: bool,
config: &Arc<Options>,
) -> ExitCode { ) -> ExitCode {
let mut results: Vec<ExitCode> = Vec::new(); let mut results: Vec<ExitCode> = Vec::new();
loop { loop {
@ -36,10 +39,15 @@ pub fn job(
Err(_) => break, 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 the lock so that other threads can read from the receiver.
drop(lock); drop(lock);
// Generate a command, execute it and store its exit code. // 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. // Returns error in case of any error.
merge_exitcodes(results) merge_exitcodes(results)
@ -50,6 +58,7 @@ pub fn batch(
cmd: &CommandTemplate, cmd: &CommandTemplate,
show_filesystem_errors: bool, show_filesystem_errors: bool,
buffer_output: bool, buffer_output: bool,
config: &Arc<Options>,
) -> ExitCode { ) -> ExitCode {
let paths = rx.iter().filter_map(|value| match value { let paths = rx.iter().filter_map(|value| match value {
WorkerResult::Entry(val) => Some(val), WorkerResult::Entry(val) => Some(val),
@ -59,6 +68,13 @@ pub fn batch(
} }
None None
} }
})
.map(|m| {
if config.no_strip {
m
} else {
strip_current_dir(&m).to_path_buf()
}
}); });
cmd.generate_and_execute_batch(paths, buffer_output) cmd.generate_and_execute_batch(paths, buffer_output)
} }

View file

@ -14,7 +14,6 @@ use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use crate::exit_codes::ExitCode; use crate::exit_codes::ExitCode;
use crate::filesystem::strip_current_dir;
use self::command::execute_command; use self::command::execute_command;
use self::input::{basename, dirname, remove_extension}; use self::input::{basename, dirname, remove_extension};
@ -145,7 +144,6 @@ impl CommandTemplate {
out_perm: Arc<Mutex<()>>, out_perm: Arc<Mutex<()>>,
buffer_output: bool, buffer_output: bool,
) -> ExitCode { ) -> ExitCode {
let input = strip_current_dir(input);
let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref())); let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref()));
for arg in &self.args[1..] { for arg in &self.args[1..] {
@ -178,7 +176,7 @@ impl CommandTemplate {
// A single `Tokens` is expected // A single `Tokens` is expected
// So we can directly consume the iterator once and for all // So we can directly consume the iterator once and for all
for path in &mut paths { 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; has_path = true;
} }
} else { } else {

View file

@ -179,7 +179,7 @@ fn spawn_receiver(
// This will be set to `Some` if the `--exec` argument was supplied. // This will be set to `Some` if the `--exec` argument was supplied.
if let Some(ref cmd) = config.command { if let Some(ref cmd) = config.command {
if cmd.in_batch_mode() { 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 { } else {
let shared_rx = Arc::new(Mutex::new(rx)); let shared_rx = Arc::new(Mutex::new(rx));
@ -191,7 +191,7 @@ fn spawn_receiver(
let rx = Arc::clone(&shared_rx); let rx = Arc::clone(&shared_rx);
let cmd = Arc::clone(cmd); let cmd = Arc::clone(cmd);
let out_perm = Arc::clone(&out_perm); let out_perm = Arc::clone(&out_perm);
let config = Arc::clone(&config);
// Spawn a job thread that will listen for and execute inputs. // Spawn a job thread that will listen for and execute inputs.
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
exec::job( exec::job(
@ -200,6 +200,7 @@ fn spawn_receiver(
out_perm, out_perm,
show_filesystem_errors, show_filesystem_errors,
enable_output_buffering, enable_output_buffering,
&config,
) )
}); });

View file

@ -1935,4 +1935,15 @@ fn test_no_strip() {
./one/two/three/directory_foo ./one/two/three/directory_foo
./symlink", ./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"
)
} }