aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/supergit/src/files/tree.rs
blob: 5f4fb6671aa5e4f8f00bd92219f2d18eabd5a7e2 (plain)
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
//! Low-level abstraction over finding refs inside a commit tree

use super::tree_utils as utils;
use crate::HashId;
use git2::{ObjectType, Repository, TreeWalkMode, TreeWalkResult};
use std::sync::Arc;

/// A git directory tree walker abstraction
///
/// This type is meant to be used ephemerally, and internally uses the
/// libgit2 `Tree` abstraction to walk directory trees lazily to
/// resolve paths to [`TreeEntry`](self::TreeEntry)'s.
///
/// Note: this type _may_ be removed in the future.  For a more
/// high-level (and stable) API, check
/// [`Explorer`](crate::files::Explorer)
pub struct FileTree {
    repo: Arc<Repository>,
    c: HashId,
}

impl FileTree {
    /// Construct a new FileTree with a repository
    pub(crate) fn new(repo: Arc<Repository>, c: HashId) -> Self {
        Self { repo, c }
    }

    /// Resolve a path inside this file tree
    ///
    /// Will return `None` if there is no tree for the selected
    /// commit, or the file inside the tree does not exist.
    pub fn resolve(&self, path: &str) -> Option<TreeEntry> {
        let tree = utils::open_tree(&self.repo, &self.c)?;
        let target = utils::path_split(path);

        // Initialise entry to None as a fallback
        let mut entry = None;

        // Walk over tree and swallor errors (which we use to
        // terminace traversal to speed up indexing time)
        let _ = tree.walk(TreeWalkMode::PreOrder, |p, e| {
            if utils::path_cmp(&target, p, e.name().unwrap()) {
                entry = Some(TreeEntry::new(p, &e));
                TreeWalkResult::Ok
            } else {
                TreeWalkResult::Skip
            }
        });

        // Return whatever the entry is now
        entry
    }
}

/// An entry in a commit tree
///
/// This type is lazily loaded, and can represent either a Blob or a
/// Directory.  You can resolve its value by calling
/// [`resolve()`](Self::resolve)
pub struct TreeEntry {
    tt: EntryType,
    id: HashId,
    path: String,
}

impl TreeEntry {
    fn new(path: &str, entry: &git2::TreeEntry) -> Self {
        let tt = match entry.kind() {
            Some(ObjectType::Blob) => EntryType::File,
            Some(ObjectType::Tree) => EntryType::Dir,
            _ => unimplemented!(),
        };
        let id = entry.id().into();
        let path = path.into();

        Self { tt, id, path }
    }

    /// Resolve this type to a [`Yield`]()
    pub fn resolve(&self) {}
}

/// Type of a TreeEntry
pub enum EntryType {
    /// A file that can be loaded
    File,
    /// A directory that can be indexed
    Dir,
}

#[test]
fn index_tree() {
    let path = env!("CARGO_MANIFEST_DIR").to_owned() + "/test-repo";
    use crate::Repository as Repo;

    eprintln!("Path: `{}`", path);

    let r = Repo::open(&path).unwrap();
    let b = r.branch("master".into()).unwrap();
    let h = b.head();

    let t = h.tree();
    t.resolve("README".into()).unwrap();
}