From d8fae26b5fc25c0da92741e38d994ef25119811b Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Tue, 3 Sep 2019 18:08:42 -0700 Subject: [PATCH 1/2] Fix a few issues with rush change when rush.json isn't in the repo root. --- apps/rush-lib/src/cli/actions/ChangeAction.ts | 41 ++++++------------- apps/rush-lib/src/utilities/VersionControl.ts | 13 ++++++ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/apps/rush-lib/src/cli/actions/ChangeAction.ts b/apps/rush-lib/src/cli/actions/ChangeAction.ts index 3b44acc9c1e..48878573de7 100644 --- a/apps/rush-lib/src/cli/actions/ChangeAction.ts +++ b/apps/rush-lib/src/cli/actions/ChangeAction.ts @@ -170,10 +170,16 @@ export class ChangeAction extends BaseRushAction { } const changedPackageNames: Set = new Set(); + const repoRootFolder: string | undefined = VersionControl.getRepositoryRootPath(); this.rushConfiguration.projects .filter(project => project.shouldPublish) .filter(project => !project.versionPolicy || !project.versionPolicy.exemptFromRushChange) - .filter(project => this._hasProjectChanged(changedFolders, project)) + .filter(project => { + const projectFolder: string = repoRootFolder + ? path.relative(repoRootFolder, project.projectFolder) + : project.projectRelativeFolder; + return this._hasProjectChanged(changedFolders, projectFolder); + }) .forEach(project => { const hostName: string | undefined = this._projectHostMap.get(project.packageName); if (hostName) { @@ -195,15 +201,15 @@ export class ChangeAction extends BaseRushAction { }); } - private _hasProjectChanged(changedFolders: Array, - project: RushConfigurationProject): boolean { - let normalizedFolder: string = project.projectRelativeFolder; - - const rushPathDiff: string = this._findRushPathDiff(changedFolders, project); + private _hasProjectChanged( + changedFolders: Array, + projectFolder: string + ): boolean { + let normalizedFolder: string = projectFolder.replace(/\\/g, '/'); // Replace backslashes with forward slashes if (normalizedFolder.charAt(normalizedFolder.length - 1) !== '/') { normalizedFolder = normalizedFolder + '/'; } - normalizedFolder = path.join(rushPathDiff, normalizedFolder); + const pathRegex: RegExp = new RegExp(`^${normalizedFolder}`, 'i'); for (const folder of changedFolders) { if (folder && folder.match(pathRegex)) { @@ -214,27 +220,6 @@ export class ChangeAction extends BaseRushAction { return false; } - // find the difference between the relative and root path - private _findRushPathDiff(changedFolders: Array, - project: RushConfigurationProject): string { - const normalizedFolder: string = project.projectRelativeFolder; - const rushPathDiff: string = ''; - for (const folder of changedFolders) { - if (folder !== undefined) { - const splitted: Array = folder.split(path.sep); - if (splitted[0] !== normalizedFolder) { - // if rush.json isn't in the root directory - const changeProjectIndex: number = splitted.indexOf(normalizedFolder); - for (let i: number = 0; i < changeProjectIndex; i++) { - path.join(rushPathDiff, splitted[i]); - } - } - } - } - - return rushPathDiff; - } - /** * The main loop which continually asks user for questions about changes until they don't * have any more, at which point we collect their email and write the change file. diff --git a/apps/rush-lib/src/utilities/VersionControl.ts b/apps/rush-lib/src/utilities/VersionControl.ts index 65a7c71e85b..d61d7325b4f 100644 --- a/apps/rush-lib/src/utilities/VersionControl.ts +++ b/apps/rush-lib/src/utilities/VersionControl.ts @@ -10,6 +10,19 @@ const DEFAULT_REMOTE: string = 'origin'; const DEFAULT_FULLY_QUALIFIED_BRANCH: string = `${DEFAULT_REMOTE}/${DEFAULT_BRANCH}`; export class VersionControl { + public static getRepositoryRootPath(): string | undefined { + const output: child_process.SpawnSyncReturns = Executable.spawnSync( + 'git', + ['rev-parse', '--show-toplevel'] + ); + + if (output.status !== 0) { + return undefined; + } else { + return output.stdout.trim(); + } + } + public static getChangedFolders( targetBranch: string, skipFetch: boolean = false From 13d09627cfb34e87065ffdbdaa3fa95902fccfa6 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Tue, 3 Sep 2019 18:11:06 -0700 Subject: [PATCH 2/2] Rush change. --- .../rush/zhas-rush-change-detect_2019-08-23-23-05.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/changes/@microsoft/rush/zhas-rush-change-detect_2019-08-23-23-05.json b/common/changes/@microsoft/rush/zhas-rush-change-detect_2019-08-23-23-05.json index 97d549493a7..f035e730158 100644 --- a/common/changes/@microsoft/rush/zhas-rush-change-detect_2019-08-23-23-05.json +++ b/common/changes/@microsoft/rush/zhas-rush-change-detect_2019-08-23-23-05.json @@ -1,7 +1,7 @@ { "changes": [ { - "comment": "Fix an inssue with rush change when rush.json isn't in the repository root", + "comment": "Fix an issue with rush change that occurs when rush.json isn't in the repository root.", "packageName": "@microsoft/rush", "type": "none" }