Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add-license-headers to keep edits from previously run hooks #88

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions src/ansys/pre_commit_hooks/add_license_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"""
import argparse
from datetime import date as dt
import difflib
import filecmp
import json
import os
Expand Down Expand Up @@ -256,21 +257,24 @@ def check_exists(changed_headers, parser, values, proj, missing_headers, i):
return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)
else:
# Save current copy of files[i]
tempfile = NamedTemporaryFile(mode="w", delete=False).name
shutil.copyfile(files[i], tempfile)
before_hook = NamedTemporaryFile(mode="w", delete=False).name
shutil.copyfile(files[i], before_hook)

# Update the header
# tmp captures the stdout of the header.run() function
with NamedTemporaryFile(mode="w", delete=True) as tmp:
args = set_header_args(parser, year, files[i], copyright, template)
header.run(args, proj, tmp)

# check diffs between files and do modifications afterwards
add_hook_changes(before_hook, files[i])

# Compare the tempfile with the updated file
same_content = filecmp.cmp(tempfile, files[i], shallow=False)
# Diff the updated file with the version of the file that was git added
diff = values["git_repo"].git.diff(files[i], name_only=True)
# same_content is True if the two files that are being compared are the same
same_content = filecmp.cmp(before_hook, files[i], shallow=False)

# Print header was successfully changed if it was modified
if diff or (same_content == False):
if same_content == False:
changed_headers = 1
print(f"Successfully changed header of {files[i]}")

Expand All @@ -279,6 +283,53 @@ def check_exists(changed_headers, parser, values, proj, missing_headers, i):
return changed_headers


def add_hook_changes(before_hook, after_hook):
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
"""
Add earlier hook changes to updated file with header.

Parameters
----------
before_hook: str
Path to file before add-license-headers was run.
after_hook: str
Path to file after add-license-headers was run.
"""
# Open files
before_file = open(before_hook, "r")
after_file = open(after_hook, "r")

# Compare the files before and after the hook ran
diff = difflib.ndiff(after_file.readlines(), before_file.readlines())

before_file.close()
after_file.close()

# Combine the changes from before the hook with the changes after the hook
with NamedTemporaryFile(mode="w", delete=False) as tmp:
while True:
try:
line = str(next(diff))
if "+" in line[0] and line[2:] == "\n":
tmp.write(line[2:])
elif "-" in line[0]:
tmp.write(line[2:])
elif "?" in line[0]:
continue
elif " " in line[0]:
tmp.write(line[2:])
except StopIteration:
break
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved

tmp.close()

# Copy the file with combined changes to the file that was
# edited by the add-license-headers hook
shutil.copyfile(tmp.name, after_hook)

# Delete temporary file
os.remove(tmp.name)


def get_full_paths(file_list):
"""
Update file paths to be absolute paths with system separators.
Expand Down
Loading