Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ impl<'a> Git<'a> {
self.cmd().args(["rev-parse", "--is-inside-work-tree"]).status().map(|s| s.success())
}

pub fn is_repo_root(self) -> Result<bool> {
self.cmd().args(["rev-parse", "--show-cdup"]).exec().map(|out| out.stdout.is_empty())
}

pub fn is_clean(self) -> Result<bool> {
self.cmd().args(["status", "--porcelain"]).exec().map(|out| out.stdout.is_empty())
}
Expand Down
18 changes: 12 additions & 6 deletions crates/forge/src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ pub struct InitArgs {
#[arg(long, conflicts_with = "template")]
pub vscode: bool,

/// Use the parent git repository instead of initializing a new one.
/// Only valid if the target is in a git repository.
#[arg(long, conflicts_with = "template")]
pub use_parent_git: bool,

#[command(flatten)]
pub install: DependencyInstallOpts,
}

impl InitArgs {
pub fn run(self) -> Result<()> {
let Self { root, template, branch, install, offline, force, vscode } = self;
let Self { root, template, branch, install, offline, force, vscode, use_parent_git } = self;
let DependencyInstallOpts { shallow, no_git, commit } = install;

// create the root dir if it does not exist
Expand Down Expand Up @@ -141,7 +146,7 @@ impl InitArgs {

// set up the repo
if !no_git {
init_git_repo(git, commit)?;
init_git_repo(git, commit, use_parent_git)?;
}

// install forge-std
Expand All @@ -166,14 +171,15 @@ impl InitArgs {
}
}

/// Initialises `root` as a git repository, if it isn't one already.
/// Initialises `root` as a git repository, if it isn't one already, unless 'use_parent_git' is
/// true.
///
/// Creates `.gitignore` and `.github/workflows/test.yml`, if they don't exist already.
///
/// Commits everything in `root` if `commit` is true.
fn init_git_repo(git: Git<'_>, commit: bool) -> Result<()> {
// git init
if !git.is_in_repo()? {
fn init_git_repo(git: Git<'_>, commit: bool, use_parent_git: bool) -> Result<()> {
// `git init`
if !git.is_in_repo()? || (!use_parent_git && !git.is_repo_root()?) {
git.init()?;
}

Expand Down
47 changes: 47 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,53 @@ Warning: Target directory is not empty, but `--force` was specified
assert_eq!(gitignore, "not foundry .gitignore");
});

// `forge init --use-parent-git` works on already initialized git repository
forgetest!(can_init_using_parent_repo, |prj, cmd| {
let root = prj.root();

// initialize new git repo
let status = Command::new("git")
.arg("init")
.current_dir(root)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.expect("could not run git init");
assert!(status.success());
assert!(root.join(".git").exists());

prj.create_file("README.md", "non-empty dir");
prj.create_file(".gitignore", "not foundry .gitignore");

let folder = "foundry-folder";
cmd.arg("init").arg(folder).arg("--force").arg("--use-parent-git").assert_success().stdout_eq(
str![[r#"
Initializing [..]...
Installing forge-std in [..] (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
Installed forge-std[..]
Initialized forge project

"#]],
);

assert!(root.join(folder).join("lib/forge-std").exists());

// not overwritten
let gitignore = root.join(".gitignore");
let gitignore = fs::read_to_string(gitignore).unwrap();
assert_eq!(gitignore, "not foundry .gitignore");

// submodules are registered at root
let gitmodules = root.join(".gitmodules");
let gitmodules = fs::read_to_string(gitmodules).unwrap();
assert!(gitmodules.contains(
"
path = foundry-folder/lib/forge-std
url = https://github.com/foundry-rs/forge-std
"
));
});

// Checks that remappings.txt and .vscode/settings.json is generated
forgetest!(can_init_vscode, |prj, cmd| {
prj.wipe();
Expand Down
Loading