Skip to content

Commit

Permalink
Merge pull request #3479 from elliot-nelson/enelson/git-cherry-pick-tag
Browse files Browse the repository at this point in the history
feat(git): cherry-pick keyword supports tag attribute
  • Loading branch information
ashishjain0512 authored Sep 20, 2022
2 parents 8e3f986 + 183fc35 commit a266a9c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
44 changes: 42 additions & 2 deletions cypress/integration/rendering/gitGraph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,48 @@ describe('Git Graph diagram', () => {
{}
);
});

it('11: should render a gitgraph with cherry pick commit with custom tag', () => {
imgSnapshotTest(
`
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A" tag: "snapshot"
commit id:"THREE"
checkout develop
commit id:"C"
`,
{}
);
});
it('11: should render a gitgraph with cherry pick commit with no tag', () => {
imgSnapshotTest(
`
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A" tag: ""
commit id:"THREE"
checkout develop
commit id:"C"
`,
{}
);
});
it('11: should render a simple gitgraph with two cherry pick commit', () => {
imgSnapshotTest(
`
Expand All @@ -207,7 +248,6 @@ describe('Git Graph diagram', () => {
{}
);
});

it('12: should render commits for more than 8 branches', () => {
imgSnapshotTest(
`
Expand Down
38 changes: 38 additions & 0 deletions docs/gitgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,44 @@ Let see an example:
commit id:"C"
```

By default, the cherry-picked commit from commit with id `A` will be labeled `cherry-pick:A`. You can provide your own custom tag instead to override this behavior, using the same syntax as the `commit` keyword:

```mermaid-example
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A" tag:"fix"
commit id:"THREE"
checkout develop
commit id:"C"
```

```mermaid
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A" tag:"fix"
commit id:"THREE"
checkout develop
commit id:"C"
```

To suppress the tag entirely, use `tag:""` (empty string).

## Gitgraph specific configuration options

In Mermaid, you have the option to configure the gitgraph diagram. You can configure the following options:
Expand Down
8 changes: 5 additions & 3 deletions src/diagrams/git/gitGraphAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag
log.debug('in mergeBranch');
};

export const cherryPick = function (sourceId, targetId) {
export const cherryPick = function (sourceId, targetId, tag) {
log.debug('Entering cherryPick:', sourceId, targetId, tag);
sourceId = common.sanitizeText(sourceId, configApi.getConfig());
targetId = common.sanitizeText(targetId, configApi.getConfig());
tag = common.sanitizeText(tag, configApi.getConfig());

if (!sourceId || typeof commits[sourceId] === 'undefined') {
let error = new Error(
Expand Down Expand Up @@ -328,13 +330,13 @@ export const cherryPick = function (sourceId, targetId) {
parents: [head == null ? null : head.id, sourceCommit.id],
branch: curBranch,
type: commitType.CHERRY_PICK,
tag: 'cherry-pick:' + sourceCommit.id,
tag: tag ?? 'cherry-pick:' + sourceCommit.id,
};
head = commit;
commits[commit.id] = commit;
branches[curBranch] = commit.id;
log.debug(branches);
log.debug('in cheeryPick');
log.debug('in cherryPick');
}
};
export const checkout = function (branch) {
Expand Down
32 changes: 32 additions & 0 deletions src/diagrams/git/gitGraphParserV2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,38 @@ describe('when parsing a gitGraph', function () {
expect(commits[cherryPickCommitID].branch).toBe('main');
});

it('should support cherry-picking commits with custom tag', function () {
const str = `gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
cherry-pick id:"A" tag:"MyTag"
`;

parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = Object.keys(commits)[2];
expect(commits[cherryPickCommitID].tag).toBe('MyTag');
expect(commits[cherryPickCommitID].branch).toBe('main');
});

it('should support cherry-picking commits with no tag', function () {
const str = `gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
cherry-pick id:"A" tag:""
`;

parser.parse(str);
const commits = parser.yy.getCommits();
const cherryPickCommitID = Object.keys(commits)[2];
expect(commits[cherryPickCommitID].tag).toBe('');
expect(commits[cherryPickCommitID].branch).toBe('main');
});

it('should throw error when try to branch existing branch: main', function () {
const str = `gitGraph
commit
Expand Down
7 changes: 6 additions & 1 deletion src/diagrams/git/parser/gitGraph.jison
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ checkout(?=\s|$) return 'CHECKOUT';
"options"\r?\n this.begin("options"); //
<options>[ \r\n\t]+"end" this.popState(); // not used anymore in the renderer, fixed for backward compatibility
<options>[\s\S]+(?=[ \r\n\t]+"end") return 'OPT'; //
["]["] return 'EMPTYSTR';
["] this.begin("string");
<string>["] this.popState();
<string>[^"]* return 'STR';
Expand Down Expand Up @@ -117,7 +118,11 @@ branchStatement
;

cherryPickStatement
: CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3)}
: CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3, '', undefined)}
| CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR {yy.cherryPick($3, '', $5)}
| CHERRY_PICK COMMIT_ID STR COMMIT_TAG EMPTYSTR {yy.cherryPick($3, '', '')}
| CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR {yy.cherryPick($5, '', $3)}
| CHERRY_PICK COMMIT_TAG EMPTYSTR COMMIT_ID STR {yy.cherryPick($3, '', '')}
;

mergeStatement
Expand Down

0 comments on commit a266a9c

Please sign in to comment.