Skip to content

Commit

Permalink
feat: configurable path in git rev-list (#982)
Browse files Browse the repository at this point in the history
* feat: configurable path in git rev-list

* feat: configurable path in git rev-list

* chore: added tests for specified commitsPath
  • Loading branch information
b12k committed Mar 1, 2023
1 parent 0a23c55 commit 81a7d69
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,3 +1,7 @@
.idea
.vscode
*.log

node_modules
package-lock.json
tmp
1 change: 1 addition & 0 deletions config/release-it.json
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
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
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
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

0 comments on commit 81a7d69

Please sign in to comment.