@@ -16,15 +16,17 @@ const { shortSha } = require('./utils');
16
16
const isWindows = process . platform === 'win32' ;
17
17
18
18
class LandingSession extends Session {
19
- constructor ( cli , req , dir , prid , backport ) {
19
+ constructor ( cli , req , dir , prid , backport , autorebase ) {
20
20
super ( cli , dir , prid ) ;
21
21
this . req = req ;
22
22
this . backport = backport ;
23
+ this . autorebase = autorebase ;
23
24
}
24
25
25
26
get argv ( ) {
26
27
const args = super . argv ;
27
28
args . backport = this . backport ;
29
+ args . autorebase = this . autorebase ;
28
30
return args ;
29
31
}
30
32
@@ -131,6 +133,17 @@ class LandingSession extends Session {
131
133
return command ;
132
134
}
133
135
136
+ makeRebaseSuggestion ( subjects ) {
137
+ const suggestion = this . getRebaseSuggestion ( subjects ) ;
138
+ this . cli . log ( 'Please run the following commands to complete landing\n\n' +
139
+ `$ ${ suggestion } \n` +
140
+ '$ git node land --continue' ) ;
141
+ }
142
+
143
+ canAutomaticallyRebase ( subjects ) {
144
+ return subjects . every ( line => ! line . startsWith ( 'squash!' ) ) ;
145
+ }
146
+
134
147
async validateLint ( ) {
135
148
// The linter is currently only run on non-Windows platforms.
136
149
if ( os . platform ( ) === 'win32' ) {
@@ -168,14 +181,34 @@ class LandingSession extends Session {
168
181
}
169
182
170
183
return this . final ( ) ;
184
+ } else if ( this . autorebase && this . canAutomaticallyRebase ( subjects ) ) {
185
+ // Run git rebase in interactive mode with autosquash but without editor
186
+ // so that it will perform everything automatically.
187
+ cli . log ( `There are ${ subjects . length } commits in the PR. ` +
188
+ 'Attempting autorebase.' ) ;
189
+ const { upstream, branch } = this ;
190
+ const assumeYes = this . cli . assumeYes ? '--yes' : '' ;
191
+ const msgAmend = `-x "git node land --amend ${ assumeYes } "` ;
192
+ try {
193
+ await forceRunAsync ( 'git' ,
194
+ [ 'rebase' , `${ upstream } /${ branch } ` , '-i' , '--autosquash' , msgAmend ] ,
195
+ {
196
+ ignoreFailure : false ,
197
+ spawnArgs : {
198
+ shell : true ,
199
+ env : { ...process . env , GIT_SEQUENCE_EDITOR : ':' }
200
+ }
201
+ } ) ;
202
+ return this . final ( ) ;
203
+ } catch ( e ) {
204
+ await runAsync ( 'git' , [ 'rebase' , '--abort' ] ) ;
205
+ const count = subjects . length ;
206
+ cli . log ( `Couldn't rebase ${ count } commits in the PR automatically` ) ;
207
+ this . makeRebaseSuggestion ( subjects ) ;
208
+ }
209
+ } else {
210
+ this . makeRebaseSuggestion ( subjects ) ;
171
211
}
172
-
173
- const suggestion = this . getRebaseSuggestion ( subjects ) ;
174
-
175
- cli . log ( `There are ${ subjects . length } commits in the PR` ) ;
176
- cli . log ( 'Please run the following commands to complete landing\n\n' +
177
- `$ ${ suggestion } \n` +
178
- '$ git node land --continue' ) ;
179
212
}
180
213
181
214
async apply ( ) {
0 commit comments