-
Notifications
You must be signed in to change notification settings - Fork 7
/
git-clone.js
41 lines (39 loc) · 1.04 KB
/
git-clone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var Git = require('nodegit')
module.exports = function (git_repo, code_dir, credentials, sha) {
var opts = {
fetchOpts: {
callbacks: {
certificateCheck: function () {
return 1
}
}
}
}
// Auth via token
if (credentials && typeof credentials === 'string') {
opts.fetchOpts.callbacks.credentials = function () {
return Git.Cred.userpassPlaintextNew(credentials, 'x-oauth-basic')
}
}
// Auth via SSH Keys
if (credentials && typeof credentials === 'object') {
opts.fetchOpts.callbacks.credentials = function (url, user) {
return Git.Cred.sshKeyMemoryNew(
user,
credentials.public_key,
credentials.private_key,
''
)
}
}
return Git.Clone(git_repo, code_dir, opts).then(function (repo) {
if (sha) {
return repo.getCommit(Git.Oid.fromString(sha)).then(function (commit) {
return Git.Reset.reset(repo, commit, Git.Reset.TYPE.HARD).then(function () {
return repo
})
})
}
return repo
})
}