diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..fbfdc128a --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +pixi.lock linguist-language=YAML diff --git a/src/cli/init.rs b/src/cli/init.rs index bb5cec14f..05e0c42a3 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -4,7 +4,10 @@ use miette::IntoDiagnostic; use minijinja::{context, Environment}; use rattler_conda_types::Platform; use std::io::{Error, ErrorKind}; -use std::{fs, path::PathBuf}; +use std::{ + fs, + path::{Path, PathBuf}, +}; /// Creates a new project #[derive(Parser, Debug)] @@ -42,11 +45,17 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments "#; +const GITATTRIBUTES_TEMPLATE: &str = r#"# GitHub syntax highlighting +pixi.lock linguist-language=YAML + +"#; + pub async fn execute(args: Args) -> miette::Result<()> { let env = Environment::new(); let dir = get_dir(args.path).into_diagnostic()?; let manifest_path = dir.join(consts::PROJECT_MANIFEST); let gitignore_path = dir.join(".gitignore"); + let gitattributes_path = dir.join(".gitattributes"); // Check if the project file doesnt already exist. We don't want to overwrite it. if fs::metadata(&manifest_path).map_or(false, |x| x.is_file()) { @@ -92,10 +101,17 @@ pub async fn execute(args: Args) -> miette::Result<()> { // create a .gitignore if one is missing if !gitignore_path.is_file() { - let rv = env - .render_named_str("gitignore.txt", GITIGNORE_TEMPLATE, ()) - .into_diagnostic()?; - fs::write(&gitignore_path, rv).into_diagnostic()?; + write_contextless_file(&env, gitignore_path, "gitignore.txt", GITIGNORE_TEMPLATE)?; + } + + // create a .gitattributes if one is missing + if !gitattributes_path.is_file() { + write_contextless_file( + &env, + gitattributes_path, + "gitattributes.txt", + GITATTRIBUTES_TEMPLATE, + )?; } // Emit success @@ -128,6 +144,17 @@ fn get_dir(path: PathBuf) -> Result { } } +fn write_contextless_file>( + env: &Environment, + path: P, + name: &str, + template: &str, +) -> miette::Result<()> { + let rv = env.render_named_str(name, template, ()).into_diagnostic()?; + fs::write(&path, rv).into_diagnostic()?; + Ok(()) +} + #[cfg(test)] mod tests { use crate::cli::init::get_dir;