Skip to content

Commit a5542d3

Browse files
Add <until> argument for rebase subcommand for use with --onto
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 5fa6713 commit a5542d3

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

src/git_sim/commands.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,14 @@ def rebase(
278278
None,
279279
help="The parent of the commit to rebase (to be used with --onto)",
280280
),
281+
until: str = typer.Argument(
282+
None,
283+
help="The commit to rebase up to and including (to be used with --onto)",
284+
),
281285
):
282286
from git_sim.rebase import Rebase
283287

284-
scene = Rebase(branch=branch, rebase_merges=rebase_merges, onto=onto, oldparent=oldparent)
288+
scene = Rebase(branch=branch, rebase_merges=rebase_merges, onto=onto, oldparent=oldparent, until=until)
285289
handle_animations(scene=scene)
286290

287291

src/git_sim/git_sim_base_command.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,12 +1329,17 @@ def add_group_to_author_groups(self, author, group):
13291329

13301330
def show_command_as_title(self):
13311331
if settings.show_command_as_title:
1332-
titleText = m.Text(
1333-
self.trim_cmd(self.cmd),
1334-
font=self.font,
1335-
font_size=36,
1336-
color=self.fontColor,
1337-
)
1332+
title_len = 100
1333+
while 1:
1334+
titleText = m.Text(
1335+
self.trim_cmd(self.cmd, title_len),
1336+
font=self.font,
1337+
font_size=36,
1338+
color=self.fontColor,
1339+
)
1340+
if titleText.width < self.camera.frame.width:
1341+
break
1342+
title_len -= 5
13381343
top = 0
13391344
for element in self.toFadeOut:
13401345
if element.get_top()[1] > top:

src/git_sim/rebase.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212

1313
class Rebase(GitSimBaseCommand):
14-
def __init__(self, branch: str, rebase_merges: bool, onto: bool, oldparent: str):
14+
def __init__(self, branch: str, rebase_merges: bool, onto: bool, oldparent: str, until: str):
1515
super().__init__()
1616
self.branch = branch
1717
self.rebase_merges = rebase_merges
1818
self.onto = onto
1919
self.oldparent = oldparent
20+
self.until = until
2021

2122
try:
2223
git.repo.fun.rev_parse(self.repo, self.branch)
@@ -34,7 +35,16 @@ def __init__(self, branch: str, rebase_merges: bool, onto: bool, oldparent: str)
3435
"git-sim error: Please specify the parent of the commit to rebase ('oldparent')"
3536
)
3637
sys.exit(1)
37-
self.n = self.get_mainline_distance(oldparent, "HEAD")
38+
self.n = max(self.get_mainline_distance(self.oldparent, "HEAD"), self.n)
39+
40+
if self.until:
41+
self.until_n = self.get_mainline_distance(self.oldparent, self.until)
42+
else:
43+
if self.oldparent or self.until:
44+
print(
45+
"git-sim error: Please use --onto flag when specifying <oldparent> and <until>"
46+
)
47+
sys.exit(1)
3848

3949
if self.branch in [branch.name for branch in self.repo.heads]:
4050
self.selected_branches.append(self.branch)
@@ -44,7 +54,7 @@ def __init__(self, branch: str, rebase_merges: bool, onto: bool, oldparent: str)
4454
except TypeError:
4555
pass
4656

47-
self.cmd += f"{type(self).__name__.lower()}{' --rebase-merges' if self.rebase_merges else ''}{' --onto' if self.onto else ''} {self.branch}{' ' + self.oldparent if self.onto and self.oldparent else ''}"
57+
self.cmd += f"{type(self).__name__.lower()}{' --rebase-merges' if self.rebase_merges else ''}{' --onto' if self.onto else ''} {self.branch}{' ' + self.oldparent if self.onto and self.oldparent else ''}{' ' + self.until if self.onto and self.until else ''}"
4858

4959
self.alt_colors = {
5060
0: [m.BLUE_B, m.BLUE_E],
@@ -98,7 +108,12 @@ def construct(self):
98108
self.to_rebase = []
99109
for c in flat_default_commits:
100110
if self.branch not in self.repo.git.branch("--contains", c):
101-
self.to_rebase.append(c)
111+
if self.onto and self.until:
112+
range_commits = list(self.repo.iter_commits(f"{self.oldparent}...{self.until}"))
113+
if c in range_commits:
114+
self.to_rebase.append(c)
115+
else:
116+
self.to_rebase.append(c)
102117

103118
reached_base = False
104119
merge_base = self.repo.git.merge_base(self.branch, self.repo.active_branch.name)
@@ -145,7 +160,14 @@ def construct(self):
145160
self.draw_arrow_between_commits(tr.hexsha, rebased_shas[j - k], color=arrow_color)
146161

147162
if self.rebase_merges:
148-
self.reset_head_branch(rebased_sha_map[default_commits[0][0].hexsha])
163+
if self.onto and self.until:
164+
until_sha = self.get_commit(self.until).hexsha
165+
if until_sha == self.repo.head.commit.hexsha:
166+
self.reset_head_branch(rebased_sha_map[until_sha])
167+
else:
168+
self.reset_head(rebased_sha_map[until_sha])
169+
else:
170+
self.reset_head_branch(rebased_sha_map[default_commits[0][0].hexsha])
149171
else:
150172
self.reset_head_branch(parent)
151173
self.color_by(offset=2 * len(self.to_rebase))
@@ -194,21 +216,24 @@ def setup_and_draw_parent(
194216
)
195217
circle.shift(shift)
196218

197-
arrow_start_ends = []
219+
arrow_start_ends = set()
198220
arrows = []
199-
start = circle.get_center()
221+
start = tuple(circle.get_center())
200222
if not self.rebase_merges or branch_index == 0:
201-
end = self.drawnCommits[child].get_center()
202-
arrow_start_ends.append((start, end))
223+
end = tuple(self.drawnCommits[child].get_center())
224+
arrow_start_ends.add((start, end))
203225
if self.rebase_merges:
204226
for p in self.get_commit(orig).parents:
205227
if self.branch in self.repo.git.branch(
206228
"--contains", p
207-
) or p not in self.to_rebase:
229+
):
208230
continue
209231
try:
210-
end = self.drawnCommits[p.hexsha].get_center() + m.UP * 4 + (m.LEFT if settings.reverse else m.RIGHT) * len(default_commits[0]) * 2.5 + (m.LEFT * side_offset if settings.reverse else m.RIGHT * side_offset) * 5
211-
arrow_start_ends.append((start, end))
232+
if p not in self.to_rebase:
233+
end = tuple(self.drawnCommits[self.get_commit(self.branch).hexsha].get_center())
234+
else:
235+
end = tuple(self.drawnCommits[p.hexsha].get_center() + m.UP * 4 + (m.LEFT if settings.reverse else m.RIGHT) * len(default_commits[0]) * 2.5 + (m.LEFT * side_offset if settings.reverse else m.RIGHT * side_offset) * 5)
236+
arrow_start_ends.add((start, end))
212237
except KeyError:
213238
pass
214239

@@ -221,7 +246,7 @@ def setup_and_draw_parent(
221246
tip_shape=self.arrow_tip_shape,
222247
max_stroke_width_to_length_ratio=1000,
223248
)
224-
length = numpy.linalg.norm(start - end) - (1.5 if start[1] == end[1] else 3)
249+
length = numpy.linalg.norm(numpy.subtract(end, start)) - (1.5 if start[1] == end[1] else 3)
225250
arrow.set_length(length)
226251
arrows.append(arrow)
227252

0 commit comments

Comments
 (0)