Skip to content

Commit

Permalink
Revert "fix(forge): update forge remove (#4767)"
Browse files Browse the repository at this point in the history
This reverts commit cb1a04e.
  • Loading branch information
mattsse committed Apr 19, 2023
1 parent cb1a04e commit 6f1fa06
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
46 changes: 38 additions & 8 deletions cli/src/cmd/forge/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,58 @@ impl Cmd for RemoveArgs {

fn run(self) -> eyre::Result<Self::Output> {
let config = self.try_load_config_emit_warnings()?;
let prj_root = config.__root.0.clone();
let git_root =
find_git_root_path(&config.__root.0).wrap_err("Unable to detect git root directory")?;
find_git_root_path(&prj_root).wrap_err("Unable to detect git root directory")?;
let libs = config.install_lib_dir();
let libs_relative = libs
.strip_prefix(prj_root)
.wrap_err("Dependencies are not relative to project root")?;
let git_mod_libs = git_root.join(".git/modules").join(libs_relative);

self.dependencies.iter().try_for_each(|dep| -> eyre::Result<_> {
let target_dir: PathBuf =
if let Some(alias) = &dep.alias { alias } else { &dep.name }.into();

let mut git_mod_path = git_mod_libs.join(&target_dir);
let mut dep_path = libs.join(&target_dir);
// handle relative paths that start with the install dir, so we convert `lib/forge-std`
// to `forge-std`
if !dep_path.exists() {
if let Ok(rel_target) = target_dir.strip_prefix(libs_relative) {
dep_path = libs.join(rel_target);
git_mod_path = git_mod_libs.join(rel_target);
}
}

for dep in &self.dependencies {
let name = dep.name();
let dep_path = libs.join(name);
if !dep_path.exists() {
eyre::bail!("Could not find dependency {name:?} in {}", dep_path.display());
eyre::bail!("{}: No such dependency", target_dir.display());
}

println!(
"Removing {} in {dep_path:?}, (url: {:?}, tag: {:?})",
dep.name, dep.url, dep.tag
);

// remove submodule entry from .git/config
Command::new("git")
.args(["submodule", "deinit", "-f", &dep_path.display().to_string()])
.current_dir(&git_root)
.exec()?;

// remove the submodule repository from .git/modules directory
Command::new("rm")
.args(["-rf", &git_mod_path.display().to_string()])
.current_dir(&git_root)
.exec()?;

// remove the leftover submodule directory
Command::new("git")
.args(["rm", &dep_path.display().to_string()])
.args(["rm", "-f", &dep_path.display().to_string()])
.current_dir(&git_root)
.exec()?;
}

Ok(())
Ok(())
})
}
}
7 changes: 0 additions & 7 deletions cli/src/opts/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ impl FromStr for Dependency {
}
}

impl Dependency {
/// Returns the name of the dependency, prioritizing the alias if it exists.
pub fn name(&self) -> &str {
self.alias.as_deref().unwrap_or(self.name.as_str())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 6f1fa06

Please sign in to comment.