8
8
9
9
import { performance } from "perf_hooks"
10
10
import { isAbsolute , join , normalize , posix , relative , resolve } from "path"
11
- import { isString } from "lodash-es"
12
11
import fsExtra from "fs-extra"
13
12
import { PassThrough } from "stream"
14
13
import type {
@@ -67,21 +66,23 @@ export function parseGitUrl(url: string) {
67
66
(e.g. https://github.com/org/repo.git#main). Actually got: '${ url } '` ,
68
67
} )
69
68
}
70
- const parsed = { repositoryUrl : parts [ 0 ] , hash : parts [ 1 ] }
71
- return parsed
69
+ return { repositoryUrl : parts [ 0 ] , hash : parts [ 1 ] }
72
70
}
73
71
74
72
export interface GitCli {
75
- ( ...args : ( string | undefined ) [ ] ) : Promise < string [ ] >
73
+ /**
74
+ * @throws ChildProcessError
75
+ */
76
+ ( ...args : string [ ] ) : Promise < string [ ] >
76
77
}
77
78
78
79
export function gitCli ( log : Log , cwd : string , failOnPrompt = false ) : GitCli {
79
80
/**
80
81
* @throws ChildProcessError
81
82
*/
82
- return async ( ...args : ( string | undefined ) [ ] ) => {
83
+ return async ( ...args : string [ ] ) => {
83
84
log . silly ( `Calling git with args '${ args . join ( " " ) } ' in ${ cwd } ` )
84
- const { stdout } = await exec ( "git" , args . filter ( isString ) , {
85
+ const { stdout } = await exec ( "git" , args , {
85
86
cwd,
86
87
maxBuffer : 10 * 1024 * 1024 ,
87
88
env : failOnPrompt ? { GIT_TERMINAL_PROMPT : "0" , GIT_ASKPASS : "true" } : undefined ,
@@ -90,7 +91,7 @@ export function gitCli(log: Log, cwd: string, failOnPrompt = false): GitCli {
90
91
}
91
92
}
92
93
93
- async function getModifiedFiles ( git : GitCli , path : string ) {
94
+ export async function getModifiedFiles ( git : GitCli , path : string ) : Promise < string [ ] > {
94
95
try {
95
96
return await git ( "diff-index" , "--name-only" , "HEAD" , path )
96
97
} catch ( err ) {
@@ -103,6 +104,26 @@ async function getModifiedFiles(git: GitCli, path: string) {
103
104
}
104
105
}
105
106
107
+ export async function getLastCommitHash ( git : GitCli ) : Promise < string > {
108
+ const result = await git ( "rev-parse" , "HEAD" )
109
+ return result [ 0 ]
110
+ }
111
+
112
+ export async function getRepositoryRoot ( git : GitCli ) : Promise < string > {
113
+ const result = await git ( "rev-parse" , "--show-toplevel" )
114
+ return result [ 0 ]
115
+ }
116
+
117
+ export async function getBranchName ( git : GitCli ) : Promise < string > {
118
+ const result = await git ( "rev-parse" , "--abbrev-ref" , "HEAD" )
119
+ return result [ 0 ]
120
+ }
121
+
122
+ export async function getOriginUrl ( git : GitCli ) : Promise < string > {
123
+ const result = await git ( "config" , "--get" , "remote.origin.url" )
124
+ return result [ 0 ]
125
+ }
126
+
106
127
interface GitSubTreeIncludeExcludeFiles extends BaseIncludeExcludeFiles {
107
128
hasIncludes : boolean
108
129
}
@@ -170,7 +191,7 @@ export class GitHandler extends VcsHandler {
170
191
171
192
try {
172
193
const git = gitCli ( log , path , failOnPrompt )
173
- const repoRoot = ( await git ( "rev-parse" , "--show-toplevel" ) ) [ 0 ]
194
+ const repoRoot = await getRepositoryRoot ( git )
174
195
this . repoRoots . set ( path , repoRoot )
175
196
return repoRoot
176
197
} catch ( err ) {
@@ -572,7 +593,7 @@ export class GitHandler extends VcsHandler {
572
593
const gitLog = log . createLog ( { name, showDuration : true } ) . info ( "Getting remote state" )
573
594
await git ( "remote" , "update" )
574
595
575
- const localCommitId = ( await git ( "rev-parse" , "HEAD" ) ) [ 0 ]
596
+ const localCommitId = await getLastCommitHash ( git )
576
597
const remoteCommitId = isSha1 ( hash ) ? hash : getCommitIdFromRefList ( await git ( "ls-remote" , repositoryUrl , hash ) )
577
598
578
599
if ( localCommitId !== remoteCommitId ) {
@@ -679,16 +700,16 @@ export class GitHandler extends VcsHandler {
679
700
}
680
701
681
702
try {
682
- output . branch = ( await git ( "rev-parse" , "--abbrev-ref" , "HEAD" ) ) [ 0 ]
683
- output . commitHash = ( await git ( "rev-parse" , "HEAD" ) ) [ 0 ]
703
+ output . branch = await getBranchName ( git )
704
+ output . commitHash = await getLastCommitHash ( git )
684
705
} catch ( err ) {
685
706
if ( err instanceof ChildProcessError && err . details . code !== 128 ) {
686
707
throw err
687
708
}
688
709
}
689
710
690
711
try {
691
- output . originUrl = ( await git ( "config" , "--get" , "remote.origin.url" ) ) [ 0 ]
712
+ output . originUrl = await getOriginUrl ( git )
692
713
} catch ( err ) {
693
714
// Just ignore if not available
694
715
log . silly ( `Tried to retrieve git remote.origin.url but encountered an error: ${ err } ` )
0 commit comments