diff --git a/CHANGELOG.md b/CHANGELOG.md index 381833fcf3..080b119de0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ incremented for features. ### Features * cli: Add `anchor clean` command that's the same as `cargo clean` but preserves keypairs inside `target/deploy` ([#1470](https://github.com/project-serum/anchor/issues/1470)). +* cli: Running `anchor init` now initializes a new git repository for the workspace. This can be disabled with the `--no-git` flag ([#1605](https://github.com/project-serum/anchor/pull/1605)). * lang: Add new `AccountSysvarMismatch` error code and test cases for sysvars ([#1535](https://github.com/project-serum/anchor/pull/1535)). * spl: Add support for revoke instruction ([#1493](https://github.com/project-serum/anchor/pull/1493)). * ts: Add provider parameter to `Spl.token` factory method ([#1597](https://github.com/project-serum/anchor/pull/1597)). diff --git a/cli/src/lib.rs b/cli/src/lib.rs index f8c486e1e8..0181322039 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -64,6 +64,8 @@ pub enum Command { name: String, #[clap(short, long)] javascript: bool, + #[clap(long)] + no_git: bool, }, /// Builds the workspace. Build { @@ -363,7 +365,11 @@ pub enum ClusterCommand { pub fn entry(opts: Opts) -> Result<()> { match opts.command { - Command::Init { name, javascript } => init(&opts.cfg_override, name, javascript), + Command::Init { + name, + javascript, + no_git, + } => init(&opts.cfg_override, name, javascript, no_git), Command::New { name } => new(&opts.cfg_override, name), Command::Build { idl, @@ -461,7 +467,7 @@ pub fn entry(opts: Opts) -> Result<()> { } } -fn init(cfg_override: &ConfigOverride, name: String, javascript: bool) -> Result<()> { +fn init(cfg_override: &ConfigOverride, name: String, javascript: bool, no_git: bool) -> Result<()> { if Config::discover(cfg_override)?.is_some() { return Err(anyhow!("Workspace already initialized")); } @@ -574,6 +580,18 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool) -> Result println!("Failed to install node dependencies") } + if !no_git { + let git_result = std::process::Command::new("git") + .arg("init") + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(|e| anyhow::format_err!("git init failed: {}", e.to_string()))?; + if !git_result.status.success() { + eprintln!("Failed to automatically initialize a new git repository"); + } + } + println!("{} initialized", name); Ok(())