Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forge): pin tags/revs for deps #9522

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
yash-atreya committed Dec 10, 2024
commit 0ea584dfea100732d99691ab4761a86e5aaba6cc
25 changes: 15 additions & 10 deletions crates/forge/bin/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -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";
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved

/// CLI arguments for `forge install`.
#[derive(Clone, Debug, Parser)]
#[command(override_usage = "forge install [OPTIONS] [DEPENDENCIES]...
@@ -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();

@@ -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 {
@@ -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)?;
}
6 changes: 4 additions & 2 deletions crates/forge/bin/cmd/update.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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();

@@ -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(())