Skip to content

Commit 304f139

Browse files
committed
fixes
1 parent d1c4fba commit 304f139

5 files changed

Lines changed: 31 additions & 14 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"type": "patch",
5+
"comment": "Refactor internals for getting changed packages",
6+
"packageName": "beachball",
7+
"email": "elcraig@microsoft.com",
8+
"dependentChangeType": "patch"
9+
}
10+
]
11+
}

packages/beachball/src/__fixtures__/repository.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,21 @@ ${gitResult.stderr.toString()}`);
167167

168168
/**
169169
* Update the content of a JSON file that already exists in the repo.
170-
* The updates will be merged with the original.
170+
* The updates will be shallowly merged with the original. Throws if the file doesn't exist.
171171
*
172172
* This is useful if you'd like to mostly use a built-in fixture but change one package,
173173
* such as making it private.
174174
*/
175-
updateJsonFile(filename: string, updates: object, options?: { mustExist?: boolean; commit?: boolean }): void {
175+
updateJsonFile(filename: string, updates: object, options?: { commit?: boolean }): void {
176176
if (!filename.endsWith('.json')) {
177177
throw new Error('This method only works with json files');
178178
}
179179

180180
const fullPath = this.pathTo(filename);
181-
const oldContent = readJson<object>(fullPath);
182-
if (options?.mustExist && !oldContent) {
181+
if (!fs.existsSync(fullPath)) {
183182
throw new Error(`JSON file does not exist: ${filename}`);
184183
}
184+
const oldContent = readJson<object>(fullPath);
185185
writeJson(fullPath, { ...oldContent, ...updates });
186186

187187
if (options?.commit) {

packages/beachball/src/__functional__/git/getAllChangedPackages.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ describe('getAllChangedPackages', () => {
199199
repo = getReusedRepoWithBranch('monorepo');
200200

201201
// Update packages so they'll be ignored
202-
repo.updateJsonFile('packages/foo/package.json', { private: true }, { mustExist: true });
202+
repo.updateJsonFile('packages/foo/package.json', { private: true });
203203
repo.writeFile('packages/foo/foo.js');
204-
repo.updateJsonFile('packages/bar/package.json', { beachball: { shouldPublish: false } }, { mustExist: true });
204+
repo.updateJsonFile('packages/bar/package.json', { beachball: { shouldPublish: false } });
205205
repo.writeFile('packages/bar/bar.js');
206206
// baz is not ignored
207207
repo.writeFile('packages/baz/baz.js');

packages/beachball/src/changefile/getChangedPackages.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ import { readJson } from '../object/readJson';
99
import { bulletedList } from '../logging/bulletedList';
1010
import { getAllChangedPackages } from './getAllChangedPackages';
1111
import { getIncludedLoggers, isPackageIncluded } from './isPackageIncluded';
12-
import { ensureSharedHistory, hasCommonCommit } from '../git/ensureSharedHistory';
12+
import { ensureSharedHistory } from '../git/ensureSharedHistory';
1313

1414
/**
1515
* Gets all the changed packages which **do not already have a change file** and are in scope.
16+
* This is only used by the `change` and `check` commands, not the bump/publish process.
1617
*
17-
* Exceptions:
18+
* Special cases:
1819
* - If `options.package` is provided, use that as-is (skipping all git operations).
1920
* - If `options.all` is true, gets all the packages in scope regardless of whether they've changed
20-
* (skipping git diff of files), filtered by packages that already have change files.
21+
* (skipping git diff of files), omitting packages that already have change files.
22+
*
23+
* Usually (without `options.package`) this has the side effect of calling `ensureSharedHistory` to
24+
* verify that enough git history is available to check for changes between `HEAD` and
25+
* `options.branch` (only an issue for shallow clones), and deepens the history if needed.
26+
* Unless `options.fetch` is `false`, it will also fetch from the remote.
2127
*/
2228
export function getChangedPackages(
2329
options: BeachballOptions,
@@ -33,10 +39,10 @@ export function getChangedPackages(
3339

3440
console.log(`Checking for changes against "${options.branch}"`);
3541

36-
// We should fetch shared history even with --all for accurate change file checks later
37-
if (!hasCommonCommit(branch, options.path)) {
38-
ensureSharedHistory(options);
39-
}
42+
// Ensure the current branch and target branch have a common shared commit. This has the side
43+
// effect of fetching from the remote (unless disabled), which should be done even if there's
44+
// already shared history (and even with --all) for accurate added change file checks later.
45+
ensureSharedHistory(options);
4046

4147
let changedPackages: string[];
4248

packages/beachball/src/git/ensureSharedHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,6 @@ function isShallowRepository(cwd: string): boolean {
197197
}
198198

199199
/** Returns whether `branch` and HEAD have a common commit anywhere in their history */
200-
export function hasCommonCommit(branch: string, cwd: string): boolean {
200+
function hasCommonCommit(branch: string, cwd: string): boolean {
201201
return git(['merge-base', branch, 'HEAD'], { cwd }).success;
202202
}

0 commit comments

Comments
 (0)