Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix revparsing with windows paths in blame example #722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ fn run(args: &Args) -> Result<(), git2::Error> {
}
}

let spec = format!("{}:{}", commit_id, path.display());
let spec = if cfg!(unix) {
format!("{}:{}", commit_id, path.display())
} else {
format!(
"{}:{}",
commit_id,
path.display().to_string().replace("\\", "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like I mentioned in extrawurst/gitui#791 I was under the impression that preventing the need for such manual hacks is the reason we have a type like Path in the first place - is display() the problem here? I am using Path::to_str in gitui to get the correct type of path seperators

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, using revparse_single with the syntax <ref>:<path> implies using /, as it refers to the path of a git object.

blame_file uses a disk path and should (according to libgit2's conventions) accept \ on Windows (in any case git2-rs normalizes with a call to path_to_repo_path).

Normalization is required for this example to accept Windows-style paths.

What I can see as a possible fix:

  • normalize before the revparse call as in this PR (ugly)
  • normalize inside the revparse_single function (weird IMO)
  • remove revparse from this example
  • accept that windows-style paths are rejected in this example (with a comment/help message for the path argument to clear things up)

)
};
let blame = repo.blame_file(path, Some(&mut opts))?;
let object = repo.revparse_single(&spec[..])?;
let blob = repo.find_blob(object.id())?;
Expand Down