better treatment when working in symlinked paths

This commit is contained in:
Grimmauld 2024-09-24 13:03:43 +02:00
parent fe2c39e74c
commit c8f70b03e8
Signed by: Grimmauld
GPG Key ID: C2946668769F91FB
2 changed files with 23 additions and 10 deletions

View File

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1715774670,
"narHash": "sha256-iJYnKMtLi5u6hZhJm94cRNSDG5Rz6ZzIkGbhPFtDRm0=",
"lastModified": 1727089097,
"narHash": "sha256-ZMHMThPsthhUREwDebXw7GX45bJnBCVbfnH1g5iuSPc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b3fcfcfabd01b947a1e4f36622bbffa3985bdac6",
"rev": "568bfef547c14ca438c56a0bece08b8bb2b71a9c",
"type": "github"
},
"original": {

View File

@ -4,15 +4,25 @@ mod args;
use crate::args::Cli;
use clap::Parser;
use std::fs;
use std::path;
use std::process::exit;
use std::env;
use std::path::PathBuf;
fn main() {
let cli = Cli::parse();
// FIXME: use abs path api once stable, see https://github.com/rust-lang/rust/issues/92750
let mut directory =
fs::canonicalize(cli.search_root).expect("Encountered error while parsing search root");
let mut directory = path::absolute(cli.search_root.clone()).expect("Encountered error while parsing search root");
// resolution of the . path is always canonical, which is an issue here. This tests whether pwd ebv var contains a valid path, and if so prefers that.
let pwd = env::var("PWD");
let canonical_dir = fs::canonicalize(cli.search_root.clone()).expect("Encountered error while parsing search root");
let test_dir = pwd.as_deref().map(path::Path::new);
if cli.search_root == path::Path::new(".") && test_dir.clone().map(fs::canonicalize).is_ok_and(|r| r.is_ok_and(|p| canonical_dir == p)) {
directory = test_dir.map(PathBuf::from).unwrap();
}
let mut found = false;
loop {
let files = match fs::read_dir(&directory) {
@ -21,10 +31,13 @@ fn main() {
};
for file in files {
let file_name = format!(
"{}",
file.as_ref().unwrap().file_name().into_string().unwrap()
);
let file_name = file
.as_ref()
.unwrap()
.file_name()
.into_string()
.unwrap()
.to_string();
if (!cli.exact && file_name.contains(&cli.search.to_string()))
|| file_name == cli.search.to_string()