#[cfg(not(feature = "git"))]
pub fn feature_disabled(what: &str) -> anyhow::Error {
anyhow::anyhow!(
"{what} needs git support, but this build of cargo-generate was compiled \
without the `git` cargo feature.\n\
\n\
Enable it in your Cargo.toml:\n\
\x20 cargo-generate = {{ version = \"…\", features = [\"git\"] }}\n\
\n\
Templates from a local path work without it."
)
}
#[cfg(feature = "git")]
#[allow(clippy::missing_const_for_fn)]
pub fn ensure_available(_what: &str) -> anyhow::Result<()> {
Ok(())
}
#[cfg(not(feature = "git"))]
pub fn ensure_available(what: &str) -> anyhow::Result<()> {
Err(feature_disabled(what))
}
#[cfg(not(feature = "git"))]
pub const fn try_get_branch_from_path(_path: &std::path::Path) -> Option<String> {
None
}
#[cfg(not(feature = "git"))]
pub fn init(
_project_dir: &std::path::Path,
_branch: Option<&str>,
_force: bool,
) -> anyhow::Result<()> {
Err(feature_disabled(
"initializing a git repository in the generated project \
(`Vcs::Git`, or `vcs` in the template's or a favorite's config)",
))
}
#[cfg(not(feature = "git"))]
#[allow(clippy::missing_const_for_fn)]
pub fn read_config_string(_key: &str, _cwd: &std::path::Path) -> Option<String> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "git")]
#[test]
fn ensure_available_is_ok_with_the_feature() {
assert!(ensure_available("`Vcs::Git`").is_ok());
}
#[cfg(not(feature = "git"))]
#[test]
fn ensure_available_errors_without_the_feature() {
let err = ensure_available("`Vcs::Git`").unwrap_err().to_string();
assert!(
err.contains("`Vcs::Git`"),
"names the offending input: {err}"
);
assert!(
err.contains("`git` cargo feature"),
"names the feature: {err}"
);
}
#[cfg(not(feature = "git"))]
#[test]
fn read_config_string_is_none_without_the_feature() {
assert_eq!(
read_config_string("user.name", std::path::Path::new(".")),
None
);
}
#[cfg(not(feature = "git"))]
#[test]
fn init_stub_reports_the_disabled_feature() {
let err = init(std::path::Path::new("."), None, false)
.unwrap_err()
.to_string();
assert!(err.contains("`git` cargo feature"), "{err}");
}
}