11
11
12
12
13
13
class Rebase (GitSimBaseCommand ):
14
- def __init__ (self , branch : str , rebase_merges : bool ):
14
+ def __init__ (self , branch : str , rebase_merges : bool , onto : bool , oldparent : str ):
15
15
super ().__init__ ()
16
16
self .branch = branch
17
17
self .rebase_merges = rebase_merges
18
+ self .onto = onto
19
+ self .oldparent = oldparent
18
20
19
21
try :
20
22
git .repo .fun .rev_parse (self .repo , self .branch )
@@ -26,6 +28,14 @@ def __init__(self, branch: str, rebase_merges: bool):
26
28
)
27
29
sys .exit (1 )
28
30
31
+ if self .onto :
32
+ if not self .oldparent :
33
+ print (
34
+ "git-sim error: Please specify the parent of the commit to rebase ('oldparent')"
35
+ )
36
+ sys .exit (1 )
37
+ self .n = self .get_mainline_distance (oldparent , "HEAD" )
38
+
29
39
if self .branch in [branch .name for branch in self .repo .heads ]:
30
40
self .selected_branches .append (self .branch )
31
41
@@ -34,7 +44,7 @@ def __init__(self, branch: str, rebase_merges: bool):
34
44
except TypeError :
35
45
pass
36
46
37
- self .cmd += f"{ type (self ).__name__ .lower ()} { self .branch } "
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 '' } "
38
48
39
49
self .alt_colors = {
40
50
0 : [m .BLUE_B , m .BLUE_E ],
@@ -81,25 +91,25 @@ def construct(self):
81
91
self .get_default_commits (self .get_commit (), default_commits )
82
92
flat_default_commits = self .sort_and_flatten (default_commits )
83
93
84
- reached_base = False
85
- merge_base = self .repo .git .merge_base (self .branch , self .repo .active_branch .name )
86
- if merge_base in self .drawnCommits :
87
- reached_base = True
88
-
89
94
self .parse_commits (head_commit , shift = 4 * m .DOWN )
90
95
self .parse_all ()
91
96
self .center_frame_on_commit (branch_commit )
92
97
93
- to_rebase = []
98
+ self . to_rebase = []
94
99
for c in flat_default_commits :
95
100
if self .branch not in self .repo .git .branch ("--contains" , c ):
96
- to_rebase .append (c )
101
+ self .to_rebase .append (c )
102
+
103
+ reached_base = False
104
+ merge_base = self .repo .git .merge_base (self .branch , self .repo .active_branch .name )
105
+ if merge_base in self .drawnCommits or (self .onto and self .to_rebase [- 1 ].hexsha in self .drawnCommits ):
106
+ reached_base = True
97
107
98
108
parent = branch_commit .hexsha
99
109
branch_counts = {}
100
110
rebased_shas = []
101
111
rebased_sha_map = {}
102
- for j , tr in enumerate (reversed (to_rebase )):
112
+ for j , tr in enumerate (reversed (self . to_rebase )):
103
113
if not self .rebase_merges :
104
114
if len (tr .parents ) > 1 :
105
115
continue
@@ -121,7 +131,7 @@ def construct(self):
121
131
122
132
branch_counts = {}
123
133
k = 0
124
- for j , tr in enumerate (reversed (to_rebase )):
134
+ for j , tr in enumerate (reversed (self . to_rebase )):
125
135
if not self .rebase_merges :
126
136
if len (tr .parents ) > 1 :
127
137
k += 1
@@ -138,7 +148,7 @@ def construct(self):
138
148
self .reset_head_branch (rebased_sha_map [default_commits [0 ][0 ].hexsha ])
139
149
else :
140
150
self .reset_head_branch (parent )
141
- self .color_by (offset = 2 * len (to_rebase ))
151
+ self .color_by (offset = 2 * len (self . to_rebase ))
142
152
self .show_command_as_title ()
143
153
self .fadeout ()
144
154
self .show_outro ()
@@ -161,10 +171,21 @@ def setup_and_draw_parent(
161
171
fill_opacity = 0.25 ,
162
172
)
163
173
circle .height = 1
164
- if self .rebase_merges and branch_index > 0 :
174
+ side_offset = 0
175
+ num_branch_index_0_to_rebase = 0
176
+ for commit in default_commits [0 ]:
177
+ if commit in self .to_rebase :
178
+ num_branch_index_0_to_rebase += 1
179
+ if self .rebase_merges :
180
+ for bi in default_commits :
181
+ if bi > 0 :
182
+ if len (default_commits [bi ]) >= num_branch_index_0_to_rebase :
183
+ side_offset = len (default_commits [bi ]) - num_branch_index_0_to_rebase + 1
184
+
185
+ if self .rebase_merges :
165
186
circle .move_to (
166
187
self .drawnCommits [orig ].get_center (),
167
- ).shift (m .UP * 4 + (m .LEFT if settings .reverse else m .RIGHT ) * len (default_commits [0 ]) * 2.5 )
188
+ ).shift (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 )
168
189
else :
169
190
circle .next_to (
170
191
self .drawnCommits [child ],
@@ -183,10 +204,10 @@ def setup_and_draw_parent(
183
204
for p in self .get_commit (orig ).parents :
184
205
if self .branch in self .repo .git .branch (
185
206
"--contains" , p
186
- ):
207
+ ) or p not in self . to_rebase :
187
208
continue
188
209
try :
189
- end = self .drawnCommits [p .hexsha ].get_center () + m .UP * 4 + (m .LEFT if settings .reverse else m .RIGHT ) * len (default_commits [0 ]) * 2.5
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
190
211
arrow_start_ends .append ((start , end ))
191
212
except KeyError :
192
213
pass
@@ -257,6 +278,8 @@ def get_default_commits(self, commit, default_commits, branch_index=0):
257
278
if branch_index not in default_commits :
258
279
default_commits [branch_index ] = []
259
280
if len (default_commits [branch_index ]) < self .n :
281
+ if self .onto and commit .hexsha == self .get_commit (self .oldparent ).hexsha :
282
+ return default_commits
260
283
if commit not in self .sort_and_flatten (default_commits ) and self .branch not in self .repo .git .branch (
261
284
"--contains" , commit
262
285
):
0 commit comments