Skip to content

Commit

Permalink
Add changesetBaseRef configuration option for versioning (#1195) (#1201)
Browse files Browse the repository at this point in the history
* Add changesetBaseRef configuration option for versioning (#1195)

* Add changesetBaseRef, allowing for comparing files against non-master refs

* Update yarnrc documentation accordingly

* Update the changesetBaseRef configuration's default values

* renamed changesetBaseRef to changesetBaseRefs

* updated its type to array, set master, origin/master, upstream/master as default

* remove hardcoded candidates, enforce non-nullable configuration

* update docs accordingly

* Add usageError when changesetBaseRefs is []

* Add a usageError for when the changesetBaseRefs is explicitly set to the empty array

* ref -> refs in documentation

* Adds versions

Co-authored-by: fggrimshaw <[email protected]>
Co-authored-by: Maël Nison <[email protected]>
  • Loading branch information
3 people authored Apr 16, 2020
1 parent a2dd94a commit 8e47cac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .yarn/versions/b62cfa7b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@yarnpkg/plugin-version": prerelease

declined:
- "@yarnpkg/cli"
8 changes: 8 additions & 0 deletions packages/gatsby/src/pages/configuration/yarnrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
"format": "uri-reference",
"default": "./.yarn/cache"
},
"changesetBaseRefs": {
"description": "The base git refs that the current HEAD is compared against in the version plugin. This overrides the default behavior of comparing against master, origin/master, and upstream/master. Supports git branches, tags, and commits.",
"type": "array",
"items": {
"type": "string"
},
"default": ["master", "origin/master", "upstream/master"]
},
"checksumBehavior": {
"description": "If `throw` (the default), Yarn will throw an exception on `yarn install` if it detects that a package doesn't match the checksum stored within the lockfile. If `update`, the lockfile checksum will be updated to match the new value. If `ignore`, the checksum check will not happen.",
"enum": ["throw", "update", "ignore"],
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-version/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import version from './commands/version';

const plugin: Plugin = {
configuration: {
changesetBaseRefs: {
description: 'The base git refs that the current HEAD is compared against when detecting changes. Supports git branches, tags, and commits.',
type: SettingsType.STRING,
isArray: true,
isNullable: false,
default: [`master`, `origin/master`, `upstream/master`],
},
deferredVersionFolder: {
description: `Folder where are stored the versioning files`,
type: SettingsType.ABSOLUTE_PATH,
Expand Down
12 changes: 7 additions & 5 deletions packages/plugin-version/sources/versionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ export enum Decision {
export type Releases =
Map<Workspace, Exclude<Decision, Decision.UNDECIDED>>;

export async function fetchBase(root: PortablePath) {
const candidateBases = [`master`, `origin/master`, `upstream/master`];
export async function fetchBase(root: PortablePath, {baseRefs}: {baseRefs: string[]}) {
if (baseRefs.length === 0)
throw new UsageError(`Can't run this command with zero base refs specified.`);

const ancestorBases = [];

for (const candidate of candidateBases) {
for (const candidate of baseRefs) {
const {code} = await execUtils.execvp(`git`, [`merge-base`, candidate, `HEAD`], {cwd: root});
if (code === 0) {
ancestorBases.push(candidate);
}
}

if (ancestorBases.length === 0)
throw new UsageError(`No ancestor could be found between any of HEAD and ${candidateBases.join(`, `)}`);
throw new UsageError(`No ancestor could be found between any of HEAD and ${baseRefs.join(`, `)}`);

const {stdout: mergeBaseStdout} = await execUtils.execvp(`git`, [`merge-base`, `HEAD`, ...ancestorBases], {cwd: root, strict: true});
const hash = mergeBaseStdout.trim();
Expand Down Expand Up @@ -196,7 +198,7 @@ export async function openVersionFile(project: Project, {allowEmpty = false}: {a
const root = await fetchRoot(configuration.projectCwd);

const base = root !== null
? await fetchBase(root)
? await fetchBase(root, {baseRefs: configuration.get('changesetBaseRefs')})
: null;

const changedFiles = root !== null
Expand Down

0 comments on commit 8e47cac

Please sign in to comment.