This repository has been archived by the owner on Mar 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
fetch.js
226 lines (187 loc) · 5.57 KB
/
fetch.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var utils = require('shipit-utils');
var chalk = require('chalk');
var mkdirp = require('mkdirp');
var Promise = require('bluebird');
var rimraf = require('rimraf');
/**
* Fetch task.
* - Create workspace.
* - Fetch repository.
* - Checkout commit-ish.
*/
module.exports = function (gruntOrShipit) {
utils.registerTask(gruntOrShipit, 'deploy:fetch', task);
function task() {
var shipit = utils.getShipit(gruntOrShipit);
if (!shipit.config.repositoryUrl) {
shipit.log('Skipping fetching as no repository url was set.');
return;
}
return createWorkspace()
.then(initRepository)
.then(setGitConfig)
.then(addRemote)
.then(fetch)
.then(checkout)
.then(reset)
.then(merge)
.then(updateSubmodules)
.then(function () {
shipit.emit('fetched');
});
/**
* Create workspace.
*/
function createWorkspace() {
function create() {
shipit.log('Create workspace "%s"', shipit.config.workspace);
return Promise.promisify(mkdirp)(shipit.config.workspace)
.then(function () {
shipit.log(chalk.green('Workspace created.'));
});
}
if (shipit.config.shallowClone) {
shipit.log('Deleting existing workspace "%s"', shipit.config.workspace);
return Promise.promisify(rimraf)(shipit.config.workspace)
.then(create);
}
return create();
}
/**
* Initialize repository.
*/
function initRepository() {
shipit.log('Initialize local repository in "%s"', shipit.config.workspace);
return shipit.local('git init', {cwd: shipit.config.workspace})
.then(function () {
shipit.log(chalk.green('Repository initialized.'));
});
}
/**
* Set git config.
*/
function setGitConfig() {
if (!shipit.config.gitConfig) {
return Promise.resolve();
}
shipit.log('Set custom git config options for "%s"', shipit.config.workspace);
return Promise.all(Object.keys(shipit.config.gitConfig || {}).map(function (key, gitConfig) {
return shipit.local(
'git config ' + key + ' "' + shipit.config.gitConfig[key] + '"',
{cwd: shipit.config.workspace}
);
}))
.then(function () {
shipit.log(chalk.green('Git config set.'));
});
}
/**
* Add remote.
*/
function addRemote() {
shipit.log('List local remotes.');
// List remotes.
return shipit.local('git remote', {cwd: shipit.config.workspace})
.then(function (res) {
var remotes = res.stdout ? res.stdout.split(/\s/) : [];
var method = remotes.indexOf('shipit') !== -1 ? 'set-url' : 'add';
shipit.log('Update remote "%s" to local repository "%s"',
shipit.config.repositoryUrl, shipit.config.workspace);
// Update remote.
return shipit.local(
'git remote ' + method + ' shipit ' + shipit.config.repositoryUrl,
{cwd: shipit.config.workspace}
);
})
.then(function () {
shipit.log(chalk.green('Remote updated.'));
});
}
/**
* Fetch repository.
*/
function fetch() {
var fetchCommand = 'git fetch shipit --prune';
var fetchDepth = shipit.config.shallowClone ? ' --depth=1' : '';
// fetch branches and tags separate to be compatible with git versions < 1.9
fetchCommand += fetchDepth + ' && ' + fetchCommand + ' "refs/tags/*:refs/tags/*"';
shipit.log('Fetching repository "%s"', shipit.config.repositoryUrl);
return shipit.local(
fetchCommand,
{cwd: shipit.config.workspace}
)
.then(function () {
shipit.log(chalk.green('Repository fetched.'));
});
}
/**
* Checkout commit-ish.
*/
function checkout() {
shipit.log('Checking out commit-ish "%s"', shipit.config.branch);
return shipit.local(
'git checkout ' + shipit.config.branch,
{cwd: shipit.config.workspace}
)
.then(function () {
shipit.log(chalk.green('Checked out.'));
});
}
/**
* Hard reset of working tree.
*/
function reset() {
shipit.log('Resetting the working tree');
return shipit.local(
'git reset --hard HEAD',
{cwd: shipit.config.workspace}
)
.then(function () {
shipit.log(chalk.green('Reset working tree.'));
});
}
/**
* Merge branch.
*/
function merge() {
shipit.log('Testing if commit-ish is a branch.');
// Test if commit-ish is a branch.
return shipit.local(
'git branch --list ' + shipit.config.branch,
{cwd: shipit.config.workspace}
)
.then(function (res) {
var isBranch = !!res.stdout;
if (!isBranch) {
shipit.log(chalk.green('No branch, no merge.'));
return;
}
shipit.log('Commit-ish is a branch, merging...');
// Merge branch.
return shipit.local(
'git merge shipit/' + shipit.config.branch,
{cwd: shipit.config.workspace}
);
})
.then(function () {
shipit.log(chalk.green('Branch merged.'));
});
}
/**
* update submodules
*/
function updateSubmodules() {
if (!shipit.config.updateSubmodules) {
return Promise.resolve();
}
shipit.log('Updating submodules.');
return shipit.local(
'git submodule update --init --recursive',
{cwd: shipit.config.workspace}
)
.then(function () {
shipit.log(chalk.green('Submodules updated'));
});
}
}
};