Skip to content

Commit 63755f0

Browse files
committed
initial commit
0 parents  commit 63755f0

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 tennashi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# diff-parser
2+
Parse unified diff format
3+
4+
## Usage
5+
6+
```ts
7+
import { parse } from "https://deno.land/x/diff_parser/mod.ts";
8+
9+
const process = Deno.run({
10+
cmd: ["git", "diff", "--no-color"]
11+
stdout: "piped",
12+
});
13+
14+
const output = await process.output();
15+
const diffText = new TextDecoder().decode(output);
16+
process.close();
17+
18+
parse(diffText);
19+
```

diff.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
export type Diff = {
2+
beforeFileName: string
3+
afterFileName: string
4+
hunks: Hunk[]
5+
}
6+
7+
type Hunk = {
8+
header: HunkHeader
9+
lines: Line[]
10+
}
11+
12+
type HunkHeader = {
13+
beforeLines: number
14+
afterLines: number
15+
beforeStartLine: number
16+
afterStartLine: number
17+
}
18+
19+
type Line = {
20+
text: string
21+
mark: 'add' | 'delete' | 'nomodified'
22+
}
23+
24+
const hunkHeaderRegexp = /@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/
25+
26+
export function parse(text: string): Diff[] {
27+
const diffs: Diff[] = [];
28+
let currentDiffIndex = 0;
29+
let currentHunkIndex = 0;
30+
31+
text.split('\n').forEach((l) => {
32+
if (l.startsWith('---')) {
33+
diffs.push({
34+
beforeFileName: '',
35+
afterFileName: '',
36+
hunks: [],
37+
});
38+
39+
currentDiffIndex = diffs.length - 1;
40+
currentHunkIndex = 0;
41+
42+
diffs[currentDiffIndex].beforeFileName = l.slice(4);
43+
44+
return;
45+
}
46+
47+
if (l.startsWith('+++')) {
48+
diffs[currentDiffIndex].afterFileName = l.slice(4);
49+
50+
return;
51+
}
52+
53+
if (l.startsWith('@@')) {
54+
const matched = l.match(hunkHeaderRegexp);
55+
56+
if (!matched) {
57+
return;
58+
}
59+
60+
diffs[currentDiffIndex].hunks.push({
61+
header: {
62+
beforeStartLine: Number(matched[1]),
63+
beforeLines: matched[2] ? Number(matched[2]) : 1,
64+
afterStartLine: Number(matched[3]),
65+
afterLines: matched[4] ? Number(matched[4]) : 1,
66+
},
67+
lines: [],
68+
});
69+
70+
currentHunkIndex = diffs[currentDiffIndex].hunks.length - 1;
71+
72+
return;
73+
}
74+
75+
if (l.startsWith('-')) {
76+
diffs[currentDiffIndex].hunks[currentHunkIndex].lines.push({
77+
text: l.slice(1),
78+
mark: 'delete',
79+
});
80+
81+
return;
82+
}
83+
84+
if (l.startsWith('+')) {
85+
diffs[currentDiffIndex].hunks[currentHunkIndex].lines.push({
86+
text: l.slice(1),
87+
mark: 'add',
88+
});
89+
90+
return;
91+
}
92+
93+
if (l.startsWith(' ')) {
94+
diffs[currentDiffIndex].hunks[currentHunkIndex].lines.push({
95+
text: l.slice(1),
96+
mark: 'nomodified',
97+
});
98+
99+
return;
100+
}
101+
});
102+
103+
return diffs;
104+
}

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./diff.ts";

0 commit comments

Comments
 (0)