Skip to content
35 changes: 29 additions & 6 deletions src/local/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ fn repositories(root: &Path, found: &mut BTreeSet<PathBuf>) -> Result<()> {
Ok(())
}

/// Reading a dataless File Provider file (iCloud, Dropbox) blocks until it downloads.
#[cfg(target_os = "macos")]
fn cloud_only(path: &Path) -> bool {
use std::os::macos::fs::MetadataExt;
const SF_DATALESS: u32 = 0x4000_0000;
std::fs::symlink_metadata(path).is_ok_and(|metadata| metadata.st_flags() & SF_DATALESS != 0)
}

// shortcut: other platforms have no stat-visible placeholder flag; add one if Windows cloud folders hang.
#[cfg(not(target_os = "macos"))]
fn cloud_only(_: &Path) -> bool {
false
}

fn config_may_need_repair(repo: &Path, mappings: &[(PathBuf, PathBuf)]) -> std::io::Result<bool> {
let config = match std::fs::read_to_string(repo.join(".git/config")) {
Ok(config) => config,
Expand Down Expand Up @@ -313,15 +327,21 @@ fn references(data: &Path, mappings: &[(PathBuf, PathBuf)]) -> Result<References
}
// Registrations also find moved worktrees whose project/database was deleted.
let mut discovered = Vec::new();
let external = |repo: &Path| {
!mappings
.iter()
.any(|(from, to)| repo.starts_with(from) || repo.starts_with(to))
};
for repo in &repos {
// Hydrating an unrelated repository would block every command; it is rechecked once downloaded.
if external(repo) && cloud_only(&repo.join(".git/config")) {
continue;
}
let needs_repair = if repo.join(".git").is_dir() {
match config_may_need_repair(repo, mappings) {
Ok(needed) => needed,
Err(error)
if error.kind() == std::io::ErrorKind::PermissionDenied
&& !mappings
.iter()
.any(|(from, to)| repo.starts_with(from) || repo.starts_with(to)) =>
if error.kind() == std::io::ErrorKind::PermissionDenied && external(repo) =>
{
eprintln!(
"Skipping inaccessible repository {}: {error}",
Expand Down Expand Up @@ -357,7 +377,7 @@ fn references(data: &Path, mappings: &[(PathBuf, PathBuf)]) -> Result<References
for entry in std::fs::read_dir(registrations)? {
let entry = entry?;
let backlink = entry.path().join("gitdir");
if backlink.is_file() {
if backlink.is_file() && !(external(repo) && cloud_only(&backlink)) {
let old = std::fs::read_to_string(backlink)?;
let path = mapped(Path::new(old.trim()), mappings);
if let Some(worktree) = path.parent().filter(|_| path.is_file()) {
Expand All @@ -370,7 +390,7 @@ fn references(data: &Path, mappings: &[(PathBuf, PathBuf)]) -> Result<References
repos.extend(discovered);
for repo in repos {
let gitfile = repo.join(".git");
if !gitfile.is_file() {
if !gitfile.is_file() || (external(&repo) && cloud_only(&gitfile)) {
continue;
}
let Ok(contents) = std::fs::read_to_string(&gitfile) else {
Expand All @@ -382,6 +402,9 @@ fn references(data: &Path, mappings: &[(PathBuf, PathBuf)]) -> Result<References
let old = repo.join(old);
let admin = mapped(&old, mappings);
let backlink = admin.join("gitdir");
if external(&admin) && cloud_only(&backlink) {
continue;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
let actual = match std::fs::read_to_string(&backlink) {
Ok(actual) => admin.join(actual.trim()),
Err(_) if admin == old => continue,
Expand Down
Loading