Skip to content

Commit

Permalink
feat: add noPrefix options (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Aug 9, 2023
1 parent f20636c commit 4f39a03
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ console.log(result);
// }
```

### Options

#### `noPrefix` (boolean)

Specifies whether the git diff command is used with the `--no-prefix` option. (default: `false`)

```ts
// git diff HEAD~3 --no-prefix

const result = parseGitDiff(DIFF, {
noPrefix: true,
});
```

## Examples

<details>
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import parseGitDiff from '../parse-git-diff';
import { ChangedFile, RenamedFile } from '../types';

const getDiffFixture = ({
srcPrefix = 'a/',
dstPrefix = 'b/',
}) => `diff --git ${srcPrefix}b/bbb.md ${dstPrefix}b/bbb.md
index 0e05564..aa39060 100644
--- ${srcPrefix}b/bbb.md
+++ ${dstPrefix}b/bbb.md
@@ -1,2 +1 @@
newfile
-newline
+newline
\ No newline at end of file`;

describe('options', () => {
it('noPrefix=true', () => {
const output = parseGitDiff(
getDiffFixture({ srcPrefix: '', dstPrefix: '' }),
{
noPrefix: true,
}
);

expect((output.files[0] as ChangedFile).path).toBe('b/bbb.md');
});

it('noPrefix=false', () => {
const output = parseGitDiff(
getDiffFixture({ srcPrefix: 'a/', dstPrefix: 'b/' }),
{
noPrefix: false,
}
);

expect((output.files[0] as ChangedFile).path).toBe('b/bbb.md');
});
});
9 changes: 8 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { FilledGitDiffOptions, GitDiffOptions } from './types';

export default class Context {
private line: number = 1;
private lines: string[] = [];
public constructor(diff: string) {
public options: FilledGitDiffOptions = {
noPrefix: false,
};
public constructor(diff: string, options?: GitDiffOptions) {
this.lines = diff.split('\n');

this.options.noPrefix = !!options?.noPrefix;
}

public getCurLine(): string {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export type {
RenamedFile,
AnyFileChange,
GitDiff,
GitDiffOptions,
} from './types.js';
32 changes: 26 additions & 6 deletions src/parse-git-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
ChunkRange,
CombinedChunk,
AnyChunk,
FilledGitDiffOptions,
GitDiffOptions,
} from './types.js';
import {
ExtendedHeader,
Expand All @@ -15,8 +17,11 @@ import {
LineType,
} from './constants.js';

export default function parseGitDiff(diff: string): GitDiff {
const ctx = new Context(diff);
export default function parseGitDiff(
diff: string,
options?: GitDiffOptions
): GitDiff {
const ctx = new Context(diff, options);
const files = parseFileChanges(ctx);

return {
Expand Down Expand Up @@ -229,8 +234,8 @@ function parseChunkHeader(ctx: Context) {
ctx.nextLine();
return {
type: 'BinaryFiles',
fileA: fileA.replace('a/', ''),
fileB: fileB.replace('b/', ''),
fileA: getFilePath(ctx, fileA, 'src'),
fileB: getFilePath(ctx, fileB, 'dst'),
} as const;
}

Expand Down Expand Up @@ -280,8 +285,15 @@ function parseChangeMarkers(context: Context): {
deleted: string;
added: string;
} | null {
const deleted = parseMarker(context, '--- ')?.replace('a/', '');
const added = parseMarker(context, '+++ ')?.replace('b/', '');
const deleterMarker = parseMarker(context, '--- ');
const deleted = deleterMarker
? getFilePath(context, deleterMarker, 'src')
: deleterMarker;

const addedMarker = parseMarker(context, '+++ ');
const added = addedMarker
? getFilePath(context, addedMarker, 'dst')
: addedMarker;
return added && deleted ? { added, deleted } : null;
}

Expand Down Expand Up @@ -364,3 +376,11 @@ function parseChanges(
function getLineType(line: string): LineType | null {
return CHAR_TYPE_MAP[line[0]] || null;
}

function getFilePath(ctx: Context, input: string, type: 'src' | 'dst') {
if (ctx.options.noPrefix) {
return input;
}
if (type === 'src') return input.replace(/^a\//, '');
if (type === 'dst') return input.replace(/^b\//, '');
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ export type AnyFileChange = ChangedFile | AddedFile | DeletedFile | RenamedFile;
export interface GitDiff extends Base<'GitDiff'> {
files: AnyFileChange[];
}

export interface FilledGitDiffOptions {
noPrefix: boolean;
}

export type GitDiffOptions = Partial<FilledGitDiffOptions>;

0 comments on commit 4f39a03

Please sign in to comment.