fix: leave hidden files and empty folders alone when importing

Hidden files and folders are not moved into attachments or deleted, and
folders that were already empty before the import are kept.
This commit is contained in:
Yuri Karamian committed 2026-10-10 21:08:19 +02:00
1 parent af35b4dd75
commit 05249a3c81
1 file changed
+67 -11
+67 -11
View File
@@ -15,6 +15,9 @@ use crate::vault::operations::replace_file_atomically;
pub fn import(vault_path: &str) -> Result<ImportResult, String> {
let vault = Path::new(vault_path);
// Folders that are empty before the import are notebooks the user made; only folders the
// import itself empties are removed afterwards.
let already_empty = empty_dirs(vault)?;
let file_index = build_file_index(vault)?;
@@ -204,16 +207,18 @@ pub fn import(vault_path: &str) -> Result<ImportResult, String> {
}
}
cleanup_empty_dirs(vault)?;
cleanup_empty_dirs(vault, &already_empty)?;
Ok(result)
}
fn is_excluded_dir(path: &Path) -> bool {
matches!(
path.file_name().and_then(|name| name.to_str()),
Some(".helixnotes" | ".obsidian" | ".trash" | ".stfolder" | ".claude")
)
/// Hidden files and folders are never imported, moved or cleaned up. The app's own `.helixnotes`
/// and Obsidian's `.obsidian` and `.trash` are among them, and so is `.git`: every file that is
/// not a note becomes an attachment and its original is deleted, which took a repository apart.
fn is_hidden_entry(path: &Path) -> bool {
path.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with('.'))
}
fn vault_entries(
@@ -221,9 +226,7 @@ fn vault_entries(
) -> impl Iterator<Item = Result<walkdir::DirEntry, walkdir::Error>> + '_ {
walkdir::WalkDir::new(vault)
.into_iter()
.filter_entry(|entry| {
entry.depth() == 0 || !entry.file_type().is_dir() || !is_excluded_dir(entry.path())
})
.filter_entry(|entry| entry.depth() == 0 || !is_hidden_entry(entry.path()))
}
fn scanned_files(vault: &Path) -> Result<Vec<PathBuf>, String> {
@@ -782,7 +785,20 @@ fn resolve_relative_ref(base: &Path, reference: &str) -> Option<String> {
Some(rel.to_string_lossy().replace('\\', "/"))
}
fn cleanup_empty_dirs(root: &Path) -> Result<(), String> {
fn empty_dirs(root: &Path) -> Result<HashSet<PathBuf>, String> {
let mut empty = HashSet::new();
for entry in vault_entries(root) {
let entry = entry.map_err(|e| format!("Scanning {}: {e}", root.display()))?;
if entry.file_type().is_dir()
&& std::fs::read_dir(entry.path()).is_ok_and(|mut entries| entries.next().is_none())
{
empty.insert(entry.into_path());
}
}
Ok(empty)
}
fn cleanup_empty_dirs(root: &Path, keep: &HashSet<PathBuf>) -> Result<(), String> {
let mut dirs: Vec<_> = vault_entries(root)
.filter_map(|entry| match entry {
Ok(entry) if entry.file_type().is_dir() => Some(Ok(entry.into_path())),
@@ -795,7 +811,7 @@ fn cleanup_empty_dirs(root: &Path) -> Result<(), String> {
dirs.sort_by_key(|path| std::cmp::Reverse(path.components().count()));
for dir in dirs {
if dir == root {
if dir == root || keep.contains(&dir) {
continue;
}
let mut entries =
@@ -908,6 +924,46 @@ mod tests {
}
}
#[test]
fn import_leaves_hidden_files_and_existing_empty_folders_alone() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
// A git-backed vault with dotfiles, an empty notebook, and a folder of attachments only.
std::fs::create_dir_all(root.join(".git/objects/ab")).unwrap();
std::fs::write(root.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(root.join(".git/objects/ab/cdef"), b"blob").unwrap();
std::fs::write(root.join(".gitignore"), ".obsidian/workspace.json\n").unwrap();
std::fs::create_dir_all(root.join(".stversions")).unwrap();
std::fs::write(root.join(".stversions/old.png"), b"old").unwrap();
std::fs::create_dir(root.join("Empty notebook")).unwrap();
std::fs::create_dir(root.join("Pictures")).unwrap();
std::fs::write(root.join("Pictures/photo.png"), b"png").unwrap();
std::fs::write(root.join("Note.md"), "# Note\n\n![p](Pictures/photo.png)\n").unwrap();
let result = import(root.to_str().unwrap()).unwrap();
assert_eq!(result.attachments_moved, 1);
assert_eq!(
std::fs::read_to_string(root.join(".git/HEAD")).unwrap(),
"ref: refs/heads/main\n"
);
assert_eq!(
std::fs::read(root.join(".git/objects/ab/cdef")).unwrap(),
b"blob"
);
assert!(root.join(".gitignore").is_file());
assert!(root.join(".stversions/old.png").is_file());
// Nothing hidden was copied into the attachments either.
assert!(!root.join(".helixnotes/attachments/.git").exists());
assert!(!root.join(".helixnotes/attachments/.gitignore").exists());
// The folder the import emptied is gone; the one that was empty already is kept.
assert!(!root.join("Pictures").exists());
assert!(root.join("Empty notebook").is_dir());
assert!(root
.join(".helixnotes/attachments/Pictures/photo.png")
.is_file());
}
#[test]
fn import_rewrites_unicode_encoded_and_malformed_attachment_references() {
let vault = tempfile::tempdir().unwrap();