use std::fs;
use std::path::Path;
use anyhow::{Context, Result};
use crate::ui;
const GITATTRIBUTES: &str = "\
# StyLua formats to Unix line endings and CI checks that on Linux, where a
# checkout is always LF. Git for Windows defaults to core.autocrlf=true and
# would otherwise hand a Windows clone CRLF files, failing `stylua --check`
# on every file in a project nobody has touched.
* text=auto eol=lf
# Never line-ending-convert these.
*.blend binary
*.blend1 binary
*.rbxl binary
*.rbxlx binary
*.rbxm binary
*.rbxmx binary
*.png binary
*.jpg binary
*.ogg binary
*.mp3 binary
";
pub fn ensure_gitattributes(project_dir: &Path) -> Result<()> {
let path = project_dir.join(".gitattributes");
if path.exists() {
ui::ok(".gitattributes already exists");
return Ok(());
}
fs::write(&path, GITATTRIBUTES)
.with_context(|| format!("failed to write {}", path.display()))?;
ui::ok("wrote .gitattributes");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn forces_lf_in_the_working_tree() {
assert!(GITATTRIBUTES.contains("* text=auto eol=lf"), "{GITATTRIBUTES}");
}
#[test]
fn binary_formats_are_excluded_from_conversion() {
for ext in ["blend", "rbxl", "rbxlx", "rbxm", "rbxmx", "png"] {
assert!(
GITATTRIBUTES.contains(&format!("*.{ext} binary")),
"*.{ext} must be marked binary:\n{GITATTRIBUTES}"
);
}
}
}