Skip to content

Commit

Permalink
Merge pull request #1 from iclanton/ianc/zhas/rush-change-detect
Browse files Browse the repository at this point in the history
Fix a few issues with rush change when rush.json isn't in the repo root.
  • Loading branch information
Claudia Sun authored Sep 4, 2019
2 parents f627aa1 + 13d0962 commit e82b016
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
41 changes: 13 additions & 28 deletions apps/rush-lib/src/cli/actions/ChangeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,16 @@ export class ChangeAction extends BaseRushAction {
}
const changedPackageNames: Set<string> = new Set<string>();

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) {
Expand All @@ -195,15 +201,15 @@ export class ChangeAction extends BaseRushAction {
});
}

private _hasProjectChanged(changedFolders: Array<string | undefined>,
project: RushConfigurationProject): boolean {
let normalizedFolder: string = project.projectRelativeFolder;

const rushPathDiff: string = this._findRushPathDiff(changedFolders, project);
private _hasProjectChanged(
changedFolders: Array<string | undefined>,
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)) {
Expand All @@ -214,27 +220,6 @@ export class ChangeAction extends BaseRushAction {
return false;
}

// find the difference between the relative and root path
private _findRushPathDiff(changedFolders: Array<string | undefined>,
project: RushConfigurationProject): string {
const normalizedFolder: string = project.projectRelativeFolder;
const rushPathDiff: string = '';
for (const folder of changedFolders) {
if (folder !== undefined) {
const splitted: Array<string> = 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.
Expand Down
13 changes: 13 additions & 0 deletions apps/rush-lib/src/utilities/VersionControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down

0 comments on commit e82b016

Please sign in to comment.