- 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 | fn create_dir(p: &Path) -> Result<(), Error> { | 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> { | 97 | pub fn repository(directory: Option<PathBuf>) -> Result<(), Error> { |
| 98 | let mut cursor = PathBuf::from(GIT_DIR_NAME); | 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 | if cursor.is_dir() { | 106 | if cursor.is_dir() { |
| 100 | return Err(Error::DirectoryExists(cursor)); | 107 | return Err(Error::DirectoryExists(cursor)); |
| 101 | } | 108 | } |
| @@ -1,5 +1,6 @@ | |||
| 1 | use anyhow::{Context as AnyhowContext, Result}; | 1 | use anyhow::{Context as AnyhowContext, Result}; |
| 2 | use std::path::PathBuf; | ||
| 2 | 3 | ||
| 3 | pub fn init() -> Result<()> { | 4 | pub fn init(directory: Option<PathBuf>) -> Result<()> { |
| 4 | git_repository::init::repository().with_context(|| "Repository initialization failed") | 5 | git_repository::init::repository(directory).with_context(|| "Repository initialization failed") |
| 5 | } | 6 | } |
| @@ -27,7 +27,10 @@ mod options { · 26 unmodified lines | |||
| 27 | /// Initialize the repository in the current directory. | 27 | /// Initialize the repository in the current directory. |
| 28 | #[derive(FromArgs, PartialEq, Debug)] | 28 | #[derive(FromArgs, PartialEq, Debug)] |
| 29 | #[argh(subcommand, name = "init")] | 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 | /// find all repositories in a given directory. | 35 | /// find all repositories in a given directory. |
| 33 | #[derive(FromArgs, PartialEq, Debug)] | 36 | #[derive(FromArgs, PartialEq, Debug)] |
| @@ -73,7 +76,9 @@ pub fn main() -> Result<()> { · 39 unmodified lines | |||
| 73 | git_features::interrupt::init_handler(std::io::stderr()); | 76 | git_features::interrupt::init_handler(std::io::stderr()); |
| 74 | 77 | ||
| 75 | match cli.subcommand { | 78 | match cli.subcommand { |
| 76 | SubCommands::Init(_) => core::repository::init(), | 79 | SubCommands::Init(Init { |
| 80 | directory | ||
| 81 | }) => core::repository::init(), | ||
| 77 | #[cfg(feature = "gitoxide-core-organize")] | 82 | #[cfg(feature = "gitoxide-core-organize")] |
| 78 | SubCommands::Find(Find { root }) => { | 83 | SubCommands::Find(Find { root }) => { |
| 79 | use crate::shared::lean::prepare; | 84 | use crate::shared::lean::prepare; |
| @@ -23,7 +23,12 @@ mod options { · 22 unmodified lines | |||
| 23 | #[clap(alias = "initialize")] | 23 | #[clap(alias = "initialize")] |
| 24 | #[clap(setting = AppSettings::ColoredHelp)] | 24 | #[clap(setting = AppSettings::ColoredHelp)] |
| 25 | #[clap(setting = AppSettings::DisableVersion)] | 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 | /// Find all repositories in a given directory. | 32 | /// Find all repositories in a given directory. |
| 28 | #[clap(setting = AppSettings::ColoredHelp)] | 33 | #[clap(setting = AppSettings::ColoredHelp)] |
| 29 | #[clap(setting = AppSettings::DisableVersion)] | 34 | #[clap(setting = AppSettings::DisableVersion)] |
| @@ -63,7 +68,7 @@ pub fn main() -> Result<()> { · 33 unmodified lines | |||
| 63 | let verbose = true; | 68 | let verbose = true; |
| 64 | 69 | ||
| 65 | match args.cmd { | 70 | match args.cmd { |
| 66 | Subcommands::Init => core::repository::init(), | 71 | Subcommands::Init { directory } => core::repository::init(directory), |
| 67 | Subcommands::Find { root } => { | 72 | Subcommands::Find { root } => { |
| 68 | use gitoxide_core::organize; | 73 | use gitoxide_core::organize; |
| 69 | // force verbose only, being the line renderer. | 74 | // force verbose only, being the line renderer. |