Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed fetch to be async and use callbacks
  • Loading branch information
John Haley committed Dec 5, 2014
commit 22c58ce31e398ca4446ffd0585cc64479123aa6f
9 changes: 6 additions & 3 deletions example/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ var nodegit = require('../');
var path = require('path');

nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
return repo.fetch("origin");
})
.done(function() {
return repo.fetch("origin", {
credentials: function(url, userName) {
return nodegit.Cred.sshKeyFromAgent(userName);
}
});
}).done(function() {
console.log("It worked!");
});
41 changes: 38 additions & 3 deletions generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1073,18 +1073,47 @@
"reflog_message": {
"isOptional": true
}
},
"isAsync": true,
"return": {
"isErrorCode": true
}
},
"git_remote_get_fetch_refspecs": {
"ignore": true
"args": {
"array": {
"isReturn": true,
"shouldAlloc": true,
"cppClassName": "Array",
"jsClassName": "Array",
"size": "count",
"key": "strings"
}
},
"isAsync": true
},
"git_remote_get_push_refspecs": {
"ignore": true
"args": {
"array": {
"isReturn": true,
"shouldAlloc": true,
"cppClassName": "Array",
"jsClassName": "Array",
"size": "count",
"key": "strings"
}
},
"isAsync": true
},
"git_remote_list": {
"args": {
"out": {
"shouldAlloc": true
"isReturn": true,
"shouldAlloc": true,
"cppClassName": "Array",
"jsClassName": "Array",
"size": "count",
"key": "strings"
}
}
},
Expand All @@ -1093,6 +1122,12 @@
},
"git_remote_rename": {
"ignore": true
},
"git_remote_set_fetch_refspecs": {
"ignore": true
},
"git_remote_set_push_refspecs": {
"ignore": true
}
}
},
Expand Down
55 changes: 22 additions & 33 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,16 @@ function(name, commit, force, signature, logMessage) {
};

/**
* Look up a branch
* Look up a refs's commit.
*
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
* @param {Function} callback
* @return {Ref}
*/
Repository.prototype.getBranch = function(name, callback) {
name = (name instanceof Reference ||
~name.indexOf("refs/heads/")) ? name
: "refs/heads/" + name;

return this.getReference(name).then(function(reference) {
if (typeof callback === "function") {
callback(null, reference);
}

return reference;
}, callback);
};

/**
* Look up a branch's most recent commit.
*
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master" or Branch Ref
* @param {Function} callback
* @return {Commit}
*/
Repository.prototype.getBranchCommit = function(name, callback) {
Repository.prototype.getReferenceCommit = function(name, callback) {
var repository = this;

return this.getBranch(name).then(function(reference) {
return this.getReference(name).then(function(reference) {
return repository.getCommit(reference.target()).then(function(commit) {
if (typeof callback === "function") {
callback(null, commit);
Expand Down Expand Up @@ -112,8 +91,11 @@ Repository.prototype.getCurrentBranch = function() {
*/
Repository.prototype.getReference = function(name, callback) {
var repository = this;
var lookup = name.indexOf("refs/") === 0
? Reference.lookup(this, name)
: Reference.dwim(this, name);

return Reference.lookup(this, name).then(function(reference) {
return lookup.then(function(reference) {
if (reference.isSymbolic()) {
return reference.resolve(function (error, reference) {
reference.repo = repository;
Expand Down Expand Up @@ -507,29 +489,36 @@ Repository.prototype.getRemote = function(remote, callback) {
*
* @param {String|Remote} remote
*/
Repository.prototype.fetch = function(remote) {
Repository.prototype.fetch = function(remote, remoteCallbacks, callback) {
var repo = this;

return repo.getRemote(remote).then(function(remote) {
return remote.fetch(repo.defaultSignature());
});
remote.setCallbacks(remoteCallbacks);

return remote.fetch(repo.defaultSignature(), "Fetch from " + remote)
.then(function() {
if (typeof callback === "function") {
callback();
}
});
}, callback);
};

/**
* Fetches from all remotes
*/
Repository.prototype.fetchAll = function() {
Repository.prototype.fetchAll = function(remoteCallbacks, callback) {
var repo = this;

return repo.getRemotes(function(remotes) {
return repo.getRemotes().then(function(remotes) {
var fetchPromises = [];

remotes.forEach(function(remote) {
fetchPromises.push(repo.fetch(remote));
fetchPromises.push(repo.fetch(remote, remoteCallbacks, callback));
});

return Promise.all(fetchPromises);
});
}, callback);
};

/**
Expand Down