Skip to content

Commit 8227f1a

Browse files
authored
feat: provide disk_file_creation_year for usecases where no git history exist (#185)
1 parent ec5ff34 commit 8227f1a

File tree

10 files changed

+80
-5
lines changed

10 files changed

+80
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [6.1.0] 2025-06-06
6+
7+
New properties:
8+
9+
* `attrs.disk_file_creation_year` can be used to replace not existing git history attrs values like `{{attrs.git_file_created_year if attrs.git_file_created_year else attrs.disk_file_creation_year }}`
10+
511
## [6.0.0] 2025-01-28
612

713
### Breaking changes

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fmt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ repository.workspace = true
2424

2525
[dependencies]
2626
anyhow = { workspace = true }
27+
chrono = "0.4.41"
2728
gix = { version = "0.72.1", default-features = false, features = [
2829
"blob-diff",
2930
"excludes",

fmt/src/document/factory.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::collections::HashSet;
1717
use std::path::Path;
1818
use std::path::PathBuf;
1919

20+
use std::fs;
21+
2022
use anyhow::Context;
2123
use gix::date::time::CustomFormat;
2224

@@ -89,6 +91,13 @@ impl DocumentFactory {
8991
.get(filepath)
9092
.map(|attrs| attrs.authors.clone())
9193
.unwrap_or_default(),
94+
disk_file_creation_year: fs::metadata(filepath)
95+
.and_then(|meta| meta.created())
96+
.ok()
97+
.map(|created_time| {
98+
let datetime = chrono::DateTime::<chrono::Utc>::from(created_time);
99+
datetime.format("%Y").to_string()
100+
}),
92101
};
93102

94103
Document::new(

fmt/src/document/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct Attributes {
4040
pub git_file_created_year: Option<String>,
4141
pub git_file_modified_year: Option<String>,
4242
pub git_authors: BTreeSet<String>,
43+
pub disk_file_creation_year: Option<String>,
4344
}
4445

4546
#[derive(Debug)]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright {{attrs.git_file_created_year if attrs.git_file_created_year else attrs.disk_file_creation_year }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
baseDir = "."
2+
headerPath = "license.txt"
3+
4+
excludes = ["licenserc.toml", "*.expected"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright <CURRENT_YEAR>
2+
3+
fn main() {
4+
println!("Hello, world!");
5+
}

tests/it.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from pathlib import Path
1717
import difflib
1818
import subprocess
19+
import os
20+
import shutil
21+
import datetime
1922

2023
def diff_files(file1, file2):
2124
with file1.open("r", encoding="utf8") as f1, file2.open("r", encoding="utf8") as f2:
@@ -33,16 +36,46 @@ def diff_files(file1, file2):
3336
subprocess.run(["cargo", "build", "--bin", "hawkeye"], cwd=rootdir, check=True)
3437
hawkeye = rootdir / "target" / "debug" / "hawkeye"
3538

36-
def drive(name, files):
39+
def drive(name, files, create_temp_copy=False):
40+
temp_paths = []
41+
expected_files = []
3742
case_dir = basedir / name
38-
subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=case_dir, check=True)
39-
40-
for file in files:
41-
diff_files(case_dir / f"{file}.expected", case_dir / f"{file}.formatted")
43+
try:
44+
if create_temp_copy:
45+
current_year = str(datetime.datetime.now().year)
46+
for filepath in files:
47+
base, ext = os.path.splitext(filepath)
48+
temp_path = f"{base}_temp{ext}"
49+
shutil.copy2(case_dir / filepath, case_dir / temp_path)
50+
temp_paths.append(temp_path)
51+
expected_temp_path = f"{base}_temp{ext}.expected"
52+
shutil.copy2(case_dir / f"{filepath}.expected", case_dir / expected_temp_path)
53+
expected_files.append(expected_temp_path)
54+
with (case_dir / expected_temp_path).open("r", encoding="utf8") as f:
55+
content = f.read()
56+
content = content.replace("<CURRENT_YEAR>", current_year)
57+
with (case_dir / expected_temp_path).open("w", encoding="utf8") as f:
58+
f.write(content)
59+
else:
60+
temp_paths = files
61+
expected_files = [f"{file}.expected" for file in files]
62+
subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=case_dir, check=True)
4263

64+
for file in temp_paths:
65+
diff_files(case_dir / f"{file}.expected", case_dir / f"{file}.formatted")
66+
finally:
67+
# Remove all temp files at the end
68+
if create_temp_copy:
69+
for temp_path in temp_paths:
70+
if os.path.exists(case_dir / temp_path):
71+
os.remove(case_dir / temp_path)
72+
for expected_file in expected_files:
73+
if os.path.exists(case_dir / expected_file):
74+
os.remove(case_dir / expected_file)
4375

4476
drive("attrs_and_props", ["main.rs"])
4577
drive("load_header_path", ["main.rs"])
4678
drive("bom_issue", ["headless_bom.cs"])
4779
drive("regression_blank_line", ["main.rs"])
4880
drive("regression_no_blank_lines", ["repro.py"])
81+
drive("disk_file_creation_year", ["main.rs"], True)

0 commit comments

Comments
 (0)