Skip to content

Commit

Permalink
fix(git): support unusual prefixes in branch name
Browse files Browse the repository at this point in the history
jison throws an error if a branch name starts with an unusual prefix.

For example, a branch named `branch/test-branch` will throw a
parse error, since jison thinks it's a `branch` command, and not
a branch id.

An easy fix is to use the `(?=\s|$)` regex to ensure that only
'branch ' or 'branch\n' will be parsed as the branch command.

Fixes: mermaid-js#3362
  • Loading branch information
aloisklink committed Sep 11, 2022
1 parent 0dca4d3 commit 1527956
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/diagrams/git/gitGraphParserV2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,34 @@ describe('when parsing a gitGraph', function () {
expect(Object.keys(parser.yy.getBranches()).length).toBe(2);
});

it('should allow branch names starting with unusual prefixes', function () {
const str = `gitGraph:
commit
%% branch names starting with numbers are not recommended, but are supported by git
branch branch01
branch checkout02
branch cherry-pick03
branch branch/example-branch
branch merge/test_merge
`;

parser.parse(str);
const commits = parser.yy.getCommits();
expect(Object.keys(commits).length).toBe(1);
expect(parser.yy.getCurrentBranch()).toBe('merge/test_merge');
expect(parser.yy.getDirection()).toBe('LR');
expect(Object.keys(parser.yy.getBranches()).length).toBe(6);
expect(Object.keys(parser.yy.getBranches())).toEqual(
expect.arrayContaining([
'branch01',
'checkout02',
'cherry-pick03',
'branch/example-branch',
'merge/test_merge',
])
);
});

it('should handle new branch checkout', function () {
const str = `gitGraph:
commit
Expand Down
10 changes: 5 additions & 5 deletions src/diagrams/git/parser/gitGraph.jison
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ accDescr\s*"{"\s* { this.begin("ac
\#[^\n]* /* skip comments */
\%%[^\n]* /* skip comments */
"gitGraph" return 'GG';
"commit" return 'COMMIT';
commit(?=\s|$) return 'COMMIT';
"id:" return 'COMMIT_ID';
"type:" return 'COMMIT_TYPE';
"msg:" return 'COMMIT_MSG';
"NORMAL" return 'NORMAL';
"REVERSE" return 'REVERSE';
"HIGHLIGHT" return 'HIGHLIGHT';
"tag:" return 'COMMIT_TAG';
"branch" return 'BRANCH';
branch(?=\s|$) return 'BRANCH';
"order:" return 'ORDER';
"merge" return 'MERGE';
"cherry-pick" return 'CHERRY_PICK';
merge(?=\s|$) return 'MERGE';
cherry-pick(?=\s|$) return 'CHERRY_PICK';
// "reset" return 'RESET';
"checkout" return 'CHECKOUT';
checkout(?=\s|$) return 'CHECKOUT';
"LR" return 'DIR';
"BT" return 'DIR';
":" return ':';
Expand Down

0 comments on commit 1527956

Please sign in to comment.