This commit is contained in:
吴杨帆 2026-05-17 16:05:50 +08:00 committed by GitHub
commit 6b03832648
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 76 additions and 10 deletions

View file

@ -1,6 +1,6 @@
use crate::{
accessors,
args::CliArgs,
args::{resolve_select_file_path, CliArgs},
cmdbar::CommandBar,
components::{
command_pump, event_pump, CommandInfo, Component,
@ -178,14 +178,13 @@ impl App {
let mut select_file: Option<PathBuf> = None;
let tab = if let Some(file) = cliargs.select_file {
// convert to relative git path
if let Ok(abs) = file.canonicalize() {
if let Ok(path) = abs.strip_prefix(
env.repo.borrow().gitpath().canonicalize()?,
) {
select_file = Some(Path::new(".").join(path));
}
}
let workdir =
PathBuf::from(repo_work_dir(&env.repo.borrow())?);
select_file = resolve_select_file_path(
&file,
&workdir,
&std::env::current_dir()?,
);
2
} else {
env.options.borrow().current_tab()

View file

@ -9,7 +9,7 @@ use simplelog::{Config, LevelFilter, WriteLogger};
use std::{
env,
fs::{self, File},
path::PathBuf,
path::{Path, PathBuf},
};
const BUG_REPORT_FLAG_ID: &str = "bugreport";
@ -238,6 +238,73 @@ pub fn get_app_config_path() -> Result<PathBuf> {
Ok(path)
}
/// Resolves `--file` to a path relative to the repository workdir.
pub(crate) fn resolve_select_file_path(
file: &Path,
repo_workdir: &Path,
cwd: &Path,
) -> Option<PathBuf> {
let repo_workdir = repo_workdir.canonicalize().ok()?;
let absolute = if file.is_absolute() {
file.to_path_buf()
} else {
cwd.join(file)
};
let absolute = absolute.canonicalize().ok()?;
absolute
.strip_prefix(&repo_workdir)
.ok()
.map(Path::to_path_buf)
}
#[cfg(test)]
mod select_file_tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn resolve_select_file_from_subdirectory() {
let repo = tempdir().unwrap();
let frontend = repo.path().join("frontend");
fs::create_dir_all(frontend.join("src")).unwrap();
fs::write(frontend.join("src/index.html"), "hi").unwrap();
let cwd = frontend.clone();
let file = Path::new("src/index.html");
let resolved = resolve_select_file_path(
file,
repo.path(),
&cwd,
)
.unwrap();
assert_eq!(resolved, Path::new("frontend/src/index.html"));
}
#[test]
fn resolve_select_file_from_repo_root() {
let repo = tempdir().unwrap();
fs::create_dir_all(repo.path().join("frontend/src")).unwrap();
fs::write(
repo.path().join("frontend/src/index.html"),
"hi",
)
.unwrap();
let file = Path::new("frontend/src/index.html");
let resolved = resolve_select_file_path(
file,
repo.path(),
repo.path(),
)
.unwrap();
assert_eq!(resolved, file);
}
}
#[test]
fn verify_app() {
app().debug_assert();