Skip to content

stdin: Handle directory input #73

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -268,8 +268,16 @@ fn make_diff(

#[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()),
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()),
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(),
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -46,22 +46,24 @@ fn main() -> ExitCode {
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)
} 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}");
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)]
@@ -16,6 +23,7 @@ pub enum Format {
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,
@@ -29,6 +37,7 @@ impl Default for Params {
Self {
from: OsString::default(),
to: OsString::default(),
stdin_path: OsString::default(),
format: Format::default(),
context_count: 3,
report_identical_files: false,
@@ -178,10 +187,27 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
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();
if params.from == "-" {
stdin_path.push(to_path.file_name().unwrap());
} else {
stdin_path.push(from_path.file_name().unwrap());
}
params.stdin_path = stdin_path.into();
}
}

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();
}
12 changes: 10 additions & 2 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
@@ -239,8 +239,16 @@ fn make_diff(

#[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()),
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()),
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(),