diff --git a/crates/cli/src/utils/mod.rs b/crates/cli/src/utils/mod.rs index eab7a5b141e9..38467d539201 100644 --- a/crates/cli/src/utils/mod.rs +++ b/crates/cli/src/utils/mod.rs @@ -363,11 +363,12 @@ impl<'a> Git<'a> { } pub fn fetch( + self, shallow: bool, remote: impl AsRef, branch: Option>, ) -> Result<()> { - Self::cmd_no_root() + self.cmd() .stderr(Stdio::inherit()) .arg("fetch") .args(shallow.then_some("--no-tags")) diff --git a/crates/forge/bin/cmd/init.rs b/crates/forge/bin/cmd/init.rs index bfe69889843a..150a1fab713d 100644 --- a/crates/forge/bin/cmd/init.rs +++ b/crates/forge/bin/cmd/init.rs @@ -68,7 +68,7 @@ impl InitArgs { // fetch the template - always fetch shallow for templates since git history will be // collapsed. gitmodules will be initialized after the template is fetched - Git::fetch(true, &template, branch)?; + git.fetch(true, &template, branch)?; // reset git history to the head of the template // first get the commit hash that was fetched let commit_hash = git.commit_hash(true, "FETCH_HEAD")?; diff --git a/crates/forge/tests/cli/cmd.rs b/crates/forge/tests/cli/cmd.rs index 4d7666ef50db..4e31f864a017 100644 --- a/crates/forge/tests/cli/cmd.rs +++ b/crates/forge/tests/cli/cmd.rs @@ -278,6 +278,42 @@ forgetest!(can_init_with_dir, |prj: TestProject, mut cmd: TestCommand| { assert!(prj.root().join("foobar").exists()); }); +// `forge init foobar --template [template]` works with dir argument +forgetest!(can_init_with_dir_and_template, |prj: TestProject, mut cmd: TestCommand| { + cmd.args(["init", "foobar", "--template", "foundry-rs/forge-template"]); + + cmd.assert_success(); + cmd.assert_non_empty_stdout(); + assert!(prj.root().join("foobar/.git").exists()); + assert!(prj.root().join("foobar/foundry.toml").exists()); + assert!(prj.root().join("foobar/lib/forge-std").exists()); + // assert that gitmodules were correctly initialized + assert!(prj.root().join("foobar/.git/modules").exists()); + assert!(prj.root().join("foobar/src").exists()); + assert!(prj.root().join("foobar/test").exists()); +}); + +// `forge init foobar --template [template] --branch [branch]` works with dir argument +forgetest!(can_init_with_dir_and_template_and_branch, |prj: TestProject, mut cmd: TestCommand| { + cmd.args([ + "init", + "foobar", + "--template", + "foundry-rs/forge-template", + "--branch", + "test/deployments", + ]); + + cmd.assert_success(); + cmd.assert_non_empty_stdout(); + assert!(prj.root().join("foobar/.dapprc").exists()); + assert!(prj.root().join("foobar/lib/ds-test").exists()); + // assert that gitmodules were correctly initialized + assert!(prj.root().join("foobar/.git/modules").exists()); + assert!(prj.root().join("foobar/src").exists()); + assert!(prj.root().join("foobar/scripts").exists()); +}); + // `forge init --force` works on non-empty dirs forgetest!(can_init_non_empty, |prj: TestProject, mut cmd: TestCommand| { prj.create_file("README.md", "non-empty dir");