- git init supports providing an optional directory in which to initialize a new repo - the semantics is otherwise the same
Unified · Split
| @@ -91,11 +91,18 @@ fn write_file(data: &[u8], path: &Path) -> Result<(), Error> { · 90 unmodified lines | ||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | fn create_dir(p: &Path) -> Result<(), Error> { |
| 94 | − fs::create_dir(p).map_err(|e| Error::CreateDirectory(e, p.to_owned())) | |
| 94 | + fs::create_dir_all(p).map_err(|e| Error::CreateDirectory(e, p.to_owned())) | |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | −pub fn repository() -> Result<(), Error> { | |
| 98 | − let mut cursor = PathBuf::from(GIT_DIR_NAME); | |
| 97 | +pub fn repository(directory: Option<PathBuf>) -> Result<(), Error> { | |
| 98 | + | |
| 99 | + let mut cursor = match directory { | |
| 100 | + Some(dir) => PathBuf::from(dir), | |
| 101 | + None => PathBuf::from("."), | |
| 102 | + }; | |
| 103 | + | |
| 104 | + cursor.push(GIT_DIR_NAME); | |
| 105 | + | |
| 99 | 106 | if cursor.is_dir() { |
| 100 | 107 | return Err(Error::DirectoryExists(cursor)); |
| 101 | 108 | } |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | use anyhow::{Context as AnyhowContext, Result}; |
| 2 | +use std::path::PathBuf; | |
| 2 | 3 | |
| 3 | −pub fn init() -> Result<()> { | |
| 4 | − git_repository::init::repository().with_context(|| "Repository initialization failed") | |
| 4 | +pub fn init(directory: Option<PathBuf>) -> Result<()> { | |
| 5 | + git_repository::init::repository(directory).with_context(|| "Repository initialization failed") | |
| 5 | 6 | } |
| @@ -27,7 +27,10 @@ mod options { · 26 unmodified lines | ||
| 27 | 27 | /// Initialize the repository in the current directory. |
| 28 | 28 | #[derive(FromArgs, PartialEq, Debug)] |
| 29 | 29 | #[argh(subcommand, name = "init")] |
| 30 | − pub struct Init {} | |
| 30 | + pub struct Init { | |
| 31 | + #[argh(option)] | |
| 32 | + pub directory: Option<PathBuf>, | |
| 33 | + } | |
| 31 | 34 | |
| 32 | 35 | /// find all repositories in a given directory. |
| 33 | 36 | #[derive(FromArgs, PartialEq, Debug)] |
| @@ -73,7 +76,9 @@ pub fn main() -> Result<()> { · 39 unmodified lines | ||
| 73 | 76 | git_features::interrupt::init_handler(std::io::stderr()); |
| 74 | 77 | |
| 75 | 78 | match cli.subcommand { |
| 76 | − SubCommands::Init(_) => core::repository::init(), | |
| 79 | + SubCommands::Init(Init { | |
| 80 | + directory | |
| 81 | + }) => core::repository::init(), | |
| 77 | 82 | #[cfg(feature = "gitoxide-core-organize")] |
| 78 | 83 | SubCommands::Find(Find { root }) => { |
| 79 | 84 | use crate::shared::lean::prepare; |
| @@ -23,7 +23,12 @@ mod options { · 22 unmodified lines | ||
| 23 | 23 | #[clap(alias = "initialize")] |
| 24 | 24 | #[clap(setting = AppSettings::ColoredHelp)] |
| 25 | 25 | #[clap(setting = AppSettings::DisableVersion)] |
| 26 | − Init, | |
| 26 | + Init { | |
| 27 | + /// The directory in which to initialize a new git repository. | |
| 28 | + /// | |
| 29 | + /// Defaults to the current working directory. | |
| 30 | + directory: Option<PathBuf>, | |
| 31 | + }, | |
| 27 | 32 | /// Find all repositories in a given directory. |
| 28 | 33 | #[clap(setting = AppSettings::ColoredHelp)] |
| 29 | 34 | #[clap(setting = AppSettings::DisableVersion)] |
| @@ -63,7 +68,7 @@ pub fn main() -> Result<()> { · 33 unmodified lines | ||
| 63 | 68 | let verbose = true; |
| 64 | 69 | |
| 65 | 70 | match args.cmd { |
| 66 | − Subcommands::Init => core::repository::init(), | |
| 71 | + Subcommands::Init { directory } => core::repository::init(directory), | |
| 67 | 72 | Subcommands::Find { root } => { |
| 68 | 73 | use gitoxide_core::organize; |
| 69 | 74 | // force verbose only, being the line renderer. |