Skip to content

Commit

Permalink
no-op: Move external git repo canonicalization into Workspace::init_g…
Browse files Browse the repository at this point in the history
…it_external

* Move canonicalization of the external git repo path into the Workspace::init_git_external().
  This keeps necessary code together.
* Add a new variant of WorkspaceInitError for reporting path not found errors. The user error
  string is written to pass existing tests.
  • Loading branch information
essiene committed Jan 16, 2024
1 parent 45ad702 commit dc07436
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ impl From<WorkspaceInitError> for CommandError {
WorkspaceInitError::Path(err) => {
CommandError::InternalError(format!("Failed to access the repository: {err}"))
}
WorkspaceInitError::PathNotFound(path) => {
user_error(format!("{} doesn't exist", path.display()))
}
WorkspaceInitError::Backend(err) => {
user_error(format!("Failed to access the repository: {err}"))
}
Expand Down
12 changes: 1 addition & 11 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,7 @@ pub(crate) fn cmd_init(
let relative_wc_path = file_util::relative_path(&cwd, &wc_path);

if let Some(git_store_str) = &args.git_repo {
let mut git_store_path = command.cwd().join(git_store_str);
git_store_path = git_store_path
.canonicalize()
.map_err(|_| user_error(format!("{} doesn't exist", git_store_path.display())))?;
if !git_store_path.ends_with(".git") {
git_store_path.push(".git");
// Undo if .git doesn't exist - likely a bare repo.
if !git_store_path.exists() {
git_store_path.pop();
}
}
let git_store_path = cwd.join(git_store_str);
let (workspace, repo) =
Workspace::init_external_git(command.settings(), &wc_path, &git_store_path)?;
let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?;
Expand Down
15 changes: 14 additions & 1 deletion lib/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum WorkspaceInitError {
DestinationExists(PathBuf),
#[error("Repo path could not be interpreted as Unicode text")]
NonUnicodePath,
#[error("Path {0} does not exist")]
PathNotFound(PathBuf),
#[error(transparent)]
CheckOutCommit(#[from] CheckOutCommitError),
#[error(transparent)]
Expand Down Expand Up @@ -194,6 +196,17 @@ impl Workspace {
workspace_root: &Path,
git_repo_path: &Path,
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
let mut git_repo_path = git_repo_path
.canonicalize()
.map_err(|_| WorkspaceInitError::PathNotFound(git_repo_path.to_path_buf()))?;
if !git_repo_path.ends_with(".git") {
git_repo_path.push(".git");

if !git_repo_path.exists() {
git_repo_path.pop();
}
}

let backend_initializer =
|settings: &UserSettings, store_path: &Path| -> Result<Box<dyn Backend>, _> {
// If the git repo is inside the workspace, use a relative path to it so the
Expand All @@ -203,7 +216,7 @@ impl Workspace {
// Workspace::new(), but it's not yet here.
let store_relative_git_repo_path = match (
workspace_root.canonicalize(),
canonicalize_git_repo_path(git_repo_path),
canonicalize_git_repo_path(&git_repo_path),
) {
(Ok(workspace_root), Ok(git_repo_path))
if git_repo_path.starts_with(&workspace_root) =>
Expand Down

0 comments on commit dc07436

Please sign in to comment.