/
git.ts
338 lines (309 loc) · 10.4 KB
/
git.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import * as PackageManagerUtils from '@expo/package-manager';
import spawnAsync from '@expo/spawn-async';
import { Errors } from '@oclif/core';
import chalk from 'chalk';
import path from 'path';
import Log, { learnMore } from '../../log';
import { ora } from '../../ora';
import { confirmAsync, promptAsync } from '../../prompts';
import {
doesGitRepoExistAsync,
getGitDiffOutputAsync,
gitDiffAsync,
gitStatusAsync,
isGitInstalledAsync,
} from '../git';
import { Client } from '../vcs';
export default class GitClient extends Client {
public override async ensureRepoExistsAsync(): Promise<void> {
try {
if (!(await isGitInstalledAsync())) {
Log.error(
`${chalk.bold('git')} command not found. Install it before proceeding or set ${chalk.bold(
'EAS_NO_VCS=1'
)} to use EAS CLI without Git (or any other version control system).`
);
Log.error(learnMore('https://expo.fyi/eas-vcs-workflow'));
Errors.exit(1);
}
} catch (error: any) {
Log.error(
`${chalk.bold('git')} found, but ${chalk.bold(
'git --help'
)} exited with status ${error?.status}${error?.stderr ? `:` : '.'}`
);
if (error?.stderr) {
Log.error(error?.stderr);
}
Log.error(
`Repair your Git installation, or set ${chalk.bold(
'EAS_NO_VCS=1'
)} to use EAS CLI without Git (or any other version control system).`
);
Log.error(learnMore('https://expo.fyi/eas-vcs-workflow'));
Errors.exit(1);
}
if (await doesGitRepoExistAsync()) {
return;
}
Log.warn("It looks like you haven't initialized the git repository yet.");
Log.warn('EAS requires you to use a git repository for your project.');
const cwd = process.cwd();
const repoRoot = PackageManagerUtils.findWorkspaceRoot(cwd) ?? cwd;
const confirmInit = await confirmAsync({
message: `Would you like us to run 'git init' in ${repoRoot} for you?`,
});
if (!confirmInit) {
throw new Error(
'A git repository is required for building your project. Initialize it and run this command again.'
);
}
await spawnAsync('git', ['init'], { cwd: repoRoot });
Log.log("We're going to make an initial commit for your repository.");
const { message } = await promptAsync({
type: 'text',
name: 'message',
message: 'Commit message:',
initial: 'Initial commit',
validate: (input: string) => input !== '',
});
await this.commitAsync({ commitAllFiles: true, commitMessage: message, nonInteractive: false });
}
public override async commitAsync({
commitMessage,
commitAllFiles,
nonInteractive = false,
}: {
commitMessage: string;
commitAllFiles?: boolean;
nonInteractive: boolean;
}): Promise<void> {
await ensureGitConfiguredAsync({ nonInteractive });
try {
if (commitAllFiles) {
await spawnAsync('git', ['add', '-A']);
}
await spawnAsync('git', ['add', '-u']);
await spawnAsync('git', ['commit', '-m', commitMessage]);
} catch (err: any) {
if (err?.stdout) {
Log.error(err.stdout);
}
if (err?.stderr) {
Log.error(err.stderr);
}
throw err;
}
}
public override async isCommitRequiredAsync(): Promise<boolean> {
return await this.hasUncommittedChangesAsync();
}
public override async showChangedFilesAsync(): Promise<void> {
const gitStatusOutput = await gitStatusAsync({ showUntracked: true });
Log.log(gitStatusOutput);
}
public override async hasUncommittedChangesAsync(): Promise<boolean> {
const changes = await gitStatusAsync({ showUntracked: true });
return changes.length > 0;
}
public async getRootPathAsync(): Promise<string> {
return (await spawnAsync('git', ['rev-parse', '--show-toplevel'])).stdout.trim();
}
public async makeShallowCopyAsync(destinationPath: string): Promise<void> {
if (await this.hasUncommittedChangesAsync()) {
// it should already be checked before this function is called, but in case it wasn't
// we want to ensure that any changes were introduced by call to `setGitCaseSensitivityAsync`
throw new Error('You have some uncommitted changes in your repository.');
}
let gitRepoUri;
if (process.platform === 'win32') {
// getRootDirectoryAsync() will return C:/path/to/repo on Windows and path
// prefix should be file:///
gitRepoUri = `file:///${await this.getRootPathAsync()}`;
} else {
// getRootDirectoryAsync() will /path/to/repo, and path prefix should be
// file:/// so only file:// needs to be prepended
gitRepoUri = `file://${await this.getRootPathAsync()}`;
}
const isCaseSensitive = await isGitCaseSensitiveAsync();
await setGitCaseSensitivityAsync(true);
try {
if (await this.hasUncommittedChangesAsync()) {
Log.error('Detected inconsistent filename casing between your local filesystem and git.');
Log.error('This will likely cause your build to fail. Impacted files:');
await spawnAsync('git', ['status', '--short'], { stdio: 'inherit' });
Log.newLine();
Log.error(
`Error: Resolve filename casing inconsistencies before proceeding. ${learnMore(
'https://expo.fyi/macos-ignorecase'
)}`
);
throw new Error('You have some uncommitted changes in your repository.');
}
await spawnAsync('git', [
'clone',
'--no-hardlinks',
'--depth',
'1',
gitRepoUri,
destinationPath,
]);
} finally {
await setGitCaseSensitivityAsync(isCaseSensitive);
}
}
public override async getCommitHashAsync(): Promise<string | undefined> {
try {
return (await spawnAsync('git', ['rev-parse', 'HEAD'])).stdout.trim();
} catch {
return undefined;
}
}
public override async trackFileAsync(file: string): Promise<void> {
await spawnAsync('git', ['add', '--intent-to-add', file]);
}
public override async getBranchNameAsync(): Promise<string | null> {
try {
return (await spawnAsync('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.trim();
} catch {
return null;
}
}
public override async getLastCommitMessageAsync(): Promise<string | null> {
try {
return (await spawnAsync('git', ['--no-pager', 'log', '-1', '--pretty=%B'])).stdout.trim();
} catch {
return null;
}
}
public override async showDiffAsync(): Promise<void> {
const outputTooLarge = (await getGitDiffOutputAsync()).split(/\r\n|\r|\n/).length > 100;
await gitDiffAsync({ withPager: outputTooLarge });
}
public async isFileUntrackedAsync(path: string): Promise<boolean> {
const withUntrackedFiles = await gitStatusAsync({ showUntracked: true });
const trackedFiles = await gitStatusAsync({ showUntracked: false });
const pathWithoutLeadingDot = path.replace(/^\.\//, ''); // remove leading './' from path
return (
withUntrackedFiles.includes(pathWithoutLeadingDot) &&
!trackedFiles.includes(pathWithoutLeadingDot)
);
}
public override async isFileIgnoredAsync(filePath: string): Promise<boolean> {
try {
await spawnAsync('git', ['check-ignore', '-q', filePath], {
cwd: path.normalize(await this.getRootPathAsync()),
});
return true;
} catch {
return false;
}
}
public override canGetLastCommitMessage(): boolean {
return true;
}
}
async function ensureGitConfiguredAsync({
nonInteractive,
}: {
nonInteractive: boolean;
}): Promise<void> {
let usernameConfigured = true;
let emailConfigured = true;
try {
await spawnAsync('git', ['config', '--get', 'user.name']);
} catch (err: any) {
Log.debug(err);
usernameConfigured = false;
}
try {
await spawnAsync('git', ['config', '--get', 'user.email']);
} catch (err: any) {
Log.debug(err);
emailConfigured = false;
}
if (usernameConfigured && emailConfigured) {
return;
}
Log.warn(
`You need to configure Git with your ${[
!usernameConfigured && 'username (user.name)',
!emailConfigured && 'email address (user.email)',
]
.filter(i => i)
.join(' and ')}`
);
if (nonInteractive) {
throw new Error('Git cannot be configured automatically in non-interactive mode');
}
if (!usernameConfigured) {
const { username } = await promptAsync({
type: 'text',
name: 'username',
message: 'Username:',
validate: (input: string) => input !== '',
});
const spinner = ora(
`Running ${chalk.bold(`git config --local user.name ${username}`)}`
).start();
try {
await spawnAsync('git', ['config', '--local', 'user.name', username]);
spinner.succeed();
} catch (err: any) {
spinner.fail();
throw err;
}
}
if (!emailConfigured) {
const { email } = await promptAsync({
type: 'text',
name: 'email',
message: 'Email address:',
validate: (input: string) => input !== '',
});
const spinner = ora(`Running ${chalk.bold(`git config --local user.email ${email}`)}`).start();
try {
await spawnAsync('git', ['config', '--local', 'user.email', email]);
spinner.succeed();
} catch (err: any) {
spinner.fail();
throw err;
}
}
}
/**
* Checks if git is configured to be case sensitive
* @returns {boolean | undefined}
* - boolean - is git case sensitive
* - undefined - case sensitivity is not configured and git is using default behavior
*/
export async function isGitCaseSensitiveAsync(): Promise<boolean | undefined> {
if (process.platform !== 'darwin') {
return undefined;
}
try {
const result = await spawnAsync('git', ['config', '--get', 'core.ignorecase']);
const isIgnoreCaseEnabled = result.stdout.trim();
if (isIgnoreCaseEnabled === '') {
return undefined;
} else if (isIgnoreCaseEnabled === 'true') {
return false;
} else {
return true;
}
} catch {
return undefined;
}
}
async function setGitCaseSensitivityAsync(enable: boolean | undefined): Promise<void> {
// we are assuming that if someone sets that on non-macos device then
// they know what they are doing
if (process.platform !== 'darwin') {
return;
}
if (enable === undefined) {
await spawnAsync('git', ['config', '--unset', 'core.ignorecase']);
} else {
await spawnAsync('git', ['config', 'core.ignorecase', String(!enable)]);
}
}