33
44import gitInfo = require( 'git-repo-info' ) ;
55import * as child_process from 'child_process' ;
6+ import * as os from 'os' ;
67
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' ;
12+
13+ interface IResultOrError < TResult > {
14+ error ?: Error ;
15+ result ?: TResult ;
16+ }
817
918export class Git {
1019 private static _hasGit : boolean | undefined = undefined ;
1120 private static _gitPath : string | undefined ;
1221
13- private _targetBranch : string | undefined ;
14-
1522 public static getGitPath ( ) : string | undefined {
1623 if ( Git . _hasGit === undefined ) {
1724 const command : string = process . platform === 'win32' ? 'where' : 'which' ;
1825 const result : child_process . SpawnSyncReturns < string > = child_process . spawnSync ( command , [ 'git' ] ) ;
1926
2027 if ( result . status === 0 ) {
2128 Git . _gitPath = result . stdout ;
29+ Git . _hasGit = ! ! result . stdout ;
2230 }
2331 }
2432
@@ -37,55 +45,57 @@ export class Git {
3745 }
3846 }
3947
40- constructor ( targetBranch : string | undefined ) {
41- this . _targetBranch = targetBranch ;
42- }
43-
44- public checkout ( branchName : string | undefined , createBranch ?: boolean ) : void {
45- const params : string = `checkout ${ createBranch ? '-b ' : '' } ${ branchName } ` ;
46-
47- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , params . split ( ' ' ) ) ;
48- }
49-
50- public merge ( branchName : string ) : void {
51- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , `merge ${ branchName } --no-edit` . split ( ' ' ) ) ;
52- }
53-
54- public deleteBranch ( branchName : string , hasRemote : boolean = true ) : void {
55- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , `branch -d ${ branchName } ` . split ( ' ' ) ) ;
56- if ( hasRemote ) {
57- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , `push origin --delete ${ branchName } ` . split ( ' ' ) ) ;
48+ public static getGitEmail ( rushConfiguration : RushConfiguration ) : string {
49+ // Determine the user's account
50+ // Ex: "bob@example.com"
51+ const emailResult : IResultOrError < string > = Git . tryGetGitEmail ( ) ;
52+ if ( emailResult . error ) {
53+ console . log (
54+ [
55+ `Error: ${ emailResult . error . message } ` ,
56+ 'Unable to determine your Git configuration using this command:' ,
57+ '' ,
58+ ' git config user.email' ,
59+ ''
60+ ] . join ( os . EOL )
61+ ) ;
62+ throw new AlreadyReportedError ( ) ;
5863 }
59- }
6064
61- public pull ( ) : void {
62- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , `pull origin ${ this . _targetBranch } ` . split ( ' ' ) ) ;
63- }
64-
65- public addChanges ( pathspec ?: string , workingDirectory ?: string ) : void {
66- const files : string = pathspec ? pathspec : '.' ;
67- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , [ 'add' , files ] ,
68- workingDirectory ? workingDirectory : process . cwd ( ) ) ;
69- }
65+ if ( ! emailResult . result ) {
66+ console . log ( [
67+ 'Git email was not specified. Unable to continue.' ,
68+ '' ,
69+ `If you didn't configure your e-mail yet, try something like this:` ,
70+ '' ,
71+ ...GitEmailPolicy . getEmailExampleLines ( rushConfiguration )
72+ ] . join ( os . EOL ) ) ;
73+ throw new AlreadyReportedError ( ) ;
74+ }
7075
71- public addTag ( shouldExecute : boolean , packageName : string , packageVersion : string ) : void {
72- // Tagging only happens if we're publishing to real NPM and committing to git.
73- const tagName : string = PublishUtilities . createTagname ( packageName , packageVersion ) ;
74- PublishUtilities . execCommand (
75- ! ! this . _targetBranch && shouldExecute ,
76- 'git' ,
77- [ 'tag' , '-a' , tagName , '-m' , `${ packageName } v${ packageVersion } ` ] ) ;
76+ return emailResult . result ;
7877 }
7978
80- public commit ( message ?: string ) : void {
81- const commitMessage : string = message ? message : 'Applying package updates.' ;
82- PublishUtilities . execCommand ( ! ! this . _targetBranch , 'git' , [ 'commit' , '-m' , commitMessage ] ) ;
83- }
79+ private static tryGetGitEmail ( ) : IResultOrError < string > {
80+ const gitPath : string | undefined = Git . getGitPath ( ) ;
81+ if ( ! gitPath ) {
82+ return {
83+ error : new Error ( 'Git isn\'t present on the path' )
84+ } ;
85+ }
8486
85- public push ( branchName : string | undefined ) : void {
86- PublishUtilities . execCommand (
87- ! ! this . _targetBranch ,
88- 'git' ,
89- [ 'push' , 'origin' , 'HEAD:' + branchName , '--follow-tags' , '--verbose' ] ) ;
87+ try {
88+ return {
89+ result : Utilities . executeCommandAndCaptureOutput (
90+ 'git' ,
91+ [ 'config' , 'user.email' ] ,
92+ '.'
93+ ) . trim ( )
94+ } ;
95+ } catch ( e ) {
96+ return {
97+ error : e
98+ } ;
99+ }
90100 }
91101}
0 commit comments