objdiff-cli diff: Accept any kind of unit path (#48)

* objdiff-cli diff: Accept any kind of unit path

* Appease clippy

* Call `resolve_paths` in slightly fewer cases
This commit is contained in:
Robin Avery 2024-03-01 20:18:27 -05:00 committed by GitHub
parent 3b1249e1ab
commit 023dd7a55b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 6 deletions

View File

@ -1,4 +1,4 @@
use std::{io::stdout, path::PathBuf};
use std::{fs, io::stdout, path::PathBuf, str::FromStr};
use anyhow::{bail, Context, Result};
use argp::FromArgs;
@ -74,12 +74,34 @@ pub fn run(args: Args) -> Result<()> {
)
};
if let Some(u) = u {
let Some(object) =
project_config.objects.iter_mut().find(|obj| obj.name() == u)
else {
let unit_path =
PathBuf::from_str(u).ok().and_then(|p| fs::canonicalize(p).ok());
let Some(object) = project_config.objects.iter_mut().find_map(|obj| {
if obj.name.as_deref() == Some(u) {
resolve_paths(obj);
return Some(obj);
}
let Some(up) = unit_path.as_deref() else {
return None;
};
resolve_paths(obj);
if [&obj.base_path, &obj.target_path]
.into_iter()
.filter_map(|p| p.as_ref().and_then(|p| p.canonicalize().ok()))
.any(|p| p == up)
{
return Some(obj);
}
None
}) else {
bail!("Unit not found: {}", u)
};
resolve_paths(object);
object
} else {
let mut idx = None;
@ -92,7 +114,7 @@ pub fn run(args: Args) -> Result<()> {
.as_deref()
.map(|o| obj::elf::has_function(o, &args.symbol))
.transpose()?
.unwrap_or_default()
.unwrap_or(false)
{
idx = Some(i);
count += 1;