Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Created May 21, 2018 08:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mojavelinux/173a14a1f78ab5e9b804452b3d055d05 to your computer and use it in GitHub Desktop.
Save mojavelinux/173a14a1f78ab5e9b804452b3d055d05 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 = 'git@github.com: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)
}
}
},
},
})
})()
@Sushma-13
Copy link

Can you please explain your code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment