-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: add diff-utils for diff parsing into Hunk[] * refactor: use diff-utils to parse diff into Hunk, convert to Range * refactor: use diff-utils for parsing FileDiffContent into Hunk[] * chore: fix lint * test: cleanup patch to range tests * test: fix test cases * chore: fix lint Co-authored-by: Justin Beckwith <[email protected]>
- Loading branch information
1 parent
1526a40
commit 4993d62
Showing
16 changed files
with
341 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 0 additions & 124 deletions
124
src/github-handler/comment-handler/get-hunk-scope-handler/github-patch-text-handler.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Hunk} from '../types'; | ||
import * as parseDiff from 'parse-diff'; | ||
import {createPatch} from 'diff'; | ||
|
||
/** | ||
* Given a diff expressed in GNU diff format, return the range of lines | ||
* from the original content that are changed. | ||
* @param diff Diff expressed in GNU diff format. | ||
* @returns Hunk[] | ||
*/ | ||
export function parseHunks(diff: string): Hunk[] { | ||
const chunks = parseDiff(diff)[0].chunks; | ||
return chunks.map(chunk => { | ||
let oldStart = chunk.oldStart; | ||
let newStart = chunk.newStart; | ||
let normalLines = 0; | ||
let changeSeen = false; | ||
|
||
chunk.changes.forEach(change => { | ||
switch (change.type) { | ||
case 'add': | ||
case 'del': | ||
if (!changeSeen) { | ||
oldStart += normalLines; | ||
newStart += normalLines; | ||
changeSeen = true; | ||
} | ||
break; | ||
case 'normal': | ||
normalLines++; | ||
break; | ||
} | ||
}); | ||
const newEnd = newStart + chunk.newLines - normalLines - 1; | ||
const oldEnd = oldStart + chunk.oldLines - normalLines - 1; | ||
return { | ||
oldStart: oldStart, | ||
oldEnd: oldEnd, | ||
newStart: newStart, | ||
newEnd: newEnd, | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Given two texts, return the range of lines that are changed. | ||
* @param oldContent The original content. | ||
* @param newContent The new content. | ||
* @returns Hunk[] | ||
*/ | ||
export function getSuggestedHunks( | ||
oldContent: string, | ||
newContent: string | ||
): Hunk[] { | ||
const diff = createPatch('unused', oldContent, newContent); | ||
return parseHunks(diff); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {describe, it, before} from 'mocha'; | ||
import {readFileSync} from 'fs'; | ||
import {setup} from './util'; | ||
import {resolve} from 'path'; | ||
import {parseHunks} from '../src/github-handler/diff-utils'; | ||
import {expect} from 'chai'; | ||
|
||
const fixturePath = 'test/fixtures/diffs'; | ||
|
||
before(() => { | ||
setup(); | ||
}); | ||
|
||
describe('parseHunks', () => { | ||
it('parses one-to-one hunks', () => { | ||
const diff = readFileSync( | ||
resolve(fixturePath, 'one-line-to-one.diff') | ||
).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 5, oldEnd: 5, newStart: 5, newEnd: 5}]); | ||
}); | ||
it('parses one-to-many hunks', () => { | ||
const diff = readFileSync( | ||
resolve(fixturePath, 'one-line-to-many.diff') | ||
).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 7, oldEnd: 7, newStart: 7, newEnd: 8}]); | ||
}); | ||
it('parses one-to-many hunks with a newline added', () => { | ||
const diff = readFileSync( | ||
resolve(fixturePath, 'one-line-to-many-newline.diff') | ||
).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 5, oldEnd: 5, newStart: 5, newEnd: 6}]); | ||
}); | ||
it('parses many-to-many-hunks', () => { | ||
const diff = readFileSync( | ||
resolve(fixturePath, 'many-to-many.diff') | ||
).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 2, oldEnd: 5, newStart: 2, newEnd: 3}]); | ||
}); | ||
|
||
it('parses many-to-one-hunks', () => { | ||
const diff = readFileSync( | ||
resolve(fixturePath, 'many-to-one.diff') | ||
).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 2, oldEnd: 5, newStart: 2, newEnd: 2}]); | ||
}); | ||
it('parses deletions', () => { | ||
const diff = readFileSync(resolve(fixturePath, 'deletion.diff')).toString(); | ||
const hunks = parseHunks(diff); | ||
expect(hunks).to.eql([{oldStart: 4, oldEnd: 5, newStart: 4, newEnd: 3}]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
diff --git a/cloudbuild.yaml b/cloudbuild.yaml | ||
index cac8fbc..bc796a0 100644 | ||
--- a/cloudbuild.yaml | ||
+++ b/cloudbuild.yaml | ||
@@ -1,7 +1,5 @@ | ||
steps: | ||
- name: 'ubuntu' | ||
args: ['echo', 'foobar'] | ||
-- name: 'ubuntu' | ||
- args: ['sleep', '30'] | ||
- name: 'ubuntu' | ||
args: ['echo', 'done'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
diff --git a/cloudbuild.yaml b/cloudbuild.yaml | ||
index cac8fbc..87f387c 100644 | ||
--- a/cloudbuild.yaml | ||
+++ b/cloudbuild.yaml | ||
@@ -1,7 +1,5 @@ | ||
steps: | ||
-- name: 'ubuntu' | ||
- args: ['echo', 'foobar'] | ||
-- name: 'ubuntu' | ||
- args: ['sleep', '30'] | ||
+- name: 'foo' | ||
+ args: ['sleep 1'] | ||
- name: 'ubuntu' | ||
args: ['echo', 'done'] |
Oops, something went wrong.