Merge pull request 'make the Commit restore more safe' (#6) from bug/blockingUI into master
publish / publish-tauri (, windows-latest) (release) Successful in 8m34s
publish / publish-tauri (, windows-latest) (release) Successful in 8m34s
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
+30
-4
@@ -988,7 +988,21 @@ pub fn restore_to_commit(path: String, commit: String) -> Result<GitStatus, Stri
|
||||
let repo = resolve_repo(&path)?;
|
||||
let commit_hash = verify_commit(&repo, &commit)?;
|
||||
|
||||
run_git(&repo, ["reset", "--hard", commit_hash.as_str()])?;
|
||||
// Non-destructive: bring the working tree back to how it looked at `commit` without
|
||||
// moving the branch pointer (unlike `git reset --hard`, which would rewrite history and
|
||||
// hide any newer commits from the log). The result lands as ordinary unstaged changes
|
||||
// that the user reviews in the status panel and stages/commits or discards explicitly.
|
||||
run_git(
|
||||
&repo,
|
||||
[
|
||||
"restore",
|
||||
"--source",
|
||||
commit_hash.as_str(),
|
||||
"--worktree",
|
||||
"--",
|
||||
".",
|
||||
],
|
||||
)?;
|
||||
status_for_repo(&repo)
|
||||
}
|
||||
|
||||
@@ -3913,7 +3927,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restore_to_commit_resets_branch_to_selected_commit() {
|
||||
fn restore_to_commit_leaves_branch_untouched_and_stages_change_as_unstaged() {
|
||||
let repo = init_temp_repo("restore_to_commit");
|
||||
commit_initial_file(&repo.path);
|
||||
let first_commit = git_output_test(&repo.path, ["rev-parse", "HEAD"]);
|
||||
@@ -3921,6 +3935,7 @@ mod tests {
|
||||
fs::write(repo.path.join("old.txt"), "second\n").expect("second file should be written");
|
||||
run_git_test(&repo.path, ["add", "old.txt"]);
|
||||
run_git_test(&repo.path, ["commit", "-q", "-m", "second"]);
|
||||
let second_commit = git_output_test(&repo.path, ["rev-parse", "HEAD"]);
|
||||
|
||||
let status = restore_to_commit(
|
||||
repo.path.to_string_lossy().to_string(),
|
||||
@@ -3930,9 +3945,20 @@ mod tests {
|
||||
let current_commit = git_output_test(&repo.path, ["rev-parse", "HEAD"]);
|
||||
let contents = fs::read_to_string(repo.path.join("old.txt")).unwrap();
|
||||
|
||||
assert_eq!(current_commit, first_commit);
|
||||
// The branch must stay exactly where it was: no commit is rewritten or hidden.
|
||||
assert_eq!(current_commit, second_commit);
|
||||
assert_ne!(current_commit, first_commit);
|
||||
// The old content lands in the worktree as a reviewable, unstaged change.
|
||||
assert_eq!(contents.replace("\r\n", "\n"), "original\n");
|
||||
assert!(status.clean, "{:?}", status.files);
|
||||
assert!(!status.clean, "{:?}", status.files);
|
||||
assert_eq!(
|
||||
status
|
||||
.files
|
||||
.iter()
|
||||
.find(|f| f.path == "old.txt")
|
||||
.and_then(|f| f.unstaged),
|
||||
Some(FileStatusKind::Modified)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user