Skip to content

Commit e296edc

Browse files
authored
Merge pull request #3197 from itowlson/git-init-in-new-app
Initialise git repo in new app
2 parents c1011ac + 44ab8b2 commit e296edc

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::io::ErrorKind;
1+
use std::{io::ErrorKind, path::Path, process::Stdio};
2+
3+
use anyhow::Context;
24

35
// TODO: the following and the second half of plugins/git.rs are duplicates
46

@@ -46,3 +48,27 @@ impl UnderstandGitResult for Result<std::process::Output, std::io::Error> {
4648
}
4749
}
4850
}
51+
52+
pub(crate) async fn is_in_git_repo(dir: &Path) -> anyhow::Result<bool> {
53+
let mut cmd = tokio::process::Command::new("git");
54+
cmd.arg("-C")
55+
.arg(dir)
56+
.arg("rev-parse")
57+
.arg("--git-dir")
58+
.stdout(Stdio::null())
59+
.stderr(Stdio::null());
60+
61+
let status = cmd
62+
.status()
63+
.await
64+
.context("checking if new app is in a git repo")?;
65+
Ok(status.success())
66+
}
67+
68+
pub(crate) async fn init_git_repo(dir: &Path) -> Result<(), GitError> {
69+
let mut cmd = tokio::process::Command::new("git");
70+
cmd.arg("-C").arg(dir).arg("init");
71+
72+
let result = cmd.output().await;
73+
result.understand_git_result().map(|_| ())
74+
}

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.maybe_initialise_git())
79+
.await
7780
.err()
7881
}
7982

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

358+
async fn maybe_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)