| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | use std::collections::HashMap; |
| 27 | use std::sync::Arc; |
| 28 | |
| 29 | use globset::{GlobBuilder, GlobMatcher}; |
| 30 | |
| 31 | use crate::GitError; |
| 32 | use crate::repo::Repo; |
| 33 | use crate::types::Oid; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 41 | pub enum AttrValue { |
| 42 | |
| 43 | Set, |
| 44 | |
| 45 | Unset, |
| 46 | |
| 47 | Unspecified, |
| 48 | |
| 49 | Value(String), |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 57 | pub struct Attributes { |
| 58 | map: HashMap<String, AttrValue>, |
| 59 | } |
| 60 | |
| 61 | impl Attributes { |
| 62 | |
| 63 | #[must_use] |
| 64 | pub fn get(&self, name: &str) -> Option<&AttrValue> { |
| 65 | self.map.get(name) |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | #[must_use] |
| 71 | pub fn is_set(&self, name: &str) -> bool { |
| 72 | match self.map.get(name) { |
| 73 | Some(AttrValue::Set) => true, |
| 74 | Some(AttrValue::Value(v)) => v != "false", |
| 75 | _ => false, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | #[must_use] |
| 81 | pub fn is_unset(&self, name: &str) -> bool { |
| 82 | matches!(self.map.get(name), Some(AttrValue::Unset)) |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | #[must_use] |
| 88 | pub fn language(&self) -> Option<&str> { |
| 89 | for key in ["fabrica-language", "linguist-language"] { |
| 90 | if let Some(AttrValue::Value(v)) = self.map.get(key) { |
| 91 | return Some(v.as_str()); |
| 92 | } |
| 93 | } |
| 94 | None |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | #[must_use] |
| 100 | pub fn is_binary(&self) -> bool { |
| 101 | self.is_set("binary") || self.is_unset("text") |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | #[must_use] |
| 107 | pub fn no_diff(&self) -> bool { |
| 108 | self.is_unset("diff") || self.is_set("binary") |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #[must_use] |
| 113 | pub fn is_generated(&self) -> bool { |
| 114 | self.is_set("linguist-generated") || self.is_set("fabrica-generated") |
| 115 | } |
| 116 | |
| 117 | |
| 118 | #[must_use] |
| 119 | pub fn is_vendored(&self) -> bool { |
| 120 | self.is_set("linguist-vendored") |
| 121 | } |
| 122 | |
| 123 | |
| 124 | #[must_use] |
| 125 | pub fn is_lfs(&self) -> bool { |
| 126 | matches!(self.map.get("filter"), Some(AttrValue::Value(v)) if v == "lfs") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | #[derive(Debug)] |
| 133 | struct AttrFile { |
| 134 | |
| 135 | dir: String, |
| 136 | |
| 137 | rules: Vec<Rule>, |
| 138 | } |
| 139 | |
| 140 | |
| 141 | #[derive(Debug)] |
| 142 | struct Rule { |
| 143 | |
| 144 | matcher: GlobMatcher, |
| 145 | |
| 146 | |
| 147 | floating: bool, |
| 148 | |
| 149 | assigns: Vec<(String, AttrValue)>, |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | #[derive(Debug, Default)] |
| 156 | pub struct AttributesSet { |
| 157 | files: Vec<AttrFile>, |
| 158 | } |
| 159 | |
| 160 | impl AttributesSet { |
| 161 | |
| 162 | |
| 163 | #[must_use] |
| 164 | pub fn attributes(&self, path: &str) -> Attributes { |
| 165 | let path = path.trim_start_matches('/'); |
| 166 | let mut map: HashMap<String, AttrValue> = HashMap::new(); |
| 167 | |
| 168 | |
| 169 | |
| 170 | for file in &self.files { |
| 171 | let Some(rel) = relative_to(path, &file.dir) else { |
| 172 | continue; |
| 173 | }; |
| 174 | let rel_base = rel.rsplit('/').next().unwrap_or(rel); |
| 175 | for rule in &file.rules { |
| 176 | let hit = if rule.floating { |
| 177 | rule.matcher.is_match(rel_base) |
| 178 | } else { |
| 179 | rule.matcher.is_match(rel) |
| 180 | }; |
| 181 | if hit { |
| 182 | for (name, value) in &rule.assigns { |
| 183 | match value { |
| 184 | AttrValue::Unspecified => { |
| 185 | map.remove(name); |
| 186 | } |
| 187 | other => { |
| 188 | map.insert(name.clone(), other.clone()); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | Attributes { map } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | fn relative_to<'a>(path: &'a str, dir: &str) -> Option<&'a str> { |
| 202 | if dir.is_empty() { |
| 203 | return Some(path); |
| 204 | } |
| 205 | path.strip_prefix(dir)?.strip_prefix('/') |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | fn parse_attr_file(dir: &str, text: &str) -> AttrFile { |
| 212 | let mut rules = Vec::new(); |
| 213 | for raw in text.lines() { |
| 214 | let line = raw.trim(); |
| 215 | if line.is_empty() || line.starts_with('#') { |
| 216 | continue; |
| 217 | } |
| 218 | let mut fields = line.split_whitespace(); |
| 219 | let Some(pattern) = fields.next() else { |
| 220 | continue; |
| 221 | }; |
| 222 | let assigns: Vec<(String, AttrValue)> = fields.map(parse_assignment).collect(); |
| 223 | if assigns.is_empty() { |
| 224 | continue; |
| 225 | } |
| 226 | if let Some((matcher, floating)) = compile_pattern(pattern) { |
| 227 | rules.push(Rule { |
| 228 | matcher, |
| 229 | floating, |
| 230 | assigns, |
| 231 | }); |
| 232 | } |
| 233 | } |
| 234 | AttrFile { |
| 235 | dir: dir.to_string(), |
| 236 | rules, |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | fn parse_assignment(token: &str) -> (String, AttrValue) { |
| 242 | if let Some(name) = token.strip_prefix('!') { |
| 243 | (name.to_string(), AttrValue::Unspecified) |
| 244 | } else if let Some(name) = token.strip_prefix('-') { |
| 245 | (name.to_string(), AttrValue::Unset) |
| 246 | } else if let Some((name, value)) = token.split_once('=') { |
| 247 | (name.to_string(), AttrValue::Value(value.to_string())) |
| 248 | } else { |
| 249 | (token.to_string(), AttrValue::Set) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | fn compile_pattern(pattern: &str) -> Option<(GlobMatcher, bool)> { |
| 261 | let trimmed = pattern.trim_end_matches('/'); |
| 262 | let floating = !trimmed.contains('/'); |
| 263 | let glob_src = trimmed.strip_prefix('/').unwrap_or(trimmed); |
| 264 | let glob = GlobBuilder::new(glob_src) |
| 265 | .literal_separator(!floating) |
| 266 | .build() |
| 267 | .ok()?; |
| 268 | Some((glob.compile_matcher(), floating)) |
| 269 | } |
| 270 | |
| 271 | impl Repo { |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | pub fn attributes_set(&self, oid: &Oid) -> Result<AttributesSet, GitError> { |
| 280 | let commit = self.commit_at(oid)?; |
| 281 | let tree = commit.tree()?; |
| 282 | |
| 283 | |
| 284 | let mut found: Vec<(String, git2::Oid)> = Vec::new(); |
| 285 | tree.walk(git2::TreeWalkMode::PreOrder, |root, entry| { |
| 286 | if entry.name() == Some(".gitattributes") |
| 287 | && entry.filemode() != i32::from(git2::FileMode::Tree) |
| 288 | { |
| 289 | |
| 290 | found.push((root.trim_end_matches('/').to_string(), entry.id())); |
| 291 | } |
| 292 | git2::TreeWalkResult::Ok |
| 293 | })?; |
| 294 | |
| 295 | let mut files: Vec<AttrFile> = Vec::with_capacity(found.len()); |
| 296 | for (dir, blob_oid) in found { |
| 297 | let blob = self.raw().find_blob(blob_oid)?; |
| 298 | let text = String::from_utf8_lossy(blob.content()); |
| 299 | files.push(parse_attr_file(&dir, &text)); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | files.sort_by(|a, b| { |
| 304 | depth(&a.dir) |
| 305 | .cmp(&depth(&b.dir)) |
| 306 | .then_with(|| a.dir.cmp(&b.dir)) |
| 307 | }); |
| 308 | Ok(AttributesSet { files }) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | |
| 313 | fn depth(dir: &str) -> usize { |
| 314 | if dir.is_empty() { |
| 315 | 0 |
| 316 | } else { |
| 317 | dir.split('/').count() |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | #[derive(Clone)] |
| 328 | pub struct AttributesCache { |
| 329 | inner: moka::sync::Cache<String, Arc<AttributesSet>>, |
| 330 | } |
| 331 | |
| 332 | impl AttributesCache { |
| 333 | |
| 334 | #[must_use] |
| 335 | pub fn new(capacity: u64) -> Self { |
| 336 | Self { |
| 337 | inner: moka::sync::Cache::new(capacity), |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | pub fn get(&self, repo: &Repo, oid: &Oid) -> Result<Arc<AttributesSet>, GitError> { |
| 347 | if let Some(hit) = self.inner.get(oid.as_str()) { |
| 348 | return Ok(hit); |
| 349 | } |
| 350 | let set = Arc::new(repo.attributes_set(oid)?); |
| 351 | self.inner.insert(oid.to_string(), Arc::clone(&set)); |
| 352 | Ok(set) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | impl Default for AttributesCache { |
| 357 | fn default() -> Self { |
| 358 | Self::new(256) |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | #[cfg(test)] |
| 363 | mod tests { |
| 364 | #![allow(clippy::unwrap_used, clippy::expect_used)] |
| 365 | |
| 366 | use super::*; |
| 367 | |
| 368 | |
| 369 | |
| 370 | fn set_from(files: &[(&str, &str)]) -> AttributesSet { |
| 371 | let mut parsed: Vec<AttrFile> = files |
| 372 | .iter() |
| 373 | .map(|(dir, text)| parse_attr_file(dir, text)) |
| 374 | .collect(); |
| 375 | parsed.sort_by(|a, b| { |
| 376 | depth(&a.dir) |
| 377 | .cmp(&depth(&b.dir)) |
| 378 | .then_with(|| a.dir.cmp(&b.dir)) |
| 379 | }); |
| 380 | AttributesSet { files: parsed } |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn floating_pattern_matches_basename_at_any_depth() { |
| 385 | let set = set_from(&[("", "*.rs linguist-language=Rust\n")]); |
| 386 | assert_eq!(set.attributes("src/deep/main.rs").language(), Some("Rust")); |
| 387 | assert_eq!(set.attributes("main.rs").language(), Some("Rust")); |
| 388 | assert_eq!(set.attributes("main.py").language(), None); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn anchored_pattern_only_matches_its_directory_level() { |
| 393 | |
| 394 | let set = set_from(&[("", "/*.rs linguist-language=Rust\n")]); |
| 395 | assert_eq!(set.attributes("main.rs").language(), Some("Rust")); |
| 396 | assert_eq!(set.attributes("src/main.rs").language(), None); |
| 397 | } |
| 398 | |
| 399 | #[test] |
| 400 | fn double_star_spans_directories() { |
| 401 | let set = set_from(&[("", "src/**/*.rs linguist-language=Rust\n")]); |
| 402 | assert_eq!(set.attributes("src/a/b/x.rs").language(), Some("Rust")); |
| 403 | assert_eq!(set.attributes("other/x.rs").language(), None); |
| 404 | } |
| 405 | |
| 406 | #[test] |
| 407 | fn deeper_file_wins_over_shallower() { |
| 408 | |
| 409 | let set = set_from(&[ |
| 410 | ("", "* linguist-generated\n"), |
| 411 | ("src", "* -linguist-generated\n"), |
| 412 | ]); |
| 413 | assert!(set.attributes("README.md").is_generated()); |
| 414 | assert!(!set.attributes("src/main.rs").is_generated()); |
| 415 | } |
| 416 | |
| 417 | #[test] |
| 418 | fn later_line_wins_within_a_file() { |
| 419 | let set = set_from(&[( |
| 420 | "", |
| 421 | "*.rs linguist-language=Rust\nmain.rs linguist-language=Ruby\n", |
| 422 | )]); |
| 423 | assert_eq!(set.attributes("main.rs").language(), Some("Ruby")); |
| 424 | assert_eq!(set.attributes("lib.rs").language(), Some("Rust")); |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn negation_clears_a_value_from_a_less_specific_rule() { |
| 429 | |
| 430 | let set = set_from(&[("", "* linguist-language=Text\n*.rs !linguist-language\n")]); |
| 431 | assert_eq!(set.attributes("notes.md").language(), Some("Text")); |
| 432 | assert_eq!(set.attributes("main.rs").language(), None); |
| 433 | } |
| 434 | |
| 435 | #[test] |
| 436 | fn binary_and_minus_text_are_binary() { |
| 437 | let set = set_from(&[("", "*.bin binary\n*.dat -text\n")]); |
| 438 | assert!(set.attributes("x.bin").is_binary()); |
| 439 | assert!(set.attributes("x.dat").is_binary()); |
| 440 | assert!(!set.attributes("x.txt").is_binary()); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn minus_diff_suppresses_diff_rendering() { |
| 445 | let set = set_from(&[("", "*.lock -diff\n")]); |
| 446 | assert!(set.attributes("Cargo.lock").no_diff()); |
| 447 | assert!(!set.attributes("src/main.rs").no_diff()); |
| 448 | |
| 449 | let bin = set_from(&[("", "*.png binary\n")]); |
| 450 | assert!(bin.attributes("logo.png").no_diff()); |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn comments_and_blanks_are_ignored() { |
| 455 | let set = set_from(&[("", "# a comment\n\n*.rs linguist-language=Rust\n")]); |
| 456 | assert_eq!(set.attributes("main.rs").language(), Some("Rust")); |
| 457 | } |
| 458 | |
| 459 | #[test] |
| 460 | fn fabrica_language_beats_linguist_language() { |
| 461 | let set = set_from(&[( |
| 462 | "", |
| 463 | "*.rs linguist-language=Rust\n*.rs fabrica-language=RustCustom\n", |
| 464 | )]); |
| 465 | assert_eq!(set.attributes("main.rs").language(), Some("RustCustom")); |
| 466 | } |
| 467 | |
| 468 | #[test] |
| 469 | fn attributes_set_reads_gitattributes_from_a_real_tree() { |
| 470 | use std::path::Path; |
| 471 | |
| 472 | use git2::{Repository, Signature, Time}; |
| 473 | |
| 474 | use crate::{create_bare, repo_path}; |
| 475 | |
| 476 | let dir = tempfile::tempdir().unwrap(); |
| 477 | let id = "01hzxk9m2n8p7q6r5s4t3v2w1x"; |
| 478 | create_bare(dir.path(), id, "main", Path::new("/usr/bin/fabrica")).unwrap(); |
| 479 | let path = repo_path(dir.path(), id).unwrap(); |
| 480 | let raw = Repository::open_bare(&path).unwrap(); |
| 481 | |
| 482 | |
| 483 | let root_attr = raw |
| 484 | .blob(b"vendor/** linguist-vendored\n*.rs linguist-language=Rust\n") |
| 485 | .unwrap(); |
| 486 | let src_attr = raw.blob(b"*.rs -linguist-vendored\n").unwrap(); |
| 487 | let code = raw.blob(b"fn main() {}\n").unwrap(); |
| 488 | |
| 489 | let mut src = raw.treebuilder(None).unwrap(); |
| 490 | src.insert(".gitattributes", src_attr, 0o100_644).unwrap(); |
| 491 | src.insert("main.rs", code, 0o100_644).unwrap(); |
| 492 | let src_tree = src.write().unwrap(); |
| 493 | |
| 494 | let mut vendor = raw.treebuilder(None).unwrap(); |
| 495 | vendor.insert("lib.rs", code, 0o100_644).unwrap(); |
| 496 | let vendor_tree = vendor.write().unwrap(); |
| 497 | |
| 498 | let mut root = raw.treebuilder(None).unwrap(); |
| 499 | root.insert(".gitattributes", root_attr, 0o100_644).unwrap(); |
| 500 | root.insert("src", src_tree, 0o040_000).unwrap(); |
| 501 | root.insert("vendor", vendor_tree, 0o040_000).unwrap(); |
| 502 | let root_tree = raw.find_tree(root.write().unwrap()).unwrap(); |
| 503 | |
| 504 | let sig = Signature::new("Ada", "ada@example.com", &Time::new(1, 0)).unwrap(); |
| 505 | let commit = raw |
| 506 | .commit(Some("refs/heads/main"), &sig, &sig, "init", &root_tree, &[]) |
| 507 | .unwrap(); |
| 508 | |
| 509 | let repo = Repo::open_path(&path).unwrap(); |
| 510 | let set = repo.attributes_set(&Oid::new(commit.to_string())).unwrap(); |
| 511 | |
| 512 | |
| 513 | |
| 514 | assert!(set.attributes("vendor/lib.rs").is_vendored()); |
| 515 | assert!(!set.attributes("src/main.rs").is_vendored()); |
| 516 | assert_eq!(set.attributes("src/main.rs").language(), Some("Rust")); |
| 517 | } |
| 518 | } |