forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.rs
251 lines (209 loc) · 4.89 KB
/
tree.rs
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
use super::{CommitId, RepoPath};
use crate::{
error::{Error, Result},
sync::repository::repo,
};
use git2::{Oid, Repository, Tree};
use scopetime::scope_time;
use std::{
cmp::Ordering,
path::{Path, PathBuf},
};
/// `tree_files` returns a list of `FileTree`
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TreeFile {
/// path of this file
pub path: PathBuf,
/// unix filemode
pub filemode: i32,
// internal object id
id: Oid,
}
/// guarantees sorting the result
pub fn tree_files(
repo_path: &RepoPath,
commit: CommitId,
) -> Result<Vec<TreeFile>> {
scope_time!("tree_files");
let repo = repo(repo_path)?;
let commit = repo.find_commit(commit.into())?;
let tree = commit.tree()?;
let mut files: Vec<TreeFile> = Vec::new();
tree_recurse(&repo, &PathBuf::from("./"), &tree, &mut files)?;
sort_file_list(&mut files);
Ok(files)
}
fn sort_file_list(files: &mut [TreeFile]) {
files.sort_by(|a, b| path_cmp(&a.path, &b.path));
}
// applies topologically order on paths sorting
fn path_cmp(a: &Path, b: &Path) -> Ordering {
let mut comp_a = a.components().peekable();
let mut comp_b = b.components().peekable();
loop {
let a = comp_a.next();
let b = comp_b.next();
let a_is_file = comp_a.peek().is_none();
let b_is_file = comp_b.peek().is_none();
if a_is_file && !b_is_file {
return Ordering::Greater;
} else if !a_is_file && b_is_file {
return Ordering::Less;
}
let cmp = a.cmp(&b);
if cmp != Ordering::Equal {
return cmp;
}
}
}
/// will only work on utf8 content
pub fn tree_file_content(
repo_path: &RepoPath,
file: &TreeFile,
) -> Result<String> {
scope_time!("tree_file_content");
let repo = repo(repo_path)?;
let blob = repo.find_blob(file.id)?;
if blob.is_binary() {
return Err(Error::BinaryFile);
}
let content = String::from_utf8_lossy(blob.content()).to_string();
Ok(content)
}
///
fn tree_recurse(
repo: &Repository,
path: &Path,
tree: &Tree,
out: &mut Vec<TreeFile>,
) -> Result<()> {
out.reserve(tree.len());
for e in tree {
let p = String::from_utf8_lossy(e.name_bytes());
let path = path.join(p.to_string());
match e.kind() {
Some(git2::ObjectType::Blob) => {
let id = e.id();
let filemode = e.filemode();
out.push(TreeFile { path, filemode, id });
}
Some(git2::ObjectType::Tree) => {
let obj = e.to_object(repo)?;
let tree = obj.peel_to_tree()?;
tree_recurse(repo, &path, &tree, out)?;
}
Some(_) | None => (),
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sync::tests::{repo_init, write_commit_file};
use pretty_assertions::{assert_eq, assert_ne};
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
let c1 =
write_commit_file(&repo, "test.txt", "content", "c1");
let files = tree_files(repo_path, c1).unwrap();
assert_eq!(files.len(), 1);
assert_eq!(files[0].path, PathBuf::from("./test.txt"));
let c2 =
write_commit_file(&repo, "test.txt", "content2", "c2");
let content =
tree_file_content(repo_path, &files[0]).unwrap();
assert_eq!(&content, "content");
let files_c2 = tree_files(repo_path, c2).unwrap();
assert_eq!(files_c2.len(), 1);
assert_ne!(files_c2[0], files[0]);
}
#[test]
fn test_sorting() {
let mut list = vec!["file", "folder/file", "folder/afile"]
.iter()
.map(|f| TreeFile {
path: PathBuf::from(f),
filemode: 0,
id: Oid::zero(),
})
.collect::<Vec<_>>();
sort_file_list(&mut list);
assert_eq!(
list.iter()
.map(|f| f.path.to_string_lossy())
.collect::<Vec<_>>(),
vec![
String::from("folder/afile"),
String::from("folder/file"),
String::from("file")
]
);
}
#[test]
fn test_sorting_folders() {
let mut list = vec!["bfolder/file", "afolder/file"]
.iter()
.map(|f| TreeFile {
path: PathBuf::from(f),
filemode: 0,
id: Oid::zero(),
})
.collect::<Vec<_>>();
sort_file_list(&mut list);
assert_eq!(
list.iter()
.map(|f| f.path.to_string_lossy())
.collect::<Vec<_>>(),
vec![
String::from("afolder/file"),
String::from("bfolder/file"),
]
);
}
#[test]
fn test_sorting_folders2() {
let mut list = vec!["bfolder/sub/file", "afolder/file"]
.iter()
.map(|f| TreeFile {
path: PathBuf::from(f),
filemode: 0,
id: Oid::zero(),
})
.collect::<Vec<_>>();
sort_file_list(&mut list);
assert_eq!(
list.iter()
.map(|f| f.path.to_string_lossy())
.collect::<Vec<_>>(),
vec![
String::from("afolder/file"),
String::from("bfolder/sub/file"),
]
);
}
#[test]
fn test_path_cmp() {
assert_eq!(
path_cmp(
&PathBuf::from("bfolder/sub/file"),
&PathBuf::from("afolder/file")
),
Ordering::Greater
);
}
#[test]
fn test_path_file_cmp() {
assert_eq!(
path_cmp(
&PathBuf::from("a"),
&PathBuf::from("afolder/file")
),
Ordering::Greater
);
}
}