Skip to content

Commit

Permalink
fix: Don't write to files that are unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed Apr 17, 2023
1 parent e40b8af commit 1c08a7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
21 changes: 0 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,3 @@ Install with pre-commit, using:
- --log-level=error
- --quotes=single # or double
```
or possibly soon:
```yaml
- repo: https://github.com/sondrelg/pre-commit-binary
rev: ""
hooks:
- id: pre-commit-binary
alias: printf-log-formatter
args:
# Specify binary URLs for each platform/arch combination needed
- |
--urls={
"darwin-x86_64": "https://github.com/sondrelg/printf-log-formatter/releases/v1/darwin-x64_86.tar.gz",
"darwin-arm64": "https://github.com/sondrelg/printf-log-formatter/releases/v1/darwin-arm64.tar.gz",
"linux-x64_64": "https://github.com/sondrelg/printf-log-formatter/releases/v1/linux-x64_86.zip"
}
# Remaining arguments passed to the binary
- --log-level=error
- --quotes=single
```
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ async fn fix_file(filename: String) -> Result<bool> {
let (fixed_content, changed) = fix_content(content, &filename).await?;

// Write updated content back to file
let mut file = File::create(&filename).await?;
for content in fixed_content {
file.write_all(content.replace('\n', "\\n").as_bytes())
.await?;
file.write_all(b"\n").await?;
if changed {
let mut file = File::create(&filename).await?;
let mut cleaned_content = vec![];
for content in fixed_content {
cleaned_content.push(content.replace('\n', "\\n"));
}
file.write_all(cleaned_content.join("\n").as_bytes())
.await?
}

Ok(changed)
Expand Down

0 comments on commit 1c08a7f

Please sign in to comment.