Skip to content

Commit

Permalink
feat(cli): create log from different branch
Browse files Browse the repository at this point in the history
* cmds, allow log revision range
  • Loading branch information
cdcabrera committed Jan 29, 2024
1 parent aa47a6c commit bf7e65d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ or Yarn
commit [boolean] [default: true]
-d, --date [CHANGELOG.md] release date in the form of a valid date
string. Uses system new Date([your date])
[string] [default: "2023-01-03T02:22:35.984Z"]
[string] [default: "2024-01-29T07:40:54.615Z"]
-n, --non-cc Allow non-conventional commits to apply a semver weight
and appear in [CHANGELOG.md] under a general type
description. [boolean] [default: false]
-o, --override Use a version you define. [string]
-r, --dry-run Generate [CHANGELOG.md] sample output
[boolean] [default: false]
--branch The local branch used to run `$ git log [branch]`
against, defaults to HEAD, or just `$ git log`
[string] [default: "HEAD"]
--changelog Changelog output filename and relative path
[string] [default: "./CHANGELOG.md"]
--commit-path [CHANGELOG.md] path used for commits. This will be
Expand Down
15 changes: 12 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { commitChangelog, OPTIONS } = require('../src');
*/
const {
basic: isBasic,
branch: releaseBranch,
changelog: changelogFile,
commit: isCommit,
'commit-path': commitPath,
Expand Down Expand Up @@ -65,6 +66,12 @@ const {
describe: 'Generate [CHANGELOG.md] sample output',
type: 'boolean'
})
.option('branch', {
default: 'HEAD',
describe: 'The local branch used to run `$ git log [branch]` against, defaults to HEAD, or just `$ git log`',
type: 'string',
requiresArg: true
})
.option('changelog', {
default: './CHANGELOG.md',
describe: 'Changelog output filename and relative path',
Expand Down Expand Up @@ -113,9 +120,10 @@ const {
* Set global OPTIONS
*
* @type {{comparePath: string, date: string, packagePath: Function, isOverrideVersion: boolean,
* packageFile: string, releaseDescription: string, isAllowNonConventionalCommits: boolean, prPath: string,
* isCommit: boolean, overrideVersion: string|*, changelogPath: Function, commitPath: string,
* changelogFile: string, releaseTypeScope: string[]|string, isDryRun: boolean, remoteUrl: string,
* packageFile: string, releaseDescription: string, isAllowNonConventionalCommits: boolean,
* releaseBranch: string, prPath: string, isCommit: boolean, overrideVersion: string|*,
* changelogPath: Function, commitPath: string, changelogFile: string,
* releaseTypeScope: string[]|string, isDryRun: boolean, remoteUrl: string,
* isBasic: boolean}}
* @private
*/
Expand All @@ -138,6 +146,7 @@ OPTIONS._set = {
return join(this.contextPath, packageFile);
},
prPath,
releaseBranch,
releaseDescription,
releaseTypeScope,
remoteUrl
Expand Down
10 changes: 8 additions & 2 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Functions for `git`, `package.json` version, and more
* [~getCurrentVersion(options)](#module_Commands..getCurrentVersion) ⇒ <code>\*</code>
* [~getReleaseCommit(options)](#module_Commands..getReleaseCommit) ⇒ <code>string</code>
* [~getRemoteUrls(options)](#module_Commands..getRemoteUrls) ⇒ <code>Object</code>
* [~getGit(settings)](#module_Commands..getGit) ⇒ <code>Array</code>
* [~getGit(options, settings)](#module_Commands..getGit) ⇒ <code>Array</code>
* [~getGitLog(commitHash, searchFilter)](#module_Commands..getGit..getGitLog) ⇒ <code>string</code>
* [~getOverrideVersion(options)](#module_Commands..getOverrideVersion) ⇒ <code>Object</code>
* [~getVersion(versionBump, settings)](#module_Commands..getVersion) ⇒ <code>string</code>
Expand Down Expand Up @@ -119,6 +119,8 @@ Get last release commit hash
<td>options</td><td><code>object</code></td>
</tr><tr>
<td>options.releaseTypeScope</td><td><code>Array.&lt;string&gt;</code> | <code>string</code></td>
</tr><tr>
<td>options.releaseBranch</td><td><code>string</code> | <code>undefined</code></td>
</tr> </tbody>
</table>

Expand Down Expand Up @@ -150,7 +152,7 @@ Get the repositories remote

<a name="module_Commands..getGit"></a>

### Commands~getGit(settings) ⇒ <code>Array</code>
### Commands~getGit(options, settings) ⇒ <code>Array</code>
Return output for a range of commits from a hash

**Kind**: inner method of [<code>Commands</code>](#module_Commands)
Expand All @@ -162,6 +164,10 @@ Return output for a range of commits from a hash
</thead>
<tbody>
<tr>
<td>options</td><td><code>object</code></td>
</tr><tr>
<td>options.releaseBranch</td><td><code>string</code> | <code>undefined</code></td>
</tr><tr>
<td>settings</td><td><code>object</code></td>
</tr><tr>
<td>settings.getReleaseCommit</td><td><code>function</code></td>
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/__snapshots__/cmds.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`Commands should attempt to run commands: commands 1`] = `
},
},
{
"getReleaseCommit": "<execSync>["git log --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
"getReleaseCommit": "<execSync>["git log HEAD --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
},
{
"getRemoteUrls": {
Expand All @@ -45,20 +45,20 @@ exports[`Commands should attempt to run commands: commands 1`] = `
exports[`Commands should handle multiple formats for releaseTypeScope: releaseTypeScope, multi-list item 1`] = `
{
"commitFiles": "<execSync>["git add ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md && git commit ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md -m \\"chore(release): undefined\\""]</execSync>",
"getReleaseCommit": "<execSync>["git log --grep=\\"dolor sit\\" --pretty=oneline -1"]</execSync>",
"getReleaseCommit": "<execSync>["git log HEAD --grep=\\"dolor sit\\" --pretty=oneline -1"]</execSync>",
}
`;

exports[`Commands should handle multiple formats for releaseTypeScope: releaseTypeScope, single list item 1`] = `
{
"commitFiles": "<execSync>["git add ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md && git commit ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md -m \\"chore(release): undefined\\""]</execSync>",
"getReleaseCommit": "<execSync>["git log --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
"getReleaseCommit": "<execSync>["git log HEAD --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
}
`;

exports[`Commands should handle multiple formats for releaseTypeScope: releaseTypeScope, string 1`] = `
{
"commitFiles": "<execSync>["git add ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md && git commit ../src/__fixtures__/package.json ../src/__fixtures__/CHANGELOG.md -m \\"chore(release): undefined\\""]</execSync>",
"getReleaseCommit": "<execSync>["git log --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
"getReleaseCommit": "<execSync>["git log HEAD --grep=\\"chore(release)\\" --pretty=oneline -1"]</execSync>",
}
`;
3 changes: 2 additions & 1 deletion src/__tests__/cmds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('Commands', () => {
const { mockClear } = mockObjectProperty(OPTIONS, {
date: '2022-10-01',
changelogPath: join(OPTIONS.contextPath, 'CHANGELOG.md'),
packagePath: join(OPTIONS.contextPath, 'package.json')
packagePath: join(OPTIONS.contextPath, 'package.json'),
releaseBranch: 'HEAD'
});

afterAll(() => {
Expand Down
24 changes: 15 additions & 9 deletions src/cmds.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ const getCurrentVersion = ({ packagePath } = OPTIONS) => {
*
* @param {object} options
* @param {string[]|string} options.releaseTypeScope
* @param {string|undefined} options.releaseBranch
* @returns {string}
*/
const getReleaseCommit = ({ releaseTypeScope } = OPTIONS) => {
const getReleaseCommit = ({ releaseTypeScope, releaseBranch } = OPTIONS) => {
const isArray = Array.isArray(releaseTypeScope);

const updatedReleaseBranch = (releaseBranch && ` ${releaseBranch}`) || '';
return runCmd(
`git log --grep="${
`git log${updatedReleaseBranch} --grep="${
(isArray && releaseTypeScope?.[1]) || (isArray && releaseTypeScope?.[0]) || releaseTypeScope
}" --pretty=oneline -1`,
'Skipping release commit check... {0}'
Expand Down Expand Up @@ -125,17 +126,22 @@ const getRemoteUrls = ({ commitPath, comparePath, prPath, remoteUrl } = OPTIONS)
/**
* Return output for a range of commits from a hash
*
* @param {object} options
* @param {string|undefined} options.releaseBranch
* @param {object} settings
* @param {Function} settings.getReleaseCommit
* @param {Array} settings.breakingChangeMessageFilter
* @param {Array} settings.breakingChangeScopeTypeFilter
* @returns {Array}
*/
const getGit = ({
getReleaseCommit: getAliasReleaseCommit = getReleaseCommit,
breakingChangeMessageFilter = ['BREAKING CHANGE:', 'BREAKING CHANGES:'],
breakingChangeScopeTypeFilter = [')!:', '!:']
} = {}) => {
const getGit = (
{ releaseBranch } = OPTIONS,
{
getReleaseCommit: getAliasReleaseCommit = getReleaseCommit,
breakingChangeMessageFilter = ['BREAKING CHANGE:', 'BREAKING CHANGES:'],
breakingChangeScopeTypeFilter = [')!:', '!:']
} = {}
) => {
const releaseCommitHash = getAliasReleaseCommit().split(/\s/)[0];
let breakingChangeMessageCommits;
let breakingChangeScopeTypeCommits;
Expand All @@ -154,7 +160,7 @@ const getGit = ({
* @returns {string}
*/
const getGitLog = (commitHash, searchFilter) => {
const releaseCommitHashRange = (commitHash && ` ${commitHash}..HEAD`) || '';
const releaseCommitHashRange = (commitHash && ` ${commitHash}..${releaseBranch}`) || '';
const searchFilterCommits = searchFilter?.map(value => ` --grep="${value}"`).join(' ') || '';

return runCmd(
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/code.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
exports[`General code checks should only have specific console.[warn|log|info|error] methods: console methods 1`] = `
[
"cmds.js:27: console.error(color.RED, errorMessage.replace('{0}', e.message), color.NOCOLOR)",
"cmds.js:218: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"cmds.js:248: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"cmds.js:224: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"cmds.js:254: console.error(color.RED, \`Semver: \${e.message}\`, color.NOCOLOR)",
"files.js:85: console.info(\` \${updatedBody}\`)",
"global.js:68: console.error(color.RED, \`Conventional commit types: \${e.message}\`, color.NOCOLOR)",
"index.js:54: console.info( index.js:68: console.info(color.NOCOLOR)",
Expand Down

0 comments on commit bf7e65d

Please sign in to comment.