@@ -20,13 +20,16 @@ const CHANGELOG_FILE_NAME = 'CHANGELOG.md';
20
20
*
21
21
* 1) Prompt for release type (with version suggestion)
22
22
* 2) Prompt for version name if no suggestions has been selected
23
- * 3) Assert that the proper publish branch is checked out (e.g. 6.4.x for patches)
24
- * 4) Assert that there are no local changes which are uncommitted.
25
- * 5) Assert that the local branch is up to date with the remote branch.
26
- * 6) Creates a new branch for the release staging (release-stage/{VERSION})
27
- * 7) Switches to the staging branch and updates the package.json
28
- * 8) Waits for the user to continue (users can generate the changelog in the meanwhile)
29
- * 9) Create a commit that includes all changes in the staging branch.
23
+ * 3) Assert that there are no local changes which are uncommitted.
24
+ * 4) Assert that the proper publish branch is checked out. (e.g. 6.4.x for patches)
25
+ * If a different branch is used, try switching to the publish branch automatically
26
+ * 5) Assert that the Github status checks pass for the publish branch.
27
+ * 6) Assert that the local branch is up to date with the remote branch.
28
+ * 7) Creates a new branch for the release staging (release-stage/{VERSION})
29
+ * 8) Switches to the staging branch and updates the package.json
30
+ * 9) Prompt for release name and generate changelog
31
+ * 10) Wait for the user to continue (users can customize generated changelog)
32
+ * 11) Create a commit that includes all changes in the staging branch.
30
33
*/
31
34
class StageReleaseTask {
32
35
@@ -86,9 +89,10 @@ class StageReleaseTask {
86
89
// new log messages to be more in the foreground.
87
90
console . log ( ) ;
88
91
89
- this . verifyPublishBranch ( expectedPublishBranch ) ;
90
- this . verifyLocalCommitsMatchUpstream ( expectedPublishBranch ) ;
91
92
this . verifyNoUncommittedChanges ( ) ;
93
+ this . switchToPublishBranch ( expectedPublishBranch ) ;
94
+
95
+ this . verifyLocalCommitsMatchUpstream ( expectedPublishBranch ) ;
92
96
await this . verifyPassingGithubStatus ( ) ;
93
97
94
98
const newVersionName = newVersion . format ( ) ;
@@ -137,16 +141,28 @@ class StageReleaseTask {
137
141
// TODO(devversion): automatic push and PR open URL shortcut.
138
142
}
139
143
140
- /** Verifies that the user is on the specified publish branch. */
141
- private verifyPublishBranch ( expectedPublishBranch : string ) {
144
+ /**
145
+ * Checks if the user is on the expected publish branch. If the user is on a different branch,
146
+ * this function automatically tries to checkout the publish branch.
147
+ */
148
+ private switchToPublishBranch ( expectedPublishBranch : string ) : boolean {
142
149
const currentBranchName = this . git . getCurrentBranch ( ) ;
143
150
144
- // Check if current branch matches the expected publish branch.
145
- if ( expectedPublishBranch !== currentBranchName ) {
146
- console . error ( red ( ` ✘ Cannot stage release from "${ italic ( currentBranchName ) } ". Please ` +
147
- `stage the release from "${ bold ( expectedPublishBranch ) } ".` ) ) ;
151
+ // If current branch already matches the expected publish branch, just continue
152
+ // by exiting this function.
153
+ if ( expectedPublishBranch === currentBranchName ) {
154
+ return ;
155
+ }
156
+
157
+ if ( ! this . git . checkoutBranch ( expectedPublishBranch ) ) {
158
+ console . error ( red ( ` ✘ Could not switch to the "${ italic ( expectedPublishBranch ) } " ` +
159
+ `branch.` ) ) ;
160
+ console . error ( red ( ` Please ensure that the branch exists or manually switch to the ` +
161
+ `branch.` ) ) ;
148
162
process . exit ( 1 ) ;
149
163
}
164
+
165
+ console . log ( green ( ` ✓ Switched to the "${ italic ( expectedPublishBranch ) } " branch.` ) ) ;
150
166
}
151
167
152
168
/** Verifies that the local branch is up to date with the given publish branch. */
@@ -166,8 +182,8 @@ class StageReleaseTask {
166
182
/** Verifies that there are no uncommitted changes in the project. */
167
183
private verifyNoUncommittedChanges ( ) {
168
184
if ( this . git . hasUncommittedChanges ( ) ) {
169
- console . error ( red ( ` ✘ Cannot stage release. There are changes which are not committed and ` +
170
- `should be stashed .` ) ) ;
185
+ console . error ( red ( ` ✘ Cannot stage release. There are changes which are not committed ` +
186
+ `and should be discarded .` ) ) ;
171
187
process . exit ( 1 ) ;
172
188
}
173
189
}
0 commit comments