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::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<Mutex<()>>,
show_filesystem_errors: bool,
buffer_output: bool,
config: &Arc<Options>,
) -> ExitCode {
let mut results: Vec<ExitCode> = 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<Options>,
) -> 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)
}

View file

@ -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<Mutex<()>>,
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 {

View file

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

View file

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