Skip to content

Commit

Permalink
feat(config): support commit SHA in repos[].head
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Apr 15, 2024
1 parent 16fbb03 commit b9aecfb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
51 changes: 43 additions & 8 deletions src/core/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import { RepoConfig, RootConfig } from './config'
import { sparseClone } from './sparseClone'
import { isHomeDir } from './helpers'
import { Package } from './Package'
import shell from '@cush/shell'

export const git = {
clone(cwd: string, repo: RepoConfig, path: string) {
if (repo.head?.includes(':')) {
const [head, subpath] = repo.head.split(':')
return sparseClone(join(cwd, path), repo.url, head, subpath)
const { branch, commit, subpath } = parseGitString(repo.head)

if (subpath) {
return sparseClone(join(cwd, path), repo.url, branch, commit, subpath)
}

let checkoutCommand = `git clone ${repo.url} ${path} --depth 1`
if (branch) {
checkoutCommand += ` -b ${branch}`
}
return exec(
`git clone ${repo.url} ${path} --depth 1`,
[repo.head ? ['-b', repo.head] : null],
{ cwd }
)
if (commit) {
checkoutCommand += ` && git checkout ${commit}`
return shell(checkoutCommand, { cwd })
}
return exec(checkoutCommand, { cwd })
},
getRemoteUrl(cwd: string, remote = 'origin') {
return exec.sync(`git remote get-url ${remote}`, { cwd })
Expand Down Expand Up @@ -71,3 +78,31 @@ export const git = {
return gitRoots
},
}

function parseGitString(
gitString: string | undefined
): {
branch: string | undefined
commit: string | undefined
subpath: string | undefined
} {
let branch: string | undefined
let commit: string | undefined
let subpath: string | undefined

if (gitString?.length > 0) {
if (gitString.includes(':')) {
;[gitString, subpath] = gitString.split(':')
}

if (gitString.includes('#')) {
;[branch, commit] = gitString.split('#')
} else if (/^[0-9a-f]{40}$/.test(gitString)) {
commit = gitString
} else {
branch = gitString
}
}

return { branch, commit, subpath }
}
13 changes: 10 additions & 3 deletions src/core/sparseClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import fs from 'saxon/sync'
export function sparseClone(
dest: string,
url: string,
branch: string,
subpath: string
branch: string | undefined,
commit: string | undefined,
subpath: string | undefined
) {
fs.mkdir(dest)
let checkoutCommand = `git clone ${url} . --no-checkout --depth 1`
if (commit) {
checkoutCommand += ` && git checkout ${commit}`
} else if (branch) {
checkoutCommand += ` -b ${branch}`
}
return shell(
`git clone ${url} . -b ${branch} --no-checkout --depth 1
`${checkoutCommand}
git config core.sparseCheckout true
echo "${subpath}" >> .git/info/sparse-checkout
git checkout
Expand Down

0 comments on commit b9aecfb

Please sign in to comment.