Skip to content

Commit

Permalink
[#1043] Made a commits parser for generating a branch graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishirak committed Sep 23, 2016
1 parent fb51a20 commit 9090d19
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 17 deletions.
1 change: 1 addition & 0 deletions frontend/src/components/commits-table/CommitsTable.tsx
Expand Up @@ -80,6 +80,7 @@ export default class CommitsTable extends React.Component<{}, {}> {
const notAbleNoteIndex = findIndex(commits, (commit: Commit, index: number) => (
!commit.isEnabled && index < commits.length - 1
));
const bla = store.visualizationData;

return (
<table className='vp-table widefat fixed'>
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/components/service-panel/ServicePanel.tsx
Expand Up @@ -4,7 +4,6 @@ import { observer } from 'mobx-react';
import Button from './Button';
import FlashMessage from './flash-message/FlashMessage';
import Panel from './panel/Panel';
import VisualizationPanel from './visualizationPanel/VisualizationPanel';

import store from '../../stores/servicePanelStore';

Expand All @@ -22,7 +21,7 @@ export default class ServicePanel extends React.Component<ServicePanelProps, {}>

render() {
const { children } = this.props;
const { message, isVisible, isVisualizationVisible } = store;
const { message, isVisible } = store;

return (
<div>
Expand All @@ -31,12 +30,7 @@ export default class ServicePanel extends React.Component<ServicePanelProps, {}>
{message &&
<FlashMessage message={message} />
}
<Panel isVisible={false} />
<VisualizationPanel
isVisible={isVisualizationVisible}
environments={store.environments}
visualization={store.visualization}
/>
<Panel isVisible={isVisible} />
</div>
);
}
Expand Down
59 changes: 59 additions & 0 deletions frontend/src/stores/commitsTableStore.ts
Expand Up @@ -4,6 +4,7 @@ import CommitRow from './CommitRow';

import appStore from './appStore';
import { indexOf } from "../utils/CommitUtils";
import { generateGraphData } from "./utils";

class CommitsTableStore {
@observable commitRows: CommitRow[] = [];
Expand All @@ -26,6 +27,64 @@ class CommitsTableStore {
!_.differenceBy(this.selectableCommits, appStore.selectedCommits, ((value: Commit) => value.hash)).length;
}

@computed get visualizationData() {
console.log(this.commits.length);
const visualization = generateGraphData([
{
sha: '9',
parents: ['10']
},
{
sha: '10',
parents: ['11', '30']
},
{
sha: '11',
parents: ['12']
},
{
sha: '12',
parents: ['13', '20']
},
{
sha: '20',
parents: ['21']
},
{
sha: '30',
parents: ['31']
},
{
sha: '21',
parents: ['22']
},
{
sha: '31',
parents: ['13']
},
{
sha: '13',
parents: ['14']
},
{
sha: '22',
parents: ['14']
},
{
sha: '14',
parents: ['15']
},
{
sha: '15',
parents: []
}
]);

console.log(visualization);

return this.commits.length;
}

@action
changeCommitRows = (commitRows: CommitRow[]) => {
this.commitRows = commitRows;
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/stores/servicePanelStore.ts
Expand Up @@ -28,7 +28,6 @@ function getRndCom(id: number, isMerge: boolean, environment: string): Commit {
class ServicePanelStore {
@observable message: InfoMessage = null;
@observable isVisible: boolean = false;
@observable isVisualizationVisible: boolean = false;

get commits() {
return commitsTableStore.commits;
Expand Down Expand Up @@ -116,14 +115,6 @@ class ServicePanelStore {
@action
changeVisibility = (isVisible?: boolean) => {
this.isVisible = typeof isVisible === 'boolean' ? isVisible : !this.isVisible;
this.changeVisualizationVisibility();
};

@action
changeVisualizationVisibility = (isVisible?: boolean) => {
this.isVisualizationVisible = typeof isVisible === 'boolean'
? isVisible
: !this.isVisualizationVisible;
};
}

Expand Down
104 changes: 104 additions & 0 deletions frontend/src/stores/utils.ts
Expand Up @@ -53,3 +53,107 @@ export function getDiff(hash: string) {
});
});
}

interface CommitGraph {
sha: string;
parents: string[];
}

/*
Generate graph data.
:param commits: a list of commit, which should have
`sha`, `parents` properties.
:returns: data nodes, a json list of
[
sha,
[offset, branch], //dot
[
[from, to, branch], // route 1
[from, to, branch], // route 2
[from, to, branch],
] // routes
], // node
*/
export function generateGraphData(commits: CommitGraph[]) {
let
nodes = [],
branchIndex = [0],
reserve = [],
branches = {};

const remove = (list, item) => {
list.splice(list.indexOf(item), 1);
return list;
};

const getBranch = (sha) => {
if (branches[sha] == null) {
branches[sha] = branchIndex[0];
reserve.push(branchIndex[0]);
branchIndex[0]++;
}
return branches[sha];
};

commits.forEach(commit => {
const branch = getBranch(commit.sha);
const parentsCount = commit.parents.length;
const offset = reserve.indexOf(branch);
let routes = [];

const insertToRoutes = (from, to, branch) => {
routes.push({
from: from,
to: to,
branch: branch
});
};

if (parentsCount === 1) {
if (branches[commit.parents[0]] != null) {
// Create branch

let temp = reserve.slice(offset + 1);
for (let i = 0; i < temp.length; i++) {
insertToRoutes(i + offset + 1, i + offset + 1 - 1, temp[i]);
}

temp = reserve.slice(0, offset);
for (let i = 0; i < temp.length; i++) {
insertToRoutes(i, i, temp[i]);
}

remove(reserve, branch);
insertToRoutes(offset, reserve.indexOf(branches[commit.parents[0]]), branch);
} else {
// Straight branch

for (let i = 0; i < reserve.length; i++) {
insertToRoutes(i, i, reserve[i]);
}

branches[commit.parents[0]] = branch;
}
} else if (parentsCount === 2) {
// Merge branch
branches[commit.parents[0]] = branch;

for (let i = 0; i < reserve.length; i++) {
insertToRoutes(i, i, reserve[i]);
}

const otherBranch = getBranch(commit.parents[1]);
insertToRoutes(offset, reserve.indexOf(otherBranch), otherBranch);
}

nodes.push({
sha: commit.sha,
offset: offset,
branch: branch,
routes: routes
});
});

return nodes;
}

0 comments on commit 9090d19

Please sign in to comment.