Skip to content

Commit 8c56ef8

Browse files
committed
updates based on feedback from sindresorhus
1 parent 045d3dc commit 8c56ef8

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

source/git-util.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ exports.currentBranch = async () => {
5555
};
5656

5757
exports.verifyCurrentBranchIsReleaseBranch = async releaseBranch => {
58-
const expectedBranch = await exports.defaultBranch(releaseBranch);
5958
const currentBranch = await exports.currentBranch();
60-
if (!expectedBranch === currentBranch) {
61-
throw new Error(`Not on \`${expectedBranch}\` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`);
59+
if (currentBranch !== releaseBranch) {
60+
throw new Error(`Not on \`${releaseBranch}\` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`);
6261
}
6362
};
6463

@@ -133,26 +132,30 @@ exports.tagExistsOnRemote = async tagName => {
133132
}
134133
};
135134

136-
exports.defaultBranch = async options => {
137-
if (options.releaseBranch) {
138-
return options.releaseBranch;
135+
async function hasLocalBranch(branch) {
136+
try {
137+
await execa('git', [
138+
'show-ref',
139+
'--verify',
140+
'--quiet',
141+
`refs/heads/${branch}`
142+
]);
143+
return true;
144+
} catch {
145+
return false;
139146
}
147+
}
140148

149+
exports.defaultBranch = async () => {
141150
for (const branch of ['main', 'master', 'gh-pages']) {
142-
try {
143-
// eslint-disable-next-line no-await-in-loop
144-
await execa('git', [
145-
'show-ref',
146-
'--verify',
147-
'--quiet',
148-
`refs/heads/${branch}`
149-
]);
151+
// eslint-disable-next-line no-await-in-loop
152+
if (await hasLocalBranch(branch)) {
150153
return branch;
151-
} catch {}
154+
}
152155
}
153156

154157
throw new Error(
155-
'Could not determine a default branch. Please specify one via --branch.'
158+
'Could not infer the default Git branch. Please specify one with the --branch flag or with an .np-config file.'
156159
);
157160
};
158161

source/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module.exports = async (input = 'patch', options) => {
4747
options.cleanup = false;
4848
}
4949

50+
options.releaseBranch = options.releaseBranch || git.defaultBranch();
51+
5052
const pkg = util.readPkg(options.contents);
5153
const runTests = options.tests && !options.yolo;
5254
const runCleanup = options.cleanup && !options.yolo;

source/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = async (options, pkg) => {
7272
const repoUrl = pkg.repository && githubUrlFromGit(pkg.repository.url, {extraBaseUrls});
7373
const pkgManager = options.yarn ? 'yarn' : 'npm';
7474
const registryUrl = await getRegistryUrl(pkgManager, pkg);
75+
const releaseBranch = options.releaseBranch || await git.defaultBranch();
7576

7677
if (options.runPublish) {
7778
checkIgnoreStrategy(pkg);
@@ -169,7 +170,6 @@ module.exports = async (options, pkg) => {
169170
}
170171
];
171172

172-
const releaseBranch = await git.defaultBranch(options.releaseBranch);
173173
const {hasCommits, releaseNotes} = await printCommitLog(repoUrl, registryUrl, releaseBranch);
174174

175175
if (options.version) {

0 commit comments

Comments
 (0)