Skip to content

Commit d9ebe9d

Browse files
committed
Added DIFF.LUA documentation.
1 parent 635a535 commit d9ebe9d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

demo/xtra/DIFF.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# DIFF.LUA
2+
3+
## Overview
4+
This Lua script is a custom implementation of a **unified diff** utility.
5+
Its purpose is to compare one or more pairs of files,
6+
highlight their differences, and print them in a unified diff format,
7+
just like `git diff` or `diff -u`.
8+
9+
Unified diffs display added, removed, and a few unchanged lines, above and below
10+
to give contextual information that can then be used by a patching utility
11+
to ensure the patching process is correct.
12+
13+
## Usage Examples
14+
### Compare a file to another
15+
The script can compare a file to another as follows:
16+
```
17+
DIFF.LUA old.txt new.txt
18+
```
19+
This command compares `old1.txt` with `new1.txt`.
20+
### Compare multiple files to each other
21+
It might be useful to compare multiple files to their counterparts
22+
at the same time. The script can compare multiple sets files as follows:
23+
```
24+
DIFF.LUA old1.txt new1.txt old2.txt new2.txt old3.txt new3.txt
25+
```
26+
This command compares `old1.txt` with `new1.txt`,
27+
`old2.txt` with `new2.txt` and `old3.txt` with `new3.txt`.
28+
This can continue indefinitely.
29+
30+
31+
## How It Works
32+
The script performs the following steps:
33+
1. **Input Validation**:
34+
- There should be at least two arguments representing a valid file.
35+
- Each two arguments will be treated as another set of files to compare.
36+
The number of arguments must always be an even number.
37+
- If the provided input is invalid,
38+
the script outputs usage instructions and exits.
39+
- If any file can't be opened for any reason,
40+
the script will print an error and exit on the spot.
41+
42+
2. **File Comparison**:
43+
- The function `diff_u` performs the actual comparison:
44+
- It reads two files line by line.
45+
- It tracks which lines are added, removed,
46+
or unchanged between the two files using the Myers diff algorithm
47+
- It groups changes in "hunks,"
48+
which are logical sections of differences,
49+
and formats these differences in the unified diff style.
50+
- It adds context lines around hunks to provide additional verification.
51+
52+
3. **Unified Diff Output**:
53+
- The script outputs differences using a standardized format:
54+
- Lines starting with a space (` `) are unchanged context lines.
55+
- Lines starting with a minus sign (`-`) are present only in the "old file" (removed lines).
56+
- Lines starting with a plus sign (`+`) are present only in the "new file" (added lines).
57+
- Each hunk starts with a header
58+
that shows the affected line numbers in both files,
59+
e.g., `@@ -start,count +start,count @@`.
60+
- If the old and new files are identical, nothing will be printed
61+
62+
## Code Breakdown
63+
Here’s an explanation of core components:
64+
65+
### Global Variable
66+
67+
| Variable | Description |
68+
|----------|------------------------------------------------------------------------------------------------------------------------------------------------|
69+
| `CL` | **C**ontext **L**ines to print above and below a hunk. Defaults to `3`, can be to something else with the `DIFF_CONTEXT` environment variable. |
70+
71+
### Functions
72+
73+
| Function | Parameters | Description |
74+
|--------------|-------------------------|---------------------------------------------------------------------|
75+
| `diff_u` | filename1<br/>filename2 | Prints differences between the two files in to stdout. |
76+
| `flush_pre` | | Flushes pre-context lines into the buffer when changes are detected |
77+
| `flush_hunk` | | Outputs a complete hunk of changes into the buffer |
78+
| `flush_buf` | | Print and clear the buffer |
79+
| `diagonal` | | Iterate over lines that match in both files |
80+
| `right` | | Iterate over a line only present in the old file |
81+
| `down` | | Iterate over a line only present in the new file |

0 commit comments

Comments
 (0)