Skip to content

Commit 1ca2d70

Browse files
Refactor numCommits variable to a more general n
Signed-off-by: Jacob Stopak <[email protected]>
1 parent 3533002 commit 1ca2d70

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

git_sim/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def main(
3232
settings.animate,
3333
help="Animate the simulation and output as an mp4 video",
3434
),
35+
n: int = typer.Option(
36+
settings.n,
37+
"-n",
38+
help="Number of commits to display from each branch head",
39+
),
3540
auto_open: bool = typer.Option(
3641
settings.auto_open,
3742
"--auto-open",
@@ -117,6 +122,7 @@ def main(
117122
),
118123
):
119124
settings.animate = animate
125+
settings.n = n
120126
settings.auto_open = auto_open
121127
settings.img_format = img_format
122128
settings.light_mode = light_mode

git_sim/commit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ def __init__(self, message: str, amend: bool):
1515
self.message = message
1616
self.amend = amend
1717

18-
self.defaultNumCommits = 4 if not self.amend else 5
19-
self.numCommits = 4 if not self.amend else 5
18+
self.n_default = 4 if not self.amend else 5
19+
self.n = self.n_default
20+
2021
self.hide_first_tag = True
2122
settings.hide_merged_chains = True
2223

git_sim/git_sim_base_command.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def __init__(self):
2525
self.trimmed = False
2626
self.prevRef = None
2727
self.topref = None
28-
self.numCommits = settings.commits
29-
self.defaultNumCommits = settings.commits
28+
self.n_default = settings.n_default
29+
self.n = settings.n
30+
self.n_orig = self.n
3031
self.selected_branches = []
3132
self.stop = False
3233
self.zone_title_offset = 2.6 if platform.system() == "Windows" else 2.6
@@ -50,9 +51,9 @@ def construct(self):
5051
self.show_outro()
5152

5253
def get_commits(self, start="HEAD"):
53-
if not self.numCommits:
54+
if not self.n:
5455
if settings.allow_no_commits:
55-
self.numCommits = self.defaultNumCommits
56+
self.n = self.n_default
5657
self.commits = ["dark"] * 5
5758
self.zone_title_offset = 2
5859
return
@@ -63,29 +64,27 @@ def get_commits(self, start="HEAD"):
6364
try:
6465
self.commits = (
6566
list(self.repo.iter_commits(start))
66-
if self.numCommits == 1
67+
if self.n == 1
6768
else list(
68-
self.repo.iter_commits(
69-
start + "~" + str(self.numCommits) + "..." + start
70-
)
69+
self.repo.iter_commits(start + "~" + str(self.n) + "..." + start)
7170
)
7271
)
73-
if len(self.commits) < self.defaultNumCommits:
72+
if len(self.commits) < self.n_default:
7473
self.commits = list(self.repo.iter_commits(start))
75-
while len(self.commits) < self.defaultNumCommits:
74+
while len(self.commits) < self.n_default:
7675
self.commits.append(self.create_dark_commit())
77-
self.numCommits = self.defaultNumCommits
76+
self.n = self.n_orig
7877

7978
except GitCommandError:
80-
self.numCommits -= 1
79+
self.n -= 1
8180
self.get_commits(start=start)
8281

8382
def parse_commits(
8483
self, commit, i, prevCircle=None, shift=numpy.array([0.0, 0.0, 0.0])
8584
):
8685
isNewCommit = commit.hexsha not in self.drawnCommits
8786

88-
if i < self.numCommits and commit in self.commits:
87+
if i < self.n and commit in self.commits:
8988
commitId, circle, arrow, hide_refs = self.draw_commit(
9089
commit, i, prevCircle, shift
9190
)
@@ -113,7 +112,7 @@ def parse_commits(
113112
if i == 0 and len(self.drawnRefs) < 2:
114113
self.draw_dark_ref()
115114

116-
if i < self.numCommits: # len(self.commits) - 1:
115+
if i < self.n:
117116
i += 1
118117
commitParents = list(commit.parents)
119118
if len(commitParents) > 0:

git_sim/log.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99

1010
class Log(GitSimBaseCommand):
11-
def __init__(self, commits: int, all: bool):
11+
def __init__(self, ctx: typer.Context, n: int, all: bool):
1212
super().__init__()
13-
self.numCommits = commits
14-
self.defaultNumCommits = commits
13+
14+
n_command = ctx.parent.params.get("n")
15+
n_subcommand = n
16+
if n_subcommand:
17+
n = n_subcommand
18+
else:
19+
n = n_command
20+
self.n = n
21+
self.n_orig = self.n
22+
1523
try:
1624
self.selected_branches.append(self.repo.active_branch.name)
1725
except TypeError:
@@ -37,15 +45,17 @@ def construct(self):
3745

3846

3947
def log(
40-
commits: int = typer.Option(
41-
default=settings.commits,
42-
help="The number of commits to display in the simulated log output",
48+
ctx: typer.Context,
49+
n: int = typer.Option(
50+
settings.n_subcommand,
51+
"-n",
52+
help="Number of commits to display from branch heads",
4353
min=1,
4454
),
4555
all: bool = typer.Option(
46-
default=False,
56+
False,
4757
help="Display all local branches in the log output",
4858
),
4959
):
50-
scene = Log(commits=commits, all=all)
60+
scene = Log(ctx=ctx, n=n, all=all)
5161
handle_animations(scene=scene)

git_sim/revert.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def __init__(self, commit: str):
2525
)
2626
sys.exit(1)
2727

28-
self.defaultNumCommits = 4
29-
self.numCommits = 4
28+
self.n_default = 4
29+
self.n = self.n_default
30+
3031
self.hide_first_tag = True
3132
self.zone_title_offset += 0.1
3233

git_sim/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class Settings(BaseSettings):
1919
allow_no_commits = False
2020
animate = False
2121
auto_open = True
22-
commits = 5
22+
n_default = 5
23+
n = 5
24+
n_subcommand: Union[int, None] = None
2325
files: Union[List[pathlib.Path], None] = None
2426
hide_first_tag = False
2527
img_format: ImgFormat = ImgFormat.jpg

0 commit comments

Comments
 (0)