mirror of
				https://github.com/mermaid-js/mermaid.git
				synced 2025-11-04 12:54:08 +01:00 
			
		
		
		
	@@ -68,11 +68,13 @@ export const getOptions = function() {
 | 
			
		||||
  return options;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const commit = function(msg) {
 | 
			
		||||
export const commit = function(msg, id, type, tag) {
 | 
			
		||||
  const commit = {
 | 
			
		||||
    id: getId(),
 | 
			
		||||
    id: id ? id : getId(),
 | 
			
		||||
    message: msg,
 | 
			
		||||
    seq: seq++,
 | 
			
		||||
    type: type ? type : commitType.NORMAL,
 | 
			
		||||
    tag: tag ? tag : '',
 | 
			
		||||
    parent: head == null ? null : head.id
 | 
			
		||||
  };
 | 
			
		||||
  head = commit;
 | 
			
		||||
@@ -227,6 +229,12 @@ export const getHead = function() {
 | 
			
		||||
  return head;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const commitType = {
 | 
			
		||||
  NORMAL: 0,
 | 
			
		||||
  REVERSE: 1,
 | 
			
		||||
  HIGHLIGHT: 2
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  setDirection,
 | 
			
		||||
  setOptions,
 | 
			
		||||
@@ -244,5 +252,6 @@ export default {
 | 
			
		||||
  getCommitsArray,
 | 
			
		||||
  getCurrentBranch,
 | 
			
		||||
  getDirection,
 | 
			
		||||
  getHead
 | 
			
		||||
  getHead,
 | 
			
		||||
  commitType
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										222
									
								
								src/diagrams/git/gitGraphParserV2.spec.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										222
									
								
								src/diagrams/git/gitGraphParserV2.spec.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,222 @@
 | 
			
		||||
/* eslint-env jasmine */
 | 
			
		||||
// Todo reintroduce without cryptoRandomString
 | 
			
		||||
import gitGraphAst from './gitGraphAst';
 | 
			
		||||
import { parser } from './parser/gitGraph';
 | 
			
		||||
//import randomString from 'crypto-random-string';
 | 
			
		||||
//import cryptoRandomString from 'crypto-random-string';
 | 
			
		||||
import { logger } from '../../logger';
 | 
			
		||||
 | 
			
		||||
//jest.mock('crypto-random-string');
 | 
			
		||||
 | 
			
		||||
describe('when parsing a gitGraph', function() {
 | 
			
		||||
  let randomNumber;
 | 
			
		||||
  beforeEach(function() {
 | 
			
		||||
    parser.yy = gitGraphAst;
 | 
			
		||||
    parser.yy.clear();
 | 
			
		||||
    randomNumber = 0;
 | 
			
		||||
  
 | 
			
		||||
  });
 | 
			
		||||
  // afterEach(function() {
 | 
			
		||||
  //   cryptoRandomString.mockReset();
 | 
			
		||||
  // });
 | 
			
		||||
  it('should handle a gitGraph commit with NO pararms, get auto-genrated reandom ID', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit
 | 
			
		||||
    `;
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    //console.info(commits);
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit id only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit id:"1111"
 | 
			
		||||
    `;
 | 
			
		||||
    //console.log(str);
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).toBe('1111');
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit tag only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit tag:"test"
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('test');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit type HIGHLIGHT only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit type: HIGHLIGHT
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(2);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit type REVERSE only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit type: REVERSE
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(1);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit type NORMAL only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit type: NORMAL
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit msg only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit "test commit"
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('test commit');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit id, tag  only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit id:"1111" tag: "test tag"
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).toBe('1111');
 | 
			
		||||
    expect(commits[key].tag).toBe('test tag');
 | 
			
		||||
    expect(commits[key].type).toBe(0);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit type, tag  only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit type:HIGHLIGHT tag: "test tag"
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('test tag');
 | 
			
		||||
    expect(commits[key].type).toBe(2);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit tag and type only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit tag: "test tag" type:HIGHLIGHT
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).not.toBeNull();
 | 
			
		||||
    expect(commits[key].tag).toBe('test tag');
 | 
			
		||||
    expect(commits[key].type).toBe(2);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should handle a gitGraph commit with custom commit id, type and tag only', function() {
 | 
			
		||||
    const str = `gitGraph:
 | 
			
		||||
    commit id:"1111" type:REVERSE tag: "test tag"
 | 
			
		||||
    `;
 | 
			
		||||
   
 | 
			
		||||
    parser.parse(str);
 | 
			
		||||
    const commits = parser.yy.getCommits();
 | 
			
		||||
    expect(Object.keys(commits).length).toBe(1);
 | 
			
		||||
    expect(parser.yy.getCurrentBranch()).toBe('master');
 | 
			
		||||
    expect(parser.yy.getDirection()).toBe('LR');
 | 
			
		||||
    expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
 | 
			
		||||
    const key = Object.keys(commits)[0];
 | 
			
		||||
    expect(commits[key].message).toBe('');
 | 
			
		||||
    expect(commits[key].id).toBe('1111');
 | 
			
		||||
    expect(commits[key].tag).toBe('test tag');
 | 
			
		||||
    expect(commits[key].type).toBe(1);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
@@ -13,12 +13,18 @@
 | 
			
		||||
 | 
			
		||||
%%
 | 
			
		||||
 | 
			
		||||
(\r?\n)+                               return 'NL';
 | 
			
		||||
(\r?\n)+                               /*{console.log('New line');return 'NL';}*/ return 'NL';
 | 
			
		||||
\s+                                    /* skip all whitespace */
 | 
			
		||||
\#[^\n]*                               /* skip comments */
 | 
			
		||||
\%%[^\n]*                              /* skip comments */
 | 
			
		||||
"gitGraph"                             return 'GG';
 | 
			
		||||
"commit"                               return 'COMMIT';
 | 
			
		||||
"id:"                                  return 'COMMIT_ID';
 | 
			
		||||
"type:"                                return 'COMMIT_TYPE';
 | 
			
		||||
"NORMAL"                               return 'NORMAL';
 | 
			
		||||
"REVERSE"                              return 'REVERSE';
 | 
			
		||||
"HIGHLIGHT"                            return 'HIGHLIGHT';                       
 | 
			
		||||
"tag:"                                 return 'COMMIT_TAG';
 | 
			
		||||
"branch"                               return 'BRANCH';
 | 
			
		||||
"merge"                                return 'MERGE';
 | 
			
		||||
"reset"                                return 'RESET';
 | 
			
		||||
@@ -69,18 +75,33 @@ line
 | 
			
		||||
    ;
 | 
			
		||||
 | 
			
		||||
statement
 | 
			
		||||
    : COMMIT commit_arg {yy.commit($2)}
 | 
			
		||||
    : commitStatement
 | 
			
		||||
    | BRANCH ID {yy.branch($2)}
 | 
			
		||||
    | CHECKOUT ID {yy.checkout($2)}
 | 
			
		||||
    | MERGE ID {yy.merge($2)}
 | 
			
		||||
    | RESET reset_arg {yy.reset($2)}
 | 
			
		||||
    ;
 | 
			
		||||
 | 
			
		||||
commitStatement
 | 
			
		||||
    : COMMIT commit_arg {yy.commit($2)}
 | 
			
		||||
    | COMMIT COMMIT_ID STR {yy.commit('',$3,yy.commitType.NORMAL,'')}
 | 
			
		||||
    | COMMIT COMMIT_TYPE commitType {yy.commit('','',$3,'')}
 | 
			
		||||
    | COMMIT COMMIT_TAG STR {yy.commit('','',yy.commitType.NORMAL,$3)}
 | 
			
		||||
    | COMMIT COMMIT_TAG STR COMMIT_TYPE commitType {yy.commit('','',$5,$3)}
 | 
			
		||||
    | COMMIT COMMIT_TYPE commitType COMMIT_TAG STR  {yy.commit('','',$3,$5)}
 | 
			
		||||
    | COMMIT COMMIT_ID STR COMMIT_TYPE commitType {yy.commit('',$3,$5,'')}
 | 
			
		||||
    | COMMIT COMMIT_ID STR COMMIT_TAG STR {yy.commit('',$3,yy.commitType.NORMAL,$5)}
 | 
			
		||||
    | COMMIT COMMIT_ID STR COMMIT_TYPE commitType COMMIT_TAG STR {yy.commit('',$3,$5,$7)}
 | 
			
		||||
    | COMMIT COMMIT_ID STR COMMIT_TAG STR COMMIT_TYPE commitType  {yy.commit('',$3,$7,$5)}
 | 
			
		||||
    ;
 | 
			
		||||
commit_arg
 | 
			
		||||
    : /* empty */ {$$ = ""}
 | 
			
		||||
    | STR {$$=$1}
 | 
			
		||||
    ;
 | 
			
		||||
 | 
			
		||||
commitType
 | 
			
		||||
    : NORMAL { $$=yy.commitType.NORMAL;}
 | 
			
		||||
    | REVERSE   { $$=yy.commitType.REVERSE;}
 | 
			
		||||
    | HIGHLIGHT { $$=yy.commitType.HIGHLIGHT;}
 | 
			
		||||
    ;    
 | 
			
		||||
reset_arg
 | 
			
		||||
    : 'HEAD' reset_parents{$$ = $1+ ":" + $2 }
 | 
			
		||||
    | ID reset_parents{$$ = $1+ ":"  + yy.count; yy.count = 0}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user