@@ -8,7 +8,7 @@ import {GitClient} from './git/git-client';
8
8
import { getGithubBranchCommitsUrl } from './git/github-urls' ;
9
9
import { promptForNewVersion } from './prompt/new-version-prompt' ;
10
10
import { parseVersionName , Version } from './version-name/parse-version' ;
11
- import { getExpectedPublishBranch } from './version-name/publish-branch ' ;
11
+ import { getAllowedPublishBranches } from './version-name/publish-branches ' ;
12
12
13
13
/** Default filename for the changelog. */
14
14
const CHANGELOG_FILE_NAME = 'CHANGELOG.md' ;
@@ -84,20 +84,22 @@ class StageReleaseTask {
84
84
console . log ( ) ;
85
85
86
86
const newVersion = await promptForNewVersion ( this . currentVersion ) ;
87
- const expectedPublishBranch = getExpectedPublishBranch ( newVersion ) ;
87
+ const newVersionName = newVersion . format ( ) ;
88
+ const stagingBranch = `release-stage/${ newVersionName } ` ;
88
89
89
90
// After the prompt for the new version, we print a new line because we want the
90
91
// new log messages to be more in the foreground.
91
92
console . log ( ) ;
92
93
94
+ // Ensure there are no uncommitted changes. Checking this before switching to a
95
+ // publish branch is sufficient as unstaged changes are not specific to Git branches.
93
96
this . verifyNoUncommittedChanges ( ) ;
94
- this . switchToPublishBranch ( expectedPublishBranch ) ;
95
97
96
- this . verifyLocalCommitsMatchUpstream ( expectedPublishBranch ) ;
97
- await this . verifyPassingGithubStatus ( expectedPublishBranch ) ;
98
+ // Branch that will be used to stage the release for the new selected version.
99
+ const publishBranch = this . switchToPublishBranch ( newVersion ) ;
98
100
99
- const newVersionName = newVersion . format ( ) ;
100
- const stagingBranch = `release-stage/ ${ newVersionName } ` ;
101
+ this . verifyLocalCommitsMatchUpstream ( publishBranch ) ;
102
+ await this . verifyPassingGithubStatus ( publishBranch ) ;
101
103
102
104
if ( ! this . git . checkoutNewBranch ( stagingBranch ) ) {
103
105
console . error ( red ( `Could not create release staging branch: ${ stagingBranch } . Aborting...` ) ) ;
@@ -146,24 +148,39 @@ class StageReleaseTask {
146
148
* Checks if the user is on the expected publish branch. If the user is on a different branch,
147
149
* this function automatically tries to checkout the publish branch.
148
150
*/
149
- private switchToPublishBranch ( expectedPublishBranch : string ) : boolean {
151
+ private switchToPublishBranch ( newVersion : Version ) : string {
152
+ const allowedBranches = getAllowedPublishBranches ( newVersion ) ;
150
153
const currentBranchName = this . git . getCurrentBranch ( ) ;
151
154
152
- // If current branch already matches the expected publish branch, just continue
153
- // by exiting this function.
154
- if ( expectedPublishBranch === currentBranchName ) {
155
- return ;
155
+ // If current branch already matches one of the allowed publish branches, just continue
156
+ // by exiting this function and returning the currently used publish branch.
157
+ if ( allowedBranches . includes ( currentBranchName ) ) {
158
+ console . log ( green ( ` ✓ Using the "${ italic ( currentBranchName ) } " branch.` ) ) ;
159
+ return currentBranchName ;
156
160
}
157
161
158
- if ( ! this . git . checkoutBranch ( expectedPublishBranch ) ) {
159
- console . error ( red ( ` ✘ Could not switch to the "${ italic ( expectedPublishBranch ) } " ` +
162
+ // In case there are multiple allowed publish branches for this version, we just
163
+ // exit and let the user decide which branch they want to release from.
164
+ if ( allowedBranches . length !== 1 ) {
165
+ console . warn ( yellow ( ' ✘ You are not on an allowed publish branch.' ) ) ;
166
+ console . warn ( yellow ( ` Please switch to one of the following branches: ` +
167
+ `${ allowedBranches . join ( ', ' ) } ` ) ) ;
168
+ process . exit ( 0 ) ;
169
+ }
170
+
171
+ // For this version there is only *one* allowed publish branch, so we could
172
+ // automatically switch to that branch in case the user isn't on it yet.
173
+ const defaultPublishBranch = allowedBranches [ 0 ] ;
174
+
175
+ if ( ! this . git . checkoutBranch ( defaultPublishBranch ) ) {
176
+ console . error ( red ( ` ✘ Could not switch to the "${ italic ( defaultPublishBranch ) } " ` +
160
177
`branch.` ) ) ;
161
178
console . error ( red ( ` Please ensure that the branch exists or manually switch to the ` +
162
179
`branch.` ) ) ;
163
180
process . exit ( 1 ) ;
164
181
}
165
182
166
- console . log ( green ( ` ✓ Switched to the "${ italic ( expectedPublishBranch ) } " branch.` ) ) ;
183
+ console . log ( green ( ` ✓ Switched to the "${ italic ( defaultPublishBranch ) } " branch.` ) ) ;
167
184
}
168
185
169
186
/** Verifies that the local branch is up to date with the given publish branch. */
0 commit comments