Skip to content

Commit b6eb90e

Browse files
authored
Merge pull request microsoft#1354 from microsoft/ianc/fix-rush-change
[rush] Fix an issue where rush change will detect unrelated changes.
2 parents 601c52f + 3b107dd commit b6eb90e

2 files changed

Lines changed: 50 additions & 33 deletions

File tree

apps/rush-lib/src/utilities/VersionControl.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class VersionControl {
1515
skipFetch: boolean = false
1616
): Array<string | undefined> | undefined {
1717
if (!skipFetch) {
18-
VersionControl._fetchNonDefaultBranch(targetBranch);
18+
VersionControl._fetchRemoteBranch(targetBranch);
1919
}
2020

2121
const output: string = child_process.execSync(`git diff ${targetBranch}... --dirstat=files,0`).toString();
@@ -36,11 +36,11 @@ export class VersionControl {
3636
* @returns
3737
* An array of paths of repo-root-relative paths of files that are different from
3838
* those in the provided {@param targetBranch}. If a {@param pathPrefix} is provided,
39-
* this function only returns reuslts under the that path.
39+
* this function only returns results under the that path.
4040
*/
4141
public static getChangedFiles(targetBranch: string, skipFetch: boolean = false, pathPrefix?: string): string[] {
4242
if (!skipFetch) {
43-
VersionControl._fetchNonDefaultBranch(targetBranch);
43+
VersionControl._fetchRemoteBranch(targetBranch);
4444
}
4545

4646
const output: string = child_process.execSync(
@@ -71,48 +71,56 @@ export class VersionControl {
7171
* @param repositoryUrl - repository url
7272
*/
7373
public static getRemoteMasterBranch(repositoryUrl?: string): string {
74-
let matchingRemotes: string[] = [];
75-
7674
if (repositoryUrl) {
7775
const output: string = child_process
7876
.execSync(`git remote`)
7977
.toString();
80-
matchingRemotes = output.split('\n').filter(remoteName => {
78+
const normalizedRepositoryUrl: string = repositoryUrl.toUpperCase();
79+
const matchingRemotes: string[] = output.split('\n').filter((remoteName) => {
8180
if (remoteName) {
8281
const remoteUrl: string = child_process.execSync(`git remote get-url ${remoteName}`)
8382
.toString()
8483
.trim();
85-
if (remoteUrl === repositoryUrl) {
84+
85+
if (!remoteUrl) {
86+
return false;
87+
}
88+
89+
const normalizedRemoteUrl: string = remoteUrl.toUpperCase();
90+
if (normalizedRemoteUrl.toUpperCase() === normalizedRepositoryUrl) {
8691
return true;
8792
}
93+
8894
// When you copy a URL from the GitHub web site, they append the ".git" file extension to the URL.
89-
// So we allow that to be specified in rush.json, even though the file extension gets dropped
95+
// We allow that to be specified in rush.json, even though the file extension gets dropped
9096
// by "git clone".
91-
if (remoteUrl + '.git' === repositoryUrl) {
97+
if (`${normalizedRemoteUrl}.GIT` === normalizedRepositoryUrl) {
9298
return true;
9399
}
94100
}
101+
95102
return false;
96103
});
97-
} else {
98-
console.log(colors.yellow(
99-
'A git remote URL has not been specified in rush.json. Setting the baseline remote URL is recommended.'
100-
));
101-
return DEFAULT_FULLY_QUALIFIED_BRANCH;
102-
}
103104

104-
if (matchingRemotes.length > 0) {
105-
if (matchingRemotes.length > 1) {
106-
console.log(
107-
`More than one git remote matches the repository URL. Using the first remote (${matchingRemotes[0]}).`
108-
);
109-
}
105+
if (matchingRemotes.length > 0) {
106+
if (matchingRemotes.length > 1) {
107+
console.log(
108+
`More than one git remote matches the repository URL. Using the first remote (${matchingRemotes[0]}).`
109+
);
110+
}
111+
112+
return `${matchingRemotes[0]}/${DEFAULT_BRANCH}`;
113+
} else {
114+
console.log(colors.yellow(
115+
`Unable to find a git remote matching the repository URL (${repositoryUrl}). ` +
116+
'Detected changes are likely to be incorrect.'
117+
));
110118

111-
return `${matchingRemotes[0]}/${DEFAULT_BRANCH}`;
119+
return DEFAULT_FULLY_QUALIFIED_BRANCH;
120+
}
112121
} else {
113122
console.log(colors.yellow(
114-
`Unable to find a git remote matching the repository URL (${matchingRemotes[0]}). ` +
115-
'Detected changes are likely to be incorrect.'
123+
'A git remote URL has not been specified in rush.json. Setting the baseline remote URL is recommended.'
116124
));
117125
return DEFAULT_FULLY_QUALIFIED_BRANCH;
118126
}
@@ -170,15 +178,13 @@ export class VersionControl {
170178
return spawnResult.status === 0;
171179
}
172180

173-
private static _fetchNonDefaultBranch(remoteBranchName: string): void {
174-
if (remoteBranchName !== DEFAULT_FULLY_QUALIFIED_BRANCH) {
175-
console.log(`Checking for updates to ${remoteBranchName}...`);
176-
const fetchResult: boolean = VersionControl._tryFetchRemoteBranch(remoteBranchName);
177-
if (!fetchResult) {
178-
console.log(colors.yellow(
179-
`Error fetching git remote branch ${remoteBranchName}. Detected changed files may be incorrect.`
180-
));
181-
}
181+
private static _fetchRemoteBranch(remoteBranchName: string): void {
182+
console.log(`Checking for updates to ${remoteBranchName}...`);
183+
const fetchResult: boolean = VersionControl._tryFetchRemoteBranch(remoteBranchName);
184+
if (!fetchResult) {
185+
console.log(colors.yellow(
186+
`Error fetching git remote branch ${remoteBranchName}. Detected changed files may be incorrect.`
187+
));
182188
}
183189
}
184190
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix an issue where rush change will detect unrelated changes.",
5+
"packageName": "@microsoft/rush",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "iclanton@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)