|
3 | 3 |
|
4 | 4 | import gitInfo = require('git-repo-info'); |
5 | 5 | import * as child_process from 'child_process'; |
| 6 | +import * as os from 'os'; |
6 | 7 |
|
7 | | -import { PublishUtilities } from './PublishUtilities'; |
| 8 | +import { Utilities } from '../utilities/Utilities'; |
| 9 | +import { AlreadyReportedError } from '../utilities/AlreadyReportedError'; |
| 10 | +import { GitEmailPolicy } from './policy/GitEmailPolicy'; |
| 11 | +import { RushConfiguration } from '../api/RushConfiguration'; |
8 | 12 |
|
9 | | -export class Git { |
10 | | - private _targetBranch: string | undefined; |
11 | | - |
12 | | - public static detectIfGitIsSupported(): boolean { |
13 | | - const command: string = process.platform === 'win32' ? 'where' : 'which'; |
14 | | - const result: child_process.SpawnSyncReturns<string> = child_process.spawnSync(command, ['git']); |
15 | | - |
16 | | - if (result.status !== 0) { |
17 | | - return false; |
18 | | - } |
| 13 | +interface IResultOrError<TResult> { |
| 14 | + error?: Error; |
| 15 | + result?: TResult; |
| 16 | +} |
19 | 17 |
|
20 | | - try { |
21 | | - return !!gitInfo().sha; |
22 | | - } catch (e) { |
23 | | - return false; // Unexpected, but possible if the .git directory is corrupted. |
| 18 | +export class Git { |
| 19 | + private static _hasGit: boolean | undefined = undefined; |
| 20 | + private static _gitPath: string | undefined; |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns the path to the git binary if git is found. If git can't be found, return undefined. |
| 24 | + */ |
| 25 | + public static getGitPath(): string | undefined { |
| 26 | + if (Git._hasGit === undefined) { |
| 27 | + const command: string = process.platform === 'win32' ? 'where' : 'which'; |
| 28 | + const result: child_process.SpawnSyncReturns<string> = child_process.spawnSync(command, ['git']); |
| 29 | + |
| 30 | + if (result.status === 0) { |
| 31 | + Git._gitPath = result.stdout; |
| 32 | + Git._hasGit = !!result.stdout; |
| 33 | + } |
24 | 34 | } |
25 | | - } |
26 | 35 |
|
27 | | - constructor(targetBranch: string | undefined) { |
28 | | - this._targetBranch = targetBranch; |
| 36 | + return Git._gitPath; |
29 | 37 | } |
30 | 38 |
|
31 | | - public checkout(branchName: string | undefined, createBranch?: boolean): void { |
32 | | - const params: string = `checkout ${createBranch ? '-b ' : ''}${branchName}`; |
33 | | - |
34 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', params.split(' ')); |
| 39 | + public static isGitPresent(): boolean { |
| 40 | + return !!Git.getGitPath(); |
35 | 41 | } |
36 | 42 |
|
37 | | - public merge(branchName: string): void { |
38 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', `merge ${branchName} --no-edit`.split(' ')); |
39 | | - } |
40 | | - |
41 | | - public deleteBranch(branchName: string, hasRemote: boolean = true): void { |
42 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', `branch -d ${branchName}`.split(' ')); |
43 | | - if (hasRemote) { |
44 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', `push origin --delete ${branchName}`.split(' ')); |
| 43 | + /** |
| 44 | + * Checks if git is supported and if the current path is under a git working tree. |
| 45 | + */ |
| 46 | + public static isPathUnderGitWorkingTree(): boolean { |
| 47 | + if (Git.isGitPresent()) { // Do we even have a git binary? |
| 48 | + try { |
| 49 | + return !!gitInfo().sha; |
| 50 | + } catch (e) { |
| 51 | + return false; // Unexpected, but possible if the .git directory is corrupted. |
| 52 | + } |
| 53 | + } else { |
| 54 | + return false; |
45 | 55 | } |
46 | 56 | } |
47 | 57 |
|
48 | | - public pull(): void { |
49 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', `pull origin ${this._targetBranch}`.split(' ')); |
50 | | - } |
| 58 | + public static getGitEmail(rushConfiguration: RushConfiguration): string { |
| 59 | + // Determine the user's account |
| 60 | + // Ex: "bob@example.com" |
| 61 | + const emailResult: IResultOrError<string> = Git.tryGetGitEmail(); |
| 62 | + if (emailResult.error) { |
| 63 | + console.log( |
| 64 | + [ |
| 65 | + `Error: ${emailResult.error.message}`, |
| 66 | + 'Unable to determine your Git configuration using this command:', |
| 67 | + '', |
| 68 | + ' git config user.email', |
| 69 | + '' |
| 70 | + ].join(os.EOL) |
| 71 | + ); |
| 72 | + throw new AlreadyReportedError(); |
| 73 | + } |
51 | 74 |
|
52 | | - public addChanges(pathspec?: string, workingDirectory?: string): void { |
53 | | - const files: string = pathspec ? pathspec : '.'; |
54 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', ['add', files], |
55 | | - workingDirectory ? workingDirectory : process.cwd()); |
56 | | - } |
| 75 | + if (!emailResult.result) { |
| 76 | + console.log([ |
| 77 | + 'This operation requires that a git email be specified.', |
| 78 | + '', |
| 79 | + `If you didn't configure your email yet, try something like this:`, |
| 80 | + '', |
| 81 | + ...GitEmailPolicy.getEmailExampleLines(rushConfiguration), |
| 82 | + '' |
| 83 | + ].join(os.EOL)); |
| 84 | + throw new AlreadyReportedError(); |
| 85 | + } |
57 | 86 |
|
58 | | - public addTag(shouldExecute: boolean, packageName: string, packageVersion: string): void { |
59 | | - // Tagging only happens if we're publishing to real NPM and committing to git. |
60 | | - const tagName: string = PublishUtilities.createTagname(packageName, packageVersion); |
61 | | - PublishUtilities.execCommand( |
62 | | - !!this._targetBranch && shouldExecute, |
63 | | - 'git', |
64 | | - ['tag', '-a', tagName, '-m', `${packageName} v${packageVersion}`]); |
| 87 | + return emailResult.result; |
65 | 88 | } |
66 | 89 |
|
67 | | - public commit(message?: string): void { |
68 | | - const commitMessage: string = message ? message : 'Applying package updates.'; |
69 | | - PublishUtilities.execCommand(!!this._targetBranch, 'git', ['commit', '-m', commitMessage]); |
70 | | - } |
| 90 | + private static tryGetGitEmail(): IResultOrError<string> { |
| 91 | + const gitPath: string | undefined = Git.getGitPath(); |
| 92 | + if (!gitPath) { |
| 93 | + return { |
| 94 | + error: new Error('Git isn\'t present on the path') |
| 95 | + }; |
| 96 | + } |
71 | 97 |
|
72 | | - public push(branchName: string | undefined): void { |
73 | | - PublishUtilities.execCommand( |
74 | | - !!this._targetBranch, |
75 | | - 'git', |
76 | | - ['push', 'origin', 'HEAD:' + branchName, '--follow-tags', '--verbose']); |
| 98 | + try { |
| 99 | + return { |
| 100 | + result: Utilities.executeCommandAndCaptureOutput( |
| 101 | + 'git', |
| 102 | + ['config', 'user.email'], |
| 103 | + '.' |
| 104 | + ).trim() |
| 105 | + }; |
| 106 | + } catch (e) { |
| 107 | + return { |
| 108 | + error: e |
| 109 | + }; |
| 110 | + } |
77 | 111 | } |
78 | 112 | } |
0 commit comments