This commit is contained in:
Drew Skelton 2026-07-13 18:16:01 -04:00 committed by GitHub
commit 7bff02fd35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 71 additions and 5 deletions

View File

@ -4,7 +4,7 @@ use anyhow::{Result, bail};
use crate::cmd::{Add, Run};
use crate::db::Database;
use crate::{config, util};
use crate::{config, path_resolver, util};
impl Run for Add {
fn run(&self) -> Result<()> {
@ -19,10 +19,7 @@ impl Run for Add {
let mut db = Database::open()?;
for path in &self.paths {
let path =
if config::resolve_symlinks() { util::canonicalize } else { util::resolve_path }(
path,
)?;
let path = path_resolver::resolve(path, config::resolve_symlinks())?;
let path = util::path_to_str(&path)?;
// Ignore path if it contains unsupported characters, or if it's in the exclude

View File

@ -5,6 +5,7 @@ mod config;
mod db;
mod error;
mod import;
mod path_resolver;
mod shell;
mod util;

68
src/path_resolver.rs Normal file
View File

@ -0,0 +1,68 @@
use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
use std::process::Command;
#[cfg(target_os = "macos")]
use anyhow::Context;
use anyhow::Result;
use crate::util;
pub fn resolve(path: impl AsRef<Path>, resolve_symlinks: bool) -> Result<PathBuf> {
if resolve_symlinks {
return util::canonicalize(path);
}
#[cfg(target_os = "macos")]
{
return resolve_macos(path);
}
#[cfg(not(target_os = "macos"))]
{
util::resolve_path(path)
}
}
#[cfg(target_os = "macos")]
fn resolve_macos(path: impl AsRef<Path>) -> Result<PathBuf> {
let path = util::resolve_path(path)?;
if path.is_file() {
if let Some(resolved) = resolve_finder_alias(&path)? {
return Ok(resolved);
}
}
Ok(path)
}
#[cfg(target_os = "macos")]
fn resolve_finder_alias(path: &Path) -> Result<Option<PathBuf>> {
let script = r#"
on run argv
tell application "Finder"
set alias_path to POSIX file (item 1 of argv) as alias
return POSIX path of (original item of alias_path as alias)
end tell
end run
"#;
let output = Command::new("osascript")
.arg("-e")
.arg(script)
.arg(path)
.output()
.context("could not run osascript to resolve Finder alias")?;
if !output.status.success() {
return Ok(None);
}
let resolved = String::from_utf8_lossy(&output.stdout).trim().to_string();
if resolved.is_empty() {
return Ok(None);
}
Ok(Some(util::resolve_path(resolved)?))
}