-
Notifications
You must be signed in to change notification settings - Fork 604
/
VersionControl.ts
184 lines (164 loc) · 6.36 KB
/
VersionControl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as child_process from 'child_process';
import * as colors from 'colors';
import { Executable } from '@microsoft/node-core-library';
const DEFAULT_BRANCH: string = 'master';
const DEFAULT_REMOTE: string = 'origin';
const DEFAULT_FULLY_QUALIFIED_BRANCH: string = `${DEFAULT_REMOTE}/${DEFAULT_BRANCH}`;
export class VersionControl {
public static getChangedFolders(
targetBranch: string,
skipFetch: boolean = false
): Array<string | undefined> | undefined {
if (!skipFetch) {
VersionControl._fetchNonDefaultBranch(targetBranch);
}
const output: string = child_process.execSync(`git diff ${targetBranch}... --dirstat=files,0`).toString();
return output.split('\n').map((line) => {
if (line) {
const delimiterIndex: number = line.indexOf('%');
if (delimiterIndex > 0 && delimiterIndex + 1 < line.length) {
return line.substring(delimiterIndex + 1).trim();
}
}
return undefined;
});
}
/**
* @param pathPrefix - An optional path prefix "git diff"s should be filtered by.
* @returns
* An array of paths of repo-root-relative paths of files that are different from
* those in the provided {@param targetBranch}. If a {@param pathPrefix} is provided,
* this function only returns reuslts under the that path.
*/
public static getChangedFiles(targetBranch: string, skipFetch: boolean = false, pathPrefix?: string): string[] {
if (!skipFetch) {
VersionControl._fetchNonDefaultBranch(targetBranch);
}
const output: string = child_process.execSync(
`git diff ${targetBranch}... --name-only --no-renames --diff-filter=A`
).toString();
const regex: RegExp | undefined = pathPrefix ? new RegExp(`^${pathPrefix}`, 'i') : undefined;
return output.split('\n').map((line) => {
if (line) {
const trimmedLine: string = line.trim();
if (regex && trimmedLine.match(regex)) {
return trimmedLine;
}
}
return undefined;
}).filter((line) => {
return line && line.length > 0;
}) as string[];
}
/**
* Gets the remote master branch that maps to the provided repository url.
* This method is used by 'Rush change' to find the default remote branch to compare against.
* If repository url is not provided or if there is no match, returns the default remote
* master branch 'origin/master'.
* If there are more than one matches, returns the first remote's master branch.
*
* @param repositoryUrl - repository url
*/
public static getRemoteMasterBranch(repositoryUrl?: string): string {
let matchingRemotes: string[] = [];
if (repositoryUrl) {
const output: string = child_process
.execSync(`git remote`)
.toString();
matchingRemotes = output.split('\n').filter(remoteName => {
if (remoteName) {
const remoteUrl: string = child_process.execSync(`git remote get-url ${remoteName}`)
.toString()
.trim();
if (remoteUrl === repositoryUrl) {
return true;
}
// When you copy a URL from the GitHub web site, they append the ".git" file extension to the URL.
// So we allow that to be specified in rush.json, even though the file extension gets dropped
// by "git clone".
if (remoteUrl + '.git' === repositoryUrl) {
return true;
}
}
return false;
});
} else {
console.log(colors.yellow(
'A git remote URL has not been specified in rush.json. Setting the baseline remote URL is recommended.'
));
return DEFAULT_FULLY_QUALIFIED_BRANCH;
}
if (matchingRemotes.length > 0) {
if (matchingRemotes.length > 1) {
console.log(
`More than one git remote matches the repository URL. Using the first remote (${matchingRemotes[0]}).`
);
}
return `${matchingRemotes[0]}/${DEFAULT_BRANCH}`;
} else {
console.log(colors.yellow(
`Unable to find a git remote matching the repository URL (${matchingRemotes[0]}). ` +
'Detected changes are likely to be incorrect.'
));
return DEFAULT_FULLY_QUALIFIED_BRANCH;
}
}
public static hasUncommittedChanges(): boolean {
return VersionControl.getUncommittedChanges().length > 0;
}
/**
* The list of files changed but not committed
*/
public static getUncommittedChanges(): ReadonlyArray<string> {
const changes: string[] = [];
changes.push(...VersionControl._getUntrackedChanges());
changes.push(...VersionControl._getDiffOnHEAD());
return changes.filter(change => {
return change.trim().length > 0;
});
}
private static _getUntrackedChanges(): string[] {
const output: string = child_process
.execSync(`git ls-files --exclude-standard --others`)
.toString();
return output.trim().split('\n');
}
private static _getDiffOnHEAD(): string[] {
const output: string = child_process
.execSync(`git diff HEAD --name-only`)
.toString();
return output.trim().split('\n');
}
private static _tryFetchRemoteBranch(remoteBranchName: string): boolean {
const firstSlashIndex: number = remoteBranchName.indexOf('/');
if (firstSlashIndex === -1) {
throw new Error(
`Unexpected git remote branch format: ${remoteBranchName}. ` +
'Expected branch to be in the <remote>/<branch name> format.'
);
}
const remoteName: string = remoteBranchName.substr(0, firstSlashIndex);
const branchName: string = remoteBranchName.substr(firstSlashIndex + 1);
const spawnResult: child_process.SpawnSyncReturns<string> = Executable.spawnSync(
'git',
['fetch', remoteName, branchName],
{
stdio: 'ignore'
}
);
return spawnResult.status === 0;
}
private static _fetchNonDefaultBranch(remoteBranchName: string): void {
if (remoteBranchName !== DEFAULT_FULLY_QUALIFIED_BRANCH) {
console.log(`Checking for updates to ${remoteBranchName}...`);
const fetchResult: boolean = VersionControl._tryFetchRemoteBranch(remoteBranchName);
if (!fetchResult) {
console.log(colors.yellow(
`Error fetching git remote branch ${remoteBranchName}. Detected changed files may be incorrect.`
));
}
}
}
}