Closes https://github.com/zed-industries/zed/issues/38690 Closes #37353 ### Background On Windows, paths are normally separated by `\`, unlike mac and linux where they are separated by `/`. When editing code in a project that uses a different path style than your local system (e.g. remoting from Windows to Linux, using WSL, and collaboration between windows and unix users), the correct separator for a path may differ from the "native" separator. Previously, to work around this, Zed converted paths' separators in numerous places. This was applied to both absolute and relative paths, leading to incorrect conversions in some cases. ### Solution Many code paths in Zed use paths that are *relative* to either a worktree root or a git repository. This PR introduces a dedicated type for these paths called `RelPath`, which stores the path in the same way regardless of host platform, and offers `Path`-like manipulation APIs. RelPath supports *displaying* the path using either separator, so that we can display paths in a style that is determined at runtime based on the current project. The representation of absolute paths is left untouched, for now. Absolute paths are different from relative paths because (except in contexts where we know that the path refers to the local filesystem) they should generally be treated as opaque strings. Currently we use a mix of types for these paths (std::path::Path, String, SanitizedPath). Release Notes: - N/A --------- Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Co-authored-by: Peter Tripp <petertripp@gmail.com> Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com> Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
101 lines
3.2 KiB
Rust
101 lines
3.2 KiB
Rust
use crate::{Oid, status::StatusCode};
|
|
use anyhow::{Context as _, Result};
|
|
use collections::HashMap;
|
|
use std::path::Path;
|
|
|
|
pub async fn get_messages(working_directory: &Path, shas: &[Oid]) -> Result<HashMap<Oid, String>> {
|
|
if shas.is_empty() {
|
|
return Ok(HashMap::default());
|
|
}
|
|
|
|
const MARKER: &str = "<MARKER>";
|
|
|
|
let output = util::command::new_smol_command("git")
|
|
.current_dir(working_directory)
|
|
.arg("show")
|
|
.arg("-s")
|
|
.arg(format!("--format=%B{}", MARKER))
|
|
.args(shas.iter().map(ToString::to_string))
|
|
.output()
|
|
.await
|
|
.context("starting git blame process")?;
|
|
|
|
anyhow::ensure!(
|
|
output.status.success(),
|
|
"'git show' failed with error {:?}",
|
|
output.status
|
|
);
|
|
|
|
Ok(shas
|
|
.iter()
|
|
.cloned()
|
|
.zip(
|
|
String::from_utf8_lossy(&output.stdout)
|
|
.trim()
|
|
.split_terminator(MARKER)
|
|
.map(|str| str.trim().replace("<", "<").replace(">", ">")),
|
|
)
|
|
.collect::<HashMap<Oid, String>>())
|
|
}
|
|
|
|
/// Parse the output of `git diff --name-status -z`
|
|
pub fn parse_git_diff_name_status(content: &str) -> impl Iterator<Item = (&str, StatusCode)> {
|
|
let mut parts = content.split('\0');
|
|
std::iter::from_fn(move || {
|
|
loop {
|
|
let status_str = parts.next()?;
|
|
let path = parts.next()?;
|
|
let status = match status_str {
|
|
"M" => StatusCode::Modified,
|
|
"A" => StatusCode::Added,
|
|
"D" => StatusCode::Deleted,
|
|
_ => continue,
|
|
};
|
|
return Some((path, status));
|
|
}
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_parse_git_diff_name_status() {
|
|
let input = concat!(
|
|
"M\x00Cargo.lock\x00",
|
|
"M\x00crates/project/Cargo.toml\x00",
|
|
"M\x00crates/project/src/buffer_store.rs\x00",
|
|
"D\x00crates/project/src/git.rs\x00",
|
|
"A\x00crates/project/src/git_store.rs\x00",
|
|
"A\x00crates/project/src/git_store/git_traversal.rs\x00",
|
|
"M\x00crates/project/src/project.rs\x00",
|
|
"M\x00crates/project/src/worktree_store.rs\x00",
|
|
"M\x00crates/project_panel/src/project_panel.rs\x00",
|
|
);
|
|
|
|
let output = parse_git_diff_name_status(input).collect::<Vec<_>>();
|
|
assert_eq!(
|
|
output,
|
|
&[
|
|
("Cargo.lock", StatusCode::Modified),
|
|
("crates/project/Cargo.toml", StatusCode::Modified),
|
|
("crates/project/src/buffer_store.rs", StatusCode::Modified),
|
|
("crates/project/src/git.rs", StatusCode::Deleted),
|
|
("crates/project/src/git_store.rs", StatusCode::Added),
|
|
(
|
|
"crates/project/src/git_store/git_traversal.rs",
|
|
StatusCode::Added,
|
|
),
|
|
("crates/project/src/project.rs", StatusCode::Modified),
|
|
("crates/project/src/worktree_store.rs", StatusCode::Modified),
|
|
(
|
|
"crates/project_panel/src/project_panel.rs",
|
|
StatusCode::Modified
|
|
),
|
|
]
|
|
);
|
|
}
|
|
}
|