Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
9 changes: 9 additions & 0 deletions lib/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ var Branch = NodeGit.Branch;
*/
Reference.lookup = LookupWrapper(Reference);

/**
* Retrieves the reference by it's short name
* @param {Repository} repo The repo that the reference lives in
* @param {String|Reference} id The reference to lookup
* @param {Function} callback
* @return {Reference}
*/
Reference.dwim = LookupWrapper(Reference, Reference.dwim);

/**
* Returns true if this reference is valid
* @return {Boolean}
Expand Down
91 changes: 57 additions & 34 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,17 @@ 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 All @@ -94,6 +74,30 @@ Repository.prototype.getBranchCommit = function(name, callback) {
}, callback);
};

/**
* Look up a branch. Alias for `getReference`
*
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
* or Branch Ref
* @param {Function} callback
* @return {Ref}
*/
Repository.prototype.getBranch = function(name, callback) {
return this.getReference(name, callback);
};

/**
* Look up a branch's most recent commit. Alias to `getReferenceCommit`
*
* @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) {
return this.getReferenceCommit(name, callback);
};

/**
* Gets the branch that HEAD currently points to
* Is an alias to head()
Expand All @@ -106,14 +110,15 @@ Repository.prototype.getCurrentBranch = function() {
/**
* Lookup the reference with the given name.
*
* @param {String} name
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
* or Branch Ref
* @param {Function} callback
* @return {Reference}
*/
Repository.prototype.getReference = function(name, callback) {
var repository = this;

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

return repo.getRemote(remote).then(function(remote) {
return remote.fetch(repo.defaultSignature());
});
remote.setCallbacks(remoteCallbacks);
remote.checkCert(ignoreCertErrors ? 0 : 1);

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,
ignoreCertErrors,
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, ignoreCertErrors, callback));
});

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

/**
Expand Down
23 changes: 17 additions & 6 deletions test/tests/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,22 @@ describe("Remote", function() {
});

it("can fetch from a remote", function() {
return this.repository.fetch("origin")
.then(function() {
assert(true);
}, function() {
assert(false);
});
return this.repository.fetch("origin", {
credentials: function(url, userName) {
return NodeGit.Cred.sshKeyFromAgent(userName);
}
}, true);
});

it("can fetch from all remotes", function() {
// Set a reasonable timeout here for the fetchAll test
this.timeout(15000);

return this.repository.fetchAll({
credentials: function(url, userName) {
return NodeGit.Cred.sshKeyFromAgent(userName);
}
}, true);
});

});
4 changes: 2 additions & 2 deletions test/tests/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe("Repository", function() {

it("can list remotes", function() {
return this.repository.getRemotes().then(function(remotes) {
assert.equal(remotes.count(), 1);
assert.equal(remotes.strings(), "origin");
assert.equal(remotes.length, 1);
assert.equal(remotes[0], "origin");
});
});

Expand Down