дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
Accept in a worktree task copied every changed path into the local checkout without checking where it lands, so a path could reach the checkout's .git through a name that opens it (8.3 short name GIT~1, a case variant, trailing dots or spaces, an NTFS stream suffix) or go through a symlink or junction on either side. Accept now refuses, and lists as skipped, any path that names git metadata, leaves the tree once links are followed, lands in the repository's git dir or common dir, or is a link itself, on both the worktree and the checkout side. The .git-name and real-path helpers the plugin sandbox used move to ide_core::real_path so both share one implementation. Co-authored-by: rohitdanda <[email protected]>
54 рядки
2.2 KiB
Rust
54 рядки
2.2 KiB
Rust
//! Where a path really lands, and whether it reaches git's own metadata, for writes that must
|
|
//! stay inside a project and out of its `.git`.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// `joined` after `..`/symlink resolution as far as the file system allows:
|
|
/// the canonical path when it exists, else the canonicalized deepest existing
|
|
/// ancestor with the still-missing tail resolved textually — so the location
|
|
/// of a missing path is still judged where it would really land.
|
|
pub fn resolve_real(joined: &Path) -> PathBuf {
|
|
let mut tail = Vec::new();
|
|
let mut candidate = joined.to_path_buf();
|
|
let mut real = loop {
|
|
match dunce::canonicalize(&candidate) {
|
|
Ok(real) => break real,
|
|
Err(_) => {
|
|
let Some(last) = candidate.components().next_back() else {
|
|
return joined.to_path_buf();
|
|
};
|
|
tail.push(last.as_os_str().to_owned());
|
|
if !candidate.pop() {
|
|
return joined.to_path_buf();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
for part in tail.iter().rev() {
|
|
if part == ".." {
|
|
real.pop();
|
|
} else if part != "." {
|
|
real.push(part);
|
|
}
|
|
}
|
|
real
|
|
}
|
|
|
|
/// Whether a path (relative to the project root) goes through git's own `.git` folder or file.
|
|
/// git runs what it finds in `.git` (hooks, `gpg.program`, filters), so a content write must
|
|
/// never reach it. Names are normalized for trailing dots and spaces and NTFS stream suffixes;
|
|
/// the caller also checks the short-name-resolved path via [`resolve_real`].
|
|
pub fn names_git_dir(rel: &Path) -> bool {
|
|
rel.components().any(|part| {
|
|
let name = part.as_os_str().to_string_lossy();
|
|
let name = name.split(':').next().unwrap_or_default().trim_end_matches(['.', ' ']);
|
|
name.eq_ignore_ascii_case(".git")
|
|
})
|
|
}
|
|
|
|
/// A resolved write destination under `root` that reaches git's own `.git` metadata, judged by
|
|
/// the name given and by the name the file system resolves it to.
|
|
pub fn reaches_git_dir(root: &Path, real: &Path) -> bool {
|
|
real.strip_prefix(root).is_ok_and(names_git_dir) || resolve_real(real).strip_prefix(root).is_ok_and(names_git_dir)
|
|
}
|