blame: fixup windows paths (#984)

closes #981
This commit is contained in:
Stephan Dilly 2021-11-12 15:52:29 +01:00 committed by GitHub
parent 3c8eb7e049
commit a55dcf62a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- honor options (for untracked files) in `stage_all` command ([#933](https://github.com/extrawurst/gitui/issues/933)) - honor options (for untracked files) in `stage_all` command ([#933](https://github.com/extrawurst/gitui/issues/933))
- improved file diff speed dramatically ([#976](https://github.com/extrawurst/gitui/issues/976)) - improved file diff speed dramatically ([#976](https://github.com/extrawurst/gitui/issues/976))
- blaming files in sub-folders on windows ([#981](https://github.com/extrawurst/gitui/issues/981))
## [0.18] - 2021-10-11 ## [0.18] - 2021-10-11

View File

@ -39,6 +39,19 @@ pub struct FileBlame {
pub lines: Vec<(Option<BlameHunk>, String)>, pub lines: Vec<(Option<BlameHunk>, String)>,
} }
/// fixup `\` windows path seperators to git compatible `/`
fn fixup_windows_path(path: &str) -> String {
#[cfg(windows)]
{
path.replace("\\", "/")
}
#[cfg(not(windows))]
{
path.to_string()
}
}
/// ///
pub fn blame_file( pub fn blame_file(
repo_path: &str, repo_path: &str,
@ -50,7 +63,11 @@ pub fn blame_file(
let commit_id = utils::get_head_repo(&repo)?; let commit_id = utils::get_head_repo(&repo)?;
let spec = format!("{}:{}", commit_id.to_string(), file_path); let spec = format!(
"{}:{}",
commit_id.to_string(),
fixup_windows_path(file_path)
);
let object = repo.revparse_single(&spec)?; let object = repo.revparse_single(&spec)?;
let blob = repo.find_blob(object.id())?; let blob = repo.find_blob(object.id())?;
@ -214,4 +231,24 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn test_blame_windows_path_dividers() {
let file_path = Path::new("bar\\foo");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
std::fs::create_dir(&root.join("bar")).unwrap();
File::create(&root.join(file_path))
.unwrap()
.write_all(b"line 1\n")
.unwrap();
stage_add_file(repo_path, file_path).unwrap();
commit(repo_path, "first commit").unwrap();
assert!(blame_file(&repo_path, "bar\\foo").is_ok());
}
} }