Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin: Handle directory input #73

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,16 @@

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
let from_modified_time =
match !params.stdin_path.is_empty() && params.from.to_string_lossy().starts_with('-') {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 273 in src/context_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/context_diff.rs#L273

Added line #L273 was not covered by tests
false => get_modification_time(&params.from.to_string_lossy()),
};
let to_modified_time =
match !params.stdin_path.is_empty() && params.to.to_string_lossy().starts_with('-') {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 278 in src/context_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/context_diff.rs#L278

Added line #L278 was not covered by tests
false => get_modification_time(&params.to.to_string_lossy()),
};
let mut output = format!(
"*** {0}\t{1}\n--- {2}\t{3}\n",
params.from.to_string_lossy(),
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@
return ExitCode::SUCCESS;
}
// read files
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
if filepath == "-" {
fn read_file_contents(filepath: &OsString, stdin_path: &OsString) -> io::Result<Vec<u8>> {
if filepath.to_string_lossy().starts_with('-') && !stdin_path.is_empty() {
fs::read(stdin_path)

Check warning on line 51 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L51

Added line #L51 was not covered by tests
} else if filepath == "-" {
let mut content = Vec::new();
io::stdin().read_to_end(&mut content).and(Ok(content))
} else {
fs::read(filepath)
}
}
let from_content = match read_file_contents(&params.from) {
let from_content = match read_file_contents(&params.from, &params.stdin_path) {
Ok(from_content) => from_content,
Err(e) => {
eprintln!("Failed to read from-file: {e}");
return ExitCode::from(2);
}
};
let to_content = match read_file_contents(&params.to) {
let to_content = match read_file_contents(&params.to, &params.stdin_path) {
Ok(to_content) => to_content,
Err(e) => {
eprintln!("Failed to read to-file: {e}");
Expand Down
30 changes: 28 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use std::ffi::OsString;
use std::path::PathBuf;

#[cfg(unix)]
use std::fs::{self, File};
#[cfg(unix)]
use std::io::stdin;
#[cfg(unix)]
use std::os::fd::AsFd;

use regex::Regex;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand All @@ -16,6 +23,7 @@
pub struct Params {
pub from: OsString,
pub to: OsString,
pub stdin_path: OsString,
pub format: Format,
pub context_count: usize,
pub report_identical_files: bool,
Expand All @@ -29,6 +37,7 @@
Self {
from: OsString::default(),
to: OsString::default(),
stdin_path: OsString::default(),
format: Format::default(),
context_count: 3,
report_identical_files: false,
Expand Down Expand Up @@ -178,10 +187,27 @@
let mut from_path: PathBuf = PathBuf::from(&params.from);
let mut to_path: PathBuf = PathBuf::from(&params.to);

if from_path.is_dir() && to_path.is_file() {
#[cfg(unix)]
{
// check if stdin is a directory
let fd = stdin().as_fd().try_clone_to_owned().unwrap();
let file = File::from(fd);
let meta = file.metadata().unwrap();
if meta.is_dir() {
let mut stdin_path = fs::canonicalize("/dev/stdin").unwrap();

Check warning on line 197 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L197

Added line #L197 was not covered by tests
if params.from == "-" {
stdin_path.push(to_path.file_name().unwrap());

Check warning on line 199 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L199

Added line #L199 was not covered by tests
} else {
stdin_path.push(from_path.file_name().unwrap());

Check warning on line 201 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L201

Added line #L201 was not covered by tests
}
params.stdin_path = stdin_path.into();

Check warning on line 203 in src/params.rs

View check run for this annotation

Codecov / codecov/patch

src/params.rs#L203

Added line #L203 was not covered by tests
}
}

if (from_path.is_dir() || !params.stdin_path.is_empty()) && to_path.is_file() {
from_path.push(to_path.file_name().unwrap());
params.from = from_path.into_os_string();
} else if from_path.is_file() && to_path.is_dir() {
} else if from_path.is_file() && (to_path.is_dir() || !params.stdin_path.is_empty()) {
to_path.push(from_path.file_name().unwrap());
params.to = to_path.into_os_string();
}
Expand Down
12 changes: 10 additions & 2 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,16 @@

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
let from_modified_time =
match !params.stdin_path.is_empty() && params.from.to_string_lossy().starts_with('-') {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 244 in src/unified_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/unified_diff.rs#L244

Added line #L244 was not covered by tests
false => get_modification_time(&params.from.to_string_lossy()),
};
let to_modified_time =
match !params.stdin_path.is_empty() && params.to.to_string_lossy().starts_with('-') {
true => get_modification_time(&params.stdin_path.to_string_lossy()),

Check warning on line 249 in src/unified_diff.rs

View check run for this annotation

Codecov / codecov/patch

src/unified_diff.rs#L249

Added line #L249 was not covered by tests
false => get_modification_time(&params.to.to_string_lossy()),
};
let mut output = format!(
"--- {0}\t{1}\n+++ {2}\t{3}\n",
params.from.to_string_lossy(),
Expand Down
Loading