Skip to content

Commit

Permalink
add context to Chunk (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCommieAxolotl committed Apr 21, 2023
1 parent b9c006f commit 6c64a1e
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/__fixtures__/chunk-context
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git a/rename.js b/rename.js
index aa39060..0e05564 100644
--- a/rename.js
+++ b/rename.js
@@ -4 +4,5 @@ function hello() {
// hello world
+ console.log("hello world");
4 changes: 4 additions & 0 deletions src/__tests__/__snapshots__/all.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`aa parse \`all\` 1`] = `
"type": "DeletedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 2,
"start": 1,
Expand All @@ -43,6 +44,7 @@ exports[`aa parse \`all\` 1`] = `
"type": "DeletedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 1,
"start": 1,
Expand All @@ -67,6 +69,7 @@ exports[`aa parse \`all\` 1`] = `
"type": "AddedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 0,
"start": 0,
Expand Down Expand Up @@ -97,6 +100,7 @@ exports[`aa parse \`all\` 1`] = `
"type": "AddedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 1,
"start": 1,
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/__snapshots__/chunk-context.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`chunk-context parse \`chunk-context\` 1`] = `
{
"files": [
{
"chunks": [
{
"changes": [
{
"content": " // hello world",
"lineAfter": 4,
"lineBefore": 4,
"type": "UnchangedLine",
},
{
"content": " console.log(\"hello world\");",
"lineAfter": 5,
"type": "AddedLine",
},
],
"context": "function hello() {",
"fromFileRange": {
"lines": 4,
"start": 4,
},
"toFileRange": {
"lines": 5,
"start": 4,
},
"type": "Chunk",
},
],
"path": "rename.js",
"type": "ChangedFile",
},
],
"type": "GitDiff",
}
`;
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/conflict-file.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ exports[`conflict-file parse \`conflict-file\` 1`] = `
"type": "UnchangedLine",
},
],
"context": undefined,
"fromFileRangeA": {
"lines": 7,
"start": 8,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/deleted-file.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`deleted-file parse \`deleted-file\` 1`] = `
"type": "DeletedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 1,
"start": 1,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/deleted-line.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`deleted-line parse \`deleted-line\` 1`] = `
"type": "DeletedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 2,
"start": 1,
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/message-line.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports[`message-line parse \`message-line\` 1`] = `
"type": "MessageLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 2,
"start": 1,
Expand Down Expand Up @@ -67,6 +68,7 @@ exports[`message-line parse \`message-line\` 1`] = `
"type": "MessageLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 2,
"start": 1,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/new-file.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`new-file parse \`new-file\` 1`] = `
"type": "AddedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 0,
"start": 0,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/new-line.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`new-line parse \`new-line\` 1`] = `
"type": "AddedLine",
},
],
"context": undefined,
"fromFileRange": {
"lines": 1,
"start": 1,
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/chunk-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFixture } from './test-utils';
import parseGitDiff from '../parse-git-diff';

describe('chunk-context', () => {
const fixture = getFixture('chunk-context');

it('parse `chunk-context`', () => {
expect(parseGitDiff(fixture)).toMatchSnapshot();
});
});
15 changes: 10 additions & 5 deletions src/parse-git-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,13 @@ function parseExtendedHeader(ctx: Context) {

function parseChunkHeader(ctx: Context) {
const line = ctx.getCurLine();
const normalChunkExec = /^@@\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@/.exec(
line
);
const normalChunkExec =
/^@@\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@\s?(.+)?/.exec(line);
if (!normalChunkExec) {
const combinedChunkExec =
/^@@@\s\-(\d+),?(\d+)?\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@@/.exec(line);
/^@@@\s\-(\d+),?(\d+)?\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@@\s?(.+)?/.exec(
line
);

if (!combinedChunkExec) {
return null;
Expand All @@ -194,18 +195,22 @@ function parseChunkHeader(ctx: Context) {
delLinesB,
addStart,
addLines,
context,
] = combinedChunkExec;
ctx.nextLine();
return {
context,
type: 'Combined',
fromFileRangeA: getRange(delStartA, delLinesA),
fromFileRangeB: getRange(delStartB, delLinesB),
toFileRange: getRange(addStart, addLines),
} as const;
}
const [all, delStart, delLines, addStart, addLines] = normalChunkExec;
const [all, delStart, delLines, addStart, addLines, context] =
normalChunkExec;
ctx.nextLine();
return {
context,
type: 'Normal',
toFileRange: getRange(addStart, addLines),
fromFileRange: getRange(delStart, delLines),
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export interface Chunk extends Base<'Chunk'> {
fromFileRange: ChunkRange;
toFileRange: ChunkRange;
changes: AnyLineChange[];
context: string | undefined;
}

export interface CombinedChunk extends Base<'CombinedChunk'> {
fromFileRangeA: ChunkRange;
fromFileRangeB: ChunkRange;
toFileRange: ChunkRange;
changes: AnyLineChange[];
context: string | undefined;
}

export type AnyChunk = Chunk | CombinedChunk;
Expand Down

0 comments on commit 6c64a1e

Please sign in to comment.