Skip to content

Commit 8e13291

Browse files
committed
Merge pull request #65 from h-no/master
Add git.fetch and update docs
2 parents 7b64a89 + 8ec1b13 commit 8e13291

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ gulp.task('pull', function(){
109109
});
110110
});
111111

112+
// Run git fetch
113+
// Fetch refs from all remotes
114+
gulp.task('fetch', function(){
115+
git.fetch('', '', {args: '--all'}, function (err) {
116+
if (err) throw err;
117+
});
118+
});
119+
120+
// Run git fetch
121+
// Fetch refs from origin
122+
gulp.task('fetch', function(){
123+
git.fetch('origin', '', function (err) {
124+
if (err) throw err;
125+
});
126+
});
112127

113128
// Clone a remote repo
114129
gulp.task('clone', function(){
@@ -280,6 +295,25 @@ git.addRemote('origin', 'git-repo-url', function (err) {
280295
});
281296
```
282297

298+
### git.fetch(remote, branch, opt, cb)
299+
`git fetch <remote> <branch>`
300+
301+
Fetches refs and objects from remote repo
302+
303+
`remote`: String, name of remote, default: `origin`
304+
305+
`branch`: String, branch, default: ``
306+
307+
`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true}`
308+
309+
`cb`: function, passed err if any
310+
311+
```js
312+
git.fetch('origin', '', function (err) {
313+
//if (err) ...
314+
});
315+
```
316+
283317
### git.pull(remote, branch, opt, cb)
284318
`git pull <remote> <branch>`
285319

lib/fetch.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var gutil = require('gulp-util');
4+
var exec = require('child_process').exec;
5+
var escape = require('any-shell-escape');
6+
7+
module.exports = function (remote, branch, opt, cb) {
8+
if (!cb && typeof opt === 'function') {
9+
// optional options
10+
cb = opt;
11+
opt = {};
12+
}
13+
if (!cb || typeof cb !== 'function') cb = function () {};
14+
if (!branch) branch = '';
15+
if (!opt) opt = {};
16+
if (!opt.cwd) opt.cwd = process.cwd();
17+
if (!opt.args) opt.args = ' ';
18+
if (!remote && opt.args.indexOf('--all') === -1) remote = 'origin';
19+
20+
var cmd = 'git fetch ' + opt.args + ' ' + escape([remote, branch]);
21+
return exec(cmd, {cwd: opt.cwd}, function(err, stdout, stderr){
22+
if (err) return cb(err);
23+
if (!opt.quiet) gutil.log(stdout, stderr);
24+
cb();
25+
});
26+
};

test/fetch.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
/* global describe, it, after, before, afterEach, beforeEach */
4+
5+
var fs = require('fs');
6+
var rimraf = require('rimraf');
7+
var should = require('should');
8+
var exec = require('child_process').exec;
9+
10+
module.exports = function(git, util){
11+
12+
13+
beforeEach(function(done){
14+
var repo = 'git://github.com/stevelacy/git-test';
15+
git.clone(repo, {args: './test/tmp'}, function(){
16+
exec('git update-ref -d refs/tags/v1.1.1', {cwd: './test/tmp'}, function(err){
17+
if (err) return done(err);
18+
done();
19+
});
20+
});
21+
});
22+
23+
it('should fetch a tag from remote origin', function(done){
24+
git.fetch('origin', '', {cwd: './test/tmp'}, function(){
25+
fs.open('./test/tmp/.git/refs/tags/v1.1.1', 'r', function(err, fd) {
26+
should.not.exist(err);
27+
fs.close(fd, function() {
28+
done();
29+
});
30+
});
31+
});
32+
});
33+
34+
afterEach(function(done){
35+
rimraf('./test/tmp', function(err){
36+
if(err) return done(err);
37+
done();
38+
});
39+
});
40+
};

0 commit comments

Comments
 (0)