Skip to content

Commit

Permalink
feat(visual): add basic branch line output demo
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 3, 2021
1 parent b42a908 commit b39d827
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 31 deletions.
35 changes: 33 additions & 2 deletions src/app/git_analysis/commit_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@ use crate::domain::git::CocoCommit;
use crate::infrastructure::git::git_command::get_commit_message;
use crate::infrastructure::git::git_log_parser::GitMessageParser;
use crate::infrastructure::url_format;
use serde::{Deserialize, Serialize};

pub fn analysis(url: &str) -> Vec<CocoCommit> {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ShortCommit {
pub branch: String,
pub commit_id: String,
pub author: String,
pub date: i64,
pub message: String,
pub parent_hashes: Vec<String>,
pub tree_hash: String,
}

impl ShortCommit {
pub fn convert(commit: CocoCommit) -> ShortCommit {
Self {
branch: commit.branch,
commit_id: commit.commit_id,
author: commit.author,
date: commit.date,
message: commit.message,
parent_hashes: commit.parent_hashes,
tree_hash: commit.tree_hash,
}
}
}

pub fn analysis(url: &str) -> Vec<ShortCommit> {
let local_path = url_format::uri_to_path(url);

let messages = get_commit_message(Some(format!("{}", local_path.display())));
let vec = GitMessageParser::parse(messages.as_str());

return vec;
let mut results = vec![];
for commit in vec {
results.push(ShortCommit::convert(commit))
}

return results;
}
53 changes: 53 additions & 0 deletions web/data/git-commits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"branch": "origin/no-commits",
"commit_id": "ee5c5f2",
"author": "Phodal Huang",
"date": 1611631370,
"message": "feat: add no commits",
"parent_hashes": [],
"tree_hash": "5216993"
},
{
"branch": "origin/20200122",
"commit_id": "cc0bf30",
"author": "Phodal Huang",
"date": 1611306195,
"message": "docs: add notes",
"parent_hashes": [
"bb84b36"
],
"tree_hash": "2fcee80"
},
{
"branch": "master",
"commit_id": "bb84b36",
"author": "Phodal Huang",
"date": 1610694788,
"message": "docs: update projects for testings",
"parent_hashes": [
"17c580a"
],
"tree_hash": "55b2a8d"
},
{
"branch": "origin/main",
"commit_id": "17c580a",
"author": "Phodal Huang",
"date": 1610541520,
"message": "build: add second commit for master",
"parent_hashes": [
"de114a6"
],
"tree_hash": "b1a0885"
},
{
"branch": "origin/gh-pages",
"commit_id": "de114a6",
"author": "Phodal Huang",
"date": 1610519809,
"message": "Initial commit",
"parent_hashes": [],
"tree_hash": "052c659"
}
]
62 changes: 33 additions & 29 deletions web/public/js/graph/commits-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,54 @@
// IN THE SOFTWARE.
let renderCommitsTree = function (data) {
let color = d3.scaleOrdinal(d3.schemeSet2).domain(data)
let shaMap = {};
let idx = 1;
let column = 1;
let lastSha = "";

let shaMap = {};
let branchMap = {};
for (let datum of data) {
if (!datum.commits) {
return;
shaMap[datum["commit_id"]] = datum;
if (!branchMap[datum["branch"]]) {
branchMap[datum["branch"]] = column;
column++;
}
}

for (let commit of datum.commits) {
let short = commit.substr(0, 6);

if (!shaMap[short]) {
shaMap[short] = {
idx: idx,
id: short,
column: column,
color: color(column),
parents_paths: lastSha ? [] : [
{
"id": lastSha,
path: [{
x: lastSha.id,
y: lastSha.column,
type: 0,
}]
}
]
};
idx++;

for (let datum of data) {
let short = datum["commit_id"];
let parent_hashes = [];
for (let hash of datum["parent_hashes"]) {
let item = shaMap[hash];
if (item) {
parent_hashes.push({
id: hash,
path: [{
x: item.id,
y: branchMap[item["branch"]],
type: 0,
}]
})
}

lastSha = shaMap[short];
}

column++;
shaMap[short] = {
idx: idx,
id: short,
column: branchMap[datum["branch"]],
color: color(branchMap[datum["branch"]]),
parents_paths: parent_hashes
};

idx++;
}

let tree = [];
for (let value in shaMap) {
tree.push(shaMap[value]);
}

console.log(tree);

let xGap = 11;
let yGap = 20;
let gap = 2 / 5 * yGap;
Expand Down
3 changes: 3 additions & 0 deletions web/public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ d3.json("data/git.json").then(function (json) {
}

renderBranches(data)
});

d3.json("data/git-commits.json").then(function (data) {
renderCommitsTree(data)
});

0 comments on commit b39d827

Please sign in to comment.