aboutsummaryrefslogtreecommitdiffstats
path: root/tools/fs_patch.sh
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2026-02-03 16:54:42 +0100
committerLeonard Kugis <leonard@kug.is>2026-02-03 16:54:42 +0100
commit27e319f8d1e3a0e8f2db79f62d451768b11d62a3 (patch)
tree501423803651a8442b81ecb52177dc524880ccfa /tools/fs_patch.sh
parent808af93158fce5c5197a646a9148191840800621 (diff)
downloadllm-functions-docker-27e319f8d1e3a0e8f2db79f62d451768b11d62a3.tar.gz
Refined tool return error codes.
Now they all return with exit 0 and error messages instead. This prevents early query abortion on fatal errors.
Diffstat (limited to 'tools/fs_patch.sh')
-rwxr-xr-xtools/fs_patch.sh62
1 files changed, 39 insertions, 23 deletions
diff --git a/tools/fs_patch.sh b/tools/fs_patch.sh
index ce71628..84ae49e 100755
--- a/tools/fs_patch.sh
+++ b/tools/fs_patch.sh
@@ -1,36 +1,52 @@
#!/usr/bin/env bash
-set -e
+set -uo pipefail
# @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
ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
+OUT="${LLM_OUTPUT:-/dev/stdout}"
+
+err() {
+ echo "ERROR: $*" >> "$OUT"
+ exit 0
+}
main() {
- if [ ! -f "$argc_path" ]; then
- echo "Not found file: $argc_path"
- exit 1
- fi
- 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
- "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?"
- printf "%s" "$new_contents" > "$argc_path"
-
- echo "The patch applied to: $argc_path" >> "$LLM_OUTPUT"
+ path="${argc_path:-}"
+ patch="${argc_contents:-}"
+
+ [[ -z "$path" ]] && err "missing --path"
+ [[ -z "$patch" ]] && err "missing --contents"
+ [[ ! -e "$path" ]] && err "file not found: $path"
+ [[ ! -f "$path" ]] && err "not a regular file: $path"
+ [[ ! -r "$path" ]] && err "file not readable: $path"
+
+ new_contents=""
+ if ! new_contents="$(awk -f "$ROOT_DIR/utils/patch.awk" "$path" <(printf "%s" "$patch") 2>/tmp/fs_patch.err)"; then
+ msg="$(cat /tmp/fs_patch.err 2>/dev/null || true)"
+ err "patch apply failed for $path: ${msg:-unknown error}"
+ fi
+
+ # Diff in Tool-Output schreiben (damit das LLM es sieht)
+ printf "%s" "$new_contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true
+
+ # Guard kann “abbrechen” -> als ERROR ausgeben, aber exit 0
+ if ! "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?" >>"$OUT" 2>&1; then
+ err "operation cancelled by user"
+ fi
+
+ if ! printf "%s" "$new_contents" > "$path" 2>/tmp/fs_patch_write.err; then
+ msg="$(cat /tmp/fs_patch_write.err 2>/dev/null || true)"
+ err "failed to write patched file $path: ${msg:-unknown error}"
+ fi
+
+ echo "The patch applied to: $path" >> "$OUT"
+ exit 0
}
eval "$(argc --argc-eval "$0" "$@")"
+main
+