Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gate3/08d5a565bcbea923ad52a356d9027dde to your computer and use it in GitHub Desktop.
Save gate3/08d5a565bcbea923ad52a356d9027dde to your computer and use it in GitHub Desktop.
Clone a private repository using nodegit
const git = require('nodegit')
const fs = require('fs-extra')
const { URL } = require('url')
const REPO_URL = '[email protected]:org/path.git'
const CLONE_DIR = '/tmp/private-repo-clone-test'
;(async () => {
await fs.emptyDir(CLONE_DIR)
let authAttempted = false
await git.Clone.clone(REPO_URL, CLONE_DIR, {
fetchOpts: {
callbacks: {
certificateCheck: () => 1,
credentials: (url, username) => {
if (authAttempted) return git.Cred.defaultNew()
authAttempted = true
if (url.startsWith('https://') && url.includes('@')) {
url = new URL(url)
return git.Cred.userpassPlaintextNew(url.username, url.password)
} else {
return git.Cred.sshKeyFromAgent(username)
}
}
},
},
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment