Skip to content
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

feat: configurable path in git rev-list #982

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea
.vscode
*.log

node_modules
package-lock.json
tmp
1 change: 1 addition & 0 deletions config/release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"requireBranch": false,
"requireUpstream": true,
"requireCommits": false,
"commitsPath": "",
"addUntrackedFiles": false,
"commit": true,
"commitMessage": "Release ${version}",
Expand Down
6 changes: 3 additions & 3 deletions lib/plugin/git/Git.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Git extends GitBase {
if (this.options.requireUpstream && !(await this.hasUpstreamBranch())) {
throw e(`No upstream configured for current branch.${EOL}Please set an upstream branch.`, docs);
}
if (this.options.requireCommits && (await this.getCommitsSinceLatestTag()) === 0) {
if (this.options.requireCommits && (await this.getCommitsSinceLatestTag(this.options.commitsPath)) === 0) {
throw e(`There are no commits since the latest tag.`, docs);
}
}
Expand Down Expand Up @@ -120,10 +120,10 @@ class Git extends GitBase {
);
}

async getCommitsSinceLatestTag() {
async getCommitsSinceLatestTag(commitsPath = '') {
const latestTagName = await this.getLatestTagName();
const ref = latestTagName ? `${latestTagName}..HEAD` : 'HEAD';
return this.exec(`git rev-list ${ref} --count`, { options }).then(Number);
return this.exec(`git rev-list ${ref} --count ${commitsPath}`, { options }).then(Number);
}

async getUpstreamArgs(pushRepo) {
Expand Down
16 changes: 16 additions & 0 deletions test/git.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ test.serial('should not throw if there are commits', async t => {
await t.notThrowsAsync(gitClient.init());
});

test.serial('should throw if there are no commits in specified path', async t => {
const options = { git: { requireCommits: true, commitsPath: 'dir' } };
const gitClient = factory(Git, { options });
sh.mkdir('dir');
sh.exec('git tag 1.0.0');
await t.throwsAsync(gitClient.init(), { message: /^There are no commits since the latest tag/ });
});

test.serial('should not throw if there are commits in specified path', async t => {
const options = { git: { requireCommits: true, commitsPath: 'dir' } };
const gitClient = factory(Git, { options });
sh.exec('git tag 1.0.0');
gitAdd('line', 'dir/file', 'Add file');
await t.notThrowsAsync(gitClient.init());
});

test.serial('should not throw if there are no tags', async t => {
const options = { git: { requireCommits: true } };
const gitClient = factory(Git, { options });
Expand Down
11 changes: 8 additions & 3 deletions test/util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ const mkTmpDir = () => {

const readFile = file => fs.promises.readFile(path.resolve(file), 'utf8');

const gitAdd = (content, file, message) => {
sh.ShellString(content).toEnd(file);
sh.exec(`git add ${file}`);
const gitAdd = (content, filePath, message) => {
const pathSegments = filePath.split('/').filter(Boolean);
pathSegments.pop();
if (pathSegments.length) {
sh.mkdir('-p', pathSegments.join('/'));
}
sh.ShellString(content).toEnd(filePath);
sh.exec(`git add ${filePath}`);
const { stdout } = sh.exec(`git commit -m "${message}"`);
const match = stdout.match(/\[.+([a-z0-9]{7})\]/);
return match ? match[1] : null;
Expand Down