-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathworker.js
More file actions
174 lines (155 loc) · 4.38 KB
/
worker.js
File metadata and controls
174 lines (155 loc) · 4.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var path = require('path')
, fs = require('fs')
, spawn = require('child_process').spawn
, mkdirp = require('mkdirp')
, utils = require('./lib')
function safespawn() {
var c
try {
c = spawn.apply(null, arguments)
} catch (e) {
throw new Error('Failed to start command: ' + JSON.stringify([].slice.call(arguments)))
}
c.on('error', function (err) {
// suppress node errors
})
return c
}
function httpCloneCmd(config, branch) {
var urls = utils.httpUrl(config)
, screen = 'git clone --recursive ' + urls[1] + ' .'
, args = ['clone', '--recursive', urls[0], '.']
if (branch) {
args = args.concat(['-b', branch])
screen += ' -b ' + branch
}
return {
command: 'git',
args: args,
screen: screen
}
}
function pull(dest, config, context, done) {
context.cmd({
cmd: 'git reset --hard',
cwd: dest
}, function (exitCode) {
utils.gitCmd('git pull', dest, config.auth, context, done)
})
}
function gitVersion(next) {
var child = safespawn('git', ['--version'])
, out = ''
child.stdout.setEncoding('utf8')
child.stderr.setEncoding('utf8')
child.stdout.on('data', function (data) {
out += data
})
child.stderr.on('data', function (data) {
out += data
})
child.on('close', function (code) {
if (code) return next(new Error('Failed to get git version: ' + out))
next(null, out)
})
child.on('error', function () {})
}
function clone(dest, config, ref, context, done) {
if (config.auth.type === 'ssh') {
var cmd = 'git clone --recursive ' + utils.sshUrl(config)[0] + ' .'
if (ref.branch) {
cmd += ' -b ' + ref.branch
// this /only/ gets the one branch; so only use if we won't be caching
if (!config.cache) cmd += ' --depth 1'
}
return utils.gitaneCmd(cmd, dest, config.auth.privkey, context, done)
}
context.cmd({
cmd: httpCloneCmd(config),
cwd: dest
}, done)
}
function badCode(name, code) {
var e = new Error(name + ' failed with code ' + code)
e.code = code
e.exitCode = code
return e
}
module.exports = {
init: function (dirs, config, job, done) {
return done(null, {
config: config,
fetch: function (context, done) {
module.exports.fetch(dirs.data, config, job, context, done)
}
})
},
fetch: fetch
}
function getMasterPrivKey(branches) {
for (var i=0; i<branches.length; i++) {
if (branches[i].name === 'master') {
return branches[i].privkey
}
}
}
function checkoutRef(dest, cmd, ref, done) {
return cmd({
cmd: 'git checkout -qf ' + utils.shellEscape(ref.id || ref.branch),
cwd: dest
}, function (exitCode) {
done(exitCode && badCode('Checkout', exitCode))
})
}
function fetch(dest, config, job, context, done) {
if (config.auth.type === 'ssh' && !config.auth.privkey) {
config.auth.privkey = getMasterPrivKey(job.project.branches)
}
var get = pull
, cloning = false
, pleaseClone = function () {
cloning = true
mkdirp(dest, function () {
clone(dest, config, job.ref, context, updateCache)
})
}
if (!config.cache) return pleaseClone()
context.cachier.get(dest, function (err) {
if (err) return pleaseClone()
// make sure .git exists
fs.exists(path.join(dest, '.git'), function (exists) {
if (exists) {
context.comment('restored code from cache')
return pull(dest, config, context, updateCache)
}
safespawn('rm', ['-rf', dest]).on('close', function (exitCode) {
pleaseClone()
})
})
})
function updateCache(exitCode) {
if (exitCode) return done(badCode('Git ' + (cloning ? 'clone' : 'pull'), exitCode))
if (!config.cache) return gotten()
context.comment('saved code to cache')
context.cachier.update(dest, gotten)
}
function gotten (err) {
if (err) return done(err)
// fetch the ref
if (job.ref.branch && !job.ref.fetch) {
return checkoutRef(dest, context.cmd, job.ref, done)
}
fetchRef(job.ref.fetch, dest, config.auth, context, done)
}
}
function fetchRef(what, dest, auth, context, done) {
utils.gitCmd('git fetch origin ' + utils.shellEscape(what), dest, auth, context, function (exitCode) {
if (exitCode) return done(badCode('Fetch ' + what, exitCode))
context.cmd({
cmd: 'git checkout -qf FETCH_HEAD',
cwd: dest
}, function (exitCode) {
done(exitCode && badCode('Checkout', exitCode))
})
})
}