ok/jj
1
0
Fork 0
forked from mirrors/jj

cleanup: use canonicalize() method instead of free function

I had somehow not noticed that `Path` and `PathBuf` have
`canonicalize()` methods. Using them saves a few characters of code.
This commit is contained in:
Martin von Zweigbergk 2022-03-29 23:13:13 -07:00 committed by Martin von Zweigbergk
parent 2c0b296f35
commit 6cd4e03c25
4 changed files with 8 additions and 8 deletions

View file

@ -91,7 +91,7 @@ impl GitBackend {
let mut buf = Vec::new();
git_target_file.read_to_end(&mut buf).unwrap();
let git_repo_path_str = String::from_utf8(buf).unwrap();
let git_repo_path = std::fs::canonicalize(store_path.join(git_repo_path_str)).unwrap();
let git_repo_path = store_path.join(git_repo_path_str).canonicalize().unwrap();
let repo = git2::Repository::open(git_repo_path).unwrap();
// TODO: Delete this migration code in early 2022 or so
if let Ok(notes) = repo.notes(Some(COMMITS_NOTES_REF)) {

View file

@ -168,7 +168,7 @@ impl Workspace {
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let jj_dir = create_jj_dir(&workspace_root)?;
let repo_dir = std::fs::canonicalize(repo.repo_path()).unwrap();
let repo_dir = repo.repo_path().canonicalize().unwrap();
let mut repo_file = File::create(jj_dir.join("repo")).unwrap();
repo_file
.write_all(repo_dir.to_str().unwrap().as_bytes())
@ -208,7 +208,7 @@ impl Workspace {
let mut buf = Vec::new();
repo_file.read_to_end(&mut buf).unwrap();
let repo_path_str = String::from_utf8(buf).unwrap();
repo_dir = std::fs::canonicalize(jj_dir.join(repo_path_str)).unwrap();
repo_dir = jj_dir.join(repo_path_str).canonicalize().unwrap();
if !repo_dir.is_dir() {
return Err(WorkspaceLoadError::RepoDoesNotExist(repo_dir));
}

View file

@ -74,7 +74,7 @@ fn test_init_additional_workspace(use_git: bool) {
assert_eq!(ws2.workspace_id(), ws2_id);
assert_eq!(
*ws2.repo_path(),
std::fs::canonicalize(workspace.repo_path()).unwrap()
workspace.repo_path().canonicalize().unwrap()
);
assert_eq!(*ws2.workspace_root(), ws2_root);
let same_workspace = Workspace::load(&settings, ws2_root);
@ -83,7 +83,7 @@ fn test_init_additional_workspace(use_git: bool) {
assert_eq!(same_workspace.workspace_id(), ws2_id);
assert_eq!(
*same_workspace.repo_path(),
std::fs::canonicalize(workspace.repo_path()).unwrap()
workspace.repo_path().canonicalize().unwrap()
);
assert_eq!(same_workspace.workspace_root(), ws2.workspace_root());
}

View file

@ -1716,14 +1716,14 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(),
} else {
fs::create_dir(&wc_path).unwrap();
}
let wc_path = std::fs::canonicalize(&wc_path).unwrap();
let wc_path = wc_path.canonicalize().unwrap();
if let Some(git_store_str) = &args.git_repo {
let mut git_store_path = ui.cwd().join(git_store_str);
if !git_store_path.ends_with(".git") {
git_store_path = git_store_path.join(".git");
}
git_store_path = std::fs::canonicalize(&git_store_path).unwrap();
git_store_path = git_store_path.canonicalize().unwrap();
// If the git repo is inside the workspace, use a relative path to it so the
// whole workspace can be moved without breaking.
if let Ok(relative_path) = git_store_path.strip_prefix(&wc_path) {
@ -1757,7 +1757,7 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(),
} else {
Workspace::init_local(ui.settings(), wc_path.clone())?;
};
let cwd = std::fs::canonicalize(&ui.cwd()).unwrap();
let cwd = ui.cwd().canonicalize().unwrap();
let relative_wc_path = ui::relative_path(&cwd, &wc_path);
writeln!(ui, "Initialized repo in \"{}\"", relative_wc_path.display())?;
Ok(())