1111
1212
1313class 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