Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(git): cherry-pick keyword supports tag attribute #3479

Merged
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
12 changes: 9 additions & 3 deletions src/diagrams/git/gitGraphAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,16 @@ 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());

if (tag === 'cherry-pick:<id>') {
tag = 'cherry-pick:' + sourceCommit.id;
}
aloisklink marked this conversation as resolved.
Show resolved Hide resolved
tag = common.sanitizeText(tag, configApi.getConfig());

if (!sourceId || typeof commits[sourceId] === 'undefined') {
let error = new Error(
'Incorrect usage of "cherryPick". Source commit id should exist and provided'
Expand Down Expand Up @@ -328,13 +334,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,
aloisklink marked this conversation as resolved.
Show resolved Hide resolved
};
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
4 changes: 3 additions & 1 deletion src/diagrams/git/parser/gitGraph.jison
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ branchStatement
;

cherryPickStatement
: CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3)}
: CHERRY_PICK COMMIT_ID STR {yy.cherryPick($3, '', 'cherry-pick:<id>')}
| CHERRY_PICK COMMIT_ID STR COMMIT_TAG STR {yy.cherryPick($3, '', $5)}
| CHERRY_PICK COMMIT_TAG STR COMMIT_ID STR {yy.cherryPick($5, '', $3)}
;

mergeStatement
Expand Down