Forked from mojavelinux/nodegit-private-clone-test.js
Created
December 10, 2018 21:42
-
-
Save waggertron/bd2e6df7665eaa8002b31c328f3d6d3f 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 = '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) | |
} | |
} | |
}, | |
}, | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment