History

Added [directory] argument to init.

- git init supports providing an optional directory in which to initialize a new repo - the semantics is otherwise the same

Daniel Levin committed 5 years ago

Commit 62f8dc61 parent f6060754 files · +27 −9Browse files

Unified · Split

4 files
  1. git-repository/src/init.rs +10 −3
  2. gitoxide-core/src/repository.rs +3 −2
  3. src/porcelain/lean.rs +7 −2
  4. src/porcelain/pretty.rs +7 −2
git-repository/src/init.rs+10−3
@@ -91,11 +91,18 @@ fn write_file(data: &[u8], path: &Path) -> Result<(), Error> { · 90 unmodified lines
9191 }
9292
9393 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()))
9595 }
9696
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+
99106 if cursor.is_dir() {
100107 return Err(Error::DirectoryExists(cursor));
101108 }
gitoxide-core/src/repository.rs+3−2
@@ -1,5 +1,6 @@
11 use anyhow::{Context as AnyhowContext, Result};
2+use std::path::PathBuf;
23
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")
56 }
src/porcelain/lean.rs+7−2
@@ -27,7 +27,10 @@ mod options { · 26 unmodified lines
2727 /// Initialize the repository in the current directory.
2828 #[derive(FromArgs, PartialEq, Debug)]
2929 #[argh(subcommand, name = "init")]
30− pub struct Init {}
30+ pub struct Init {
31+ #[argh(option)]
32+ pub directory: Option<PathBuf>,
33+ }
3134
3235 /// find all repositories in a given directory.
3336 #[derive(FromArgs, PartialEq, Debug)]
@@ -73,7 +76,9 @@ pub fn main() -> Result<()> { · 39 unmodified lines
7376 git_features::interrupt::init_handler(std::io::stderr());
7477
7578 match cli.subcommand {
76− SubCommands::Init(_) => core::repository::init(),
79+ SubCommands::Init(Init {
80+ directory
81+ }) => core::repository::init(),
7782 #[cfg(feature = "gitoxide-core-organize")]
7883 SubCommands::Find(Find { root }) => {
7984 use crate::shared::lean::prepare;
src/porcelain/pretty.rs+7−2
@@ -23,7 +23,12 @@ mod options { · 22 unmodified lines
2323 #[clap(alias = "initialize")]
2424 #[clap(setting = AppSettings::ColoredHelp)]
2525 #[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+ },
2732 /// Find all repositories in a given directory.
2833 #[clap(setting = AppSettings::ColoredHelp)]
2934 #[clap(setting = AppSettings::DisableVersion)]
@@ -63,7 +68,7 @@ pub fn main() -> Result<()> { · 33 unmodified lines
6368 let verbose = true;
6469
6570 match args.cmd {
66− Subcommands::Init => core::repository::init(),
71+ Subcommands::Init { directory } => core::repository::init(directory),
6772 Subcommands::Find { root } => {
6873 use gitoxide_core::organize;
6974 // force verbose only, being the line renderer.