Skip to content

Commit 43762f9

Browse files
John Haleymaxkorp
authored andcommitted
Added some convenience methods for remotes
1 parent caadc4d commit 43762f9

5 files changed

Lines changed: 106 additions & 21 deletions

File tree

example/details-for-tree-entry.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ var path = require('path');
77

88
nodegit.Repository.open(path.resolve(__dirname, '../.git'))
99
.then(function(repo) {
10-
return repo.getTree(
11-
nodegit.Oid.fromString("e1b0c7ea57bfc5e30ec279402a98168a27838ac9"))
10+
return repo.getTree("e1b0c7ea57bfc5e30ec279402a98168a27838ac9")
1211
.then(function(tree) {
1312
var treeEntry = tree.entryByIndex(0);
1413

generate/input/descriptor.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,13 @@
10681068
"git_remote_default_branch": {
10691069
"ignore": true
10701070
},
1071+
"git_remote_fetch": {
1072+
"args": {
1073+
"reflog_message": {
1074+
"isOptional": true
1075+
}
1076+
}
1077+
},
10711078
"git_remote_get_fetch_refspecs": {
10721079
"ignore": true
10731080
},
@@ -1156,6 +1163,9 @@
11561163
},
11571164
"signature": {
11581165
"functions": {
1166+
"git_signature_default": {
1167+
"isAsync": false
1168+
},
11591169
"git_signature_new": {
11601170
"isAsync": false
11611171
},

lib/repository.js

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,6 @@ Repository.prototype.getBranchCommit = function(name, callback) {
9292
}, callback);
9393
};
9494

95-
/**
96-
* Lists out the remotes in the given repository.
97-
*
98-
* @param {Function} Optional callback
99-
* @return {Object} Promise object.
100-
*/
101-
Repository.prototype.getRemotes = function(callback) {
102-
return Remote.list(this).then(function(remotes) {
103-
if (typeof callback === "function") {
104-
callback(null, remotes);
105-
}
106-
107-
return remotes;
108-
}, callback);
109-
};
110-
11195
/**
11296
* Lookup the reference with the given name.
11397
*
@@ -425,9 +409,10 @@ Repository.prototype.createCommitOnHead = function(
425409
committer,
426410
message,
427411
treeOid,
428-
[parent]);
412+
[parent],
413+
callback);
429414
});
430-
});
415+
}, callback);
431416
};
432417

433418
/**
@@ -455,4 +440,85 @@ Repository.prototype.treeBuilder = function() {
455440
return builder;
456441
};
457442

443+
/**
444+
* Gets the default signature for the default user and now timestamp
445+
* @return {Signature}
446+
*/
447+
Repository.prototype.defaultSignature = function() {
448+
return NodeGit.Signature.default(this);
449+
};
450+
451+
/**
452+
* Lists out the remotes in the given repository.
453+
*
454+
* @param {Function} Optional callback
455+
* @return {Object} Promise object.
456+
*/
457+
Repository.prototype.getRemotes = function(callback) {
458+
return Remote.list(this).then(function(remotes) {
459+
if (typeof callback === "function") {
460+
callback(null, remotes);
461+
}
462+
463+
return remotes;
464+
}, callback);
465+
};
466+
467+
/**
468+
* Gets a remote from the repo
469+
*
470+
* @param {String|Remote} remote
471+
* @param {Function} callback
472+
* @return {Remote} The remote object
473+
*/
474+
Repository.prototype.getRemote = function(remote, callback) {
475+
if (remote instanceof NodeGit.Remote) {
476+
return Promise.resolve(remote).then(function(remoteObj) {
477+
if (typeof callback === "function") {
478+
callback(null, remoteObj);
479+
}
480+
481+
return remoteObj;
482+
}, callback);
483+
}
484+
485+
return NodeGit.Remote.load(this, remote).then(function(remoteObj) {
486+
if (typeof callback === "function") {
487+
callback(null, remoteObj);
488+
}
489+
490+
return remoteObj;
491+
}, callback);
492+
};
493+
494+
/**
495+
* Fetches from a remote
496+
*
497+
* @param {String|Remote} remote
498+
*/
499+
Repository.prototype.fetch = function(remote) {
500+
var repo = this;
501+
502+
return repo.getRemote(remote).then(function(remote) {
503+
return remote.fetch(repo.defaultSignature());
504+
});
505+
};
506+
507+
/**
508+
* Fetches from all remotes
509+
*/
510+
Repository.prototype.fetchAll = function() {
511+
var repo = this;
512+
513+
return repo.getRemotes(function(remotes) {
514+
var fetchPromises = [];
515+
516+
remotes.forEach(function(remote) {
517+
fetchPromises.push(repo.fetch(remote));
518+
});
519+
520+
return Promise.all(fetchPromises);
521+
});
522+
};
523+
458524
module.exports = Repository;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"lint": "jshint lib test/tests",
8787
"cov": "node test",
8888
"mocha": "mocha test/runner test/tests",
89+
"mochaDebug": "mocha --debug-brk test/runner test/tests",
8990
"test": "npm run lint && npm run cov",
9091
"generateJson": "node generate/scripts/generateJson",
9192
"generateNativeCode": "node generate/scripts/generateNativeCode",

test/tests/remote.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe("Remote", function() {
8888
});
8989
});
9090

91-
it("can fetch from a remote", function() {
91+
it("can download from a remote", function() {
9292
var repo = this.repository;
9393

9494
return Remote.load(repo, "origin")
@@ -102,4 +102,13 @@ describe("Remote", function() {
102102
assert(false);
103103
});
104104
});
105+
106+
it("can fetch from a remote", function() {
107+
return this.repository.fetch("origin")
108+
.then(function() {
109+
assert(true);
110+
}, function() {
111+
assert(false);
112+
});
113+
});
105114
});

0 commit comments

Comments
 (0)