From 0dca4d3393fcbea357522e037f00cee2ea186c0d Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Thu, 1 Sep 2022 17:20:36 +0100 Subject: [PATCH] fix(git): support numeric branch names gitGraph does not support branch names that start with a number, because the gitGraph.jison file parses these as NUM (number) instead. To fix this, I've changed the NUM parser to only accept strings that end with whitespace (e.g 1234 is a NUM, but 1234abc is not a NUM). To do this, I had to move the "skip all whitespace" step to the end of the parser, but this doesn't seem to have caused any issues, so it's probably fine. --- src/diagrams/git/gitGraphParserV2.spec.js | 15 +++++++++++++++ src/diagrams/git/parser/gitGraph.jison | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/diagrams/git/gitGraphParserV2.spec.js b/src/diagrams/git/gitGraphParserV2.spec.js index 9a5cc59f1..9a33e288f 100644 --- a/src/diagrams/git/gitGraphParserV2.spec.js +++ b/src/diagrams/git/gitGraphParserV2.spec.js @@ -348,6 +348,21 @@ describe('when parsing a gitGraph', function () { expect(Object.keys(parser.yy.getBranches()).length).toBe(2); }); + it('should allow branch names starting with numbers', function () { + const str = `gitGraph: + commit + %% branch names starting with numbers are not recommended, but are supported by git + branch 1.0.1 + `; + + parser.parse(str); + const commits = parser.yy.getCommits(); + expect(Object.keys(commits).length).toBe(1); + expect(parser.yy.getCurrentBranch()).toBe('1.0.1'); + expect(parser.yy.getDirection()).toBe('LR'); + expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + }); + it('should handle new branch checkout', function () { const str = `gitGraph: commit diff --git a/src/diagrams/git/parser/gitGraph.jison b/src/diagrams/git/parser/gitGraph.jison index 937a7192a..29edec808 100644 --- a/src/diagrams/git/parser/gitGraph.jison +++ b/src/diagrams/git/parser/gitGraph.jison @@ -33,7 +33,6 @@ accDescr\s*"{"\s* { this.begin("ac [\}] { this.popState(); } [^\}]* return "acc_descr_multiline_value"; (\r?\n)+ /*{console.log('New line');return 'NL';}*/ return 'NL'; -\s+ /* skip all whitespace */ \#[^\n]* /* skip comments */ \%%[^\n]* /* skip comments */ "gitGraph" return 'GG'; @@ -61,9 +60,10 @@ accDescr\s*"{"\s* { this.begin("ac ["] this.begin("string"); ["] this.popState(); [^"]* return 'STR'; -[0-9]+ return 'NUM'; -[a-zA-Z][-_\./a-zA-Z0-9]*[-_a-zA-Z0-9] return 'ID'; +[0-9]+(?=\s|$) return 'NUM'; +\w[-\./\w]*[-\w] return 'ID'; // only a subset of https://git-scm.com/docs/git-check-ref-format <> return 'EOF'; +\s+ /* skip all whitespace */ // lowest priority so we can use lookaheads in earlier regex /lex