Skip to content

fix: Don't fail when there are no releases #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,25 @@ export async function runPreReleaseCommand(
// Not running pre-release command
logger.warn('Not running the pre-release command: no command specified');
return false;
} else if (preReleaseCommand) {
}

// This is a workaround for the case when the old version is empty, which
// should only happen when the project is new and has no version yet.
// Instead of using an empty string, we use "0.0.0" as the old version to
// avoid breaking the pre-release command as most scripts expect a non-empty
// version string.
const nonEmptyOldVersion = oldVersion || '0.0.0';
if (preReleaseCommand) {
[sysCommand, ...args] = shellQuote.parse(preReleaseCommand) as string[];
} else {
sysCommand = '/bin/bash';
args = [DEFAULT_BUMP_VERSION_PATH];
}
args = [...args, oldVersion, newVersion];
logger.info(`Running the pre-release command...`);
args = [...args, nonEmptyOldVersion, newVersion];
logger.info('Running the pre-release command...');
const additionalEnv = {
CRAFT_NEW_VERSION: newVersion,
CRAFT_OLD_VERSION: oldVersion,
CRAFT_OLD_VERSION: nonEmptyOldVersion,
};
await spawnProcess(sysCommand, args, {
env: { ...process.env, ...additionalEnv },
Expand Down Expand Up @@ -354,7 +362,9 @@ async function prepareChangelog(
`Changelog policy is set to "${changelogPolicy}", nothing to do.`
);
return;
} else if (
}

if (
changelogPolicy !== ChangelogPolicy.Auto &&
changelogPolicy !== ChangelogPolicy.Simple
) {
Expand Down
33 changes: 25 additions & 8 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import simpleGit, { SimpleGit } from 'simple-git';
import simpleGit, { type SimpleGit, type LogOptions, type Options } from 'simple-git';

import { getConfigFileDir } from '../config';
import { ConfigurationError } from './errors';
Expand All @@ -20,6 +20,8 @@ export interface GitChange {
// https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit
export const PRExtractor = /(?<=\(#)\d+(?=\)$)/;

export const defaultInitialTag = '0.0.0';

export async function getDefaultBranch(
git: SimpleGit,
remoteName: string
Expand All @@ -34,16 +36,26 @@ export async function getDefaultBranch(
}

export async function getLatestTag(git: SimpleGit): Promise<string> {
// This part is courtesy of https://stackoverflow.com/a/7261049/90297
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
try {
// This part is courtesy of https://stackoverflow.com/a/7261049/90297
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
} catch (err) {
// If there are no tags, return an empty string
if (
err instanceof Error &&
err.message.startsWith('fatal: No names found')
) {
return '';
}
throw err;
}
}

export async function getChangesSince(
git: SimpleGit,
rev: string
): Promise<GitChange[]> {
const { all: commits } = await git.log({
from: rev,
const gitLogArgs: Options | LogOptions = {
to: 'HEAD',
// The symmetric option defaults to true, giving us all the different commits
// reachable from both `from` and `to` whereas what we are interested in is only the ones
Expand All @@ -57,7 +69,12 @@ export async function getChangesSince(
// this should still return all commits for individual repos when run from
// the repo root.
file: '.',
});
};

if (rev) {
gitLogArgs.from = rev;
}
const { all: commits } = await git.log(gitLogArgs);
return commits.map(commit => ({
hash: commit.hash,
title: commit.message,
Expand All @@ -71,7 +88,7 @@ export function stripRemoteName(
remoteName: string
): string {
const branchName = branch || '';
const remotePrefix = remoteName + '/';
const remotePrefix = `${remoteName}/`;
if (branchName.startsWith(remotePrefix)) {
return branchName.slice(remotePrefix.length);
}
Expand All @@ -82,7 +99,7 @@ export async function getGitClient(): Promise<SimpleGit> {
const configFileDir = getConfigFileDir() || '.';
// Move to the directory where the config file is located
process.chdir(configFileDir);
logger.debug(`Working directory:`, process.cwd());
logger.debug("Working directory:", process.cwd());

const git = simpleGit(configFileDir);
const isRepo = await git.checkIsRepo();
Expand Down
Loading