ORIGINAL/UPDATED -> HEAD/updated

This commit is contained in:
Paul Gauthier 2023-08-09 16:16:43 -03:00
parent fa2da0ef70
commit fb80377d18
2 changed files with 22 additions and 24 deletions

View file

@ -32,10 +32,10 @@ class EditBlockCoder(Coder):
continue continue
raise ValueError(f"""InvalidEditBlock: edit failed! raise ValueError(f"""InvalidEditBlock: edit failed!
{path} does not contain the *exact sequence* of ORIGINAL lines you specified. {path} does not contain the *exact sequence* of HEAD lines you specified.
Try again. Try again.
DO NOT skip blank lines, comments, docstrings, etc! DO NOT skip blank lines, comments, docstrings, etc!
The ORIGINAL block needs to be EXACTLY the same as the lines in {path} with nothing missing! The HEAD block needs to be EXACTLY the same as the lines in {path} with nothing missing!
{path} does not contain these {len(original.splitlines())} exact lines in a row: {path} does not contain these {len(original.splitlines())} exact lines in a row:
``` ```
@ -304,11 +304,11 @@ def do_replace(fname, content, before_text, after_text, fence=None):
return new_content return new_content
ORIGINAL = "<<<<<<< ORIGINAL" HEAD = "<<<<<<< HEAD"
DIVIDER = "=======" DIVIDER = "======="
UPDATED = ">>>>>>> UPDATED" UPDATED = ">>>>>>> updated"
separators = "|".join([ORIGINAL, DIVIDER, UPDATED]) separators = "|".join([HEAD, DIVIDER, UPDATED])
split_re = re.compile(r"^((?:" + separators + r")[ ]*\n)", re.MULTILINE | re.DOTALL) split_re = re.compile(r"^((?:" + separators + r")[ ]*\n)", re.MULTILINE | re.DOTALL)
@ -334,7 +334,7 @@ def find_original_update_blocks(content):
processed.append(cur) processed.append(cur)
raise ValueError(f"Unexpected {cur}") raise ValueError(f"Unexpected {cur}")
if cur.strip() != ORIGINAL: if cur.strip() != HEAD:
processed.append(cur) processed.append(cur)
continue continue
@ -348,14 +348,12 @@ def find_original_update_blocks(content):
if current_filename: if current_filename:
filename = current_filename filename = current_filename
else: else:
raise ValueError( raise ValueError(f"Bad/missing filename. It should go right above {HEAD}")
f"Bad/missing filename. It should go right above {ORIGINAL}"
)
except IndexError: except IndexError:
if current_filename: if current_filename:
filename = current_filename filename = current_filename
else: else:
raise ValueError(f"Bad/missing filename. It should go right above {ORIGINAL}") raise ValueError(f"Bad/missing filename. It should go right above {HEAD}")
current_filename = filename current_filename = filename
@ -365,7 +363,7 @@ def find_original_update_blocks(content):
divider_marker = pieces.pop() divider_marker = pieces.pop()
processed.append(divider_marker) processed.append(divider_marker)
if divider_marker.strip() != DIVIDER: if divider_marker.strip() != DIVIDER:
raise ValueError(f"Expected {DIVIDER}") raise ValueError(f"Expected `{DIVIDER}` not {divider_marker.strip()}")
updated_text = pieces.pop() updated_text = pieces.pop()
processed.append(updated_text) processed.append(updated_text)
@ -373,7 +371,7 @@ def find_original_update_blocks(content):
updated_marker = pieces.pop() updated_marker = pieces.pop()
processed.append(updated_marker) processed.append(updated_marker)
if updated_marker.strip() != UPDATED: if updated_marker.strip() != UPDATED:
raise ValueError(f"Expected {UPDATED}") raise ValueError(f"Expected `{UPDATED}` not `{updated_marker.strip()}")
yield filename, original_text, updated_text yield filename, original_text, updated_text
except ValueError as e: except ValueError as e:
@ -382,10 +380,10 @@ def find_original_update_blocks(content):
raise ValueError(f"{processed}\n^^^ {err}") raise ValueError(f"{processed}\n^^^ {err}")
except IndexError: except IndexError:
processed = "".join(processed) processed = "".join(processed)
raise ValueError(f"{processed}\n^^^ Incomplete ORIGINAL/UPDATED block.") raise ValueError(f"{processed}\n^^^ Incomplete HEAD/updated block.")
except Exception: except Exception:
processed = "".join(processed) processed = "".join(processed)
raise ValueError(f"{processed}\n^^^ Error parsing ORIGINAL/UPDATED block.") raise ValueError(f"{processed}\n^^^ Error parsing HEAD/updated block.")
if __name__ == "__main__": if __name__ == "__main__":
@ -394,11 +392,11 @@ Here's the change:
```text ```text
foo.txt foo.txt
<<<<<<< ORIGINAL <<<<<<< HEAD
Two Two
======= =======
Tooooo Tooooo
>>>>>>> UPDATED >>>>>>> updated
``` ```
Hope you like it! Hope you like it!

View file

@ -21,7 +21,7 @@ Once you understand the request you MUST:
{fence[0]}python {fence[0]}python
some/dir/example.py some/dir/example.py
<<<<<<< ORIGINAL <<<<<<< HEAD
# some comment # some comment
# Func to multiply # Func to multiply
def mul(a,b) def mul(a,b)
@ -29,26 +29,26 @@ some/dir/example.py
# updated comment # updated comment
# Function to add # Function to add
def add(a,b): def add(a,b):
>>>>>>> UPDATED >>>>>>> updated
{fence[1]} {fence[1]}
A program will parse the edit blocks you generate and replace the ORIGINAL lines with the UPDATED lines. A program will parse the edit blocks you generate and replace the HEAD lines with the `updated` lines.
So edit blocks must be precise and unambiguous! So edit blocks must be precise and unambiguous!
Every *edit block* must be fenced with {fence[0]}...{fence[1]} with the correct code language. Every *edit block* must be fenced with {fence[0]}...{fence[1]} with the correct code language.
Every *edit block* must start with the full path! *NEVER* propose edit blocks for *read-only* files. Every *edit block* must start with the full path! *NEVER* propose edit blocks for *read-only* files.
The ORIGINAL section must be an *exact set of sequential lines* from the file! The HEAD section must be an *exact set of sequential lines* from the file!
NEVER SKIP LINES in the ORIGINAL section! NEVER SKIP LINES in the HEAD section!
NEVER ELIDE LINES AND REPLACE THEM WITH A COMMENT! NEVER ELIDE LINES AND REPLACE THEM WITH A COMMENT!
NEVER OMIT ANY WHITESPACE in the ORIGINAL section! NEVER OMIT ANY WHITESPACE in the HEAD section!
Edits to different parts of a file each need their own *edit block*. Edits to different parts of a file each need their own *edit block*.
If you want to put code in a new file, use an edit block with: If you want to put code in a new file, use an edit block with:
- A new file path, including dir name if needed - A new file path, including dir name if needed
- An empty ORIGINAL section - An empty HEAD section
- The new file's contents in the UPDATED section - The new file's contents in the `updated` section
""" """
files_content_prefix = "These are the *read-write* files:\n" files_content_prefix = "These are the *read-write* files:\n"