Refactor code for improved readability and maintainability

- Simplified multi-line function calls in `PatchTree.lua` and `SHA1.luau` for better clarity.
- Enhanced the `init.lua` file by formatting the `PatchTree.build` function call across multiple lines.
- Updated `change_processor.rs` and `api.rs` to improve the formatting of function calls and enhance code consistency.
- Made minor adjustments in test files to streamline assertions and improve readability.

These changes contribute to a cleaner codebase and enhance overall maintainability.
This commit is contained in:
Joe 2026-02-17 21:05:28 -06:00
parent 91c599ecca
commit 6dec06865f
7 changed files with 111 additions and 96 deletions

View file

@ -1822,7 +1822,10 @@ impl JobThreadContext {
);
} else if patch_set.stage_ids.contains(&id) {
if let Some(ref repo_root) = self.git_repo_root {
crate::git::git_add(repo_root, std::slice::from_ref(write_path));
crate::git::git_add(
repo_root,
std::slice::from_ref(write_path),
);
}
}
} else {

View file

@ -155,23 +155,29 @@ struct ResolvedInstance {
/// Computes git metadata for the two-way sync confirmation UI.
///
/// `repo_root` must be the git repository root (from `git_repo_root()`),
/// cached at session start by `ServeSession`.
///
/// Uses a two-phase approach to avoid holding the tree lock during I/O:
/// 1. Run git commands to get changed files (no lock)
/// 2. Briefly lock tree to resolve file paths to instance Refs and class names
/// 3. Run git show for each changed script and compute hashes (no lock)
pub fn compute_git_metadata(
tree_handle: &Arc<Mutex<RojoTree>>,
project_root: &Path,
) -> Option<GitMetadata> {
let repo_root_raw = git_repo_root(project_root)?;
let repo_root = std::fs::canonicalize(&repo_root_raw).unwrap_or(repo_root_raw);
let changed_files = git_changed_files(&repo_root)?;
pub fn compute_git_metadata(tree_handle: &Arc<Mutex<RojoTree>>, repo_root: &Path) -> GitMetadata {
let changed_files = match git_changed_files(repo_root) {
Some(files) => files,
None => {
return GitMetadata {
changed_ids: Vec::new(),
script_committed_hashes: HashMap::new(),
};
}
};
if changed_files.is_empty() {
return Some(GitMetadata {
return GitMetadata {
changed_ids: Vec::new(),
script_committed_hashes: HashMap::new(),
});
};
}
// Brief lock: resolve changed file paths to instance Refs and class names
@ -207,11 +213,11 @@ pub fn compute_git_metadata(
let mut hashes = Vec::with_capacity(2);
if let Some(head_content) = git_show_head(&repo_root, &ri.rel_path) {
if let Some(head_content) = git_show_head(repo_root, &ri.rel_path) {
hashes.push(compute_blob_sha1(&head_content));
}
if let Some(staged_content) = git_show_staged(&repo_root, &ri.rel_path) {
if let Some(staged_content) = git_show_staged(repo_root, &ri.rel_path) {
let staged_hash = compute_blob_sha1(&staged_content);
if hashes.is_empty() || hashes[0] != staged_hash {
hashes.push(staged_hash);
@ -223,10 +229,10 @@ pub fn compute_git_metadata(
}
}
Some(GitMetadata {
GitMetadata {
changed_ids,
script_committed_hashes,
})
}
}
#[cfg(test)]
@ -251,13 +257,7 @@ mod tests {
.output()
.unwrap();
Command::new("git")
.args([
"-C",
&dir.to_string_lossy(),
"config",
"user.name",
"Test",
])
.args(["-C", &dir.to_string_lossy(), "config", "user.name", "Test"])
.output()
.unwrap();
}
@ -382,10 +382,7 @@ mod tests {
// Subdirectory should resolve to the same repo root
assert_eq!(
root.canonicalize().unwrap(),
git_repo_root(dir.path())
.unwrap()
.canonicalize()
.unwrap()
git_repo_root(dir.path()).unwrap().canonicalize().unwrap()
);
}

View file

@ -273,10 +273,9 @@ impl ApiService {
Vec::new()
};
let git_metadata = crate::git::compute_git_metadata(
&self.serve_session.tree_handle(),
self.serve_session.root_dir(),
);
let git_metadata = self.serve_session.git_repo_root().map(|repo_root| {
crate::git::compute_git_metadata(&self.serve_session.tree_handle(), repo_root)
});
msgpack_ok(&ServerInfoResponse {
server_version: SERVER_VERSION.to_owned(),
@ -742,33 +741,35 @@ impl ApiService {
let stage_ids: HashSet<Ref> = request.stage_ids.into_iter().collect();
let paths_to_stage: Vec<PathBuf> = if !stage_ids.is_empty() {
let tree = self.serve_session.tree();
stage_ids
.iter()
.filter_map(|&id| {
tree.get_instance(id).and_then(|inst| {
inst.metadata()
.instigating_source
.as_ref()
.map(|src| src.path().to_path_buf())
})
})
.collect()
} else {
Vec::new()
};
tree_mutation_sender
.send(PatchSet {
removed_instances: actually_removed,
added_instances,
updated_instances,
stage_ids: stage_ids.clone(),
stage_ids,
})
.unwrap();
if let Some(repo_root) = self.serve_session.git_repo_root() {
if !stage_ids.is_empty() {
let tree = self.serve_session.tree();
let paths_to_stage: Vec<PathBuf> = stage_ids
.iter()
.filter_map(|&id| {
tree.get_instance(id).and_then(|inst| {
inst.metadata().instigating_source.as_ref().map(|src| {
src.path().to_path_buf()
})
})
})
.collect();
drop(tree);
if !paths_to_stage.is_empty() {
crate::git::git_add(repo_root, &paths_to_stage);
}
if !paths_to_stage.is_empty() {
crate::git::git_add(repo_root, &paths_to_stage);
}
}