diff --git a/scripts/bump-version.sh b/scripts/bump-version.sh index 312f68e10..9648b5722 100644 --- a/scripts/bump-version.sh +++ b/scripts/bump-version.sh @@ -12,4 +12,5 @@ export npm_config_git_tag_version=false NPM_VERSION=$(npm version $NEW_VERSION) VERSION=${NPM_VERSION:1} +echo "Previous version: $OLD_VERSION" echo "New version: $VERSION" diff --git a/src/commands/__tests__/prepare.test.ts b/src/commands/__tests__/prepare.test.ts index 58a248e53..d05f29da2 100644 --- a/src/commands/__tests__/prepare.test.ts +++ b/src/commands/__tests__/prepare.test.ts @@ -5,6 +5,7 @@ import { runPreReleaseCommand } from '../prepare'; jest.mock('../../utils/system'); describe('runPreReleaseCommand', () => { + const oldVersion = '2.3.3'; const newVersion = '2.3.4'; const mockedSpawnProcess = spawnProcess as jest.Mock; @@ -15,16 +16,16 @@ describe('runPreReleaseCommand', () => { test('runs with default command', async () => { expect.assertions(1); - await runPreReleaseCommand(newVersion); + await runPreReleaseCommand(oldVersion, newVersion); expect(mockedSpawnProcess).toBeCalledWith( '/bin/bash', - [pathJoin('scripts', 'bump-version.sh'), '', newVersion], + [pathJoin('scripts', 'bump-version.sh'), oldVersion, newVersion], { env: { ...process.env, CRAFT_NEW_VERSION: newVersion, - CRAFT_OLD_VERSION: '', + CRAFT_OLD_VERSION: oldVersion, }, } ); @@ -34,18 +35,19 @@ describe('runPreReleaseCommand', () => { expect.assertions(1); await runPreReleaseCommand( + oldVersion, newVersion, 'python ./increase_version.py "argument 1"' ); expect(mockedSpawnProcess).toBeCalledWith( 'python', - ['./increase_version.py', 'argument 1', '', newVersion], + ['./increase_version.py', 'argument 1', oldVersion, newVersion], { env: { ...process.env, CRAFT_NEW_VERSION: newVersion, - CRAFT_OLD_VERSION: '', + CRAFT_OLD_VERSION: oldVersion, }, } ); diff --git a/src/commands/prepare.ts b/src/commands/prepare.ts index 33f3b9b58..743e9ee06 100644 --- a/src/commands/prepare.ts +++ b/src/commands/prepare.ts @@ -24,7 +24,7 @@ import { handleGlobalError, reportError, } from '../utils/errors'; -import { getGitClient, getDefaultBranch } from '../utils/git'; +import { getGitClient, getDefaultBranch, getLatestTag } from '../utils/git'; import { isDryRun, promptConfirmation } from '../utils/helpers'; import { formatJson } from '../utils/strings'; import { spawnProcess } from '../utils/system'; @@ -141,7 +141,7 @@ async function createReleaseBranch( const branchPrefix = releaseBranchPrefix || DEFAULT_RELEASE_BRANCH_NAME; const branchName = `${branchPrefix}/${newVersion}`; - const branchHead = await git.raw(['show-ref', '--heads', branchName]); + const branchHead = await git.raw('show-ref', '--heads', branchName); // in case `show-ref` can't find a branch it returns `null` if (branchHead) { @@ -227,6 +227,7 @@ async function commitNewVersion( * @param preReleaseCommand Custom pre-release command */ export async function runPreReleaseCommand( + oldVersion: string, newVersion: string, preReleaseCommand?: string ): Promise { @@ -242,11 +243,11 @@ export async function runPreReleaseCommand( sysCommand = '/bin/bash'; args = [DEFAULT_BUMP_VERSION_PATH]; } - args = [...args, '', newVersion]; + args = [...args, oldVersion, newVersion]; logger.info(`Running the pre-release command...`); const additionalEnv = { CRAFT_NEW_VERSION: newVersion, - CRAFT_OLD_VERSION: '', + CRAFT_OLD_VERSION: oldVersion, }; await spawnProcess(sysCommand, args, { env: { ...process.env, ...additionalEnv }, @@ -472,8 +473,7 @@ export async function prepareMain(argv: PrepareOptions): Promise { logger.info(`Preparing to release the version: ${newVersion}`); - // Create a new release branch and check it out. Throw an error if it already - // exists. + // Create a new release branch and check it out. Fail if it already exists. const branchName = await createReleaseBranch( git, rev, @@ -482,6 +482,13 @@ export async function prepareMain(argv: PrepareOptions): Promise { config.releaseBranchPrefix ); + // Do this once we are on the release branch as we might be releasing from + // a custom revision and it is harder to tell git to give us the tag right + // before a specific revision. + // TL;DR - WARNING: + // The order matters here, do not move this command above craeteReleaseBranch! + const oldVersion = await getLatestTag(git); + // Check & update the changelog await prepareChangelog( newVersion, @@ -491,6 +498,7 @@ export async function prepareMain(argv: PrepareOptions): Promise { // Run a pre-release script (e.g. for version bumping) const preReleaseCommandRan = await runPreReleaseCommand( + oldVersion, newVersion, config.preReleaseCommand ); diff --git a/src/utils/git.ts b/src/utils/git.ts index 8cdcbcc5c..b71ab09dd 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -17,6 +17,11 @@ export async function getDefaultBranch( ); } +export async function getLatestTag(git: SimpleGit): Promise { + // This part is courtesy of https://stackoverflow.com/a/7261049/90297 + return (await git.raw('describe', '--tags', '--abbrev=0')).trim(); +} + export function stripRemoteName( branch: string | undefined, remoteName: string