Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Dec 10, 2024
1 parent f666e8c commit 0ea584d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
25 changes: 15 additions & 10 deletions crates/forge/bin/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use yansi::Paint;
static DEPENDENCY_VERSION_TAG_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^v?\d+(\.\d+)*$").unwrap());

pub const FORGE_SUBMODULES_INFO: &str = "forge-submodules-info.json";

/// CLI arguments for `forge install`.
#[derive(Clone, Debug, Parser)]
#[command(override_usage = "forge install [OPTIONS] [DEPENDENCIES]...
Expand Down Expand Up @@ -116,7 +118,7 @@ impl DependencyInstallOpts {
let install_lib_dir = config.install_lib_dir();
let libs = git.root.join(install_lib_dir);

let submodule_info_path = config.root.join("submodules-info.json");
let submodule_info_path = config.root.join(FORGE_SUBMODULES_INFO);
let mut submodule_info: HashMap<PathBuf, TagType> =
fs::read_json_file(&submodule_info_path).unwrap_or_default();

Expand Down Expand Up @@ -168,32 +170,37 @@ impl DependencyInstallOpts {
installed_tag = installer.install_as_submodule(&dep, &path)?;

// Pin branch to submodule if branch is used
if let Some(branch) = &installed_tag {
if let Some(tag_or_branch) = &installed_tag {
// First, check if this tag has a branch
if git.has_branch(branch, &path)? {
if git.has_branch(tag_or_branch, &path)? {
// always work with relative paths when directly modifying submodules
git.cmd()
.args(["submodule", "set-branch", "-b", branch])
.args(["submodule", "set-branch", "-b", tag_or_branch])
.arg(rel_path)
.exec()?;
}

tag_type = TagType::resolve_type(&git, &path, tag_or_branch).ok();
if let Some(tag_type) = &tag_type {
submodule_info.insert(rel_path.to_path_buf(), tag_type.clone());
}
// update .gitmodules which is at the root of the repo,
// not necessarily at the root of the current Foundry project
let root = Git::root_of(git.root)?;
git.root(&root).add(Some(".gitmodules"))?;
}

if !submodule_info.is_empty() {
fs::write_json_file(&submodule_info_path, &submodule_info)?;
}

// commit the installation
if !no_commit {
let mut msg = String::with_capacity(128);
msg.push_str("forge install: ");
msg.push_str(dep.name());
if let Some(tag) = &installed_tag {
tag_type = TagType::resolve_type(&git, &path, tag).ok();

if let Some(tag_type) = &tag_type {
submodule_info.insert(rel_path.to_path_buf(), tag_type.clone());
msg.push_str("\n\n");
msg.push_str(tag_type.to_string().as_str());
} else {
Expand All @@ -202,10 +209,8 @@ impl DependencyInstallOpts {
}
}

// write .submodules-info.json
if !submodule_info.is_empty() {
fs::write_json_file(&submodule_info_path, &submodule_info)?;
git.root(&config.root).add(Some("submodules-info.json"))?;
git.root(&config.root).add(Some(FORGE_SUBMODULES_INFO))?;
}
git.commit(&msg)?;
}
Expand Down
6 changes: 4 additions & 2 deletions crates/forge/bin/cmd/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use foundry_common::fs;
use foundry_config::{impl_figment_convert_basic, Config};
use std::{collections::hash_map::Entry, path::PathBuf};

use super::install::FORGE_SUBMODULES_INFO;

/// CLI arguments for `forge update`.
#[derive(Clone, Debug, Parser)]
pub struct UpdateArgs {
Expand Down Expand Up @@ -37,7 +39,7 @@ impl UpdateArgs {
let config = self.try_load_config_emit_warnings()?;
let (root, paths) = dependencies_paths(&self.dependencies, &config)?;
let mut submodule_infos: HashMap<PathBuf, TagType> =
fs::read_json_file(&root.join("submodules-info.json")).unwrap_or_default();
fs::read_json_file(&root.join(FORGE_SUBMODULES_INFO)).unwrap_or_default();

let prev_len = submodule_infos.len();

Expand Down Expand Up @@ -74,7 +76,7 @@ impl UpdateArgs {
}

if prev_len < submodule_infos.len() {
fs::write_json_file(&root.join("submodules-info.json"), &submodule_infos)?;
fs::write_json_file(&root.join(FORGE_SUBMODULES_INFO), &submodule_infos)?;
}

Ok(())
Expand Down

0 comments on commit 0ea584d

Please sign in to comment.