Browse Source

cli: initialize a new git repository on `anchor init` (#1605)

Matthew Callens 3 years ago
parent
commit
2a039f2e45
2 changed files with 21 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 20 2
      cli/src/lib.rs

+ 1 - 0
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)).
 * lang: Replace `std::io::Cursor` with a custom `Write` impl that uses the Solana mem syscalls ([#1589](https://github.com/project-serum/anchor/pull/1589)).
 * spl: Add support for revoke instruction ([#1493](https://github.com/project-serum/anchor/pull/1493)).

+ 20 - 2
cli/src/lib.rs

@@ -66,6 +66,8 @@ pub enum Command {
         name: String,
         #[clap(short, long)]
         javascript: bool,
+        #[clap(long)]
+        no_git: bool,
     },
     /// Builds the workspace.
     Build {
@@ -365,7 +367,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,
@@ -463,7 +469,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"));
     }
@@ -576,6 +582,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(())