Skip to content

Commit 4fef9bd

Browse files
committed
implement tidy bless for alphabetical blocks
1 parent 3a0f23b commit 4fef9bd

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5615,6 +5615,7 @@ dependencies = [
56155615
"semver",
56165616
"serde",
56175617
"similar",
5618+
"tempfile",
56185619
"termcolor",
56195620
"toml 0.7.8",
56205621
"walkdir",

src/tools/tidy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc-hash = "2.0.0"
1818
fluent-syntax = "0.12"
1919
similar = "2.5.0"
2020
toml = "0.7.8"
21+
tempfile = "3.15.0"
2122

2223
[features]
2324
build-metrics = ["dep:serde"]

src/tools/tidy/src/alphabetical.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,36 @@ fn check_lines<'a>(path: &Path, content: &'a str, tidy_ctx: &TidyCtx, check: &mu
214214

215215
// oh nyooo :(
216216
if sorted != section {
217-
let base_line_number = content[..offset + start_nl_end].lines().count();
218-
let line_offset = sorted
219-
.lines()
220-
.zip(section.lines())
221-
.enumerate()
222-
.find(|(_, (a, b))| a != b)
223-
.unwrap()
224-
.0;
225-
let line_number = base_line_number + line_offset;
226-
227-
check.error(format!(
228-
"{path}:{line_number}: line not in alphabetical order (tip: use --bless to sort this list)",
229-
path = path.display(),
230-
));
217+
if !tidy_ctx.is_bless_enabled() {
218+
let base_line_number = content[..offset + start_nl_end].lines().count();
219+
let line_offset = sorted
220+
.lines()
221+
.zip(section.lines())
222+
.enumerate()
223+
.find(|(_, (a, b))| a != b)
224+
.unwrap()
225+
.0;
226+
let line_number = base_line_number + line_offset;
227+
228+
check.error(format!(
229+
"{path}:{line_number}: line not in alphabetical order (tip: use --bless to sort this list)",
230+
path = path.display(),
231+
));
232+
} else {
233+
// Use atomic rename as to not corrupt the file upon crashes/ctrl+c
234+
let mut tempfile =
235+
tempfile::Builder::new().tempfile_in(path.parent().unwrap()).unwrap();
236+
237+
fs::copy(path, tempfile.path()).unwrap();
238+
239+
tempfile
240+
.as_file_mut()
241+
.seek(std::io::SeekFrom::Start((offset + start_nl_end) as u64))
242+
.unwrap();
243+
tempfile.as_file_mut().write_all(sorted.as_bytes()).unwrap();
244+
245+
tempfile.persist(path).unwrap();
246+
}
231247
}
232248

233249
// Start the next search after the end section

0 commit comments

Comments
 (0)