Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
22 changes: 20 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum Command {
name: String,
#[clap(short, long)]
javascript: bool,
#[clap(long)]
no_git: bool,
},
/// Builds the workspace.
Build {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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(())
Expand Down