Skip to content

Commit 8368dbe

Browse files
committed
Fix e.g. "re.PatternError: bad escape \s at position" error
1 parent 701a0f1 commit 8368dbe

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

git-grok

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,9 +2051,8 @@ def body_suffix_upsert(
20512051
suffix = (
20522052
BODY_SUFFIX_TITLE + "\n" + "\n".join(items) + "\n\n" + BODY_SUFFIX_FOOTER + "\n"
20532053
)
2054-
if re.search(BODY_SUFFIX_RE, body, flags=re.M | re.I | re.X):
2055-
return re.sub(BODY_SUFFIX_RE, suffix, body, flags=re.M | re.I | re.X)
2056-
return body.rstrip() + "\n\n" + suffix
2054+
new_body = re.sub(BODY_SUFFIX_RE, lambda _: suffix, body, flags=re.M | re.I | re.X)
2055+
return new_body if new_body != body else body.rstrip() + "\n\n" + suffix
20572056

20582057

20592058
#
@@ -2065,7 +2064,7 @@ def body_build_from_injected_text(
20652064
) -> str:
20662065
return re.sub(
20672066
r"\s*" + re.escape(AI_PLACEHOLDER) + r"\s*",
2068-
f"\n\n{injected_text.text}\n\n",
2067+
lambda _: f"\n\n{injected_text.text}\n\n",
20692068
injected_text.template_with_placeholder,
20702069
)
20712070

@@ -2095,7 +2094,7 @@ def body_convert_to_injected_text(
20952094
# Returns an AI hash comment.
20962095
#
20972096
def ai_hash_build(hash: str) -> str:
2098-
return re.sub(r"(?=-->)", ":" + hash, AI_PLACEHOLDER)
2097+
return re.sub(r"(?=-->)", lambda _: f":{hash}", AI_PLACEHOLDER)
20992098

21002099

21012100
#

tests/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ def git_init_and_cd_to_test_dir(
197197
# branch has already been deleted by a parallel test run or so).
198198

199199

200-
def git_touch(file: str):
201-
check_output_x("touch", file)
200+
def git_touch(file: str, content: str | None = None):
201+
with open(file, "w") as f:
202+
f.write(content or "")
202203

203204

204205
def git_add_commit(msg: str):

tests/test_ai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Test(TestCaseWithEmptyTestRepo):
1212
def test_ai(self):
1313
self.git_init_and_cd_to_test_dir(method="push.default")
14-
git_touch("commit01")
14+
git_touch("commit01", r"some \s text")
1515
git_add_commit("commit01")
1616
run_git_grok()
1717

0 commit comments

Comments
 (0)