Skip to content

Commit 9c2f053

Browse files
evanchen7codex
andauthored
fix(desktop): show complete repository trees (block#5102)
## Summary Large repositories were silently limited to the first 250 lexicographically sorted paths, which could leave the Files tab showing only dot-directories. Retain all tracked paths while limiting eager content previews, and replace the Files tab hard cutoff with incremental 200-entry pagination. ### Related issue Fixes block#4960. No duplicate PR found. ### Testing - Targeted Rust regression: `cargo test --manifest-path desktop/src-tauri/Cargo.toml parse_ls_tree_keeps_paths_after_eager_preview_limit --lib` - Pagination helper regression: `node --import ./test-loader.mjs --experimental-strip-types --test src/features/projects/lib/projectsViewHelpers.test.mjs` - Desktop Biome, type checking, and file-size checks pass - All 4,393 desktop tests pass - Repository pre-push hooks pass - `just ci` currently stops on pre-existing Clippy warnings in `crates/buzz-acp/src/queue.rs` and `crates/buzz-agent/src/hints.rs`, outside this PR diff Generated with Codex --------- Signed-off-by: Evan Chen <[email protected]> Co-authored-by: Codex <[email protected]>
1 parent 9a1e861 commit 9c2f053

18 files changed

Lines changed: 721 additions & 162 deletions

‎desktop/src-tauri/src/commands/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod project_git;
5050
mod project_git_branches;
5151
mod project_git_diff;
5252
mod project_git_exec;
53+
mod project_git_file_content;
5354
mod project_git_merge_error;
5455
mod project_git_push;
5556
mod project_git_recipient_notes;
@@ -107,6 +108,7 @@ pub use profile::*;
107108
pub use project_git::*;
108109
pub use project_git_branches::*;
109110
pub use project_git_diff::*;
111+
pub use project_git_file_content::*;
110112
pub use project_git_recipient_notes::*;
111113
pub use project_git_workflow::*;
112114
pub use project_terminal::*;

‎desktop/src-tauri/src/commands/project_git.rs‎

Lines changed: 33 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ use super::project_git_exec::{
22
build_git_auth_config, clean_branch, clean_target_ref, run_git, validate_workspace_clone_url,
33
GitAuthConfig,
44
};
5+
use super::project_git_file_content::{checkout_project_repo, read_preview_content};
56
use super::project_git_push::push_project_local_repository_blocking;
67
use super::project_repo_paths::{canonical_repos_roots, find_local_repo_dir};
78
use crate::app_state::AppState;
89
use serde::Serialize;
910
use std::time::UNIX_EPOCH;
1011
use tauri::State;
12+
13+
// Bound eager content without truncating the repository tree.
14+
const MAX_EAGER_FILE_PREVIEWS: usize = 250;
15+
16+
#[cfg(test)]
17+
#[path = "project_git_tests.rs"]
18+
mod tests;
19+
1120
#[derive(Clone, Serialize)]
1221
pub struct ProjectRepoCommitInfo {
1322
pub hash: String,
@@ -134,30 +143,6 @@ fn has_untracked_files(output: &str) -> bool {
134143
output.lines().any(|line| line.starts_with("??"))
135144
}
136145

137-
fn read_preview_content(
138-
repo_dir: &std::path::Path,
139-
path: &str,
140-
size: Option<u64>,
141-
) -> Option<String> {
142-
const MAX_PREVIEW_BYTES: u64 = 64 * 1024;
143-
if size.is_some_and(|value| value > MAX_PREVIEW_BYTES) {
144-
return None;
145-
}
146-
147-
let full_path = repo_dir.join(path);
148-
let normalized = full_path.canonicalize().ok()?;
149-
let repo_root = repo_dir.canonicalize().ok()?;
150-
if !normalized.starts_with(repo_root) {
151-
return None;
152-
}
153-
154-
let bytes = std::fs::read(normalized).ok()?;
155-
if bytes.contains(&0) {
156-
return None;
157-
}
158-
String::from_utf8(bytes).ok()
159-
}
160-
161146
fn parse_commits(output: &str) -> Vec<ProjectRepoCommitInfo> {
162147
output
163148
.lines()
@@ -254,24 +239,26 @@ fn parse_worktree_files(
254239
.filter_map(|path| {
255240
let full_path = repo_dir.join(path);
256241
let metadata = std::fs::metadata(&full_path).ok()?;
257-
if !metadata.is_file() {
258-
return None;
259-
}
242+
metadata.is_file().then_some((path, full_path, metadata))
243+
})
244+
.enumerate()
245+
.map(|(index, (path, full_path, metadata))| {
260246
let size = Some(metadata.len());
261247
let latest_commit = latest_commit_by_path.get(path).cloned();
262-
Some(ProjectRepoFileInfo {
248+
ProjectRepoFileInfo {
263249
path: path.to_string(),
264250
kind: "blob".to_string(),
265251
size,
266-
preview_content: read_preview_content(repo_dir, path, size),
252+
preview_content: (index < MAX_EAGER_FILE_PREVIEWS)
253+
.then(|| read_preview_content(repo_dir, path, size))
254+
.flatten(),
267255
last_changed_at: latest_commit
268256
.as_ref()
269257
.map(|commit| commit.timestamp)
270258
.or_else(|| path_modified_at(&full_path)),
271259
latest_commit,
272-
})
260+
}
273261
})
274-
.take(250)
275262
.collect()
276263
}
277264

@@ -314,6 +301,7 @@ fn parse_ls_tree(
314301
output: &str,
315302
latest_commit_by_path: &std::collections::HashMap<String, ProjectRepoCommitInfo>,
316303
) -> Vec<ProjectRepoFileInfo> {
304+
let mut blob_index = 0;
317305
output
318306
.lines()
319307
.filter_map(|line| {
@@ -323,11 +311,12 @@ fn parse_ls_tree(
323311
let kind = parts.next()?.to_string();
324312
let _object = parts.next()?;
325313
let size = parts.next().and_then(|value| value.parse::<u64>().ok());
326-
let preview_content = if kind == "blob" {
327-
read_preview_content(repo_dir, path, size)
328-
} else {
329-
None
330-
};
314+
if kind == "blob" {
315+
blob_index += 1;
316+
}
317+
let preview_content = (kind == "blob" && blob_index <= MAX_EAGER_FILE_PREVIEWS)
318+
.then(|| read_preview_content(repo_dir, path, size))
319+
.flatten();
331320
Some(ProjectRepoFileInfo {
332321
path: path.to_string(),
333322
kind,
@@ -339,7 +328,6 @@ fn parse_ls_tree(
339328
latest_commit: latest_commit_by_path.get(path).cloned(),
340329
})
341330
})
342-
.take(250)
343331
.collect()
344332
}
345333

@@ -727,61 +715,14 @@ pub async fn get_project_repo_snapshot(
727715
tauri::async_runtime::spawn_blocking(move || {
728716
let temp_dir = tempfile::tempdir().map_err(|error| format!("create temp dir: {error}"))?;
729717
let repo_dir = temp_dir.path().join("repo");
730-
let repo_path = repo_dir
731-
.to_str()
732-
.ok_or_else(|| "temporary repository path is not UTF-8".to_string())?;
733-
734-
let explicit_target = target_ref.as_deref().or(target_commit.as_deref());
735-
if let Some(fetch_ref) = explicit_target {
736-
run_git(
737-
&[
738-
"clone",
739-
"--filter=blob:none",
740-
"--no-checkout",
741-
clone_url.as_str(),
742-
repo_path,
743-
],
744-
None,
745-
&auth,
746-
)?;
747-
run_git(
748-
&["fetch", "--depth=100", "origin", fetch_ref],
749-
Some(&repo_dir),
750-
&auth,
751-
)?;
752-
if let Some(expected_commit) = target_commit.as_deref() {
753-
let fetched_commit = run_git(&["rev-parse", "FETCH_HEAD"], Some(&repo_dir), &auth)
754-
.ok()
755-
.and_then(|output| first_output_line(&output))
756-
.map(|commit| commit.to_ascii_lowercase())
757-
.ok_or_else(|| "Could not resolve the requested repository ref.".to_string())?;
758-
if fetched_commit != expected_commit {
759-
return Err(
760-
"The requested repository ref changed. Refresh and try again.".to_string(),
761-
);
762-
}
763-
}
764-
run_git(
765-
&["checkout", "--detach", "FETCH_HEAD"],
766-
Some(&repo_dir),
767-
&auth,
768-
)?;
769-
} else {
770-
let mut clone_args = vec!["clone", "--filter=blob:none"];
771-
if let Some(ref branch) = branch {
772-
clone_args.push("--branch");
773-
clone_args.push(branch.as_str());
774-
}
775-
clone_args.push(clone_url.as_str());
776-
clone_args.push(repo_path);
777-
if run_git(&clone_args, None, &auth).is_err() && branch.is_some() {
778-
run_git(
779-
&["clone", "--filter=blob:none", clone_url.as_str(), repo_path],
780-
None,
781-
&auth,
782-
)?;
783-
}
784-
}
718+
checkout_project_repo(
719+
&repo_dir,
720+
&clone_url,
721+
branch.as_deref(),
722+
target_ref.as_deref(),
723+
target_commit.as_deref(),
724+
&auth,
725+
)?;
785726

786727
let snapshot =
787728
snapshot_from_repo(&repo_dir, &auth, branch.as_deref(), base_branch.as_deref());
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
use super::project_git::first_output_line;
2+
use super::project_git_exec::{
3+
build_git_auth_config, clean_branch, clean_target_ref, run_git, validate_workspace_clone_url,
4+
GitAuthConfig,
5+
};
6+
use super::project_repo_paths::find_local_repo_dir;
7+
use crate::app_state::AppState;
8+
use tauri::State;
9+
10+
const MAX_PREVIEW_BYTES: u64 = 64 * 1024;
11+
12+
pub(crate) fn read_preview_content(
13+
repo_dir: &std::path::Path,
14+
path: &str,
15+
size: Option<u64>,
16+
) -> Option<String> {
17+
if size.is_some_and(|value| value > MAX_PREVIEW_BYTES) {
18+
return None;
19+
}
20+
21+
let full_path = repo_dir.join(path);
22+
if std::fs::symlink_metadata(&full_path)
23+
.ok()?
24+
.file_type()
25+
.is_symlink()
26+
{
27+
return None;
28+
}
29+
let normalized = full_path.canonicalize().ok()?;
30+
let repo_root = repo_dir.canonicalize().ok()?;
31+
if !normalized.starts_with(repo_root) {
32+
return None;
33+
}
34+
35+
let metadata = std::fs::metadata(&normalized).ok()?;
36+
if !metadata.is_file() || metadata.len() > MAX_PREVIEW_BYTES {
37+
return None;
38+
}
39+
let bytes = std::fs::read(normalized).ok()?;
40+
if bytes.contains(&0) {
41+
return None;
42+
}
43+
String::from_utf8(bytes).ok()
44+
}
45+
46+
pub(crate) fn validate_repo_file_path(path: &str) -> Result<(), String> {
47+
if path.is_empty()
48+
|| std::path::Path::new(path)
49+
.components()
50+
.any(|component| !matches!(component, std::path::Component::Normal(_)))
51+
{
52+
return Err("Repository file path must be a relative file path.".to_string());
53+
}
54+
Ok(())
55+
}
56+
57+
pub(crate) fn checkout_project_repo(
58+
repo_dir: &std::path::Path,
59+
clone_url: &str,
60+
branch: Option<&str>,
61+
target_ref: Option<&str>,
62+
target_commit: Option<&str>,
63+
auth: &GitAuthConfig,
64+
) -> Result<(), String> {
65+
let repo_path = repo_dir
66+
.to_str()
67+
.ok_or_else(|| "temporary repository path is not UTF-8".to_string())?;
68+
let explicit_target = target_ref.or(target_commit);
69+
70+
if let Some(fetch_ref) = explicit_target {
71+
run_git(
72+
&[
73+
"clone",
74+
"--filter=blob:none",
75+
"--no-checkout",
76+
clone_url,
77+
repo_path,
78+
],
79+
None,
80+
auth,
81+
)?;
82+
run_git(
83+
&["fetch", "--depth=100", "origin", fetch_ref],
84+
Some(repo_dir),
85+
auth,
86+
)?;
87+
if let Some(expected_commit) = target_commit {
88+
let fetched_commit = run_git(&["rev-parse", "FETCH_HEAD"], Some(repo_dir), auth)
89+
.ok()
90+
.and_then(|output| first_output_line(&output))
91+
.map(|commit| commit.to_ascii_lowercase())
92+
.ok_or_else(|| "Could not resolve the requested repository ref.".to_string())?;
93+
if fetched_commit != expected_commit {
94+
return Err(
95+
"The requested repository ref changed. Refresh and try again.".to_string(),
96+
);
97+
}
98+
}
99+
run_git(
100+
&["checkout", "--detach", "FETCH_HEAD"],
101+
Some(repo_dir),
102+
auth,
103+
)?;
104+
return Ok(());
105+
}
106+
107+
let mut clone_args = vec!["clone", "--filter=blob:none"];
108+
if let Some(branch) = branch {
109+
clone_args.push("--branch");
110+
clone_args.push(branch);
111+
}
112+
clone_args.push(clone_url);
113+
clone_args.push(repo_path);
114+
if run_git(&clone_args, None, auth).is_err() && branch.is_some() {
115+
run_git(
116+
&["clone", "--filter=blob:none", clone_url, repo_path],
117+
None,
118+
auth,
119+
)?;
120+
}
121+
Ok(())
122+
}
123+
124+
#[tauri::command]
125+
pub async fn get_project_repo_file_content(
126+
clone_url: String,
127+
default_branch: Option<String>,
128+
target_ref: Option<String>,
129+
target_commit: Option<String>,
130+
path: String,
131+
state: State<'_, AppState>,
132+
) -> Result<Option<String>, String> {
133+
validate_workspace_clone_url(&clone_url, &state)?;
134+
validate_repo_file_path(&path)?;
135+
let auth = build_git_auth_config(&state)?;
136+
let branch = clean_branch(default_branch);
137+
let target_ref = clean_target_ref(target_ref);
138+
let target_commit = target_commit
139+
.map(|value| value.to_ascii_lowercase())
140+
.filter(|value| matches!(value.len(), 40 | 64))
141+
.filter(|value| value.chars().all(|c| c.is_ascii_hexdigit()));
142+
143+
tauri::async_runtime::spawn_blocking(move || {
144+
let temp_dir = tempfile::tempdir().map_err(|error| format!("create temp dir: {error}"))?;
145+
let repo_dir = temp_dir.path().join("repo");
146+
checkout_project_repo(
147+
&repo_dir,
148+
&clone_url,
149+
branch.as_deref(),
150+
target_ref.as_deref(),
151+
target_commit.as_deref(),
152+
&auth,
153+
)?;
154+
Ok(read_preview_content(&repo_dir, &path, None))
155+
})
156+
.await
157+
.map_err(|error| format!("repo file content task failed: {error}"))?
158+
}
159+
160+
#[tauri::command]
161+
pub async fn get_project_local_repo_file_content(
162+
repos_dir: Option<String>,
163+
project_dtag: String,
164+
clone_url: Option<String>,
165+
path: String,
166+
) -> Result<Option<String>, String> {
167+
validate_repo_file_path(&path)?;
168+
tauri::async_runtime::spawn_blocking(move || {
169+
let Some(repo_dir) =
170+
find_local_repo_dir(repos_dir.as_deref(), &project_dtag, clone_url.as_deref())?
171+
else {
172+
return Ok(None);
173+
};
174+
Ok(read_preview_content(&repo_dir, &path, None))
175+
})
176+
.await
177+
.map_err(|error| format!("local repo file content task failed: {error}"))?
178+
}

0 commit comments

Comments
 (0)