-
-
Save gate3/08d5a565bcbea923ad52a356d9027dde to your computer and use it in GitHub Desktop.
Clone a private repository using nodegit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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