Skip to content
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spin-common = { path = "../common" }
spin-manifest = { path = "../manifest" }
tar = { workspace = true }
tempfile = { workspace = true }
terminal = { path = "../terminal" }
tokio = { workspace = true, features = ["fs", "process", "rt", "macros"] }
toml = { workspace = true }
toml_edit = { workspace = true }
Expand Down
28 changes: 27 additions & 1 deletion crates/templates/src/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::io::ErrorKind;
use std::{io::ErrorKind, path::Path, process::Stdio};

use anyhow::Context;

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

Expand Down Expand Up @@ -46,3 +48,27 @@ impl UnderstandGitResult for Result<std::process::Output, std::io::Error> {
}
}
}

pub(crate) async fn is_in_git_repo(dir: &Path) -> anyhow::Result<bool> {
let mut cmd = tokio::process::Command::new("git");
cmd.arg("-C")
.arg(dir)
.arg("rev-parse")
.arg("--git-dir")
.stdout(Stdio::null())
.stderr(Stdio::null());

let status = cmd
.status()
.await
.context("checking if new app is in a git repo")?;
Ok(status.success())
}

pub(crate) async fn init_git_repo(dir: &Path) -> Result<(), GitError> {
let mut cmd = tokio::process::Command::new("git");
cmd.arg("-C").arg(dir).arg("init");

let result = cmd.output().await;
result.understand_git_result().map(|_| ())
}
29 changes: 29 additions & 0 deletions crates/templates/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use walkdir::WalkDir;

use crate::{
cancellable::Cancellable,
git,
interaction::{InteractionStrategy, Interactive, Silent},
renderer::MergeTarget,
template::{ExtraOutputAction, TemplateVariantInfo},
Expand Down Expand Up @@ -74,6 +75,8 @@ impl Run {
.and_then(|t| t.render())
.and_then_async(|o| async move { o.write().await })
.await
.and_then_async(|_| self.maybe_initialise_git())
.await
.err()
}

Expand Down Expand Up @@ -352,6 +355,32 @@ impl Run {
}
}

async fn maybe_initialise_git(&self) -> anyhow::Result<()> {
if !matches!(self.options.variant, TemplateVariantInfo::NewApplication) {
return Ok(());
}

if self.options.no_vcs {
return Ok(());
}

let target_dir = self.generation_target_dir();

let skip_initing_repo = git::is_in_git_repo(&target_dir).await.unwrap_or(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn if is_in_git_repo errors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say not: we don't want to error on "git not installed," and other cases seem unlikely. We could trace or something if you feel strongly?


if skip_initing_repo {
return Ok(());
}

if let Err(e) = git::init_git_repo(&target_dir).await {
if !matches!(e, git::GitError::ProgramNotFound) {
terminal::warn!("Spin was unable to initialise a Git repository. Run `git init` manually if you want one.");
}
}

Ok(())
}

fn list_content_files(from: &Path) -> anyhow::Result<Vec<PathBuf>> {
let walker = WalkDir::new(from);
let files = walker
Expand Down