refine ui
restore in a Dialog
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user