Skip to content

Commit 527a956

Browse files
authored
Write to file only if necessary (#17)
1 parent 3134297 commit 527a956

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

auto_typing_final/main.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,23 @@ def main() -> int:
4141
with path.open("r+") as file:
4242
data = file.read()
4343
transformed_content = transform_file_content(data)
44-
if data != transformed_content:
45-
has_changes = True
44+
if data == transformed_content:
45+
continue
46+
47+
has_changes = True
4648

4749
if args.check:
48-
if data != transformed_content:
49-
sys.stdout.writelines(
50-
unified_diff(
51-
data.splitlines(keepends=True),
52-
transformed_content.splitlines(keepends=True),
53-
fromfile=str(path),
54-
tofile=str(path),
55-
)
50+
sys.stdout.writelines(
51+
unified_diff(
52+
data.splitlines(keepends=True),
53+
transformed_content.splitlines(keepends=True),
54+
fromfile=str(path),
55+
tofile=str(path),
5656
)
57+
)
5758
else:
5859
file.seek(0)
5960
file.write(transformed_content)
6061
file.truncate()
6162

62-
return has_changes
63+
return has_changes if args.check else 0

0 commit comments

Comments
 (0)