Skip to content

Commit 82f51f0

Browse files
Consolidate get_commits and parse_commits methods
Signed-off-by: Jacob Stopak <[email protected]>
1 parent 1ca2d70 commit 82f51f0

File tree

14 files changed

+82
-126
lines changed

14 files changed

+82
-126
lines changed

git_sim/add.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def construct(self):
3737
)
3838

3939
self.show_intro()
40-
self.get_commits()
41-
self.parse_commits(self.commits[0], 0)
40+
self.parse_commits()
4241
self.recenter_frame()
4342
self.scale_frame()
4443
self.vsplit_frame()

git_sim/branch.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ def construct(self):
1616
print(f"{settings.INFO_STRING} {type(self).__name__.lower()} {self.name}")
1717

1818
self.show_intro()
19-
self.get_commits()
20-
self.parse_commits(self.commits[0], 0)
19+
self.parse_commits()
2120
self.recenter_frame()
2221
self.scale_frame()
2322

git_sim/cherrypick.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ def construct(self):
5353
sys.exit(1)
5454

5555
self.show_intro()
56-
self.get_commits()
57-
self.parse_commits(self.commits[0], 0)
58-
self.orig_commits = self.commits
59-
self.get_commits(start=self.commit)
60-
self.parse_commits(self.commits[0], 0, shift=4 * m.DOWN)
61-
self.center_frame_on_commit(self.orig_commits[0])
56+
head_commit = self.get_commit()
57+
self.parse_commits(head_commit)
58+
cherry_picked_commit = self.get_commit(self.commit)
59+
self.parse_commits(cherry_picked_commit, shift=4 * m.DOWN)
60+
self.center_frame_on_commit(head_commit)
6261
self.setup_and_draw_parent(
63-
self.orig_commits[0],
64-
self.edit if self.edit else self.commits[0].message,
62+
head_commit,
63+
self.edit if self.edit else cherry_picked_commit.message,
6564
)
66-
self.draw_arrow_between_commits(self.commits[0].hexsha, "abcdef")
65+
self.draw_arrow_between_commits(cherry_picked_commit.hexsha, "abcdef")
6766
self.recenter_frame()
6867
self.scale_frame()
6968
self.reset_head_branch("abcdef")

git_sim/commit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def construct(self):
4242
)
4343

4444
self.show_intro()
45-
self.get_commits()
45+
head_commit = self.get_commit()
4646

4747
if self.amend:
4848
tree = self.repo.tree()
@@ -51,17 +51,17 @@ def construct(self):
5151
tree,
5252
self.message,
5353
)
54-
self.commits[0] = amended
54+
head_commit = amended
5555

56-
self.parse_commits(self.commits[0], 0)
57-
self.center_frame_on_commit(self.commits[0])
56+
self.parse_commits(head_commit)
57+
self.center_frame_on_commit(head_commit)
5858

