oakvcs-core 0.114.0

Core version-control primitives for Oak: BLAKE3 content hashing, content-defined chunking, diff/merge, the Blob/Manifest/Commit/Tree data model, and the client-side local repository (SQLite + git backends).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use std::path::Path;

use crate::error::Result;

/// Version-controlled files that live directly inside the `.oak/` metadata
/// directory. Everything else under `.oak/` (the local `oak.db`, caches,
/// in-progress markers) is repository state and never tracked. `.oak/workflows/`
/// is a tracked *directory* and is handled separately.
///
/// `.oak/PERMISSIONS` is the retired path-permissions file (access rules now
/// live on the server, as ownership boundaries). Servers no longer read it;
/// it stays trackable so history that contains one still checks out.
pub const TRACKED_OAK_FILES: &[&str] = &[".oak/attributes", ".oak/PERMISSIONS"];

/// The state file `oak root pull` / `oak root push` keep at the top of a
/// directory that holds a root (a separate timeline of the same repo). It is
/// local bookkeeping and never tracked, in a root or in the checkout.
pub const ROOT_STATE_FILE: &str = ".oakroot";

/// Local, untracked list (inside `.oak/`) of the checkout directories that
/// hold roots, one repo-relative path per line. Every listed directory is
/// ignored by the checkout, so a root's files (`me/`) never land on `main`.
pub const ROOT_DIRS_FILE: &str = "root-dirs";

/// The repo-relative root directories listed in `<repo_root>/.oak/root-dirs`.
pub fn root_dirs(repo_root: &Path) -> Vec<String> {
    let Ok(text) = std::fs::read_to_string(repo_root.join(".oak").join(ROOT_DIRS_FILE)) else {
        return Vec::new();
    };
    text.lines()
        .map(|line| line.trim().trim_matches('/').to_string())
        .filter(|line| !line.is_empty() && !line.starts_with('#'))
        .collect()
}

/// OS-generated metadata files and directories that should never be tracked.
/// These appear on their own — a file manager browsing a directory drops a
/// `.DS_Store`; a mounted volume gets `.fseventsd`/`.Spotlight-V100` written by
/// the OS's volume-bookkeeping daemons; Windows leaves `Thumbs.db`/`desktop.ini`.
/// Versioning them is never what the user wants, so Oak ignores them everywhere,
/// the same way it ignores `.git/`.
const SYSTEM_METADATA_NAMES: &[&str] = &[
    // macOS
    ".DS_Store",
    ".AppleDouble",
    ".LSOverride",
    ".DocumentRevisions-V100",
    ".fseventsd",
    ".Spotlight-V100",
    ".TemporaryItems",
    ".Trashes",
    ".VolumeIcon.icns",
    ".com.apple.timemachine.donotpresent",
    ".AppleDB",
    ".AppleDesktop",
    ".apdisk",
    // Windows
    "Thumbs.db",
    "ehthumbs.db",
    "Desktop.ini",
    "$RECYCLE.BIN",
    // Linux file managers
    ".directory",
];

/// True if `path` (a `/`-separated relative path) is, or lives under, an
/// OS-generated metadata file or directory — including AppleDouble resource-fork
/// files (`._*`). Matching any path *component* means a write to
/// `.fseventsd/fseventsd-uuid` is caught by the `.fseventsd` directory entry.
pub fn is_system_metadata_path(path: &str) -> bool {
    path.split('/').filter(|c| !c.is_empty()).any(|comp| {
        comp.starts_with("._")
            || SYSTEM_METADATA_NAMES
                .iter()
                .any(|n| n.eq_ignore_ascii_case(comp))
    })
}

/// Handles ignore patterns for the repository
pub struct IgnorePatterns {
    gitignore: Option<Gitignore>,
}

