From ae811348fd7c78bf970887fe76a76014b7f64bc1 Mon Sep 17 00:00:00 2001 From: Steve King Date: Sun, 11 Jul 2021 09:23:33 +0100 Subject: [PATCH] fix: Commit parsing should cater for file names with square brackets --- src/lib/parsers/parse-commit.ts | 2 +- test/unit/__fixtures__/responses/commit.ts | 8 ++++++++ test/unit/commit.spec.ts | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/parsers/parse-commit.ts b/src/lib/parsers/parse-commit.ts index 206d4d88..ebf508ab 100644 --- a/src/lib/parsers/parse-commit.ts +++ b/src/lib/parsers/parse-commit.ts @@ -2,7 +2,7 @@ import { CommitResult } from '../../../typings'; import { LineParser, parseStringResponse } from '../utils'; const parsers: LineParser[] = [ - new LineParser(/\[([^\s]+)( \([^)]+\))? ([^\]]+)/, (result, [branch, root, commit]) => { + new LineParser(/^\[([^\s]+)( \([^)]+\))? ([^\]]+)/, (result, [branch, root, commit]) => { result.branch = branch; result.commit = commit; result.root = !!root; diff --git a/test/unit/__fixtures__/responses/commit.ts b/test/unit/__fixtures__/responses/commit.ts index 8048d61d..41f8b866 100644 --- a/test/unit/__fixtures__/responses/commit.ts +++ b/test/unit/__fixtures__/responses/commit.ts @@ -24,3 +24,11 @@ export function commitToRepoRoot ({message = 'Commit Message', hash = 'b13bdd8', create mode 100644 ${fileName} `; } + +export function commitToBranch ({message = 'Commit Message', hash = 'b13bdd8', fileName = 'file-name', branch = 'branch'} = {}) { + return ` +[${branch} ${hash}] ${message} + 1 file changed, 1 insertion(+) + create mode 100644 ${fileName} +`; +} diff --git a/test/unit/commit.spec.ts b/test/unit/commit.spec.ts index 6e97fc54..c4b1ac59 100644 --- a/test/unit/commit.spec.ts +++ b/test/unit/commit.spec.ts @@ -3,6 +3,7 @@ import { closeWithSuccess, commitResultNoneStaged, commitResultSingleFile, + commitToBranch, commitToRepoRoot, like, newSimpleGit @@ -135,6 +136,14 @@ describe('commit', () => { commit: 'foo', root: true })); + }); + + it('handles files with square brackets', () => { + const actual = parseCommitResult(commitToBranch({fileName: '[AB] CDE FGH.txt', branch: 'alpha'})); + expect(actual).toEqual(like({ + branch: 'alpha', + root: false + })); }) });