From 5f13be093bbba258166f8fc2010e12f97a357737 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 18 Aug 2023 11:18:31 -0400 Subject: [PATCH 1/4] Set linguist attributes for `pixi.lock` This applies syntax highlighting in the GitHub web interface, and treats it as a generated file. --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..9cc0b5fe0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +pixi.lock linguist-generated linguist-language=YAML From bbeea3073cff9c7ad84e0c741573da8826ee8613 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 18 Aug 2023 12:15:27 -0400 Subject: [PATCH 2/4] Write `.gitattributes` if it doesn't exist --- src/cli/init.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/cli/init.rs b/src/cli/init.rs index bb5cec14f..7805ca8c5 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -4,7 +4,7 @@ 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 +42,17 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments "#; +const GITATTRIBUTES_TEMPLATE: &str = r#"# GitHub syntax highlighting and stats +pixi.lock linguist-generated 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 +98,12 @@ 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 +136,14 @@ 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; From 4f9b07afc862e54813ac6cace676bced66b9cea8 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 18 Aug 2023 13:23:15 -0400 Subject: [PATCH 3/4] Remove `linguist-generated` https://github.com/prefix-dev/pixi/pull/265#issuecomment-1684202900 --- .gitattributes | 2 +- src/cli/init.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitattributes b/.gitattributes index 9cc0b5fe0..fbfdc128a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -pixi.lock linguist-generated linguist-language=YAML +pixi.lock linguist-language=YAML diff --git a/src/cli/init.rs b/src/cli/init.rs index 7805ca8c5..9b310d711 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -42,8 +42,8 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments "#; -const GITATTRIBUTES_TEMPLATE: &str = r#"# GitHub syntax highlighting and stats -pixi.lock linguist-generated linguist-language=YAML +const GITATTRIBUTES_TEMPLATE: &str = r#"# GitHub syntax highlighting +pixi.lock linguist-language=YAML "#; From d744baa3313d5eaec53bb6eb4e7458e6f11da02f Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 18 Aug 2023 13:24:07 -0400 Subject: [PATCH 4/4] Format --- src/cli/init.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/cli/init.rs b/src/cli/init.rs index 9b310d711..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::{Path, PathBuf}}; +use std::{ + fs, + path::{Path, PathBuf}, +}; /// Creates a new project #[derive(Parser, Debug)] @@ -103,7 +106,12 @@ pub async fn execute(args: Args) -> miette::Result<()> { // create a .gitattributes if one is missing if !gitattributes_path.is_file() { - write_contextless_file(&env, gitattributes_path, "gitattributes.txt", GITATTRIBUTES_TEMPLATE)?; + write_contextless_file( + &env, + gitattributes_path, + "gitattributes.txt", + GITATTRIBUTES_TEMPLATE, + )?; } // Emit success @@ -136,10 +144,13 @@ 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()?; +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(()) }