Skip to content

Commit 3dae3ea

Browse files
Add --color-by=author option to color commits by author after drawing commit graph
Signed-off-by: Jacob Stopak <[email protected]>
1 parent a45db4c commit 3dae3ea

10 files changed

+79
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Example: `$ git-sim merge <branch>`
2828
- Run a one-liner git-sim command in the terminal to generate a custom Git command visualization (.jpg) from your repo
2929
- Supported commands: `log`, `status`, `add`, `restore`, `commit`, `stash`, `branch`, `tag`, `reset`, `revert`, `merge`, `rebase`, `cherry-pick`
3030
- 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)
31+
- Color commits by parameter, such as author the `--color-by=author` option
3132
- Choose between dark mode (default) and light mode
3233
- Specify output formats of either jpg, png, mp4, or webm
3334
- 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
@@ -136,6 +137,7 @@ The `[global options]` apply to the overarching `git-sim` simulation itself, inc
136137
`-n <number>`: Number of commits to display from each branch head.
137138
`--all`: Display all local branches in the log output.
138139
`--animate`: Instead of outputting a static image, animate the Git command behavior in a .mp4 video.
140+
`--color-by author`: Color commits by parameter, such as author.
139141
`--invert-branches`: Invert positioning of branches by reversing order of multiple parents where applicable.
140142
`--hide-merged-branches`: Hide commits from merged branches, i.e. only display mainline commits.
141143
`--media-dir`: The path at which to store the simulated output media files.