5959
if not self.amend:
60-
self.setup_and_draw_parent(self.commits[0], self.message)
60+
self.setup_and_draw_parent(head_commit, self.message)
6161
else:
62-
self.draw_ref(self.commits[0], self.drawnCommitIds[amended.hexsha])
62+
self.draw_ref(head_commit, self.drawnCommitIds[amended.hexsha])
6363
self.draw_ref(
64-
self.commits[0],
64+
head_commit,
6565
self.drawnRefs["HEAD"],
6666
text=self.repo.active_branch.name,
6767
color=m.GREEN,

git_sim/git_sim_base_command.py

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def __init__(self):
1919
self.drawnCommits = {}
2020
self.drawnRefs = {}
2121
self.drawnCommitIds = {}
22-
self.commits = []
2322
self.zoomOuts = 0
2423
self.toFadeOut = m.Group()
2524
self.trimmed = False
@@ -46,45 +45,31 @@ def init_repo(self):
4645
def construct(self):
4746
print(f"{settings.INFO_STRING} {type(self).__name__.lower()}")
4847
self.show_intro()
49-
self.get_commits()
48+
self.parse_commits()
5049
self.fadeout()
5150
self.show_outro()
5251

53-
def get_commits(self, start="HEAD"):
54-
if not self.n:
55-
if settings.allow_no_commits:
56-
self.n = self.n_default
57-
self.commits = ["dark"] * 5
58-
self.zone_title_offset = 2
59-
return
60-
else:
61-
print("git-sim error: No commits in current Git repository.")
62-
sys.exit(1)
63-
64-
try:
65-
self.commits = (
66-
list(self.repo.iter_commits(start))
67-
if self.n == 1
68-
else list(
69-
self.repo.iter_commits(start + "~" + str(self.n) + "..." + start)
70-
)
71-
)
72-
if len(self.commits) < self.n_default:
73-
self.commits = list(self.repo.iter_commits(start))
74-
while len(self.commits) < self.n_default:
75-
self.commits.append(self.create_dark_commit())
76-
self.n = self.n_orig
52+
def get_commit(self, sha_or_ref="HEAD"):
53+
return self.repo.commit(sha_or_ref)
7754

78-
except GitCommandError:
79-
self.n -= 1
80-
self.get_commits(start=start)
55+
def get_default_commits(self):
56+
defaultCommits = [self.get_commit()]
57+
for x in range(self.n_default - 1):
58+
defaultCommits.append(defaultCommits[-1].parents[0])
59+
return defaultCommits
8160

8261
def parse_commits(
83-
self, commit, i, prevCircle=None, shift=numpy.array([0.0, 0.0, 0.0])
62+
self,
63+
commit=None,
64+
i=0,
65+
prevCircle=None,
66+
shift=numpy.array([0.0, 0.0, 0.0]),
8467
):
68+
commit = commit or self.get_commit()
69+
8570
isNewCommit = commit.hexsha not in self.drawnCommits
8671

87-
if i < self.n and commit in self.commits:
72+
if i < self.n:
8873
commitId, circle, arrow, hide_refs = self.draw_commit(
8974
commit, i, prevCircle, shift
9075
)
@@ -303,20 +288,11 @@ def get_nonparent_branch_names(self):
303288
exclude.append(b1.name)
304289
return [b for b in branches if b.name not in exclude]
305290

306-
def build_commit_id_and_message(self, commit, i, dots=False):
291+
def build_commit_id_and_message(self, commit, i):
307292
hide_refs = False
308293
if commit == "dark":
309294
commitId = m.Text("", font="Monospace", font_size=20, color=self.fontColor)
310295
commitMessage = ""
311-
elif (
312-
dots
313-
and self.commits[-1] != "dark"
314-
and commit.hexsha == self.commits[-1].hexsha
315-
):
316-
commitId = m.Text(
317-
"...", font="Monospace", font_size=20, color=self.fontColor
318-
)
319-
commitMessage = "..."
320296
else:
321297
commitId = m.Text(
322298
commit.hexsha[0:6],
@@ -924,9 +900,6 @@ def create_dark_commit(self):
924900

925901
def get_nondark_commits(self):
926902
nondark_commits = []
927-
for commit in self.commits:
928-
if commit != "dark":
929-
nondark_commits.append(commit)
930903
return nondark_commits
931904

932905
def draw_ref(self, commit, i, top, text="HEAD", color=m.BLUE):

git_sim/log.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def __init__(self, ctx: typer.Context, n: int, all: bool):
1212
super().__init__()
1313

1414
n_command = ctx.parent.params.get("n")
15-
n_subcommand = n
16-
if n_subcommand:
17-
n = n_subcommand
15+
self.n_subcommand = n
16+
if self.n_subcommand:
17+
n = self.n_subcommand
1818
else:
1919
n = n_command
2020
self.n = n
@@ -29,15 +29,13 @@ def __init__(self, ctx: typer.Context, n: int, all: bool):
2929
def construct(self):
3030
if not settings.stdout:
3131
print(
32-
f"{settings.INFO_STRING} {type(self).__name__.lower()}{' --all' if self.all else ''}"
32+
f"{settings.INFO_STRING} {type(self).__name__.lower()}{' --all' if self.all else ''}{' -n ' + str(self.n) if self.n_subcommand else ''}"
3333
)
3434
self.show_intro()
35-
self.get_commits()
36-
self.parse_commits(self.commits[0], 0)
35+
self.parse_commits()
3736
if self.all:
3837
for branch in self.get_nonparent_branch_names():
39-
self.get_commits(start=branch.name)
40-
self.parse_commits(self.commits[0], 0)
38+
self.parse_commits(self.get_commit(branch.name))
4139
self.recenter_frame()
4240
self.scale_frame()
4341
self.fadeout()

git_sim/merge.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,27 @@ def construct(self):
5555
sys.exit(1)
5656

5757
self.show_intro()
58-
self.get_commits()
59-
self.orig_commits = self.commits
60-
self.get_commits(start=self.branch)
58+
head_commit = self.get_commit()
59+
branch_commit = self.get_commit(self.branch)
6160

6261
# Use forward slash to determine if supplied branch arg is local or remote tracking branch
6362
if not self.is_remote_tracking_branch(self.branch):
64-
if self.branch in self.repo.git.branch(
65-
"--contains", self.orig_commits[0].hexsha
66-
):
63+
if self.branch in self.repo.git.branch("--contains", head_commit.hexsha):
6764
self.ff = True
6865
else:
6966
if self.branch in self.repo.git.branch(
70-
"-r", "--contains", self.orig_commits[0].hexsha
67+
"-r", "--contains", head_commit.hexsha
7168
):
7269
self.ff = True
7370

7471
if self.ff:
75-
self.get_commits(start=self.branch)
76-
self.parse_commits(self.commits[0], 0)
77-
reset_head_to = self.commits[0].hexsha
72+
self.parse_commits(branch_commit)
73+
reset_head_to = branch_commit.hexsha
7874
shift = numpy.array([0.0, 0.6, 0.0])
7975

8076
if self.no_ff:
81-
self.center_frame_on_commit(self.commits[0])
82-
commitId = self.setup_and_draw_parent(self.commits[0], "Merge commit")
77+
self.center_frame_on_commit(branch_commit)
78+
commitId = self.setup_and_draw_parent(branch_commit, "Merge commit")
8379
reset_head_to = "abcdef"
8480
shift = numpy.array([0.0, 0.0, 0.0])
8581

@@ -88,29 +84,27 @@ def construct(self):
8884
if "HEAD" in self.drawnRefs:
8985
self.reset_head_branch(reset_head_to, shift=shift)
9086
else:
91-
self.draw_ref(self.commits[0], commitId if self.no_ff else self.topref)
87+
self.draw_ref(branch_commit, commitId if self.no_ff else self.topref)
9288
self.draw_ref(
93-
self.commits[0],
89+
branch_commit,
9490
self.drawnRefs["HEAD"],
9591
text=self.repo.active_branch.name,
9692
color=m.GREEN,
9793
)
9894

9995
else:
100-
self.get_commits()
101-
self.parse_commits(self.commits[0], 0)
102-
self.get_commits(start=self.branch)
103-
self.parse_commits(self.commits[0], 0, shift=4 * m.DOWN)
104-
self.center_frame_on_commit(self.orig_commits[0])
96+
self.parse_commits(head_commit)
97+
self.parse_commits(branch_commit, shift=4 * m.DOWN)
98+
self.center_frame_on_commit(head_commit)
10599
self.setup_and_draw_parent(
106-
self.orig_commits[0],
100+
head_commit,
107101
"Merge commit",
108102
shift=2 * m.DOWN,
109103
draw_arrow=False,
110104
color=m.GRAY,
111105
)
112-
self.draw_arrow_between_commits("abcdef", self.commits[0].hexsha)
113-
self.draw_arrow_between_commits("abcdef", self.orig_commits[0].hexsha)
106+
self.draw_arrow_between_commits("abcdef", branch_commit.hexsha)
107+
self.draw_arrow_between_commits("abcdef", head_commit.hexsha)
114108
self.recenter_frame()
115109
self.scale_frame()
116110
self.reset_head_branch("abcdef")

git_sim/rebase.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,31 @@ def construct(self):
6464
sys.exit(1)
6565

6666
self.show_intro()
67-
self.get_commits(start=self.branch)
68-
self.parse_commits(self.commits[0], 0)
69-
self.orig_commits = self.commits
70-
self.get_commits()
67+
branch_commit = self.get_commit(self.branch)
68+
self.parse_commits(branch_commit)
69+
head_commit = self.get_commit()
7170

7271
reached_base = False
73-
for commit in self.commits:
72+
for commit in self.get_default_commits():
7473
if commit != "dark" and self.branch in self.repo.git.branch(
7574
"--contains", commit
7675
):
7776
reached_base = True
7877

79-
self.parse_commits(self.commits[0], 0, shift=4 * m.DOWN)
80-
self.center_frame_on_commit(self.orig_commits[0])
78+
self.parse_commits(head_commit, shift=4 * m.DOWN)
79+
self.center_frame_on_commit(branch_commit)
8180

8281
to_rebase = []
8382
i = 0
84-
current = self.commits[i]
83+
current = head_commit
8584
while self.branch not in self.repo.git.branch("--contains", current):
8685
to_rebase.append(current)
8786
i += 1
88-
if i >= len(self.commits):
87+
if i >= self.n:
8988
break
90-
current = self.commits[i]
89+
current = self.get_default_commits()[i]
9190

92-
parent = self.orig_commits[0].hexsha
91+
parent = branch_commit.hexsha
9392

9493
for j, tr in enumerate(reversed(to_rebase)):
9594
if not reached_base and j == 0:

git_sim/reset.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def construct(self):
5656
)
5757

5858
self.show_intro()
59-
self.get_commits()
60-
self.parse_commits(self.commits[0], 0)
59+
self.parse_commits()
6160
self.recenter_frame()
6261
self.scale_frame()
6362
self.reset_head_branch(self.resetTo.hexsha)
@@ -66,21 +65,21 @@ def construct(self):
6665
self.fadeout()
6766
self.show_outro()
6867

69-
def build_commit_id_and_message(self, commit, i, dots=False):
68+
def build_commit_id_and_message(self, commit, i):
7069
hide_refs = False
7170
if commit == "dark":
7271
commitId = m.Text("", font="Monospace", font_size=20, color=self.fontColor)
7372
commitMessage = ""
7473
elif i == 3 and self.resetTo.hexsha not in [
75-
commit.hexsha for commit in self.get_nondark_commits()
74+
c.hexsha for c in self.get_default_commits()
7675
]:
7776
commitId = m.Text(
7877
"...", font="Monospace", font_size=20, color=self.fontColor
7978
)
8079
commitMessage = "..."
8180
hide_refs = True
8281
elif i == 4 and self.resetTo.hexsha not in [
83-
commit.hexsha for commit in self.get_nondark_commits()
82+
c.hexsha for c in self.get_default_commits()
8483
]:
8584
commitId = m.Text(
8685
self.resetTo.hexsha[:6],

git_sim/restore.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def construct(self):
3535
)
3636

3737
self.show_intro()
38-
self.get_commits()
39-
self.parse_commits(self.commits[0], 0)
38+
self.parse_commits()
4039
self.recenter_frame()
4140
self.scale_frame()
4241
self.vsplit_frame()

0 commit comments

Comments
 (0)