Don't assume paths are valid UTF-8

pull/210/head
Wilfred Hughes 2022-04-26 22:33:42 +07:00
parent b5b6e15b5e
commit 1b90fcd6aa
2 changed files with 20 additions and 17 deletions

@ -16,6 +16,10 @@ Fixed crash in inline mode.
Difftastic now diffs files in parallel when diffing whole directories,
increasing performance.
## Command Line Interface
Fixed handling of paths that aren't valid UTF-8.
## 0.27 (released 18th April 2022)
### Parsing

@ -1,4 +1,4 @@
use std::{borrow::Borrow, env};
use std::{borrow::Borrow, env, ffi::OsString};
use atty::Stream;
use clap::{crate_authors, crate_description, crate_version, Arg, Command};
@ -148,8 +148,8 @@ pub enum Mode {
display_width: usize,
display_path: String,
language_override: Option<guess_language::Language>,
lhs_path: String,
rhs_path: String,
lhs_path: OsString,
rhs_path: OsString,
},
DumpTreeSitter {
path: String,
@ -194,23 +194,22 @@ pub fn parse_args() -> Mode {
};
}
// TODO: Use OsString rather than forcing paths to be valid UTF-8.
let args: Vec<_> = matches.values_of_lossy("paths").unwrap_or_default();
let args: Vec<_> = matches.values_of_os("paths").unwrap_or_default().collect();
info!("CLI arguments: {:?}", args);
// TODO: document these different ways of calling difftastic.
let (display_path, lhs_path, rhs_path) = match &args[..] {
[lhs_path, rhs_path] => (
rhs_path.to_string(),
lhs_path.to_string(),
rhs_path.to_string(),
rhs_path.to_owned(),
lhs_path.to_owned(),
rhs_path.to_owned(),
),
[display_path, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode] => {
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITEXTERNALDIFFcode
(
display_path.to_string(),
lhs_tmp_file.to_string(),
rhs_tmp_file.to_string(),
display_path.to_owned(),
lhs_tmp_file.to_owned(),
rhs_tmp_file.to_owned(),
)
}
[_old_name, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode, new_name, _similarity] =>
@ -219,9 +218,9 @@ pub fn parse_args() -> Mode {
// TODO: mention old name as well as diffing.
// TODO: where does git document these 9 arguments?
(
new_name.to_string(),
lhs_tmp_file.to_string(),
rhs_tmp_file.to_string(),
new_name.to_owned(),
lhs_tmp_file.to_owned(),
rhs_tmp_file.to_owned(),
)
}
_ => {
@ -305,10 +304,10 @@ pub fn parse_args() -> Mode {
background_color,
color_output,
display_width,
display_path,
display_path: display_path.to_string_lossy().to_string(),
language_override,
lhs_path,
rhs_path,
lhs_path: lhs_path.to_owned(),
rhs_path: rhs_path.to_owned(),
}
}