|
| 1 | +# PATCH.LUA |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`patch.lua` reads a unified diff from standard patch format |
| 6 | +and applies each hunk to the target file. |
| 7 | + |
| 8 | +- splits each hunk into context, additions, deletions |
| 9 | +- validates that the original text matches before removing lines |
| 10 | +- writes the patched output (preserving or dropping a trailing newline as directed) |
| 11 | + |
| 12 | +It is a self-contained, single-pass Lua script with no external dependencies. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +- `diff.txt` is a patch file in unified-diff format. |
| 17 | +- The script will open each file mentioned by a `--- a.txt` / `+++ b.txt` header, back up its contents to `*.orig`, then |
| 18 | + rewrite it in place. |
| 19 | + |
| 20 | +## How It Works |
| 21 | + |
| 22 | +### Command-line & Main Loop |
| 23 | + |
| 24 | +1. The script reads `diff.txt` line by line via `pf:read("*l")`. |
| 25 | +2. It keeps a look-ahead `next_line` so it can detect the `\ No newline at end of file` marker before deciding whether |
| 26 | + to emit a `\n`. |
| 27 | + |
| 28 | +### Filename Parsing (`parseFileName`) |
| 29 | + |
| 30 | +- Extract the filename from a line like `--- "a.txt"` or `--- a.txt` end |
| 31 | +- Strips quoting (`"` or `'`) if present. |
| 32 | +- Otherwise splits on whitespace and takes the second token. |
| 33 | + |
| 34 | +### Preparing a File (`setPatchFile`) |
| 35 | + |
| 36 | +1. Open `f` for reading |
| 37 | +2. Copy its entire contents to `f.orig` |
| 38 | +3. Reopen `f` for writing (truncating it) |
| 39 | +4. Store both reader (`bf`) and writer (`of`) handles end |
| 40 | + |
| 41 | +- On failure at any step, aborts cleanly and reports the I/O error. |
| 42 | + |
| 43 | +### Writing Remaining Context (`writeRemainder`) |
| 44 | + |
| 45 | +After the last hunk of a file is applied, any lines remaining in the original (`bf`) are written unchanged to the new |
| 46 | +file (`of`). |
| 47 | + |
| 48 | +### Diff-Line Dispatch |
| 49 | + |
| 50 | +Each patch-file line is classified by its first character: |
| 51 | + |
| 52 | +#### Additions (`+`) |
| 53 | + |
| 54 | +- Lines beginning `+` (but not `++ `) are written to `of`. |
| 55 | + |
| 56 | +#### Deletions (`-`) |
| 57 | + |
| 58 | +- A leading `-- ` indicates a new filename header (`--- a.txt`). |
| 59 | +- Other `-` lines are verified against `bf:read("*l")`, then skipped (i.e. not written to `of`). |
| 60 | + |
| 61 | +#### Context (` `) |
| 62 | + |
| 63 | +- Lines beginning with a space must match the next line in `bf`. |
| 64 | +- If they do, they’re copied unchanged into `of`; otherwise the script errors out. |
| 65 | + |
| 66 | +#### Hunk Headers (`@@`) |
| 67 | + |
| 68 | +- On seeing `@@ -oldStart,oldLen +newStart,newLen @@`, parse the four numbers. |
| 69 | +- Before entering the new hunk, verify you consumed exactly `oldLen` & `newLen` lines since the last header. |
| 70 | +- Skip any “leading context” between hunks by copying `(oldStart − 1) − bl` lines from `bf` → `of`. |
| 71 | + |
| 72 | +#### No-newline Marker (`\ No newline…`) |
| 73 | + |
| 74 | +- If the next patch line after an addition is `\ No newline at end of file`, |
| 75 | +the script omits the final `\n` on that file. |
0 commit comments