1 | var cp = require('child_process');
|
2 | var path = require('path');
|
3 | var util = require('util');
|
4 |
|
5 | var Q = require('q');
|
6 | var fs = require('q-io/fs');
|
7 |
|
8 | var git = 'git';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function ProcessError(code, message) {
|
18 | var callee = arguments.callee;
|
19 | Error.apply(this, [message]);
|
20 | Error.captureStackTrace(this, callee);
|
21 | this.code = code;
|
22 | this.message = message;
|
23 | this.name = callee.name;
|
24 | }
|
25 | util.inherits(ProcessError, Error);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | exports = module.exports = function(args, cwd) {
|
37 | return spawn(git, args, cwd);
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | exports.exe = function(exe) {
|
46 | git = exe;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function spawn(exe, args, cwd) {
|
58 | var deferred = Q.defer();
|
59 | var child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
|
60 | var buffer = [];
|
61 | child.stderr.on('data', function(chunk) {
|
62 | buffer.push(chunk.toString());
|
63 | });
|
64 | child.stdout.on('data', function(chunk) {
|
65 | deferred.notify(chunk);
|
66 | });
|
67 | child.on('close', function(code) {
|
68 | if (code) {
|
69 | var msg = buffer.join('') || 'Process failed: ' + code;
|
70 | deferred.reject(new ProcessError(code, msg));
|
71 | } else {
|
72 | deferred.resolve(code);
|
73 | }
|
74 | });
|
75 | return deferred.promise;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | exports.init = function init(cwd) {
|
85 | return spawn(git, ['init'], cwd);
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | exports.clone = function clone(repo, dir) {
|
96 | return fs.exists(dir).then(function(exists) {
|
97 | if (exists) {
|
98 | return Q.resolve();
|
99 | } else {
|
100 | return fs.makeTree(path.dirname(dir)).then(function() {
|
101 | return spawn(git, ['clone', repo, dir]);
|
102 | });
|
103 | }
|
104 | });
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | var clean = exports.clean = function clean(cwd) {
|
114 | return spawn(git, ['clean', '-f', '-d'], cwd);
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | var reset = exports.reset = function reset(remote, branch, cwd) {
|
126 | return spawn(git, ['reset', '--hard', remote + '/' + branch], cwd);
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | exports.fetch = function fetch(remote, cwd) {
|
137 | return spawn(git, ['fetch', remote], cwd);
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | exports.checkout = function checkout(remote, branch, cwd) {
|
149 | var treeish = remote + '/' + branch;
|
150 | return spawn(git, ['ls-remote', '--exit-code', '.', treeish], cwd)
|
151 | .then(function() {
|
152 |
|
153 | return spawn(git, ['checkout', branch], cwd)
|
154 | .then(function() {
|
155 | return clean(cwd);
|
156 | })
|
157 | .then(function() {
|
158 | return reset(remote, branch, cwd);
|
159 | });
|
160 | }, function(error) {
|
161 | if (error instanceof ProcessError && error.code === 2) {
|
162 |
|
163 | return spawn(git, ['checkout', '--orphan', branch], cwd);
|
164 | } else {
|
165 |
|
166 | return Q.reject(error);
|
167 | }
|
168 | });
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | exports.rm = function rm(files, cwd) {
|
179 | return spawn(git, ['rm', '--ignore-unmatch', '-r', '-f', files], cwd);
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | exports.add = function add(files, cwd) {
|
190 | return spawn(git, ['add', files], cwd);
|
191 | };
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | exports.commit = function commit(message, cwd) {
|
201 | return spawn(git, ['diff-index', '--quiet', 'HEAD', '.'], cwd)
|
202 | .then(function() {
|
203 |
|
204 | return Q.resolve();
|
205 | })
|
206 | .fail(function() {
|
207 | return spawn(git, ['commit', '-m', message], cwd);
|
208 | });
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | exports.tag = function tag(tag, cwd) {
|
219 | return spawn(git, ['tag', tag], cwd);
|
220 | };
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | exports.push = function push(remote, branch, cwd) {
|
231 | return spawn(git, ['push', '--tags', remote, branch], cwd);
|
232 | };
|