Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitamalinov committed May 9, 2024
1 parent 3de6137 commit 58c5ba2
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions utils/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ def apply_patch(original_text: str, diff_text: str) -> str:

print("Patch applied successfully.")
with open(file=original_file_name, mode="r", encoding="utf-8") as modified_file:
# Check that this file is computable -> get extention and run proper run
# Use GPT to fix the file and compute again
modified_text: str = modified_file.read()
print(f"{modified_text=}\n")

except subprocess.CalledProcessError as e:
print("Failed to apply patch.")
print(f"stdout: {e.stdout}")
print(f"stderr: {e.stderr}\n")
logging.error(msg=f"apply_patch stderr: {e.stderr}") # pylint: disable=no-member
logging.error(
msg=f"apply_patch stderr: {e.stderr}"
) # pylint: disable=no-member
print(f"Command: {' '.join(e.cmd)}")
print(f"Exit status: {e.returncode}")
return ""
Expand Down Expand Up @@ -88,7 +92,8 @@ def correct_hunk_headers(diff_text: str) -> str:
lines: list[str] = diff_text.splitlines()
updated_lines: list[str] = []
hunk_pattern: re.Pattern[str] = re.compile(
pattern=r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
pattern=r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@"
)

i = 0
while i < len(lines):
Expand All @@ -108,19 +113,19 @@ def correct_hunk_headers(diff_text: str) -> str:

# Count actual number of lines changed
start_index: int = i
while i < len(lines) and not lines[i].startswith('@@'):
if lines[i].startswith('+'):
while i < len(lines) and not lines[i].startswith("@@"):
if lines[i].startswith("+"):
s2_actual += 1
if lines[i].startswith('-'):
if lines[i].startswith("-"):
s1_actual += 1
i += 1

# Update the hunk header with actual numbers
updated_hunk_header: str = f'@@ -{l1},{s1_actual} +{l2},{s2_actual} @@'
updated_hunk_header: str = f"@@ -{l1},{s1_actual} +{l2},{s2_actual} @@"
updated_lines.append(updated_hunk_header)
updated_lines.extend(lines[start_index:i])

return '\n'.join(updated_lines)
return "\n".join(updated_lines)


def extract_file_name(diff_text: str) -> str:
Expand Down

0 comments on commit 58c5ba2

Please sign in to comment.