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(dependencies): support pinning of tags / revs when using .gitmodules with foundry.lock #9522

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
3bcf1b9
parse submodule status output
yash-atreya Dec 6, 2024
a3a01ec
feat(`forge`): save submodules info on install
yash-atreya Dec 9, 2024
cd075f2
re-checkout to tag/rev after forge update
yash-atreya Dec 9, 2024
917cc13
clippy
yash-atreya Dec 9, 2024
e7fb768
fmt
yash-atreya Dec 9, 2024
8e4b543
fix
yash-atreya Dec 9, 2024
53b362d
fix
yash-atreya Dec 10, 2024
f666e8c
test
yash-atreya Dec 10, 2024
0ea584d
fix
yash-atreya Dec 10, 2024
cf88ef8
override using forge update
yash-atreya Dec 10, 2024
2a55c67
nit
yash-atreya Dec 10, 2024
19194c0
nit
yash-atreya Dec 10, 2024
7f6d026
fix: update only untagged deps
yash-atreya Dec 11, 2024
ed3ccfe
allow overrides
yash-atreya Dec 11, 2024
f893917
clippy
yash-atreya Dec 11, 2024
dd072fd
remove + rename to foundry.lock
yash-atreya Dec 11, 2024
ed14a48
nit
yash-atreya Dec 11, 2024
90d92bb
fix: sync foundry.lock on install
yash-atreya Dec 11, 2024
5640bab
sync foundry lock using forge install
yash-atreya Dec 11, 2024
7d9ce8d
fix: read_and_sync_foundry_lock
yash-atreya Dec 17, 2024
92454f8
fix
yash-atreya Dec 17, 2024
46ce492
fix test
yash-atreya Dec 17, 2024
9ae88d6
fix
yash-atreya Dec 17, 2024
fdb1b32
fix
yash-atreya Dec 17, 2024
00a887c
fix
yash-atreya Dec 17, 2024
ac922fa
Merge branch 'master' into yash/fix-forge-update
yash-atreya Jan 6, 2025
07ce536
Merge branch 'master' into yash/fix-forge-update
yash-atreya Jan 10, 2025
53eed1f
Merge branch 'master' into yash/fix-forge-update
grandizzy Jan 14, 2025
3a7e7eb
Do not run can_sync_foundry_lock test on win (fails on master branch …
grandizzy Jan 14, 2025
b7954c3
Merge branch 'master' into yash/fix-forge-update
yash-atreya Jan 29, 2025
8276b42
feat: introduce `LockFile` type, use it in forge install and forge re…
yash-atreya Jan 29, 2025
2f82d39
fix: account for clean lib/ dir while syncing lockfile
yash-atreya Jan 29, 2025
c4a13fd
fix: integrate lockfile into update
yash-atreya Jan 29, 2025
1859e4d
clippy
yash-atreya Jan 29, 2025
97fba00
fix
yash-atreya Jan 29, 2025
090da39
fix
yash-atreya Jan 29, 2025
7787895
feat(`forge`): introduces a `Lockfile` type (#9781)
yash-atreya Jan 29, 2025
ddf425d
clean up forge update
yash-atreya Jan 29, 2025
1cd019a
Merge branch 'yash/lockfile' into yash/fix-forge-update
yash-atreya Jan 29, 2025
5fb8861
nits
yash-atreya Jan 29, 2025
6fc5850
nit
yash-atreya Jan 30, 2025
a55709c
fix: update branch rev in lockfile and print updates
yash-atreya Jan 30, 2025
c42c915
fix
yash-atreya Jan 30, 2025
9b93c00
clippy
yash-atreya Jan 30, 2025
94b5238
nit
yash-atreya Jan 30, 2025
29d846a
fix
yash-atreya Jan 30, 2025
f25c0da
assert foundry lock in tests
yash-atreya Jan 30, 2025
883d048
nit
yash-atreya Jan 30, 2025
94edb91
refac ExtTester and test uni v4 foundry lock sync
yash-atreya Jan 30, 2025
cb7e434
oz sync test
yash-atreya Jan 30, 2025
8fc72e1
fix: run sync after submodule update on install
yash-atreya Jan 30, 2025
4f91ecc
fix: tag_for_commit should return earliest tag that contains commit +…
yash-atreya Jan 30, 2025
50e4a4a
Merge branch 'master' into yash/fix-forge-update
yash-atreya Jan 30, 2025
4c87a7a
fix: write lockfile after git succeeds
yash-atreya Feb 5, 2025
44e334f
Merge branch 'master' into yash/fix-forge-update
yash-atreya Feb 5, 2025
d8a1e53
feat: account for deps pinned to a branch in .gitmodules while syncing
yash-atreya Feb 10, 2025
311d4bc
fix: SUBMODULE_BRANCH_REGEX
yash-atreya Feb 10, 2025
dc22515
fix: properly parse paths from .gitmodules
yash-atreya Feb 11, 2025
068ae3c
nit
yash-atreya Feb 11, 2025
3ecfcfa
Merge branch 'master' into yash/fix-forge-update
yash-atreya Feb 24, 2025
69ffe4c
clippy
yash-atreya Feb 24, 2025
16604d9
fix tests
yash-atreya Feb 24, 2025
905c5a0
fix test
yash-atreya Feb 24, 2025
e1b1a05
Merge branch 'master' into yash/fix-forge-update
zerosnacks Feb 28, 2025
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
168 changes: 168 additions & 0 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{
future::Future,
path::{Path, PathBuf},
process::{Command, Output, Stdio},
str::FromStr,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use tracing_subscriber::prelude::*;
Expand Down Expand Up @@ -375,6 +376,10 @@ impl<'a> Git<'a> {
.map(drop)
}

pub fn checkout_at(self, tag: impl AsRef<OsStr>, at: &Path) -> Result<()> {
self.cmd_at(at).arg("checkout").arg(tag).exec().map(drop)
}

pub fn init(self) -> Result<()> {
self.cmd().arg("init").exec().map(drop)
}
Expand Down Expand Up @@ -452,6 +457,22 @@ impl<'a> Git<'a> {
.map(|stdout| !stdout.is_empty())
}

pub fn has_tag(self, tag: impl AsRef<OsStr>, at: &Path) -> Result<bool> {
self.cmd_at(at)
.args(["tag", "--list"])
.arg(tag)
.get_stdout_lossy()
.map(|stdout| !stdout.is_empty())
}

pub fn has_rev(self, rev: impl AsRef<OsStr>, at: &Path) -> Result<bool> {
self.cmd_at(at)
.args(["cat-file", "-t"])
.arg(rev)
.get_stdout_lossy()
.map(|stdout| &stdout == "commit")
}

pub fn ensure_clean(self) -> Result<()> {
if self.is_clean()? {
Ok(())
Expand Down Expand Up @@ -480,6 +501,14 @@ ignore them in the `.gitignore` file, or run this command again with the `--no-c
self.cmd().arg("tag").get_stdout_lossy()
}

pub fn tag_for_commit(self, rev: &str, at: &Path) -> Result<Option<String>> {
self.cmd_at(at)
.args(["tag", "--contains"])
.arg(rev)
.get_stdout_lossy()
.map(|stdout| stdout.lines().last().map(str::to_string))
}

pub fn has_missing_dependencies<I, S>(self, paths: I) -> Result<bool>
where
I: IntoIterator<Item = S>,
Expand Down Expand Up @@ -561,6 +590,10 @@ ignore them in the `.gitignore` file, or run this command again with the `--no-c
self.cmd().stderr(self.stderr()).args(["submodule", "init"]).exec().map(drop)
}

pub fn submodules(&self) -> Result<Submodules> {
self.cmd().args(["submodule", "status"]).get_stdout_lossy().map(|stdout| stdout.parse())?
}

pub fn cmd(self) -> Command {
let mut cmd = Self::cmd_no_root();
cmd.current_dir(self.root);
Expand Down Expand Up @@ -589,13 +622,148 @@ ignore them in the `.gitignore` file, or run this command again with the `--no-c
}
}

/// Deserialized `git submodule status lib/dep` output.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct Submodule {
/// Current commit hash the submodule is checked out at.
rev: String,
/// Relative path to the submodule.
path: PathBuf,
}

impl Submodule {
pub fn new(rev: String, path: PathBuf) -> Self {
Self { rev, path }
}

pub fn rev(&self) -> &str {
&self.rev
}

pub fn path(&self) -> &PathBuf {
&self.path
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum TagType {
Branch(String),
Tag(String),
Rev(String),
}

impl TagType {
/// Resolves the [TagType] for a submodule at a given path.
/// `lib_path` is the absolute path to the submodule.
pub fn resolve_type(git: &Git<'_>, lib_path: &Path, s: &str) -> Result<Self> {
// Get the tags for the submodule
if git.has_tag(s, lib_path)? {
return Ok(Self::Tag(String::from(s)));
}

if git.has_branch(s, lib_path)? {
return Ok(Self::Branch(String::from(s)));
}

if git.has_rev(s, lib_path)? {
return Ok(Self::Rev(String::from(s)));
}

Err(eyre::eyre!("Could not resolve tag type for submodule at path {}", lib_path.display()))
}

/// Returns the raw string representation of the [TagType]. i.e without the type prefix.
pub fn raw_string(&self) -> &String {
match self {
Self::Branch(s) | Self::Tag(s) | Self::Rev(s) => s,
}
}
}

impl std::fmt::Display for TagType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Branch(s) => write!(f, "branch={s}"),
Self::Tag(s) => write!(f, "tag={s}"),
Self::Rev(s) => write!(f, "rev={s}"),
}
}
}

impl FromStr for Submodule {
type Err = eyre::Report;

fn from_str(s: &str) -> Result<Self> {
let re = regex::Regex::new(r"^[\s+-]?([a-f0-9]+)\s+([^\s]+)(?:\s+\([^)]+\))?$")?;

let caps = re.captures(s).ok_or_else(|| eyre::eyre!("Invalid submodule status format"))?;

Ok(Self {
rev: caps.get(1).unwrap().as_str().to_string(),
path: PathBuf::from(caps.get(2).unwrap().as_str()),
})
}
}

/// Deserialized `git submodule status` output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Submodules(pub Vec<Submodule>);

impl FromStr for Submodules {
type Err = eyre::Report;

fn from_str(s: &str) -> Result<Self> {
let subs = s.lines().map(str::parse).collect::<Result<Vec<Submodule>>>()?;
Ok(Self(subs))
}
}

impl Iterator for Submodules {
type Item = Submodule;

fn next(&mut self) -> Option<Self::Item> {
self.0.pop()
}
}

#[cfg(test)]
mod tests {
use super::*;
use foundry_common::fs;
use std::{env, fs::File, io::Write};
use tempfile::tempdir;

#[test]
fn parse_submodule_status() {
let s = "+8829465a08cac423dcf59852f21e448449c1a1a8 lib/openzeppelin-contracts (v4.8.0-791-g8829465a)";
let sub = Submodule::from_str(s).unwrap();
assert_eq!(sub.rev(), "8829465a08cac423dcf59852f21e448449c1a1a8");
assert_eq!(sub.path(), Path::new("lib/openzeppelin-contracts"));

let s = "-8829465a08cac423dcf59852f21e448449c1a1a8 lib/openzeppelin-contracts";
let sub = Submodule::from_str(s).unwrap();
assert_eq!(sub.rev(), "8829465a08cac423dcf59852f21e448449c1a1a8");
assert_eq!(sub.path(), Path::new("lib/openzeppelin-contracts"));

let s = "8829465a08cac423dcf59852f21e448449c1a1a8 lib/openzeppelin-contracts";
let sub = Submodule::from_str(s).unwrap();
assert_eq!(sub.rev(), "8829465a08cac423dcf59852f21e448449c1a1a8");
assert_eq!(sub.path(), Path::new("lib/openzeppelin-contracts"));
}

#[test]
fn parse_multiline_submodule_status() {
let s = r#"+d3db4ef90a72b7d24aa5a2e5c649593eaef7801d lib/forge-std (v1.9.4-6-gd3db4ef)
+8829465a08cac423dcf59852f21e448449c1a1a8 lib/openzeppelin-contracts (v4.8.0-791-g8829465a)
"#;
let subs = Submodules::from_str(s).unwrap().0;
assert_eq!(subs.len(), 2);
assert_eq!(subs[0].rev(), "d3db4ef90a72b7d24aa5a2e5c649593eaef7801d");
assert_eq!(subs[0].path(), Path::new("lib/forge-std"));
assert_eq!(subs[1].rev(), "8829465a08cac423dcf59852f21e448449c1a1a8");
assert_eq!(subs[1].path(), Path::new("lib/openzeppelin-contracts"));
}

#[test]
fn foundry_path_ext_works() {
let p = Path::new("contracts/MyTest.t.sol");
Expand Down
47 changes: 39 additions & 8 deletions crates/forge/bin/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloy_primitives::map::HashMap;
use clap::{Parser, ValueHint};
use eyre::{Context, Result};
use foundry_cli::{
opts::Dependency,
utils::{CommandUtils, Git, LoadConfig},
utils::{CommandUtils, Git, LoadConfig, TagType},
};
use foundry_common::fs;
use foundry_config::{impl_figment_convert_basic, Config};
Expand All @@ -19,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 @@ -115,6 +118,10 @@ 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(FORGE_SUBMODULES_INFO);
let mut submodule_info: HashMap<PathBuf, TagType> =
fs::read_json_file(&submodule_info_path).unwrap_or_default();

if dependencies.is_empty() && !self.no_git {
// Use the root of the git repository to look for submodules.
let root = Git::root_of(git.root)?;
Expand Down Expand Up @@ -153,6 +160,7 @@ impl DependencyInstallOpts {

// this tracks the actual installed tag
let installed_tag;
let mut tag_type = None;
if no_git {
installed_tag = installer.install_as_folder(&dep, &path)?;
} else {
Expand All @@ -162,39 +170,61 @@ 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 {
msg.push_str("\n\n");
msg.push_str(tag);
if let Some(tag_type) = &tag_type {
msg.push_str("\n\n");
msg.push_str(tag_type.to_string().as_str());
} else {
msg.push_str("\n\n");
msg.push_str(tag);
}
}

if !submodule_info.is_empty() {
git.root(&config.root).add(Some(FORGE_SUBMODULES_INFO))?;
}
git.commit(&msg)?;
}
}

let mut msg = format!(" {} {}", "Installed".green(), dep.name);
if let Some(tag) = dep.tag.or(installed_tag) {
msg.push(' ');
msg.push_str(tag.as_str());
if let Some(tag_type) = tag_type {
msg.push(' ');
msg.push_str(tag_type.to_string().as_str());
} else {
msg.push(' ');
msg.push_str(tag.as_str());
}
}
sh_println!("{msg}")?;
}
Expand All @@ -204,6 +234,7 @@ impl DependencyInstallOpts {
config.libs.push(install_lib_dir.to_path_buf());
config.update_libs()?;
}

Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl_figment_convert_basic!(RemoveArgs);
impl RemoveArgs {
pub fn run(self) -> Result<()> {
let config = self.try_load_config_emit_warnings()?;
let (root, paths) = super::update::dependencies_paths(&self.dependencies, &config)?;
let (root, paths, _) = super::update::dependencies_paths(&self.dependencies, &config)?;
let git_modules = root.join(".git/modules");

// remove all the dependencies by invoking `git rm` only once with all the paths
Expand Down
Loading
Loading