Skip to content

Commit

Permalink
feat: enable first-parent commits filtering by cli flag
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 24, 2020
1 parent 95ac7c0 commit 33306cc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions bin/cli.js
Expand Up @@ -11,6 +11,7 @@ const cli = meow(
Options
--sequential-init Avoid hypothetical concurrent initialization collisions.
--debug Output debugging information.
--first-parent Apply commit filtering to current branch only.
--help Help info.
Examples
Expand All @@ -21,6 +22,9 @@ const cli = meow(
sequentialInit: {
type: "boolean",
},
firstParent: {
type: "boolean",
},
debug: {
type: "boolean",
},
Expand Down
8 changes: 5 additions & 3 deletions lib/createInlinePluginCreator.js
Expand Up @@ -11,11 +11,12 @@ const hasChangedDeep = require("./hasChangedDeep");
* @param {Package[]} packages The multi-semantic-release context.
* @param {MultiContext} multiContext The multi-semantic-release context.
* @param {Synchronizer} synchronizer Shared synchronization assets
* @param {Object} flags argv options
* @returns {Function} A function that creates an inline package.
*
* @internal
*/
function createInlinePluginCreator(packages, multiContext, synchronizer) {
function createInlinePluginCreator(packages, multiContext, synchronizer, flags) {
// Vars.
const { cwd } = multiContext;
const { todo, waitFor, waitForAll, emit, getLucky } = synchronizer;
Expand Down Expand Up @@ -109,9 +110,10 @@ function createInlinePluginCreator(packages, multiContext, synchronizer) {
* @internal
*/
const analyzeCommits = async (pluginOptions, context) => {
const filterParentBranch = flags.filterParent ? context.branch.name : undefined;

// Filter commits by directory.
commits = await getCommitsFiltered(cwd, dir, context.lastRelease.gitHead, context.branch.name);
// context.logger.log("filtered commits", JSON.stringify(commits, null, 2));
commits = await getCommitsFiltered(cwd, dir, context.lastRelease.gitHead, filterParentBranch);

// Set context.commits so analyzeCommits does correct analysis.
context.commits = commits;
Expand Down
7 changes: 4 additions & 3 deletions lib/getCommitsFiltered.js
Expand Up @@ -15,10 +15,10 @@ const debug = require("debug")("msr:commitsFilter");
* @param {string} cwd Absolute path of the working directory the Git repo is in.
* @param {string} dir Path to the target directory to filter by. Either absolute, or relative to cwd param.
* @param {string|void} lastHead The SHA of the previous release
* @param {string|void} branch first-parent to determine which merges went into master
* @param {string|void} firstParentBranch first-parent to determine which merges went into master
* @return {Promise<Array<Commit>>} The list of commits on the branch `branch` since the last release.
*/
async function getCommitsFiltered(cwd, dir, lastHead = undefined, branch = "master") {
async function getCommitsFiltered(cwd, dir, lastHead = undefined, firstParentBranch) {
// Clean paths and make sure directories exist.
check(cwd, "cwd: directory");
check(dir, "dir: path");
Expand All @@ -44,7 +44,8 @@ async function getCommitsFiltered(cwd, dir, lastHead = undefined, branch = "mast

// Use git-log-parser to get the commits.
const relpath = relative(root, dir);
const gitLogFilterQuery = ["--first-parent", branch, lastHead ? `${lastHead}..HEAD` : "HEAD", "--", relpath];
const firstParentBranchFilter = firstParentBranch ? ["--first-parent", firstParentBranch] : [];
const gitLogFilterQuery = [...firstParentBranchFilter, lastHead ? `${lastHead}..HEAD` : "HEAD", "--", relpath];
const stream = gitLogParser.parse({ _: gitLogFilterQuery }, { cwd, env: process.env });
const commits = await getStream.array(stream);

Expand Down
2 changes: 1 addition & 1 deletion lib/multiSemanticRelease.js
Expand Up @@ -77,7 +77,7 @@ async function multiSemanticRelease(
const { getLucky, waitFor } = synchronizer;

// Release all packages.
const createInlinePlugin = createInlinePluginCreator(packages, multiContext, synchronizer);
const createInlinePlugin = createInlinePluginCreator(packages, multiContext, synchronizer, flags);
await Promise.all(
packages.map(async (pkg) => {
// Avoid hypothetical concurrent initialization collisions / throttling issues.
Expand Down

0 comments on commit 33306cc

Please sign in to comment.