@@ -15,15 +15,17 @@ const { shortSha } = require('./utils');
1515const isWindows = process . platform === 'win32' ;
1616
1717class LandingSession extends Session {
18- constructor ( cli , req , dir , prid , backport ) {
18+ constructor ( cli , req , dir , prid , backport , autorebase ) {
1919 super ( cli , dir , prid ) ;
2020 this . req = req ;
2121 this . backport = backport ;
22+ this . autorebase = autorebase ;
2223 }
2324
2425 get argv ( ) {
2526 const args = super . argv ;
2627 args . backport = this . backport ;
28+ args . autorebase = this . autorebase ;
2729 return args ;
2830 }
2931
@@ -130,6 +132,10 @@ class LandingSession extends Session {
130132 return command ;
131133 }
132134
135+ canAutomaticallyRebase ( subjects ) {
136+ return subjects . every ( line => ! line . startsWith ( 'squash!' ) ) ;
137+ }
138+
133139 async suggestAfterPatch ( patch ) {
134140 const { cli } = this ;
135141 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 {
155161 return ;
156162 }
157163 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` +
165184 '$ git node land --continue' ) ;
185+ }
166186 }
167187
168188 async apply ( ) {
0 commit comments