diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-25 13:21:56 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-25 13:21:56 +0200 |
commit | df341dc9b874f7e7bf99d9c1de3a84c49c4a04a6 (patch) | |
tree | fda4bcd92b9119bf4b95d393849d2f643eae8444 /packages/server/src/git/diff.ts | |
parent | a13786d6cc185822f5940582efde2349ef729145 (diff) |
Cleaned up the backend
Diffstat (limited to 'packages/server/src/git/diff.ts')
-rw-r--r-- | packages/server/src/git/diff.ts | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/packages/server/src/git/diff.ts b/packages/server/src/git/diff.ts index a3ea375..e0e112d 100644 --- a/packages/server/src/git/diff.ts +++ b/packages/server/src/git/diff.ts @@ -7,29 +7,25 @@ type PatchHeaderData = { last: number | null } -type DiffConstructorData = { - patch_buf: string, - patch_header_buf: string -} - export class Diff { private _ng_diff: NodeGitDiff; - public raw_patches: string; - public patch_header_indexes: number[]; - public patch_header_lengths: number[]; - - constructor(diff: NodeGitDiff, data: DiffConstructorData) { + constructor(diff: NodeGitDiff) { this._ng_diff = diff; - this.raw_patches = data.patch_buf; + } + + public async rawPatches(): Promise<string> { + return String(await this._ng_diff.toBuf(1)); + } - const raw_patches_arr = this.raw_patches.split("\n"); - const patch_headers = data.patch_header_buf.split("\n"); + public async patchHeaderData(): Promise<PatchHeaderData> { + const raw_patches = await this.rawPatches(); + const patch_headers = String(await this._ng_diff.toBuf(2)).split("\n"); - const patch_header_data = patch_headers.reduce((result, line, index) => { + return patch_headers.reduce((result, line, index) => { // The start of a patch header if((/^diff --git/u).test(line)) { - result.indexes.push(raw_patches_arr.indexOf(line)); + result.indexes.push(raw_patches.indexOf(line)); if(result.last !== null) { result.lengths.push(patch_headers.slice(result.last, index).length); @@ -44,20 +40,9 @@ export class Diff { return result; }, <PatchHeaderData>{ indexes: [], lengths: [], last: null }); - - this.patch_header_indexes = patch_header_data.indexes; - this.patch_header_lengths = patch_header_data.lengths; - - } - - async getPatches(): Promise<Patch[]> { - return (await this._ng_diff.patches()).map((patch, index) => new Patch(this, patch, index)); } - static async get(diff: NodeGitDiff): Promise<Diff> { - return new Diff(diff, { - patch_buf: String((await diff.toBuf(1))), - patch_header_buf: String((await diff.toBuf(2))) - }); + public async patches(): Promise<Patch[]> { + return (await this._ng_diff.patches()).map(patch => new Patch(this, patch)); } }
\ No newline at end of file |