@@ -11,6 +11,13 @@ patch_cache_dir="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")/patch_cache"
1111mkdir -p " $patch_cache_dir "
1212export patch_cache_dir
1313
14+ function git-with-credentials() {
15+ git -c user.name=" apply-github-patch" \
16+ -c user.email=" noreply@example.com" \
17+ -c commit.gpgsign=" false" \
18+ " $@ "
19+ }
20+
1421function git-shallow-clone {
1522 (
1623 local repo_name=$( basename " $1 " .git)
@@ -51,16 +58,9 @@ function apply-github-patch {
5158 fi
5259 fi
5360
54- _git_with_credentials () {
55- git -c user.name=" apply-github-patch" \
56- -c user.email=" noreply@example.com" \
57- -c commit.gpgsign=" false" \
58- " $@ "
59- }
60-
6161 # Approach #1: Try a simple patch application with 'git am'
62- _git_with_credentials am --keep-cr " $patch_file " && return 0
63- _git_with_credentials am --abort || true # wokeignore:rule=abort/terminate
62+ git-with-credentials am --keep-cr " $patch_file " && return 0
63+ git-with-credentials am --abort || true # wokeignore:rule=abort/terminate
6464
6565 # Approach #2: Try a three-way merge after fetching the parent commit. It can handle
6666 # scenarios in which the context of the patch has moved. However, we need the parent
@@ -70,12 +70,25 @@ function apply-github-patch {
7070 fetch_url=" https://x-access-token:${GITHUB_TOKEN} @github.com/$1 .git"
7171 fi
7272 git fetch --no-tags --quiet --depth=2 " $fetch_url " " $2 " || true
73- _git_with_credentials am --3way --keep-cr " $patch_file " && return 0
74- _git_with_credentials am --abort || true # wokeignore:rule=abort/terminate
73+ git-with-credentials am --3way --keep-cr " $patch_file " && return 0
74+ git-with-credentials am --abort || true # wokeignore:rule=abort/terminate
7575
7676 # Approach #3: Fall back to GNU 'patch'
7777 patch -p1 < " $patch_file " || return 1
7878 git add -A
79- _git_with_credentials commit -m " Applied patch $2 from $1 ."
79+ git-with-credentials commit -m " Applied patch $2 from $1 ."
8080 return 0
8181}
82+
83+ function replace_once() {
84+ python3 - " $@ " << 'PY '
85+ import sys
86+ from pathlib import Path
87+
88+ path, old, new = Path(sys.argv[1]), sys.argv[2], sys.argv[3]
89+ text = path.read_text(encoding="utf-8")
90+ if old not in text:
91+ raise SystemExit(f"{path}: expected text not found: {old!r}")
92+ path.write_text(text.replace(old, new, 1), encoding="utf-8")
93+ PY
94+ }
0 commit comments