-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
# @describe Apply a patch to a file at the specified path. | ||
# This can be used to edit the file, without having to rewrite the whole file. | ||
|
||
# @option --path! The path of the file to apply to | ||
# @option --contents! The patch to apply to the file | ||
# | ||
# Here is an example of a patch block that can be applied to modify the file to request the user's name: | ||
# --- a/hello.py | ||
# +++ b/hello.py | ||
# \@@ ... @@ | ||
# def hello(): | ||
# - print("Hello World") | ||
# + name = input("What is your name? ") | ||
# + print(f"Hello {name}") | ||
|
||
# @env LLM_OUTPUT=/dev/stdout The output path | ||
|
||
main() { | ||
if [ ! -f "$argc_path" ]; then | ||
echo "Not found file: $argc_path" | ||
exit 1 | ||
fi | ||
root_dir="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" | ||
new_contents="$(awk -f "$root_dir/utils/patch.awk" "$argc_path" <(printf "%s" "$argc_contents"))" | ||
printf "%s" "$new_contents" | git diff --no-index "$argc_path" - || true | ||
if [ -t 1 ]; then | ||
echo | ||
read -r -p "Apply changes? [Y/n] " ans | ||
if [[ "$ans" == "N" || "$ans" == "n" ]]; then | ||
echo "Aborted!" | ||
exit 1 | ||
fi | ||
fi | ||
printf "%s" "$new_contents" > "$argc_path" | ||
|
||
echo "The patch applied to: $argc_path" >> "$LLM_OUTPUT" | ||
} | ||
|
||
eval "$(argc --argc-eval "$0" "$@")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/awk -f | ||
|
||
# Apply a diff file to an original | ||
# Usage: awk -f patch.awk target-file patch-file | ||
|
||
FNR == NR { | ||
lines[FNR] = $0 | ||
next; | ||
} | ||
|
||
{ | ||
patchLines[FNR] = $0 | ||
} | ||
|
||
END { | ||
totalPatchLines=length(patchLines) | ||
totalLines = length(lines) | ||
patchLineIndex = 1 | ||
|
||
mode = "none" | ||
|
||
while (patchLineIndex <= totalPatchLines) { | ||
line = patchLines[patchLineIndex] | ||
|
||
if (line ~ /^--- / || line ~ /^\+\+\+ /) { | ||
patchLineIndex++ | ||
continue | ||
} | ||
|
||
if (line ~ /^@@ /) { | ||
mode = "hunk" | ||
hunkIndex++ | ||
patchLineIndex++ | ||
continue | ||
} | ||
|
||
if (mode == "hunk") { | ||
while (patchLineIndex <= totalPatchLines && line ~ /^[-+ ]/ && line !~ /^--- /) { | ||
sanitizedLine = substr(line, 2) | ||
if (line !~ /^\+/) { | ||
hunkTotalOriginalLines[hunkIndex]++; | ||
hunkOriginalLines[hunkIndex,hunkTotalOriginalLines[hunkIndex]] = sanitizedLine | ||
} | ||
if (line !~ /^-/) { | ||
hunkTotalUpdatedLines[hunkIndex]++; | ||
hunkUpdatedLines[hunkIndex,hunkTotalUpdatedLines[hunkIndex]] = sanitizedLine | ||
} | ||
patchLineIndex++ | ||
line = patchLines[patchLineIndex] | ||
} | ||
mode = "none" | ||
} else { | ||
patchLineIndex++ | ||
} | ||
} | ||
|
||
if (hunkIndex == 0) { | ||
print "No patch" > "/dev/stderr" | ||
exit 1 | ||
} | ||
|
||
totalHunks = hunkIndex | ||
hunkIndex = 1 | ||
|
||
# inspectHunks() | ||
|
||
for (lineIndex = 1; lineIndex <= totalLines; lineIndex++) { | ||
line = lines[lineIndex] | ||
nextLineIndex = 0 | ||
|
||
if (line == hunkOriginalLines[hunkIndex,1]) { | ||
nextLineIndex = lineIndex + 1 | ||
for (i = 2; i <= hunkTotalOriginalLines[hunkIndex]; i++) { | ||
if (lines[nextLineIndex] != hunkOriginalLines[hunkIndex,i]) { | ||
nextLineIndex = 0 | ||
break | ||
} | ||
nextLineIndex++ | ||
} | ||
} | ||
if (nextLineIndex > 0) { | ||
for (i = 1; i <= hunkTotalUpdatedLines[hunkIndex]; i++) { | ||
print hunkUpdatedLines[hunkIndex,i] | ||
} | ||
hunkIndex++ | ||
lineIndex = nextLineIndex -1; | ||
} else { | ||
print line | ||
} | ||
} | ||
|
||
if (hunkIndex != totalHunks + 1) { | ||
print "Failed to patch the file" > "/dev/stderr" | ||
exit 1 | ||
} | ||
} | ||
|
||
function inspectHunks() { | ||
print "/* Begin inspecting hunks" | ||
for (i = 1; i <= totalHunks; i++) { | ||
print ">>>>>> Original" | ||
for (j = 1; j <= hunkTotalOriginalLines[i]; j++) { | ||
print hunkOriginalLines[i,j] | ||
} | ||
print "======" | ||
for (j = 1; j <= hunkTotalUpdatedLines[i]; j++) { | ||
print hunkUpdatedLines[i,j] | ||
} | ||
print "<<<<<< Updated" | ||
} | ||
print "End inspecting hunks */\n" | ||
} |