forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionControl.ts
More file actions
202 lines (178 loc) · 6.85 KB
/
VersionControl.ts
File metadata and controls
202 lines (178 loc) · 6.85 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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,
Path
} from '@rushstack/node-core-library';
import { RushConfiguration } from '../api/RushConfiguration';
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
): (string | undefined)[] | undefined {
if (!skipFetch) {
VersionControl._fetchRemoteBranch(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 results under the that path.
*/
public static getChangedFiles(targetBranch: string, skipFetch: boolean = false, pathPrefix?: string): string[] {
if (!skipFetch) {
VersionControl._fetchRemoteBranch(targetBranch);
}
const output: string = child_process.execSync(
`git diff ${targetBranch}... --name-only --no-renames --diff-filter=A`
).toString();
return output.split('\n').map((line) => {
if (line) {
const trimmedLine: string = line.trim();
if (!pathPrefix || Path.isUnderOrEqual(trimmedLine, pathPrefix)) {
return trimmedLine;
}
} else {
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 rushConfiguration - rush configuration
*/
public static getRemoteMasterBranch(rushConfiguration: RushConfiguration): string {
if (rushConfiguration.repositoryUrl) {
const output: string = child_process
.execSync(`git remote`)
.toString();
const normalizedRepositoryUrl: string = rushConfiguration.repositoryUrl.toUpperCase();
const matchingRemotes: string[] = output.split('\n').filter((remoteName) => {
if (remoteName) {
const remoteUrl: string = child_process.execSync(`git remote get-url ${remoteName}`)
.toString()
.trim();
if (!remoteUrl) {
return false;
}
const normalizedRemoteUrl: string = remoteUrl.toUpperCase();
if (normalizedRemoteUrl.toUpperCase() === normalizedRepositoryUrl) {
return true;
}
// When you copy a URL from the GitHub web site, they append the ".git" file extension to the URL.
// We allow that to be specified in rush.json, even though the file extension gets dropped
// by "git clone".
if (`${normalizedRemoteUrl}.GIT` === normalizedRepositoryUrl) {
return true;
}
}
return false;
});
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]}/${rushConfiguration.repositoryDefaultBranch}`;
} else {
console.log(colors.yellow(
`Unable to find a git remote matching the repository URL (${rushConfiguration.repositoryUrl}). ` +
'Detected changes are likely to be incorrect.'
));
return rushConfiguration.repositoryDefaultFullyQualifiedRemoteBranch;
}
} else {
console.log(colors.yellow(
'A git remote URL has not been specified in rush.json. Setting the baseline remote URL is recommended.'
));
return rushConfiguration.repositoryDefaultFullyQualifiedRemoteBranch;
}
}
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 _fetchRemoteBranch(remoteBranchName: string): void {
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.`
));
}
}
}