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

Feature/5114 add parallel commit config #5161

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
00ac6cc
Add parallelCommits config option for gitGraphs
mathbraga Dec 22, 2023
222c46e
Implement support for parallel commits config
mathbraga Dec 23, 2023
eb3e924
Add tests for gitGraph with parallel commits
mathbraga Dec 23, 2023
46583cf
Merge branch 'develop' into feature/5114_add_parallel_commit_config
mathbraga Dec 23, 2023
41f3f9f
Fix linter issues
mathbraga Dec 23, 2023
c50a82a
Build config types
mathbraga Dec 23, 2023
6f09bc7
Use already defined const instead of method call
mathbraga Jan 16, 2024
bf1edd9
Remove unnecessary argument on findClosestParent call
mathbraga Jan 16, 2024
02246f6
Include undefined on findClosestParent return types and update the fu…
mathbraga Jan 16, 2024
c77a6e1
Include logic for gitgraph with unconnected branches
mathbraga Jan 16, 2024
9213afb
Add tests for gitgraphs with unconnected branches
mathbraga Jan 16, 2024
7a8a131
Merge branch 'develop' into feature/5114_add_parallel_commit_config
mathbraga Jan 17, 2024
fdfa917
Merge branch 'develop' into feature/5114_add_parallel_commit_config
sidharthv96 Jan 19, 2024
4f60a27
Change repetitive values into consts
mathbraga Jan 20, 2024
2401f33
Merge branch 'develop' of https://github.com/mathbraga/mermaid into f…
mathbraga Jan 21, 2024
3e87412
Merge branch 'feature/5114_add_parallel_commit_config' of https://git…
mathbraga Jan 21, 2024
e668698
Reposition const declaration to ideal place
mathbraga Jan 22, 2024
81825f2
Swap condition blocks to avoid using negation
mathbraga Jan 22, 2024
1bad612
Merge branch 'develop' into feature/5114_add_parallel_commit_config
mathbraga Jan 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions cypress/integration/rendering/gitGraph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,121 @@ gitGraph TB:
cherry-pick id: "M" parent:"B"`
);
});
it('41: should render default GitGraph with parallelCommits set to false', () => {
imgSnapshotTest(
`gitGraph
commit id:"1-abcdefg"
commit id:"2-abcdefg"
branch develop
commit id:"3-abcdefg"
commit id:"4-abcdefg"
checkout main
branch feature
commit id:"5-abcdefg"
commit id:"6-abcdefg"
checkout main
commit id:"7-abcdefg"
commit id:"8-abcdefg"
`,
{ gitGraph: { parallelCommits: false } }
);
});
it('42: should render GitGraph with parallel commits', () => {
imgSnapshotTest(
`gitGraph
commit id:"1-abcdefg"
commit id:"2-abcdefg"
branch develop
commit id:"3-abcdefg"
commit id:"4-abcdefg"
checkout main
branch feature
commit id:"5-abcdefg"
commit id:"6-abcdefg"
checkout main
commit id:"7-abcdefg"
commit id:"8-abcdefg"
`,
{ gitGraph: { parallelCommits: true } }
);
});
it('43: should render GitGraph with parallel commits | Vertical Branch', () => {
imgSnapshotTest(
`gitGraph TB:
commit id:"1-abcdefg"
commit id:"2-abcdefg"
branch develop
commit id:"3-abcdefg"
commit id:"4-abcdefg"
checkout main
branch feature
commit id:"5-abcdefg"
commit id:"6-abcdefg"
checkout main
commit id:"7-abcdefg"
commit id:"8-abcdefg"
`,
{ gitGraph: { parallelCommits: true } }
);
});
it('44: should render GitGraph with unconnected branches and no parallel commits', () => {
imgSnapshotTest(
`gitGraph
branch dev
branch v2
branch feat
commit id:"1-abcdefg"
commit id:"2-abcdefg"
checkout main
commit id:"3-abcdefg"
checkout dev
commit id:"4-abcdefg"
checkout v2
commit id:"5-abcdefg"
checkout main
commit id:"6-abcdefg"
`,
{ gitGraph: { parallelCommits: false } }
);
});
it('45: should render GitGraph with unconnected branches and parallel commits', () => {
imgSnapshotTest(
`gitGraph
branch dev
branch v2
branch feat
commit id:"1-abcdefg"
commit id:"2-abcdefg"
checkout main
commit id:"3-abcdefg"
checkout dev
commit id:"4-abcdefg"
checkout v2
commit id:"5-abcdefg"
checkout main
commit id:"6-abcdefg"
`,
{ gitGraph: { parallelCommits: true } }
);
});
it('46: should render GitGraph with unconnected branches and parallel commits | Vertical Branch', () => {
imgSnapshotTest(
`gitGraph TB:
branch dev
branch v2
branch feat
commit id:"1-abcdefg"
commit id:"2-abcdefg"
checkout main
commit id:"3-abcdefg"
checkout dev
commit id:"4-abcdefg"
checkout v2
commit id:"5-abcdefg"
checkout main
commit id:"6-abcdefg"
`,
{ gitGraph: { parallelCommits: true } }
);
});
});
1 change: 1 addition & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ export interface GitGraphDiagramConfig extends BaseDiagramConfig {
showCommitLabel?: boolean;
showBranches?: boolean;
rotateCommitLabel?: boolean;
parallelCommits?: boolean;
/**
* Controls whether or arrow markers in html code are absolute paths or anchors.
* This matters if you are using base tag settings.
Expand Down
37 changes: 37 additions & 0 deletions packages/mermaid/src/diagrams/git/gitGraphRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ const drawText = (txt) => {
return svgLabel;
};

/**
* Searches for the closest parent from the parents list passed as argument.
* The parents list comes from an individual commit. The closest parent is actually
* the one farther down the graph, since that means it is closer to its child.
*
* @param {string[]} parents
* @returns {string | undefined}
*/
const findClosestParent = (parents) => {
let closestParent = '';
mathbraga marked this conversation as resolved.
Show resolved Hide resolved
let maxPosition = 0;

parents.forEach((parent) => {
const parentPosition = dir === 'TB' ? commitPos[parent].y : commitPos[parent].x;
if (parentPosition >= maxPosition) {
closestParent = parent;
maxPosition = parentPosition;
}
});

return closestParent || undefined;
};

/**
* Draws the commits with its symbol and labels. The function has two modes, one which only
* calculates the positions and one that does the actual drawing. This for a simple way getting the
Expand All @@ -87,9 +110,23 @@ const drawCommits = (svg, commits, modifyGraph) => {
const sortedKeys = keys.sort((a, b) => {
return commits[a].seq - commits[b].seq;
});

const isParallelCommits = gitGraphConfig.parallelCommits;
sortedKeys.forEach((key) => {
const commit = commits[key];

if (isParallelCommits) {
if (!commit.parents.length) {
pos = 0;
if (dir === 'TB') {
pos = 30;
}
} else {
const closestParent = findClosestParent(commit.parents);
pos = dir === 'TB' ? commitPos[closestParent].y + 40 : commitPos[closestParent].x + 40;
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
}
}

const y = dir === 'TB' ? pos + 10 : branchPos[commit.branch].pos;
const x = dir === 'TB' ? branchPos[commit.branch].pos : pos + 10;

Expand Down
3 changes: 3 additions & 0 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ $defs: # JSON Schema definition (maybe we should move these to a separate file)
rotateCommitLabel:
type: boolean
default: true
parallelCommits:
type: boolean
default: false
# YAML anchor reference, don't use $ref since ajv doesn't load defaults
arrowMarkerAbsolute: *arrowMarkerAbsolute

Expand Down
Loading