Skip to content

Commit 9e409e0

Browse files
committed
Initialise git repo in new app
Signed-off-by: itowlson <[email protected]>
1 parent 7f2a6bf commit 9e409e0

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/templates/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ spin-common = { path = "../common" }
2727
spin-manifest = { path = "../manifest" }
2828
tar = { workspace = true }
2929
tempfile = { workspace = true }
30+
terminal = { path = "../terminal" }
3031
tokio = { workspace = true, features = ["fs", "process", "rt", "macros"] }
3132
toml = { workspace = true }
3233
toml_edit = { workspace = true }

crates/templates/src/git.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::ErrorKind;
1+
use std::{io::ErrorKind, path::Path, process::Stdio};
22

33
// TODO: the following and the second half of plugins/git.rs are duplicates
44

@@ -46,3 +46,24 @@ impl UnderstandGitResult for Result<std::process::Output, std::io::Error> {
4646
}
4747
}
4848
}
49+
50+
pub(crate) async fn is_in_git_repo(dir: &Path) -> anyhow::Result<bool> {
51+
let mut cmd = tokio::process::Command::new("git");
52+
cmd.arg("-C")
53+
.arg(dir)
54+
.arg("rev-parse")
55+
.arg("--git-dir")
56+
.stdout(Stdio::null())
57+
.stderr(Stdio::null());
58+
59+
let status = cmd.status().await?;
60+
Ok(status.success())
61+
}
62+
63+
pub(crate) async fn init_git_repo(dir: &Path) -> Result<(), GitError> {
64+
let mut cmd = tokio::process::Command::new("git");
65+
cmd.arg("-C").arg(dir).arg("init");
66+
67+
let result = cmd.output().await;
68+
result.understand_git_result().map(|_| ())
69+
}

crates/templates/src/run.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use walkdir::WalkDir;
1010

1111
use crate::{
1212
cancellable::Cancellable,
13+
git,
1314
interaction::{InteractionStrategy, Interactive, Silent},
1415
renderer::MergeTarget,
1516
template::{ExtraOutputAction, TemplateVariantInfo},
@@ -74,6 +75,8 @@ impl Run {
7475
.and_then(|t| t.render())
7576
.and_then_async(|o| async move { o.write().await })
7677
.await
78+
.and_then_async(|_| self.initialise_git())
79+
.await
7780
.err()
7881
}
7982

@@ -352,6 +355,32 @@ impl Run {
352355
}
353356
}
354357

358+
async fn initialise_git(&self) -> anyhow::Result<()> {
359+
if !matches!(self.options.variant, TemplateVariantInfo::NewApplication) {
360+
return Ok(());
361+
}
362+
363+
if self.options.no_vcs {
364+
return Ok(());
365+
}
366+
367+
let target_dir = self.generation_target_dir();
368+
369+
let skip_initing_repo = git::is_in_git_repo(&target_dir).await.unwrap_or(true);
370+
371+
if skip_initing_repo {
372+
return Ok(());
373+
}
374+
375+
if let Err(e) = git::init_git_repo(&target_dir).await {
376+
if !matches!(e, git::GitError::ProgramNotFound) {
377+
terminal::warn!("Spin was unable to initialise a Git repository. Run `git init` manually if you want one.");
378+
}
379+
}
380+
381+
Ok(())
382+
}
383+
355384
fn list_content_files(from: &Path) -> anyhow::Result<Vec<PathBuf>> {
356385
let walker = WalkDir::new(from);
357386
let files = walker

0 commit comments

Comments
 (0)