@@ -15,15 +15,17 @@ const { shortSha } = require('./utils');
15
15
const isWindows = process . platform === 'win32' ;
16
16
17
17
class LandingSession extends Session {
18
- constructor ( cli , req , dir , prid , backport ) {
18
+ constructor ( cli , req , dir , prid , backport , autorebase ) {
19
19
super ( cli , dir , prid ) ;
20
20
this . req = req ;
21
21
this . backport = backport ;
22
+ this . autorebase = autorebase ;
22
23
}
23
24
24
25
get argv ( ) {
25
26
const args = super . argv ;
26
27
args . backport = this . backport ;
28
+ args . autorebase = this . autorebase ;
27
29
return args ;
28
30
}
29
31
@@ -130,6 +132,10 @@ class LandingSession extends Session {
130
132
return command ;
131
133
}
132
134
135
+ canAutomaticallyRebase ( subjects ) {
136
+ return subjects . every ( line => ! line . startsWith ( 'squash!' ) ) ;
137
+ }
138
+
133
139
async suggestAfterPatch ( patch ) {
134
140
const { cli } = this ;
135
141
const subjects = patch . match ( / S u b j e c t : \[ P A T C H .* ?\] .* / g) ;
@@ -155,14 +161,28 @@ class LandingSession extends Session {
155
161
return ;
156
162
}
157
163
return this . final ( ) ;
158
- }
159
-
160
- const suggestion = this . getRebaseSuggestion ( subjects ) ;
161
-
162
- cli . log ( `There are ${ subjects . length } commits in the PR` ) ;
163
- cli . log ( 'Please run the following commands to complete landing\n\n' +
164
- `$ ${ suggestion } \n` +
164
+ } else if ( this . autorebase && this . canAutomaticallyRebase ( subjects ) ) {
165
+ // Run git rebase in interactive mode with autosquash but without editor
166
+ // so that it will perform everything automatically.
167
+ cli . log ( `There are ${ subjects . length } commits in the PR. ` +
168
+ 'Attempring autorebase.' ) ;
169
+ const { upstream, branch } = this ;
170
+ const msgAmend = '-x "git-node land --amend"' ;
171
+ await runAsync ( 'git' ,
172
+ [ 'rebase' , `${ upstream } /${ branch } ` , '-i' , '--autosquash' , msgAmend ] ,
173
+ {
174
+ spawnArgs : {
175
+ shell : true ,
176
+ env : { ...process . env , GIT_SEQUENCE_EDITOR : ':' }
177
+ }
178
+ } ) ;
179
+ } else {
180
+ const suggestion = this . getRebaseSuggestion ( subjects ) ;
181
+ cli . log ( `There are ${ subjects . length } commits in the PR` ) ;
182
+ cli . log ( 'Please run the following commands to complete landing\n\n' +
183
+ `$ ${ suggestion } \n` +
165
184
'$ git node land --continue' ) ;
185
+ }
166
186
}
167
187
168
188
async apply ( ) {
0 commit comments