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 install): add @tag= @branch= @rev= specific refs #9214

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
46 changes: 39 additions & 7 deletions crates/cli/src/opts/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub static GH_REPO_PREFIX_REGEX: LazyLock<Regex> = LazyLock::new(|| {
.unwrap()
});

static VERSION_PREFIX_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"@(tag|branch|commit)="#).unwrap());

const GITHUB: &str = "github.com";
const VERSION_SEPARATOR: char = '@';
const ALIAS_SEPARATOR: char = '=';
Expand Down Expand Up @@ -49,6 +52,12 @@ pub struct Dependency {
impl FromStr for Dependency {
type Err = eyre::Error;
fn from_str(dependency: &str) -> Result<Self, Self::Err> {
// Handle dependency exact ref type (`@tag=`, `@branch=` or `@commit=`)`.
// Only extract version for first tag/branch/commit specified.
let url_and_version: Vec<&str> = VERSION_PREFIX_REGEX.split(dependency).collect();
let dependency = url_and_version[0];
let mut tag_or_branch = url_and_version.get(1).map(|version| version.to_string());

// everything before "=" should be considered the alias
let (mut alias, dependency) = if let Some(split) = dependency.split_once(ALIAS_SEPARATOR) {
(Some(String::from(split.0)), split.1.to_string())
Expand Down Expand Up @@ -96,14 +105,15 @@ impl FromStr for Dependency {
// `tag` does not contain a slash
let mut split = url_with_version.rsplit(VERSION_SEPARATOR);

let mut tag = None;
let mut url = url_with_version.as_str();

let maybe_tag = split.next().unwrap();
if let Some(actual_url) = split.next() {
if !maybe_tag.contains('/') {
tag = Some(maybe_tag.to_string());
url = actual_url;
if tag_or_branch.is_none() {
let maybe_tag_or_branch = split.next().unwrap();
if let Some(actual_url) = split.next() {
if !maybe_tag_or_branch.contains('/') {
tag_or_branch = Some(maybe_tag_or_branch.to_string());
url = actual_url;
}
}
}

Expand All @@ -114,7 +124,7 @@ impl FromStr for Dependency {
.ok_or_else(|| eyre::eyre!("no dependency name found"))?
.to_string();

(Some(url), Some(name), tag)
(Some(url), Some(name), tag_or_branch)
} else {
(None, None, None)
};
Expand Down Expand Up @@ -360,4 +370,26 @@ mod tests {
let dep: Dependency = "[email protected]:my-org/my-repo.git".parse().unwrap();
assert_eq!(dep.url.unwrap(), "https://github.com/my-org/my-repo");
}

#[test]
fn can_parse_with_explicit_ref_type() {
let dep = Dependency::from_str("smartcontractkit/ccip@tag=contracts-ccip/v1.2.1").unwrap();
assert_eq!(dep.name, "ccip");
assert_eq!(dep.url, Some("https://github.com/smartcontractkit/ccip".to_string()));
assert_eq!(dep.tag, Some("contracts-ccip/v1.2.1".to_string()));
assert_eq!(dep.alias, None);

let dep =
Dependency::from_str("smartcontractkit/ccip@branch=contracts-ccip/v1.2.1").unwrap();
assert_eq!(dep.name, "ccip");
assert_eq!(dep.url, Some("https://github.com/smartcontractkit/ccip".to_string()));
assert_eq!(dep.tag, Some("contracts-ccip/v1.2.1".to_string()));
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(dep.alias, None);

let dep = Dependency::from_str("smartcontractkit/ccip@commit=80eb41b").unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soldeer / Rust uses rev to refer to a commit, perhaps it makes sense to align here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in e9d2967, please check

assert_eq!(dep.name, "ccip");
assert_eq!(dep.url, Some("https://github.com/smartcontractkit/ccip".to_string()));
assert_eq!(dep.tag, Some("80eb41b".to_string()));
assert_eq!(dep.alias, None);
}
}
2 changes: 2 additions & 0 deletions crates/forge/bin/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct InstallArgs {
/// - A tag: v1.2.3
/// - A commit: 8e8128
///
/// For exact match, a ref can be provided with `@tag=`, `@branch=` or `@commit=` prefix.
///
/// Target installation directory can be added via `<alias>=` suffix.
/// The dependency will installed to `lib/<alias>`.
dependencies: Vec<Dependency>,
Expand Down
Loading