Handle the case of first populating an empty file

This commit is contained in:
Paul Gauthier 2023-05-06 10:32:52 -07:00
parent d50b4c4de2
commit cfd415c8f2
2 changed files with 18 additions and 11 deletions

View file

@ -296,13 +296,14 @@ class Coder:
return ''.join(resp) return ''.join(resp)
pattern = re.compile(r'(\S+)\s+<<<<<<< ORIGINAL\n(.*?)\n=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL) pattern = re.compile(r'(\S+)\s+(```)?<<<<<<< ORIGINAL\n(.*?\n?)=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL)
def update_files(self, content, inp): def update_files(self, content, inp):
edited = set() edited = set()
for match in self.pattern.finditer(content): for match in self.pattern.finditer(content):
path, original, updated = match.groups() path, _, original, updated = match.groups()
edited.add(path) edited.add(path)
if self.do_replace(path, original, updated): if self.do_replace(path, original, updated):
continue continue
@ -317,6 +318,11 @@ class Coder:
fname = Path(fname) fname = Path(fname)
content = fname.read_text().splitlines() content = fname.read_text().splitlines()
if not before_text and not content:
# first populating an empty file
new_content = after_text
else:
before_lines = [l.strip() for l in before_text.splitlines()] before_lines = [l.strip() for l in before_text.splitlines()]
stripped_content = [l.strip() for l in content] stripped_content = [l.strip() for l in content]
where = find_index(stripped_content, before_lines) where = find_index(stripped_content, before_lines)

View file

@ -28,6 +28,7 @@ some/dir/names/example.py
>>>>>>> UPDATED >>>>>>> UPDATED
Be sure to include the correct path and filename for each edit, exactly as specified by the user. Be sure to include the correct path and filename for each edit, exactly as specified by the user.
Don't use ``` to demarcate code blocks!
LEADING WHITESPACE IS IMPORTANT IN ALL CODE! LEADING WHITESPACE IS IMPORTANT IN ALL CODE!
NEVER RETURN THE ENTIRE SOURCE FILE. ONLY RETURN CODE IN ORIGINAL/UPDATED FORMAT CHANGES! NEVER RETURN THE ENTIRE SOURCE FILE. ONLY RETURN CODE IN ORIGINAL/UPDATED FORMAT CHANGES!
''' '''