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
mod from_tree {
use std::collections::VecDeque;
use bstr::{BStr, BString, ByteSlice, ByteVec};
use git_object::{
tree::{self, EntryMode},
TreeRefIter,
};
use git_traverse::tree::{breadthfirst, visit::Action, Visit};
use crate::{
entry::{Flags, Mode, Stat},
Entry, PathStorage, State, Version,
};
impl State {
pub fn from_tree<Find>(tree: &git_hash::oid, mut find: Find) -> Result<Self, breadthfirst::Error>
where
Find: for<'a> FnMut(&git_hash::oid, &'a mut Vec<u8>) -> Option<TreeRefIter<'a>>,
{
let mut buf = Vec::new();
let root = find(tree, &mut buf).ok_or(breadthfirst::Error::NotFound { oid: tree.into() })?;
let mut delegate = CollectEntries::new();
breadthfirst(root, breadthfirst::State::default(), &mut find, &mut delegate)?;
let CollectEntries {
mut entries,
path_backing,
path: _,
path_deque: _,
} = delegate;
entries.sort_by(|a, b| Entry::cmp_filepaths(a.path_in(&path_backing), b.path_in(&path_backing)));
Ok(State {
object_hash: tree.kind(),
timestamp: filetime::FileTime::now(),
version: Version::V2,
entries,
path_backing,
is_sparse: false,
tree: None,
link: None,
resolve_undo: None,
untracked: None,
fs_monitor: None,
})
}
}
struct CollectEntries {
entries: Vec<Entry>,
path_backing: PathStorage,
path: BString,
path_deque: VecDeque<BString>,
}
impl CollectEntries {
pub fn new() -> CollectEntries {
CollectEntries {
entries: Vec::new(),
path_backing: Vec::new(),
path: BString::default(),
path_deque: VecDeque::new(),
}
}
fn push_element(&mut self, name: &BStr) {
if !self.path.is_empty() {
self.path.push(b'/');
}
self.path.push_str(name);
}
pub fn add_entry(&mut self, entry: &tree::EntryRef<'_>) {
let mode = match entry.mode {
EntryMode::Tree => unreachable!("visit_non_tree() called us"),
EntryMode::Blob => Mode::FILE,
EntryMode::BlobExecutable => Mode::FILE_EXECUTABLE,
EntryMode::Link => Mode::SYMLINK,
EntryMode::Commit => Mode::COMMIT,
};
let path_start = self.path_backing.len();
self.path_backing.extend_from_slice(&self.path);
let new_entry = Entry {
stat: Stat::default(),
id: entry.oid.into(),
flags: Flags::empty(),
mode,
path: path_start..self.path_backing.len(),
};
self.entries.push(new_entry);
}
}
impl Visit for CollectEntries {
fn pop_front_tracked_path_and_set_current(&mut self) {
self.path = self
.path_deque
.pop_front()
.expect("every call is matched with push_tracked_path_component");
}
fn push_back_tracked_path_component(&mut self, component: &bstr::BStr) {
self.push_element(component);
self.path_deque.push_back(self.path.clone());
}
fn push_path_component(&mut self, component: &bstr::BStr) {
self.push_element(component);
}
fn pop_path_component(&mut self) {
if let Some(pos) = self.path.rfind_byte(b'/') {
self.path.resize(pos, 0);
} else {
self.path.clear();
}
}
fn visit_tree(&mut self, _entry: &git_object::tree::EntryRef<'_>) -> git_traverse::tree::visit::Action {
Action::Continue
}
fn visit_nontree(&mut self, entry: &git_object::tree::EntryRef<'_>) -> git_traverse::tree::visit::Action {
self.add_entry(entry);
Action::Continue
}
}
}