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

Allow to attribute changesets to merge commits #1288

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .changeset/clean-ghosts-leave.md
@@ -0,0 +1,8 @@
---
"@changesets/apply-release-plan": minor
"@changesets/config": minor
"@changesets/types": minor
"@changesets/git": minor
---

Allow attributing changesets to merge commits
7 changes: 5 additions & 2 deletions packages/apply-release-plan/src/index.ts
Expand Up @@ -35,7 +35,8 @@ function stringDefined(s: string | undefined): s is string {
}
async function getCommitsThatAddChangesets(
changesetIds: string[],
cwd: string
cwd: string,
useMergeCommits: boolean
) {
const paths = changesetIds.map((id) => `.changeset/${id}.md`);
const commits = await git.getCommitsThatAddFiles(paths, { cwd });
Expand All @@ -53,6 +54,7 @@ async function getCommitsThatAddChangesets(
const legacyPaths = missingIds.map((id) => `.changeset/${id}/changes.json`);
const commitsForLegacyPaths = await git.getCommitsThatAddFiles(legacyPaths, {
cwd,
useMergeCommits,
});

// Fill in the blanks in the array of commits
Expand Down Expand Up @@ -230,7 +232,8 @@ async function getNewChangelogEntry(

let commits = await getCommitsThatAddChangesets(
changesets.map((cs) => cs.id),
cwd
cwd,
config.useMergeCommits
);
let moddedChangesets = changesets.map((cs, i) => ({
...cs,
Expand Down
5 changes: 5 additions & 0 deletions packages/config/schema.json
Expand Up @@ -75,6 +75,11 @@
"description": "Determines whether Changesets should commit the results of the add and version command.",
"default": false
},
"useMergeCommits": {
"type": "boolean",
"description": "Determines whether changesets should be attributed to merge commits.",
"default": false
},
"privatePackages": {
"anyOf": [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/index.test.ts
Expand Up @@ -43,6 +43,7 @@ test("read reads the config", async () => {
linked: [],
changelog: false,
commit: ["@changesets/cli/commit", { skipCI: "version" }],
useMergeCommits: false,
access: "restricted",
baseBranch: "master",
changedFilePatterns: ["**"],
Expand All @@ -69,6 +70,7 @@ let defaults: Config = {
linked: [],
changelog: ["@changesets/cli/changelog", null],
commit: false,
useMergeCommits: false,
access: "restricted",
baseBranch: "master",
changedFilePatterns: ["**"],
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -431,6 +431,8 @@ export let parse = (json: WrittenConfig, packages: Packages): Config => {
commit: getNormalizedCommitOption(
json.commit === undefined ? defaultWrittenConfig.commit : json.commit
),
useMergeCommits:
json.useMergeCommits === undefined ? false : json.useMergeCommits,
fixed,
linked,
baseBranch:
Expand Down
9 changes: 7 additions & 2 deletions packages/git/src/index.ts
Expand Up @@ -63,7 +63,11 @@ export async function getDivergedCommit(cwd: string, ref: string) {
*/
export async function getCommitsThatAddFiles(
gitPaths: string[],
{ cwd, short = false }: { cwd: string; short?: boolean }
{
cwd,
short = false,
useMergeCommits = false,
}: { cwd: string; short?: boolean; useMergeCommits?: boolean }
): Promise<(string | undefined)[]> {
// Maps gitPath to commit SHA
const map = new Map<string, string>();
Expand All @@ -83,8 +87,9 @@ export async function getCommitsThatAddFiles(
"--diff-filter=A",
"--max-count=1",
short ? "--pretty=format:%h:%p" : "--pretty=format:%H:%p",
useMergeCommits ? "--first-parent" : "",
gitPath,
],
].filter(Boolean),
{ cwd }
)
).stdout
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/index.ts
Expand Up @@ -69,6 +69,7 @@ export interface PrivatePackages {
export type Config = {
changelog: false | readonly [string, any];
commit: false | readonly [string, any];
useMergeCommits: boolean;
fixed: Fixed;
linked: Linked;
access: AccessType;
Expand All @@ -94,6 +95,7 @@ export type Config = {
export type WrittenConfig = {
changelog?: false | readonly [string, any] | string;
commit?: boolean | readonly [string, any] | string;
useMergeCommits?: boolean;
fixed?: Fixed;
linked?: Linked;
access?: AccessType;
Expand Down