refine ui

restore in a Dialog
This commit is contained in:
Christoph Brandau
2026-06-29 17:53:02 +02:00
parent b0491d1479
commit 8d56c3f39f
8 changed files with 293 additions and 97 deletions
+89
View File
@@ -753,6 +753,66 @@ pub fn diff_file_against_working_tree(
})
}
#[tauri::command]
pub fn compare_file_to_head(
path: String,
commit: String,
file: String,
) -> Result<GitCommitComparison, String> {
let repo = resolve_repo(&path)?;
validate_files(std::slice::from_ref(&file))?;
let commit_hash = verify_commit(&repo, &commit)?;
let head_hash = verify_commit(&repo, "HEAD")?;
let name_status = run_git_with_paths(
&repo,
&[
"diff",
"--name-status",
"-M",
"-z",
commit_hash.as_str(),
head_hash.as_str(),
],
std::slice::from_ref(&file),
)?;
let numstat = run_git_with_paths(
&repo,
&[
"diff",
"--numstat",
"-M",
"-z",
commit_hash.as_str(),
head_hash.as_str(),
],
std::slice::from_ref(&file),
)?;
let patch_output = run_git_with_paths(
&repo,
&[
"diff",
"-M",
FULL_FILE_DIFF_CONTEXT,
commit_hash.as_str(),
head_hash.as_str(),
],
std::slice::from_ref(&file),
)?;
let files = parse_diff_files(&name_status, &numstat)?;
let patch = String::from_utf8_lossy(&patch_output).to_string();
Ok(GitCommitComparison {
from_short: short_hash(&commit_hash),
to_short: "HEAD".to_string(),
from_hash: commit_hash,
to_hash: head_hash,
files,
patch,
})
}
#[tauri::command]
pub fn read_conflict(path: String, file: String) -> Result<ConflictFile, String> {
let repo = resolve_repo(&path)?;
@@ -2497,6 +2557,35 @@ mod tests {
assert!(comparison.patch.contains("working tree change"));
}
#[test]
fn compare_file_to_head_reports_selected_file_against_current_commit() {
let repo = init_temp_repo("compare_file_to_head");
commit_initial_file(&repo.path);
let first_commit = git_output_test(&repo.path, ["rev-parse", "HEAD"]);
fs::write(repo.path.join("old.txt"), "original\nsecond line\n")
.expect("tracked file should change");
fs::write(repo.path.join("other.txt"), "other file\n")
.expect("other file should be written");
run_git_test(&repo.path, ["add", "old.txt", "other.txt"]);
run_git_test(&repo.path, ["commit", "-q", "-m", "second"]);
let head_commit = git_output_test(&repo.path, ["rev-parse", "HEAD"]);
let comparison = compare_file_to_head(
repo.path.to_string_lossy().to_string(),
first_commit,
"old.txt".to_string(),
)
.unwrap();
assert_eq!(comparison.to_hash, head_commit);
assert_eq!(comparison.to_short, "HEAD");
assert_eq!(comparison.files.len(), 1);
assert_eq!(comparison.files[0].path, "old.txt");
assert!(comparison.patch.contains("second line"));
assert!(!comparison.patch.contains("other file"));
}
#[test]
fn read_and_resolve_conflict_round_trip() {
let repo = init_temp_repo("resolve_conflict");
+7 -5
View File
@@ -3,11 +3,12 @@
mod git;
use git::{
cancel_code_search, checkout_branch, commit, compare_commits, diff_file_against_working_tree,
get_status, list_branches, list_commits, list_file_history, list_repository_files,
merge_branch, open_repository, pull, push, read_conflict, resolve_conflict,
resolve_conflict_side, restore_file_from_commit, restore_files, restore_to_commit,
search_code_introductions, stage_files, unstage_files, SearchCancellationState,
cancel_code_search, checkout_branch, commit, compare_commits, compare_file_to_head,
diff_file_against_working_tree, get_status, list_branches, list_commits, list_file_history,
list_repository_files, merge_branch, open_repository, pull, push, read_conflict,
resolve_conflict, resolve_conflict_side, restore_file_from_commit, restore_files,
restore_to_commit, search_code_introductions, stage_files, unstage_files,
SearchCancellationState,
};
fn main() {
@@ -32,6 +33,7 @@ fn main() {
list_repository_files,
list_file_history,
compare_commits,
compare_file_to_head,
diff_file_against_working_tree,
search_code_introductions,
cancel_code_search,