Moved tests, added color pallete for branches, fixed merges

This commit is contained in:
Alex Palaistras 2016-12-18 13:59:44 +00:00
parent d92ff6a050
commit b1291b4adf
9 changed files with 240 additions and 134 deletions

View File

@ -16,7 +16,7 @@ SHELL = /bin/bash
DIFF = $(shell which colordiff || which diff)
# Test files to execute.
TESTS ?= $(shell find tests/*/*)
TESTS ?= $(shell find tests/*)
# Color & style definitions.
BOLD = \033[1m

88
grawkit
View File

@ -46,7 +46,7 @@ function addcommit(type, message) {
# > Function `branch` renders pre-defined branch under a specific name to its
# > SVG representation.
function branch(idx, _, buf, tmp, refs, merge, m, i, bspc, cspc) {
function branch(idx, _, buf, tmp, refs, i, bspc, cspc) {
# Do not render branch with no commits.
if (branches[idx,"refs"] == "") {
return
@ -57,44 +57,62 @@ function branch(idx, _, buf, tmp, refs, merge, m, i, bspc, cspc) {
bspc = idx * style["branch/spacing"]
# Print branch root element.
buf = sprintf(svg["g"], branches[idx,"name"])
# Add merge paths for branch, if any.
split(branches[idx,"merges"], merge, ",")
for (i in merge) {
split(merge[i], m, "|")
# Add starting point to previous branch ending point.
tmp = "M" m[1] * style["branch/spacing"] "," m[2] * style["commit/spacing"]
# Add Bezier curve leading to current branch starting point.
tmp = tmp " C" m[1] * style["branch/spacing"] "," m[2] * style["commit/spacing"]
tmp = tmp " " bspc "," m[2] * style["commit/spacing"]
tmp = tmp " " bspc "," refs[1] * style["commit/spacing"]
# Draw the path.
buf = buf "\n" sprintf(svg["path"], tmp)
}
buf = sprintf(svg["g"], "branch-" branches[idx,"name"])
# Add path for branch.
tmp = "M" bspc "," refs[1] * style["commit/spacing"]
tmp = tmp " L" bspc "," refs[length(refs)] * style["commit/spacing"]
# Print path.
buf = buf "\n" sprintf(svg["path"], tmp)
buf = buf "\n\t" sprintf(svg["path"], tmp)
# Add commits on path.
for (i in refs) {
cspc = refs[i] * style["commit/spacing"]
tmp = sprintf(svg["circle"], bspc, cspc, style["commit/radius"])
buf = buf "\n" tmp
buf = buf "\n\t" tmp
}
buf = buf "\n" svg["/g"]
return buf
}
# > Function `merge` renders merge paths for a branch pointed to by `idx`.
function merge(idx, _, buf, tmp, refs, fields, m, i, bspc) {
# Do not render merge paths for branch with no commits.
if (branches[idx,"merges"] == "") {
return
}
# Get commit refs.
split(branches[idx,"refs"], refs, ",")
bspc = idx * style["branch/spacing"]
# Print branch root element.
buf = "\n\t" sprintf(svg["g"], "branch-" branches[idx,"name"])
# Add merge paths for branch, if any.
split(branches[idx,"merges"], fields, ",")
for (i in fields) {
split(fields[i], m, "|")
# Add starting point to previous branch ending point.
tmp = "M" m[1] * style["branch/spacing"] "," m[2] * style["commit/spacing"]
# Add Bezier curve leading to current branch starting point.
tmp = tmp " C" bspc "," m[2] * style["commit/spacing"]
tmp = tmp " " bspc "," m[2] * style["commit/spacing"]
tmp = tmp " " bspc "," refs[1] * style["commit/spacing"]
# Draw the path.
buf = buf "\n\t\t" sprintf(svg["path"], tmp)
}
buf = buf "\n\t" svg["/g"]
return buf
}
# Global Declarations
# -------------------
#
@ -125,19 +143,20 @@ BEGIN {
# Style definitions.
style["branch/spacing"] = "50"
style["branch/fill"] = "none"
style["branch/stroke"] = "#333"
style["branch/stroke-width"] = "10"
style["commit/spacing"] = "50"
style["commit/fill"] = "#fff"
style["commit/stroke"] = style["branch/stroke"]
style["commit/stroke-width"] = style["branch/stroke-width"] / 2
style["commit/radius"] = style["commit/stroke-width"] * 1.5
# Color scheme, based on `base16-solarized-dark`
style["pallete"] = "#002b36,#268bd2,#859900,#cb4b16,#2aa198,#dc322f,#d33682,#6c71c4,#b58900"
# Static SVG templates.
svg["svg"] = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"%d %d %d %d\">"
svg["/svg"] = "</svg>"
svg["g"] = "<g id=\"%s\">"
svg["g"] = "<g class=\"%s\">"
svg["/g"] = "</g>"
svg["path"] = "<path class=\"branch\" d=\"%s\" />"
svg["circle"] = "<circle class=\"commit\" cx=\"%d\" cy=\"%d\" r=\"%s\" />"
@ -269,6 +288,9 @@ $0 ~ rule["merge"] {
# internal state, as defined in the command-line provided.
END {
buf = ""
split(style["pallete"], pallete, ",")
xy = style["branch/stroke-width"] * -1
w = (style["branch/spacing"] * (len["branches"] - 1)) + (style["branch/stroke-width"] * 2)
h = (style["commit/spacing"] * (len["commits"] - 1)) + (style["commit/stroke-width"] * 4)
@ -281,16 +303,30 @@ END {
print "<style type=\"text/css\"><![CDATA["
print ".branch {"
print " fill: " style["branch/fill"] ";"
print " stroke: " style["branch/stroke"] ";"
print " stroke-width: " style["branch/stroke-width"] ";"
print "}"
print ".commit {"
print " fill: " style["commit/fill"] ";"
print " stroke: " style["commit/stroke"] ";"
print " stroke-width: " style["commit/stroke-width"] ";"
print "}"
# Print color scheme definitions for branches.
for (i = 0; i < len["branches"]; i++) {
print ".branch-" branches[i,"name"] " {stroke: " pallete[i + 1] "}"
}
print "]]></style>"
# Print merge paths for branches.
for (i = len["branches"] - 1; i >= 0; i--) {
buf = buf merge(i)
}
if (buf != "") {
printf svg["g"], "merge"
print buf "\n" svg["/g"]
}
# Print each branch and corresponding commits in turn.
for (i = len["branches"] - 1; i >= 0; i--) {
print branch(i)

View File

@ -8,17 +8,16 @@
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke: #333;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke: #333;
stroke-width: 5;
}
.branch-master {stroke: #002b36}
]]></style>
<g id="master">
<path class="branch" d="M0,0 L0,0" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<g class="branch-master">
<path class="branch" d="M0,0 L0,0" />
<circle class="commit" cx="0" cy="0" r="7.5" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 411 B

After

Width:  |  Height:  |  Size: 420 B

View File

@ -11,19 +11,18 @@ git commit
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke: #333;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke: #333;
stroke-width: 5;
}
.branch-master {stroke: #002b36}
]]></style>
<g id="master">
<path class="branch" d="M0,0 L0,100" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<g class="branch-master">
<path class="branch" d="M0,0 L0,100" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 583 B

After

Width:  |  Height:  |  Size: 594 B

51
tests/03-branch.svg Normal file
View File

@ -0,0 +1,51 @@
<!--
# Test a scenario of adding commits to master, then
# branching off and adding a few commits to a different
# branch, then adding a final commit to master.
git commit -m "Commit on master"
git commit -m "More stuff"
git branch test-stuff
git checkout test-stuff
git commit -m 'Testing stuff'
git commit
git checkout master
git commit
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 70 270">
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke-width: 5;
}
.branch-master {stroke: #002b36}
.branch-test-stuff {stroke: #268bd2}
]]></style>
<g class="merge">
<g class="branch-test-stuff">
<path class="branch" d="M0,100 C50,100 50,100 50,150" />
</g>
</g>
<g class="branch-test-stuff">
<path class="branch" d="M50,150 L50,200" />
<circle class="commit" cx="50" cy="150" r="7.5" />
<circle class="commit" cx="50" cy="200" r="7.5" />
</g>
<g class="branch-master">
<path class="branch" d="M0,0 L0,250" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<circle class="commit" cx="0" cy="250" r="7.5" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

52
tests/04-merge.svg Normal file
View File

@ -0,0 +1,52 @@
<!--
# Test the scenario of a branch merging from and
# back to master.
git commit -m "Commit on master"
git branch test-merging
git commit -m "Still on master"
git checkout test-merging
git commit -m 'A sample commit'
git checkout master
git commit -m "Another master commit"
git merge test-merging
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 70 270">
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke-width: 5;
}
.branch-master {stroke: #002b36}
.branch-test-merging {stroke: #268bd2}
]]></style>
<g class="merge">
<g class="branch-test-merging">
<path class="branch" d="M0,50 C50,50 50,50 50,150" />
<path class="branch" d="M0,250 C50,250 50,250 50,150" />
</g>
</g>
<g class="branch-test-merging">
<path class="branch" d="M50,150 L50,150" />
<circle class="commit" cx="50" cy="150" r="7.5" />
</g>
<g class="branch-master">
<path class="branch" d="M0,0 L0,250" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<circle class="commit" cx="0" cy="200" r="7.5" />
<circle class="commit" cx="0" cy="250" r="7.5" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

64
tests/05-multi-branch.svg Normal file
View File

@ -0,0 +1,64 @@
<!--
# Test creating and merging between multiple branches.
git commit -m "Commit on master"
git branch test-first
git branch test-second
git commit -m "Still on master"
git checkout test-first
git commit
git checkout test-second
git commit
git merge test-first
git checkout master
git merge test-second
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 120 320">
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke-width: 5;
}
.branch-master {stroke: #002b36}
.branch-test-first {stroke: #268bd2}
.branch-test-second {stroke: #859900}
]]></style>
<g class="merge">
<g class="branch-test-second">
<path class="branch" d="M0,50 C100,50 100,50 100,200" />
<path class="branch" d="M0,300 C100,300 100,300 100,200" />
</g>
<g class="branch-test-first">
<path class="branch" d="M0,50 C50,50 50,50 50,150" />
<path class="branch" d="M100,250 C50,250 50,250 50,150" />
</g>
</g>
<g class="branch-test-second">
<path class="branch" d="M100,200 L100,250" />
<circle class="commit" cx="100" cy="200" r="7.5" />
<circle class="commit" cx="100" cy="250" r="7.5" />
</g>
<g class="branch-test-first">
<path class="branch" d="M50,150 L50,150" />
<circle class="commit" cx="50" cy="150" r="7.5" />
</g>
<g class="branch-master">
<path class="branch" d="M0,0 L0,300" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<circle class="commit" cx="0" cy="300" r="7.5" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,47 +0,0 @@
<!--
# Test a scenario of adding commits to master, then
# branching off and adding a few commits to a different
# branch, then adding a final commit to master.
git commit -m "Commit on master"
git commit -m "More stuff"
git branch test-stuff
git checkout test-stuff
git commit -m 'Testing stuff'
git commit
git checkout master
git commit
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 70 270">
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke: #333;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke: #333;
stroke-width: 5;
}
]]></style>
<g id="test-stuff">
<path class="branch" d="M0,100 C0,100 50,100 50,150" />
<path class="branch" d="M50,150 L50,200" />
<circle class="commit" cx="50" cy="150" r="7.5" />
<circle class="commit" cx="50" cy="200" r="7.5" />
</g>
<g id="master">
<path class="branch" d="M0,0 L0,250" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<circle class="commit" cx="0" cy="250" r="7.5" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1,48 +0,0 @@
<!--
# Test the scenario of a branch merging from and
# back to master.
git commit -m "Commit on master"
git branch test-merging
git commit -m "Still on master"
git checkout test-merging
git commit -m 'A sample commit'
git checkout master
git commit -m "Another master commit"
git merge test-merging
-->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 70 270">
<style type="text/css"><![CDATA[
.branch {
fill: none;
stroke: #333;
stroke-width: 10;
}
.commit {
fill: #fff;
stroke: #333;
stroke-width: 5;
}
]]></style>
<g id="test-merging">
<path class="branch" d="M0,50 C0,50 50,50 50,150" />
<path class="branch" d="M0,250 C0,250 50,250 50,150" />
<path class="branch" d="M50,150 L50,150" />
<circle class="commit" cx="50" cy="150" r="7.5" />
</g>
<g id="master">
<path class="branch" d="M0,0 L0,250" />
<circle class="commit" cx="0" cy="0" r="7.5" />
<circle class="commit" cx="0" cy="50" r="7.5" />
<circle class="commit" cx="0" cy="100" r="7.5" />
<circle class="commit" cx="0" cy="200" r="7.5" />
<circle class="commit" cx="0" cy="250" r="7.5" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB