@@ -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' ) {
@@ -172,14 +185,34 @@ class LandingSession extends Session {
172
185
}
173
186
174
187
return this . final ( ) ;
188
+ } else if ( this . autorebase && this . canAutomaticallyRebase ( subjects ) ) {
189
+ // Run git rebase in interactive mode with autosquash but without editor
190
+ // so that it will perform everything automatically.
191
+ cli . log ( `There are ${ subjects . length } commits in the PR. ` +
192
+ 'Attempting autorebase.' ) ;
193
+ const { upstream, branch } = this ;
194
+ const assumeYes = this . cli . assumeYes ? '--yes' : '' ;
195
+ const msgAmend = `-x "git node land --amend ${ assumeYes } "` ;
196
+ try {
197
+ await forceRunAsync ( 'git' ,
198
+ [ 'rebase' , `${ upstream } /${ branch } ` , '-i' , '--autosquash' , msgAmend ] ,
199
+ {
200
+ ignoreFailure : false ,
201
+ spawnArgs : {
202
+ shell : true ,
203
+ env : { ...process . env , GIT_SEQUENCE_EDITOR : ':' }
204
+ }
205
+ } ) ;
206
+ return this . final ( ) ;
207
+ } catch ( e ) {
208
+ await runAsync ( 'git' , [ 'rebase' , '--abort' ] ) ;
209
+ const count = subjects . length ;
210
+ cli . log ( `Couldn't rebase ${ count } commits in the PR automatically` ) ;
211
+ this . makeRebaseSuggestion ( subjects ) ;
212
+ }
213
+ } else {
214
+ this . makeRebaseSuggestion ( subjects ) ;
175
215
}
176
-
177
- const suggestion = this . getRebaseSuggestion ( subjects ) ;
178
-
179
- cli . log ( `There are ${ subjects . length } commits in the PR` ) ;
180
- cli . log ( 'Please run the following commands to complete landing\n\n' +
181
- `$ ${ suggestion } \n` +
182
- '$ git node land --continue' ) ;
183
216
}
184
217
185
218
async apply ( ) {
0 commit comments