-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
138 lines (119 loc) · 3.97 KB
/
index.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
var fs = require('fs'),
path = require('path'),
childProcess = require('child_process'),
spawnWithSanityChecks = require('smart-spawn');
var gitBinary = childProcess.spawnSync('git', ['--version']).status === 0;
var NodeGit;
try {
NodeGit = require('nodegit');
} catch (e) {
// NodeGit not supported on this platform
NodeGit = null;
}
function withNodeGit(url, opts, callback) {
fs.access(opts.path, function(err) {
if (err) {
// Not yet cloned
NodeGit.Clone(url, opts.path)
.then(function(repo) {
// We don't want to directly pass `callback` because then the consumer gets a copy
// of the repository object, which is public API
callback();
})
.catch(callback);
} else {
// Cloned already; we need to pull
var repo;
NodeGit.Repository.open(opts.path).then(function(repository) {
repo = repository;
return repo;
}).then(function() {
return repo.fetchAll();
}).then(function() {
return repo.mergeBranches(opts.branch, 'origin/' + opts.branch);
}).then(function() {
callback();
}).catch(callback);
}
});
}
function withGitSubprocess(url, opts, callback) {
fs.access(opts.path, function(err) {
// Chop off the last path element to get the proper cwd for git subprocesses
var targetDir = opts.path.split(path.sep).slice(0, -1).join(path.sep);
if (err) {
// Not yet cloned
spawnWithSanityChecks('git', ['clone', '--quiet', '--branch', opts.branch, url, opts.path], targetDir, function(err, stdout) {
// Pass undefined, not null
if (!err) err = undefined;
callback(err);
});
} else {
// Cloned already; we need to pull
// Fetch
spawnWithSanityChecks('git', ['fetch', '--quiet', '--all'], opts.path, function(err, stdout) {
if (err) {
callback(err);
return;
}
// Checkout
spawnWithSanityChecks('git', ['checkout','--quiet', opts.branch], opts.path, function(err, stdout) {
if (err) {
callback(err);
return;
}
// Merge
spawnWithSanityChecks('git', ['merge','--quiet', '--ff-only', 'origin/' + opts.branch], opts.path, function(err, stdout) {
if (err) {
callback(err);
return;
}
callback();
});
});
});
}
});
}
function gitCloneOrPull(url, opts, callback) {
if (typeof opts === 'string') opts = { path: opts };
if (!opts.branch) opts.branch = 'master';
// If a specific implementation has been requested, abort if it's unavailable
// Otherwise, choose a sensible default.
if (opts.implementation) {
if (opts.implementation === 'nodegit' && !NodeGit) return callback(new Error('NodeGit could not be loaded, so implementation "nodegit" is unavailable'));
if (opts.implementation === 'subprocess' && !gitBinary) return callback(new Error('Running `git --version` failed, so implementation "subprocess" is unavailable'));
} else {
if (NodeGit) {
opts.implementation = 'nodegit';
} else if (gitBinary) {
opts.implementation = 'subprocess';
} else {
callback(new Error('No suitable implementation'));
}
}
switch (opts.implementation) {
case 'nodegit':
withNodeGit(url, opts, callback);
break;
case 'subprocess':
withGitSubprocess(url, opts, callback);
break;
default:
callback(new Error('Implementation "' + opts.implementation + '" not recognized'));
}
}
module.exports = gitCloneOrPull;