blob: 6cd38a376c326950ff65ddc0fc4884203634d7b4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import { Diff as NodeGitDiff } from "nodegit";
import { Patch } from "./patch";
type PatchHeaderData = {
indexes: number[],
lengths: number[],
last: number | null
}
export class Diff {
private _ng_diff: NodeGitDiff;
constructor(diff: NodeGitDiff) {
this._ng_diff = diff;
}
public async rawPatches(): Promise<string> {
return String(await this._ng_diff.toBuf(1));
}
public async patchHeaderData(): Promise<PatchHeaderData> {
const raw_patches = (await this.rawPatches()).split("\n");
const patch_headers = String(await this._ng_diff.toBuf(2)).split("\n");
return patch_headers.reduce((result, line, index) => {
// The start of a patch header
if((/^diff --git/u).test(line)) {
result.indexes.push(raw_patches.indexOf(line));
if(result.last !== null) {
result.lengths.push(patch_headers.slice(result.last, index).length);
}
result.last = index;
}
// Include the last patch header when the end is reached
if(index === patch_headers.length - 1 && result.last !== null) {
result.lengths.push(patch_headers.slice(result.last, index).length);
}
return result;
}, <PatchHeaderData>{ indexes: [], lengths: [], last: null });
}
public async patches(): Promise<Patch[]> {
return (await this._ng_diff.patches()).map(patch => new Patch(this, patch));
}
}
|