🌳
pt0/deployF/libF/gitF/gitRepoF.mts
1import * as _ from 'lodash-es'
2import fs from 'fs'
3import { execSync } from 'child_process'
10type LiveRepoExecProps = {
11 git_repo_name?: string
12 cmd: string
13 doFetch?: boolean
14 verboseOutCmd?: boolean
17export const liveRepoExec = async ({git_repo_name, cmd, doFetch=false, verboseOutCmd}: LiveRepoExecProps) => {
18 if (doFetch) {
19 await liveRepoExec({git_repo_name, cmd: `git fetch`})
20 }
22 let existRepoPath = _.startsWith(git_repo_name, '/') && git_repo_name
23 if (!existRepoPath) {
24 const ptGetDir = pathDownJoin(ptDir, '.git')
25 if (fs.existsSync(ptGetDir)) {
26 existRepoPath = ptGetDir
27 }
28 }
30 assertDefined(existRepoPath)
32 const fullCmd = `GIT_DIR=${existRepoPath} ${cmd}`
33 if (verboseOutCmd) {
34 console.log(fullCmd)
35 }
36 const ret = await doExec(fullCmd)
37 return ret.stdout.trim()
40type GetHeadGitShaProps = {
41 filterPathList?: string[]
42 git_repo_name?: string
43 verboseOutCmd?: boolean
46export let headShaSnapshot: string | undefined
47try { headShaSnapshot = execSync('git rev-parse HEAD', {cwd: ptDir, encoding: 'utf8', stdio: 'pipe'}).trim() } catch {}
49export const getHeadGitSha = async ({filterPathList=['.'], git_repo_name, verboseOutCmd}: GetHeadGitShaProps = {}) => {
50 const isDefaultHead = filterPathList.length === 1 && filterPathList[0] === '.' && !git_repo_name
51 if (isDefaultHead && headShaSnapshot) return headShaSnapshot
52 const cmd = `git log --format=format:%H -1 -- ${filterPathList.join(' ')}`
53 try {
54 return await liveRepoExec({git_repo_name, cmd, verboseOutCmd})
55 } catch(err) {
56 throw new PtErr('!getHeadGitSha', {cmd, err, git_repo_name})
57 }