@@ -37,13 +37,18 @@ pub struct InitArgs {
3737 #[ arg( long, conflicts_with = "template" ) ]
3838 pub vscode : bool ,
3939
40+ /// Use the parent git repository instead of initializing a new one.
41+ /// Only valid if the target is in a git repository.
42+ #[ arg( long, conflicts_with = "template" ) ]
43+ pub use_parent_git : bool ,
44+
4045 #[ command( flatten) ]
4146 pub install : DependencyInstallOpts ,
4247}
4348
4449impl InitArgs {
4550 pub fn run ( self ) -> Result < ( ) > {
46- let Self { root, template, branch, install, offline, force, vscode } = self ;
51+ let Self { root, template, branch, install, offline, force, vscode, use_parent_git } = self ;
4752 let DependencyInstallOpts { shallow, no_git, no_commit } = install;
4853
4954 // create the root dir if it does not exist
@@ -138,7 +143,7 @@ impl InitArgs {
138143
139144 // set up the repo
140145 if !no_git {
141- init_git_repo ( git, no_commit) ?;
146+ init_git_repo ( git, no_commit, use_parent_git ) ?;
142147 }
143148
144149 // install forge-std
@@ -163,15 +168,22 @@ impl InitArgs {
163168 }
164169}
165170
166- /// Initialises `root` as a git repository, if it isn't one already.
171+ /// Initialises `root` as a git repository, if it isn't one already, unless 'use_parent_git' is
172+ /// true.
167173///
168174/// Creates `.gitignore` and `.github/workflows/test.yml`, if they don't exist already.
169175///
170- /// Commits everything in `root` if `no_commit` is false.
171- fn init_git_repo ( git : Git < ' _ > , no_commit : bool ) -> Result < ( ) > {
176+ /// Commits everything if `no_commit` is false.
177+ fn init_git_repo ( git : Git < ' _ > , no_commit : bool , use_parent_git : bool ) -> Result < ( ) > {
172178 // git init
179+ // if not in a git repo, initialize one
173180 if !git. is_in_repo ( ) ? {
174181 git. init ( ) ?;
182+ } else {
183+ // if target is not the repo root init a new one unless `use_parent_git` is true
184+ if !git. is_repo_root ( ) ? && !use_parent_git {
185+ git. init ( ) ?;
186+ }
175187 }
176188
177189 // .gitignore
0 commit comments