git_sim/__main__.py

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ def main(
134134
settings.all,
135135
help="Display all local branches in the log output",
136136
),
137+
color_by: str = typer.Option(
138+
settings.color_by,
139+
help="Color commits by parameter, such as author",
140+
),
137141
):
138142
settings.animate = animate
139143
settings.n = n
@@ -159,6 +163,7 @@ def main(
159163
settings.invert_branches = invert_branches
160164
settings.hide_merged_branches = hide_merged_branches
161165
settings.all = all
166+
settings.color_by = color_by
162167

163168
if sys.platform == "linux" or sys.platform == "darwin":
164169
repo_name = git.repo.Repo(

git_sim/branch.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def construct(self):
4949

5050
self.recenter_frame()
5151
self.scale_frame()
52+
self.color_by()
5253
self.fadeout()
5354
self.show_outro()
5455

git_sim/cherrypick.py

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def construct(self):
6767
self.recenter_frame()
6868
self.scale_frame()
6969
self.reset_head_branch("abcdef")
70+
self.color_by(offset=2)
7071
self.fadeout()
7172
self.show_outro()
7273

git_sim/git_sim_base_command.py

+61-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ def __init__(self):
2828
self.selected_branches = []
2929
self.zone_title_offset = 2.6 if platform.system() == "Windows" else 2.6
3030
self.arrow_map = []
31+
self.arrows = []
3132
self.all = settings.all
3233
self.first_parse = True
34+
self.author_groups = {}
35+
self.colors = [
36+
m.ORANGE,
37+
m.YELLOW,
38+
m.GREEN,
39+
m.BLUE,
40+
m.MAROON,
41+
m.PURPLE,
42+
m.GOLD,
43+
]
3344

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

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

280293
self.toFadeOut.add(circle, commitId, message)
281294
self.prevRef = commitId
@@ -374,7 +387,7 @@ def draw_branch(self, commit, i):
374387
else:
375388
self.add(fullbranch)
376389

377-
self.toFadeOut.add(branchRec, branchText)
390+
self.toFadeOut.add(fullbranch)
378391
self.drawnRefs[branch] = fullbranch
379392

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

426+
fulltag = m.VGroup(tagRec, tagText)
427+
413428
self.prevRef = tagRec
414429

415430
if settings.animate:
416431
self.play(
417-
m.Create(tagRec),
418-
m.Create(tagText),
432+
m.Create(fulltag),
419433
run_time=1 / settings.speed,
420434
)
421435
else:
422-
self.add(tagRec, tagText)
436+
self.add(fulltag)
423437

424-
self.toFadeOut.add(tagRec, tagText)
438+
self.toFadeOut.add(fulltag)
439+
self.drawnRefs[tag] = fulltag
425440

426441
if i == 0 and self.first_parse:
427442
self.topref = self.prevRef
@@ -439,6 +454,7 @@ def draw_arrow(self, prevCircle, arrow):
439454
else:
440455
self.add(arrow)
441456

457+
self.arrows.append(arrow)
442458
self.toFadeOut.add(arrow)
443459

444460
def recenter_frame(self):
@@ -896,6 +912,7 @@ def setup_and_draw_parent(
896912
self.play(m.Create(arrow), run_time=1 / settings.speed)
897913
else:
898914
self.add(arrow)
915+
self.arrows.append(arrow)
899916
self.toFadeOut.add(arrow)
900917

901918
return commitId
@@ -1042,6 +1059,45 @@ def create_zone_text(
10421059
thirdColumnFiles.add(text)
10431060
thirdColumnFilesDict[f] = text
10441061

1062+
def color_by(self, offset=0):
1063+
if settings.color_by == "author":
1064+
sorted_authors = sorted(
1065+
self.author_groups.keys(),
1066+
key=lambda k: len(self.author_groups[k]),
1067+
reverse=True,
1068+
)
1069+
for i, author in enumerate(sorted_authors):
1070+
authorText = m.Text(
1071+
f"{author[:15]} ({str(len(self.author_groups[author]))})",
1072+
font="Monospace",
1073+
font_size=36,
1074+
color=self.colors[int(i % 7)],
1075+
)
1076+
authorText.move_to(
1077+
[(-5 - offset) if settings.reverse else (5 + offset), -i, 0]
1078+
)
1079+
self.toFadeOut.add(authorText)
1080+
if i == 0:
1081+
self.recenter_frame()
1082+
self.scale_frame()
1083+
if settings.animate:
1084+
self.play(m.AddTextLetterByLetter(authorText))
1085+
else:
1086+
self.add(authorText)
1087+
for g in self.author_groups[author]:
1088+
g[0].set_color(self.colors[int(i % 7)])
1089+
self.recenter_frame()
1090+
self.scale_frame()
1091+
1092+
elif settings.color_by == "branch":
1093+
pass
1094+
1095+
def add_group_to_author_groups(self, author, group):
1096+
if author not in self.author_groups:
1097+
self.author_groups[author] = [group]
1098+
else:
1099+
self.author_groups[author].append(group)
1100+
10451101

10461102
class DottedLine(m.Line):
10471103
def __init__(self, *args, dot_spacing=0.4, dot_kwargs={}, **kwargs):

git_sim/log.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def construct(self):
4343
self.parse_all()
4444
self.recenter_frame()
4545
self.scale_frame()
46+
self.color_by()
4647
self.fadeout()
4748
self.show_outro()
4849

git_sim/merge.py

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def construct(self):
9393
text=self.repo.active_branch.name,
9494
color=m.GREEN,
9595
)
96+
if self.no_ff:
97+
self.color_by(offset=2)
98+
else:
99+
self.color_by()
96100

97101
else:
98102
self.parse_commits(head_commit)
@@ -111,6 +115,7 @@ def construct(self):
111115
self.recenter_frame()
112116
self.scale_frame()
113117
self.reset_head_branch("abcdef")
118+
self.color_by(offset=2)
114119

115120
self.fadeout()
116121
self.show_outro()

git_sim/rebase.py

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def construct(self):
102102
self.recenter_frame()
103103
self.scale_frame()
104104
self.reset_head_branch(parent)
105+
self.color_by(offset=2 * len(to_rebase))
105106
self.fadeout()
106107
self.show_outro()
107108

git_sim/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Settings(BaseSettings):
4545
invert_branches = False
4646
hide_merged_branches = False
4747
all = False
48+
color_by: Union[str, None] = None
4849

4950
class Config:
5051
env_prefix = "git_sim_"

git_sim/tag.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def construct(self):
4949

5050
self.recenter_frame()
5151
self.scale_frame()
52+
self.color_by()
5253
self.fadeout()
5354
self.show_outro()
5455

0 commit comments

Comments
 (0)