Skip to content

Commit

Permalink
Modified git fetch command to method to use correct root folder. (#6119)
Browse files Browse the repository at this point in the history
* Modified git fetch command to method to use correct root folder.

* Added directory test for template initialization

* Updated test comment

* Cargo fmt

---------

Co-authored-by: Alejandro Muñoz-McDonald <alejandro@immunefi.com>
  • Loading branch information
janbro and alejandro-immunefi authored Oct 26, 2023
1 parent 6893e38 commit 93f64c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,12 @@ impl<'a> Git<'a> {
}

pub fn fetch(
self,
shallow: bool,
remote: impl AsRef<OsStr>,
branch: Option<impl AsRef<OsStr>>,
) -> Result<()> {
Self::cmd_no_root()
self.cmd()
.stderr(Stdio::inherit())
.arg("fetch")
.args(shallow.then_some("--no-tags"))
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down
36 changes: 36 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 93f64c7

Please sign in to comment.