feat: impl pull request file tree

This commit is contained in:
2026-05-28 00:37:35 +01:00
parent c4ebe91669
commit f8a1c3f42b
12 changed files with 642 additions and 249 deletions

View File

@@ -17,12 +17,20 @@ pub(crate) enum FileType {
pub(crate) struct SortedByPath<T>(Vec<T>);
#[derive(Clone)]
pub(crate) struct FileTreeItem {
pub(crate) kind: FileTreeItemKind,
pub(crate) full_path: Arc<str>,
pub(crate) name: Arc<str>,
pub(crate) level: usize,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum FileTreeItemKind {
File,
Directory,
}
pub(crate) fn classify_content(content: &[u8]) -> ContentType {
if content.is_empty() {
ContentType::Text
@@ -35,17 +43,17 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
ContentType::Text
} else {
match memchr(0, &content[..content.len().min(8192)]) {
| None => ContentType::Text,
| Some(_) => ContentType::Binary,
| None => ContentType::Text,
| Some(_) => ContentType::Binary,
}
}
}
pub(crate) fn file_type_from_path(path: &str) -> FileType {
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
| Some("rs") => FileType::Rust,
| Some("js") | Some("jsx") => FileType::JavaScript,
| _ => FileType::Unknown,
| Some("rs") => FileType::Rust,
| Some("js") | Some("jsx") => FileType::JavaScript,
| _ => FileType::Unknown,
}
}
@@ -63,18 +71,18 @@ pub(crate) fn line_ranges(content: &[u8]) -> Vec<std::ops::Range<usize>> {
let c = content[i];
match (c, content.get(i + 1)) {
| (b'\r', Some(b'\n')) => {
// if \r found, check if its \r\n or if its a lone \r
// if \r\n, then treat as one line break
ranges.push(line_start..i + 1);
// because we already counted the \n byte, the next iter into it needs to be skipped
skip_next = true;
line_start = i + 2;
}
| _ => {
ranges.push(line_start..i);
line_start = i + 1;
}
| (b'\r', Some(b'\n')) => {
// if \r found, check if its \r\n or if its a lone \r
// if \r\n, then treat as one line break
ranges.push(line_start..i + 1);
// because we already counted the \n byte, the next iter into it needs to be skipped
skip_next = true;
line_start = i + 2;
}
| _ => {
ranges.push(line_start..i);
line_start = i + 1;
}
}
}
@@ -93,33 +101,38 @@ pub(crate) fn sort_by_path<T>(mut items: Vec<T>, key: impl Fn(&T) -> &str) -> So
let b_is_root_file = !b_path.contains('/');
match (a_is_root_file, b_is_root_file) {
| (true, false) => return std::cmp::Ordering::Greater,
| (false, true) => return std::cmp::Ordering::Less,
| _ => {}
| (true, false) => return std::cmp::Ordering::Greater,
| (false, true) => return std::cmp::Ordering::Less,
| _ => {}
}
let mut a_parts = a_path.split('/');
let mut b_parts = b_path.split('/');
let mut a_parts = a_path.split('/').peekable();
let mut b_parts = b_path.split('/').peekable();
loop {
match (a_parts.next(), b_parts.next()) {
| (Some(a), Some(b)) => {
if a != b {
return a.cmp(b);
| (Some(a), Some(b)) => {
if a != b {
match (a_parts.peek().is_some(), b_parts.peek().is_some()) {
| (true, false) => return std::cmp::Ordering::Less,
| (false, true) => return std::cmp::Ordering::Greater,
| _ => {}
}
return a.cmp(b);
}
| (Some(_), None) => return std::cmp::Ordering::Greater,
| (None, Some(_)) => return std::cmp::Ordering::Less,
| (None, None) => return std::cmp::Ordering::Equal,
}
| (Some(_), None) => return std::cmp::Ordering::Greater,
| (None, Some(_)) => return std::cmp::Ordering::Less,
| (None, None) => return std::cmp::Ordering::Equal,
}
}
});
SortedByPath(items)
}
pub(crate) fn build_file_tree_from_sorted_paths<T>(paths: &SortedByPath<T>) -> Vec<FileTreeItem>
where
T: AsRef<str>,
{
pub(crate) fn build_file_tree<T>(
paths: &SortedByPath<T>,
key: impl Fn(&T) -> &str,
) -> Vec<FileTreeItem> {
let mut stack: Vec<&str> = Vec::with_capacity(50);
let mut leafs: Vec<&str> = Vec::with_capacity(50);
@@ -127,7 +140,7 @@ where
fn strip_path_prefix<'a>(path: &'a str, prefix: &str) -> &'a str {
path.strip_prefix(prefix)
.and_then(|it| it.strip_prefix('/'))
.map(|it| it.strip_prefix('/').unwrap_or(it))
.unwrap_or(path)
}
@@ -137,115 +150,139 @@ where
items: &mut Vec<FileTreeItem>,
emitted_depth: usize,
base_depth: usize,
) {
) -> bool {
let mut base_dir_created = false;
if leafs.is_empty() && stack.is_empty() {
return;
return false;
}
let stack_dir_path = Arc::<str>::from(stack.join("/"));
let (common_dir_path, stack_dir_name) =
if (base_depth == 0 || base_depth == stack.len()) && emitted_depth == 0 {
(None, Arc::clone(&stack_dir_path))
(None, Some(Arc::clone(&stack_dir_path)))
} else {
let common_dir_path = if base_depth == stack.len() {
let common_dir_path = if base_depth == stack.len() || emitted_depth == stack.len() {
Arc::<str>::from(stack[..emitted_depth].join("/"))
} else {
Arc::<str>::from(stack[..base_depth].join("/"))
};
let stack_dir_name =
Arc::<str>::from(strip_path_prefix(&stack_dir_path, &common_dir_path));
(Some(common_dir_path), stack_dir_name)
(
Some(common_dir_path),
if stack_dir_name.len() == 0 {
None
} else {
Some(stack_dir_name)
},
)
};
let stack_dir_depth = if let Some(common_dir_path) = common_dir_path
&& emitted_depth == 0
{
items.push(FileTreeItem {
kind: FileTreeItemKind::Directory,
full_path: Arc::clone(&common_dir_path),
name: common_dir_path,
level: base_depth.saturating_sub(1),
});
base_dir_created = true;
base_depth
} else {
emitted_depth
};
items.push(FileTreeItem {
full_path: Arc::clone(&stack_dir_path),
name: stack_dir_name,
level: stack_dir_depth,
});
if let Some(stack_dir_name) = stack_dir_name {
items.push(FileTreeItem {
kind: FileTreeItemKind::Directory,
full_path: Arc::clone(&stack_dir_path),
name: stack_dir_name,
level: stack_dir_depth,
});
}
for leaf in leafs.drain(..) {
items.push(FileTreeItem {
kind: FileTreeItemKind::File,
full_path: Arc::<str>::from(leaf),
name: strip_path_prefix(&leaf, &stack_dir_path).into(),
level: stack.len(),
});
}
base_dir_created
}
let mut base_depth = 0;
let mut emitted_depth = 0;
for path in paths.0.iter() {
let path = path.as_ref();
let path = key(path);
match path.rsplit_once('/') {
| None => {
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
stack.clear();
// top level file
items.push(FileTreeItem {
full_path: path.into(),
name: path.into(),
level: 0,
});
| None => {
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
stack.clear();
// top level file
items.push(FileTreeItem {
kind: FileTreeItemKind::File,
full_path: path.into(),
name: path.into(),
level: 0,
});
}
| Some((parent, _)) => {
let mut common_depth = 0;
for (i, seg) in parent.split('/').enumerate() {
let stack_item = stack.get(i);
if stack_item.is_none() {
// segment is unseen, push to stack
stack.push(seg);
common_depth += 1;
} else if Some(&seg) == stack.get(i) {
// segment matches stack, continue comparison
common_depth += 1;
} else {
// segment differs from stack, stop comparison
break;
}
}
| Some((parent, _)) => {
let mut common_depth = 0;
for (i, seg) in parent.split('/').enumerate() {
let stack_item = stack.get(i);
if stack_item.is_none() {
// segment is unseen, push to stack
stack.push(seg);
common_depth += 1;
} else if Some(&seg) == stack.get(i) {
// segment matches stack, continue comparison
common_depth += 1;
} else {
// segment differs from stack, stop comparison
break;
}
}
if common_depth == stack.len() {
// current path is in same directory as stack, add to leafs
leafs.push(path);
base_depth = common_depth;
} else {
// e.g. stack = ["a", "b", "c"], path = ["a", "c"]
// common dir path = "a/", stack dir path = "a/b/c", common count = 1
// push common dir a to items
// also push stack dir a/b/c to items but strip a from name so that it becomes "b/c" with level equal to common_count
// finally push any leaf under a/b/c
if common_depth == stack.len() {
// current path is in same directory as stack, add to leafs
leafs.push(path);
base_depth = common_depth;
} else {
// e.g. stack = ["a", "b", "c"], path = ["a", "c"]
// common dir path = "a/", stack dir path = "a/b/c", common count = 1
// push common dir a to items
// also push stack dir a/b/c to items but strip a from name so that it becomes "b/c" with level equal to common_count
// finally push any leaf under a/b/c
let base_dir_created =
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, common_depth);
// pop top of stack minus common dir
stack.truncate(common_depth);
// pop top of stack minus common dir
stack.truncate(common_depth);
if base_dir_created {
emitted_depth = common_depth;
for seg in parent.split('/').skip(common_depth) {
stack.push(seg);
}
leafs.push(path);
} else {
emitted_depth = 0;
}
for seg in parent.split('/').skip(common_depth) {
stack.push(seg);
}
leafs.push(path);
}
}
}
}
flush_leafs(&mut leafs, &stack, &mut items, emitted_depth, base_depth);
@@ -253,6 +290,12 @@ where
items
}
impl<T> Default for SortedByPath<T> {
fn default() -> Self {
Self(Vec::new())
}
}
impl<T> Deref for SortedByPath<T> {
type Target = [T];
fn deref(&self) -> &[T] {

View File

@@ -1,4 +1,5 @@
use super::*;
use serde_json::Value;
fn assert_tree(paths: &[&str], expected: &[(&str, &str, usize)]) {
let sorted_paths = sort_by_path(paths.to_vec(), |path| *path);
@@ -8,7 +9,7 @@ fn assert_tree(paths: &[&str], expected: &[(&str, &str, usize)]) {
"test inputs must already be sorted by sort_by_path",
);
let actual = build_file_tree_from_sorted_paths(&sorted_paths)
let actual = build_file_tree(&sorted_paths, |path| *path)
.into_iter()
.map(|item| {
(
@@ -54,6 +55,19 @@ fn sorts_paths_by_components_with_root_files_at_bottom() {
);
}
#[test]
fn sorts_directory_branches_before_sibling_files() {
let sorted_paths = sort_by_path(
vec!["src/query.rs", "src/screen/dashboard/issue_list.rs"],
|path| *path,
);
assert_eq!(
sorted_paths.0,
vec!["src/screen/dashboard/issue_list.rs", "src/query.rs",],
);
}
#[test]
fn builds_empty_tree_for_empty_paths() {
assert_tree(&[], &[]);
@@ -138,3 +152,58 @@ fn keeps_emitted_parent_for_mixed_multi_file_and_singleton_branches() {
],
);
}
#[test]
fn builds_tree_for_pull_request_fixture_with_root_and_nested_file() {
let fixture: Value = serde_json::from_str(include_str!(
"../../fixtures/github/issues.pull_request_file_tree.PR_kwDONovem84.json"
))
.expect("fixture json should parse");
let paths = fixture
.get("node")
.and_then(|node| node.get("files"))
.and_then(|files| files.get("edges"))
.and_then(Value::as_array)
.expect("fixture should contain file edges")
.iter()
.filter_map(|edge| edge.get("node"))
.filter_map(|node| node.get("path"))
.filter_map(Value::as_str)
.collect::<Vec<_>>();
let sorted_paths = sort_by_path(paths, |path| *path);
assert_eq!(
sorted_paths.0.as_slice(),
&["src/screen/dashboard/issue_list.rs", "src/query.rs"],
);
let actual = build_file_tree(&sorted_paths, |path| *path)
.into_iter()
.map(|item| {
(
item.full_path.to_string(),
item.name.to_string(),
item.level,
)
})
.collect::<Vec<_>>();
assert_eq!(
actual,
vec![
("src".to_string(), "src".to_string(), 0),
(
"src/screen/dashboard".to_string(),
"screen/dashboard".to_string(),
1,
),
(
"src/screen/dashboard/issue_list.rs".to_string(),
"issue_list.rs".to_string(),
3,
),
("src/query.rs".to_string(), "query.rs".to_string(), 1),
],
);
}