Created
May 21, 2018 08:45
-
-
Save mojavelinux/173a14a1f78ab5e9b804452b3d055d05 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
Can you please explain your code?