Skip to content

Commit

Permalink
Add --color-by=author option to color commits by author after drawing…
Browse files Browse the repository at this point in the history
… commit graph

Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
  • Loading branch information
initialcommit-io committed Mar 7, 2023
1 parent a45db4c commit 3dae3ea
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Example: `$ git-sim merge <branch>`
- Run a one-liner git-sim command in the terminal to generate a custom Git command visualization (.jpg) from your repo
- Supported commands: `log`, `status`, `add`, `restore`, `commit`, `stash`, `branch`, `tag`, `reset`, `revert`, `merge`, `rebase`, `cherry-pick`
- Generate an animated video (.mp4) instead of a static image using the `--animate` flag (note: significant performance slowdown, it is recommended to use `--low-quality` to speed up testing and remove when ready to generate presentation-quality video)
- Color commits by parameter, such as author the `--color-by=author` option
- Choose between dark mode (default) and light mode
- Specify output formats of either jpg, png, mp4, or webm
- Combine with bundled command [git-dummy](https://github.com/initialcommit-com/git-dummy) to generate a dummy Git repo and then simulate operations on it
Expand Down Expand Up @@ -136,6 +137,7 @@ The `[global options]` apply to the overarching `git-sim` simulation itself, inc
`-n <number>`: Number of commits to display from each branch head.
`--all`: Display all local branches in the log output.
`--animate`: Instead of outputting a static image, animate the Git command behavior in a .mp4 video.
`--color-by author`: Color commits by parameter, such as author.
`--invert-branches`: Invert positioning of branches by reversing order of multiple parents where applicable.
`--hide-merged-branches`: Hide commits from merged branches, i.e. only display mainline commits.
`--media-dir`: The path at which to store the simulated output media files.
Expand Down
5 changes: 5 additions & 0 deletions git_sim/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def main(
settings.all,
help="Display all local branches in the log output",
),
color_by: str = typer.Option(
settings.color_by,
help="Color commits by parameter, such as author",
),
):
settings.animate = animate
settings.n = n
Expand All @@ -159,6 +163,7 @@ def main(
settings.invert_branches = invert_branches
settings.hide_merged_branches = hide_merged_branches
settings.all = all
settings.color_by = color_by

if sys.platform == "linux" or sys.platform == "darwin":
repo_name = git.repo.Repo(
Expand Down
1 change: 1 addition & 0 deletions git_sim/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def construct(self):

self.recenter_frame()
self.scale_frame()
self.color_by()
self.fadeout()
self.show_outro()

Expand Down
1 change: 1 addition & 0 deletions git_sim/cherrypick.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def construct(self):
self.recenter_frame()
self.scale_frame()
self.reset_head_branch("abcdef")
self.color_by(offset=2)
self.fadeout()
self.show_outro()

Expand Down
66 changes: 61 additions & 5 deletions git_sim/git_sim_base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ def __init__(self):
self.selected_branches = []
self.zone_title_offset = 2.6 if platform.system() == "Windows" else 2.6
self.arrow_map = []
self.arrows = []
self.all = settings.all
self.first_parse = True
self.author_groups = {}
self.colors = [
m.ORANGE,
m.YELLOW,
m.GREEN,
m.BLUE,
m.MAROON,
m.PURPLE,
m.GOLD,
]

self.logo = m.ImageMobject(settings.logo)
self.logo.width = 3
Expand Down Expand Up @@ -276,6 +287,8 @@ def draw_commit(self, commit, i, prevCircle, shift=numpy.array([0.0, 0.0, 0.0]))

if commit != "dark":
self.drawnCommits[commit.hexsha] = circle
group = m.Group(circle, commitId, message)
self.add_group_to_author_groups(commit.author.name, group)

self.toFadeOut.add(circle, commitId, message)
self.prevRef = commitId
Expand Down Expand Up @@ -374,7 +387,7 @@ def draw_branch(self, commit, i):
else:
self.add(fullbranch)

self.toFadeOut.add(branchRec, branchText)
self.toFadeOut.add(fullbranch)
self.drawnRefs[branch] = fullbranch

if i == 0 and self.first_parse:
Expand Down Expand Up @@ -410,18 +423,20 @@ def draw_tag(self, commit, i):
tagRec.next_to(self.prevRef, m.UP)
tagText.move_to(tagRec.get_center())

fulltag = m.VGroup(tagRec, tagText)

self.prevRef = tagRec

if settings.animate:
self.play(
m.Create(tagRec),
m.Create(tagText),
m.Create(fulltag),
run_time=1 / settings.speed,
)
else:
self.add(tagRec, tagText)
self.add(fulltag)

self.toFadeOut.add(tagRec, tagText)
self.toFadeOut.add(fulltag)
self.drawnRefs[tag] = fulltag

if i == 0 and self.first_parse:
self.topref = self.prevRef
Expand All @@ -439,6 +454,7 @@ def draw_arrow(self, prevCircle, arrow):
else:
self.add(arrow)

self.arrows.append(arrow)
self.toFadeOut.add(arrow)

def recenter_frame(self):
Expand Down Expand Up @@ -896,6 +912,7 @@ def setup_and_draw_parent(
self.play(m.Create(arrow), run_time=1 / settings.speed)
else:
self.add(arrow)
self.arrows.append(arrow)
self.toFadeOut.add(arrow)

return commitId
Expand Down Expand Up @@ -1042,6 +1059,45 @@ def create_zone_text(
thirdColumnFiles.add(text)
thirdColumnFilesDict[f] = text

def color_by(self, offset=0):
if settings.color_by == "author":
sorted_authors = sorted(
self.author_groups.keys(),
key=lambda k: len(self.author_groups[k]),
reverse=True,
)
for i, author in enumerate(sorted_authors):
authorText = m.Text(
f"{author[:15]} ({str(len(self.author_groups[author]))})",
font="Monospace",
font_size=36,
color=self.colors[int(i % 7)],
)
authorText.move_to(
[(-5 - offset) if settings.reverse else (5 + offset), -i, 0]
)
self.toFadeOut.add(authorText)
if i == 0:
self.recenter_frame()
self.scale_frame()
if settings.animate:
self.play(m.AddTextLetterByLetter(authorText))
else:
self.add(authorText)
for g in self.author_groups[author]:
g[0].set_color(self.colors[int(i % 7)])
self.recenter_frame()
self.scale_frame()

elif settings.color_by == "branch":
pass

def add_group_to_author_groups(self, author, group):
if author not in self.author_groups:
self.author_groups[author] = [group]
else:
self.author_groups[author].append(group)


class DottedLine(m.Line):
def __init__(self, *args, dot_spacing=0.4, dot_kwargs={}, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions git_sim/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def construct(self):
self.parse_all()
self.recenter_frame()
self.scale_frame()
self.color_by()
self.fadeout()
self.show_outro()

Expand Down
5 changes: 5 additions & 0 deletions git_sim/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def construct(self):
text=self.repo.active_branch.name,
color=m.GREEN,
)
if self.no_ff:
self.color_by(offset=2)
else:
self.color_by()

else:
self.parse_commits(head_commit)
Expand All @@ -111,6 +115,7 @@ def construct(self):
self.recenter_frame()
self.scale_frame()
self.reset_head_branch("abcdef")
self.color_by(offset=2)

self.fadeout()
self.show_outro()
Expand Down
1 change: 1 addition & 0 deletions git_sim/rebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def construct(self):
self.recenter_frame()
self.scale_frame()
self.reset_head_branch(parent)
self.color_by(offset=2 * len(to_rebase))
self.fadeout()
self.show_outro()

Expand Down
1 change: 1 addition & 0 deletions git_sim/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Settings(BaseSettings):
invert_branches = False
hide_merged_branches = False
all = False
color_by: Union[str, None] = None

class Config:
env_prefix = "git_sim_"
Expand Down
1 change: 1 addition & 0 deletions git_sim/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def construct(self):

self.recenter_frame()
self.scale_frame()
self.color_by()
self.fadeout()
self.show_outro()

Expand Down

0 comments on commit 3dae3ea

Please sign in to comment.