impl IgnorePatterns {
    /// Create a new IgnorePatterns from a repository root
    pub fn new(repo_root: &Path) -> Result<Self> {
        let oakignore_path = repo_root.join(".oakignore");
        let gitignore_path = repo_root.join(".gitignore");

        let mut builder = GitignoreBuilder::new(repo_root);

        // Always ignore .oak directory, .oak-branch marker, and .git directory
        builder.add_line(None, ".oak/").ok();
        builder.add_line(None, ".oak-branch").ok();
        builder.add_line(None, ".git/").ok();

        // Load .oakignore if it exists
        if oakignore_path.exists() {
            builder.add(&oakignore_path);
        }

        // Fall back to .gitignore if no .oakignore exists
        if !oakignore_path.exists() && gitignore_path.exists() {
            builder.add(&gitignore_path);
        }

        // Directories that hold roots (`oak root pull`): never part of this
        // checkout's tree. Anchored, and added last so no `!` pattern above
        // can re-include them.
        for dir in root_dirs(repo_root) {
            builder.add_line(None, &format!("/{dir}/")).ok();
        }

        let gitignore = builder.build().ok();
        Ok(IgnorePatterns { gitignore })
    }

    /// Create IgnorePatterns from a string of newline-separated patterns.
    /// No filesystem access — useful for server-side filtering of uploaded files.
    pub fn from_patterns(patterns: &str) -> Self {
        let synthetic_root = Path::new("/");
        let mut builder = GitignoreBuilder::new(synthetic_root);
        builder.add_line(None, ".oak/").ok();
        builder.add_line(None, ".oak-branch").ok();
        builder.add_line(None, ".git/").ok();
        for line in patterns.lines() {
            let trimmed = line.trim();
            if !trimmed.is_empty() && !trimmed.starts_with('#') {
                builder.add_line(None, trimmed).ok();
            }
        }
        let gitignore = builder.build().ok();
        IgnorePatterns { gitignore }
    }

    /// Create an empty ignore patterns (ignores nothing except .oak, .oak-branch, and .git)
    pub fn empty(repo_root: &Path) -> Self {
        let mut builder = GitignoreBuilder::new(repo_root);
        builder.add_line(None, ".oak/").ok();
        builder.add_line(None, ".oak-branch").ok();
        builder.add_line(None, ".git/").ok();
        let gitignore = builder.build().ok();
        IgnorePatterns { gitignore }
    }

    /// Check if a file path or any of its ancestor directories should be ignored.
    /// Useful for filtering a flat list of file paths (e.g. from a web upload)
    /// where we can't rely on a directory walker to skip ignored directories.
    pub fn is_path_or_ancestors_ignored(&self, path_str: &str) -> bool {
        let path = Path::new(path_str);
        if self.is_ignored(path, false) {
            return true;
        }
        // Check each ancestor directory
        let mut current = path.parent();
        while let Some(ancestor) = current {
            if ancestor.as_os_str().is_empty() {
                break;
            }
            if self.is_ignored(ancestor, true) {
                return true;
            }
            current = ancestor.parent();
        }
        false
    }

    /// Check if a path should be ignored
    pub fn is_ignored(&self, path: &Path, is_dir: bool) -> bool {
        let path_str = path.to_string_lossy();

        // Allow version-controlled files inside .oak/. These live in the repo
        // metadata directory but are tracked like any other file:
        // - `.oak/attributes`: per-path attributes
        // - `.oak/PERMISSIONS`: the retired path-permissions file, inert but
        //   still trackable so old history checks out
        if TRACKED_OAK_FILES.contains(&path_str.as_ref()) {
            return false;
        }

        // - `.oak/workflows/`: retired CI workflow files (workflows are stored
        //   on the server now and these are never read). Older history holds
        //   them, so like `.oak/PERMISSIONS` they stay trackable: the
        //   directory stays walkable and its files are never ignored.
        if path_str == ".oak/workflows"
            || path_str == ".oak/workflows/"
            || path_str.starts_with(".oak/workflows/")
        {
            return false;
        }

        // The `.oak` directory itself must not be ignored as a *directory* so
        // the working-tree walker can descend into it and see the
        // version-controlled entries above. Files directly under `.oak` that
        // aren't explicitly allowed are still ignored below.
        if is_dir && (path_str == ".oak" || path_str.ends_with("/.oak")) {
            return false;
        }

        // Always ignore .oak contents (other than the exceptions above) and
        // the .oak-branch marker file.
        if path.starts_with(".oak") || path_str.contains("/.oak/") {
            return true;
        }
        // Always ignore .git directory
        if path.starts_with(".git") || path.to_string_lossy().contains("/.git/") {
            return true;
        }
        if path
            .file_name()
            .map(|f| f == ".oak-branch" || f == ROOT_STATE_FILE)
            .unwrap_or(false)
        {
            return true;
        }

        // OS-generated metadata (.DS_Store, .fseventsd, Thumbs.db, …) is never
        // tracked, regardless of any .oakignore/.gitignore.
        if is_system_metadata_path(&path_str) {
            return true;
        }

        if let Some(ref gitignore) = self.gitignore {
            gitignore.matched(path, is_dir).is_ignore()
        } else {
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn root_dirs_and_state_files_are_ignored() {
        let temp = TempDir::new().unwrap();
        fs::create_dir_all(temp.path().join(".oak")).unwrap();
        fs::write(temp.path().join(".oakignore"), "!me/\n").unwrap();
        fs::write(
            temp.path().join(".oak").join(ROOT_DIRS_FILE),
            "me\nroots/ops/\n",
        )
        .unwrap();
        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("me"), true));
        assert!(patterns.is_path_or_ancestors_ignored("me/notes.md"));
        assert!(patterns.is_path_or_ancestors_ignored("roots/ops/a/b.txt"));
        assert!(!patterns.is_path_or_ancestors_ignored("src/me/notes.md"));
        assert!(!patterns.is_path_or_ancestors_ignored("roots/other.txt"));
        assert!(patterns.is_ignored(Path::new("docs/.oakroot"), false));
        assert!(patterns.is_ignored(Path::new(".oakroot"), false));
    }

    #[test]
    fn test_ignores_oak_directory() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());
        // The .oak directory itself is walkable so the commit scanner can
        // descend to pick up version-controlled entries like `.oak/attributes`.
        // Files inside .oak that aren't explicitly allowed are still ignored.
        assert!(!patterns.is_ignored(Path::new(".oak"), true));
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".oak/internal"), true));
    }

    #[test]
    fn test_tracks_oak_workflows() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());
        // CI workflow definitions under `.oak/workflows/` are version-controlled
        // (read server-side from the commit), so the directory stays walkable
        // and its files are not ignored — unlike the rest of `.oak/`.
        assert!(!patterns.is_ignored(Path::new(".oak/workflows"), true));
        assert!(!patterns.is_ignored(Path::new(".oak/workflows/ci.yml"), false));
        assert!(!patterns.is_ignored(Path::new("apps/web/.oak"), true));
        // Only the repo root's: a nested `.oak/workflows/` is ignored.
        assert!(patterns.is_ignored(Path::new("apps/web/.oak/workflows/ci.yml"), false));
        assert!(patterns.is_ignored(Path::new("apps/web/.oak/oak.db"), false));
        assert!(!patterns.is_ignored(Path::new(".oak/workflows/deploy.yaml"), false));
        // Other `.oak/` contents remain ignored.
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
    }

    #[test]
    fn test_oakignore_patterns() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".oakignore"), "*.log\ntarget/\n").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("debug.log"), false));
        assert!(patterns.is_ignored(Path::new("target"), true));
        assert!(!patterns.is_ignored(Path::new("src/main.rs"), false));
    }

    #[test]
    fn test_gitignore_fallback() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".gitignore"), "node_modules/\n").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("node_modules"), true));
    }

    #[test]
    fn test_ignores_git_directory() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());
        assert!(patterns.is_ignored(Path::new(".git"), true));
        assert!(patterns.is_ignored(Path::new(".git/config"), false));
    }

    #[test]
    fn test_always_ignores_oak_and_git_with_no_ignore_file() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        // .oak is walkable (for committed subpaths) but its non-versioned
        // contents are still ignored.
        assert!(!patterns.is_ignored(Path::new(".oak"), true));
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".git"), true));
        assert!(patterns.is_ignored(Path::new(".oak-branch"), false));
        assert!(!patterns.is_ignored(Path::new("src/main.rs"), false));
    }

    #[test]
    fn test_oakignore_takes_precedence() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".gitignore"), "*.log\n").unwrap();
        fs::write(temp.path().join(".oakignore"), "*.txt\n").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        // .oakignore should be used, not .gitignore
        assert!(patterns.is_ignored(Path::new("file.txt"), false));
        assert!(!patterns.is_ignored(Path::new("file.log"), false));
    }

    #[test]
    fn test_oak_attributes_not_ignored() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());

        // .oak/attributes should NOT be ignored (it's version-controlled)
        assert!(!patterns.is_ignored(Path::new(".oak/attributes"), false));

        // But other .oak files should still be ignored
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
    }

    #[test]
    fn test_oak_permissions_not_ignored() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());

        // .oak/PERMISSIONS stays version-controlled (retired, but history
        // that tracks one must still check out)
        assert!(!patterns.is_ignored(Path::new(".oak/PERMISSIONS"), false));

        // Only the exact name: other .oak files and look-alikes stay ignored
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".oak/permissions"), false));
        assert!(patterns.is_ignored(Path::new(".oak/PERMISSIONS.bak"), false));
    }

    #[test]
    fn test_nested_gitignore_patterns() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".gitignore"),
            "*.o\n*.so\nbuild/\n!build/keep.txt\n",
        )
        .unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("main.o"), false));
        assert!(patterns.is_ignored(Path::new("lib.so"), false));
        assert!(patterns.is_ignored(Path::new("build"), true));
        assert!(!patterns.is_ignored(Path::new("src/lib.rs"), false));
    }

    #[test]
    fn test_negation_pattern() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".oakignore"), "*.log\n!important.log\n").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("debug.log"), false));
        // Negation pattern should un-ignore
        assert!(!patterns.is_ignored(Path::new("important.log"), false));
    }

    #[test]
    fn test_directory_specific_pattern() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".oakignore"), "logs/\n").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        // Directory pattern should match directories
        assert!(patterns.is_ignored(Path::new("logs"), true));
        // But not files named "logs"
        assert!(!patterns.is_ignored(Path::new("logs"), false));
    }

    #[test]
    fn test_oak_branch_marker_always_ignored() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new(".oak-branch"), false));
        assert!(patterns.is_ignored(Path::new("subdir/.oak-branch"), false));
    }

    #[test]
    fn test_empty_ignore_file() {
        let temp = TempDir::new().unwrap();
        fs::write(temp.path().join(".oakignore"), "").unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        // .oak contents are ignored (except the versioned entries). .git is
        // ignored wholesale.
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".git"), true));
        // But nothing else
        assert!(!patterns.is_ignored(Path::new("file.txt"), false));
    }

    #[test]
    fn test_comment_lines_in_ignore() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".oakignore"),
            "# This is a comment\n*.log\n# Another comment\n",
        )
        .unwrap();

        let patterns = IgnorePatterns::new(temp.path()).unwrap();
        assert!(patterns.is_ignored(Path::new("debug.log"), false));
        assert!(!patterns.is_ignored(Path::new("# This is a comment"), false));
    }

    #[test]
    fn test_from_patterns_godot() {
        let patterns = IgnorePatterns::from_patterns("# Godot\n.godot/\n*.import\n");
        // Directory patterns match the directory itself
        assert!(patterns.is_ignored(Path::new(".godot"), true));
        // Extension patterns match files
        assert!(patterns.is_ignored(Path::new("icon.svg.import"), false));
        // Normal files pass through
        assert!(!patterns.is_ignored(Path::new("project.godot"), false));
        assert!(!patterns.is_ignored(Path::new("scenes/main.tscn"), false));
        // Built-in ignores still work
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".git"), true));
    }

    #[test]
    fn test_from_patterns_unity() {
        let patterns = IgnorePatterns::from_patterns(
            "Library/\nTemp/\nLogs/\nUserSettings/\nobj/\nBuilds/\nBuild/\n*.csproj\n*.sln\n",
        );
        // Directory patterns match directories
        assert!(patterns.is_ignored(Path::new("Library"), true));
        assert!(patterns.is_ignored(Path::new("Temp"), true));
        // Extension globs match files
        assert!(patterns.is_ignored(Path::new("Assembly-CSharp.csproj"), false));
        assert!(patterns.is_ignored(Path::new("MyGame.sln"), false));
        // Normal files pass through
        assert!(!patterns.is_ignored(Path::new("Assets/Scripts/Player.cs"), false));
        assert!(!patterns.is_ignored(Path::new("ProjectSettings/ProjectSettings.asset"), false));
    }

    #[test]
    fn test_from_patterns_empty() {
        let patterns = IgnorePatterns::from_patterns("");
        assert!(!patterns.is_ignored(Path::new("src/main.rs"), false));
        // Built-in ignores still work
        assert!(patterns.is_ignored(Path::new(".oak/oak.db"), false));
        assert!(patterns.is_ignored(Path::new(".git"), true));
    }

    #[test]
    fn test_system_metadata_path() {
        // macOS volume bookkeeping (the original `oak mount` report)
        assert!(is_system_metadata_path(".fseventsd"));
        assert!(is_system_metadata_path(".fseventsd/fseventsd-uuid"));
        assert!(is_system_metadata_path(".Spotlight-V100/Store-V2/x"));
        assert!(is_system_metadata_path(".Trashes/501"));
        // .DS_Store anywhere in the tree
        assert!(is_system_metadata_path(".DS_Store"));
        assert!(is_system_metadata_path("src/.DS_Store"));
        // AppleDouble resource forks
        assert!(is_system_metadata_path("._foo"));
        assert!(is_system_metadata_path("dir/._image.png"));
        // Windows junk
        assert!(is_system_metadata_path("Thumbs.db"));
        assert!(is_system_metadata_path("a/b/desktop.ini")); // case-insensitive
                                                             // Normal files pass through
        assert!(!is_system_metadata_path("src/main.rs"));
        assert!(!is_system_metadata_path("README.md"));
        assert!(!is_system_metadata_path("Documents/notes.txt"));
    }

    #[test]
    fn test_is_ignored_covers_system_metadata() {
        let temp = TempDir::new().unwrap();
        let patterns = IgnorePatterns::empty(temp.path());
        assert!(patterns.is_ignored(Path::new(".fseventsd"), true));
        assert!(patterns.is_ignored(Path::new(".fseventsd/fseventsd-uuid"), false));
        assert!(patterns.is_ignored(Path::new("src/.DS_Store"), false));
        assert!(!patterns.is_ignored(Path::new("src/main.rs"), false));
    }

    #[test]
    fn test_from_patterns_is_file_in_ignored_dir() {
        // Demonstrates that callers must check parent directories separately
        // (the CLI walker does this by not descending into ignored dirs)
        let patterns = IgnorePatterns::from_patterns("Library/\n");
        assert!(patterns.is_ignored(Path::new("Library"), true));
        // A file path is checked against patterns; caller must also check ancestors
        assert!(patterns.is_path_or_ancestors_ignored("Library/foo.dll"));
        assert!(!patterns.is_path_or_ancestors_ignored("Assets/main.cs"));
    }
}