Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from cupid.svgfig import SvgFig
COMMITW = 45
COMMITGAP = 25
COMMITSTRIDE = COMMITW + COMMITGAP
MARGIN = 10
STROKE = 1.5
class Branch:
def __init__(self, fig, x, y, pod=None):
self.fig = fig
self.curx = x
self.cury = y
self.head = pod
def commit(self, name=None, **kwargs):
circle_args = dict(
center=(self.curx, self.cury),
size=(COMMITW, COMMITW),
stroke_width=STROKE,
)
circle_args.update(kwargs)
if name:
circle_args.update(text=name)
c = self.fig.circle(**circle_args)
if self.head:
self.fig.connect(self.head.east, 0, c.west, 0)
self.head = c
self.curx += COMMITSTRIDE
return c
def merge(self, other, name, **kwargs):
y = min(self.cury, other.cury)
circle_args = dict(
center=(self.curx, y),
size=(COMMITW, COMMITW),
stroke_width=STROKE
)
circle_args.update(kwargs)
c = self.fig.circle(**circle_args, text=name)
self.fig.connect(self.head.east, 0, c.west, 0)
self.fig.connect(other.head.east, 0, c.south, 270)
self.head = c
self.curx += COMMITSTRIDE
return c
class GitFig(SvgFig):
def __init__(self, nbranches, ncommits, **kwargs):
figw = (ncommits * COMMITW) + ((ncommits - 1) * COMMITGAP) + 2 * MARGIN
figh = (nbranches * COMMITW) + ((nbranches - 1) * COMMITGAP) + 2 * MARGIN
super().__init__(size=(figw, figh), **kwargs)
self.curx = MARGIN + COMMITW / 2
def branch(self, name, y, pod=None, advance=0):
y = MARGIN + COMMITW / 2 + y * COMMITSTRIDE
if pod is None:
x = self.curx + COMMITSTRIDE * advance
else:
x = pod.cx + COMMITSTRIDE * advance
return Branch(self, x, y, pod=pod)
figsvgs = []
MASTER_KWARGS = dict(class_="blue tint", fill="#ddf")
BRANCH_KWARGS = dict(class_="green tint", fill="#cfc")
RELEASE_KWARGS = dict(class_="red tint", fill="#fdd")
NEW_KWARGS = dict(class_="yellow tint", fill="#ffc")
MERGECOMMIT_KWARGS = dict(stroke_width=STROKE+1.5)
def base_fig(branchkwargs={}, pr=True, release=False):
global fig, main, pr1, relbranch
nbranches = 1
if release:
nbranches += 1
mainy = 1
else:
mainy = 0
if pr:
nbranches += 1
fig = GitFig(nbranches=nbranches, ncommits=7)
main = fig.branch("main", y=mainy)
m1 = main.commit("A", **MASTER_KWARGS)
m2 = main.commit("B", **MASTER_KWARGS)
main.commit("C", **MASTER_KWARGS)
main.commit("D", **MASTER_KWARGS)
main.commit("E", **MASTER_KWARGS)
if pr:
pr1 = fig.branch("pr1", pod=m2, advance=1.5, y=mainy+1)
pr1.commit("F", **BRANCH_KWARGS, **branchkwargs)
pr1.commit("G", **BRANCH_KWARGS, **branchkwargs)
if release:
relbranch = fig.branch("release", pod=m1, advance=1, y=0)
relbranch.commit("R", **RELEASE_KWARGS)
relbranch.commit("S", **RELEASE_KWARGS)
def finish_figure(fig):
cog.outl(fig.tostring())
# Figure 1:
base_fig()
finish_figure(fig)
# Figure 2:
base_fig()
main.merge(pr1, name="M", **NEW_KWARGS)
finish_figure(fig)
# Figure 3:
base_fig(branchkwargs={"stroke_dasharray": "4"})
main.commit("Fr", **NEW_KWARGS)
main.commit("Gr", **NEW_KWARGS)
finish_figure(fig)
# Figure 4:
base_fig(branchkwargs={"stroke_dasharray": "4"})
main.commit("FGs", **NEW_KWARGS)
finish_figure(fig)
# Figure 5:
base_fig(release=True, pr=False)
relbranch.commit("Ec", **NEW_KWARGS)
finish_figure(fig)
# Figure 6:
base_fig(release=True)
main.merge(pr1, name="M", **NEW_KWARGS, **MERGECOMMIT_KWARGS)
relbranch.commit("Fc", **NEW_KWARGS)
relbranch.commit("Gc", **NEW_KWARGS)
finish_figure(fig)
# Figure 7:
base_fig(branchkwargs={"stroke_dasharray": "4"}, release=True)
main.commit("Fr", **NEW_KWARGS)
main.commit("Gr", **NEW_KWARGS, **MERGECOMMIT_KWARGS)
relbranch.commit("Frc", **NEW_KWARGS)
relbranch.commit("Grc", **NEW_KWARGS)
finish_figure(fig)
# Figure 8:
base_fig(branchkwargs={"stroke_dasharray": "4"}, release=True)
main.commit("FGs", **NEW_KWARGS, **MERGECOMMIT_KWARGS)
relbranch.commit("FGsc", **NEW_KWARGS)
finish_figure(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment