Skip to content

Commit 7e1a27b

Browse files
committed
Merge pull request #319 from nodegit/fix-fetch
Fixed fetch to be async and use callbacks
2 parents 6b3aaf7 + d6f2b93 commit 7e1a27b

File tree

6 files changed

+129
-48
lines changed

6 files changed

+129
-48
lines changed

example/fetch.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ var nodegit = require('../');
22
var path = require('path');
33

44
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
5-
return repo.fetch("origin");
6-
})
7-
.done(function() {
5+
return repo.fetch("origin", {
6+
credentials: function(url, userName) {
7+
return nodegit.Cred.sshKeyFromAgent(userName);
8+
}
9+
});
10+
}).done(function() {
811
console.log("It worked!");
912
});

generate/input/descriptor.json

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,18 +1073,47 @@
10731073
"reflog_message": {
10741074
"isOptional": true
10751075
}
1076+
},
1077+
"isAsync": true,
1078+
"return": {
1079+
"isErrorCode": true
10761080
}
10771081
},
10781082
"git_remote_get_fetch_refspecs": {
1079-
"ignore": true
1083+
"args": {
1084+
"array": {
1085+
"isReturn": true,
1086+
"shouldAlloc": true,
1087+
"cppClassName": "Array",
1088+
"jsClassName": "Array",
1089+
"size": "count",
1090+
"key": "strings"
1091+
}
1092+
},
1093+
"isAsync": true
10801094
},
10811095
"git_remote_get_push_refspecs": {
1082-
"ignore": true
1096+
"args": {
1097+
"array": {
1098+
"isReturn": true,
1099+
"shouldAlloc": true,
1100+
"cppClassName": "Array",
1101+
"jsClassName": "Array",
1102+
"size": "count",
1103+
"key": "strings"
1104+
}
1105+
},
1106+
"isAsync": true
10831107
},
10841108
"git_remote_list": {
10851109
"args": {
10861110
"out": {
1087-
"shouldAlloc": true
1111+
"isReturn": true,
1112+
"shouldAlloc": true,
1113+
"cppClassName": "Array",
1114+
"jsClassName": "Array",
1115+
"size": "count",
1116+
"key": "strings"
10881117
}
10891118
}
10901119
},
@@ -1093,6 +1122,12 @@
10931122
},
10941123
"git_remote_rename": {
10951124
"ignore": true
1125+
},
1126+
"git_remote_set_fetch_refspecs": {
1127+
"ignore": true
1128+
},
1129+
"git_remote_set_push_refspecs": {
1130+
"ignore": true
10961131
}
10971132
}
10981133
},

lib/reference.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ var Branch = NodeGit.Branch;
1313
*/
1414
Reference.lookup = LookupWrapper(Reference);
1515

16+
/**
17+
* Retrieves the reference by it's short name
18+
* @param {Repository} repo The repo that the reference lives in
19+
* @param {String|Reference} id The reference to lookup
20+
* @param {Function} callback
21+
* @return {Reference}
22+
*/
23+
Reference.dwim = LookupWrapper(Reference, Reference.dwim);
24+
1625
/**
1726
* Returns true if this reference is valid
1827
* @return {Boolean}

lib/repository.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,17 @@ function(name, commit, force, signature, logMessage) {
5353
};
5454

5555
/**
56-
* Look up a branch
56+
* Look up a refs's commit.
5757
*
58-
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
59-
* @param {Function} callback
60-
* @return {Ref}
61-
*/
62-
Repository.prototype.getBranch = function(name, callback) {
63-
name = (name instanceof Reference ||
64-
~name.indexOf("refs/heads/")) ? name
65-
: "refs/heads/" + name;
66-
67-
return this.getReference(name).then(function(reference) {
68-
if (typeof callback === "function") {
69-
callback(null, reference);
70-
}
71-
72-
return reference;
73-
}, callback);
74-
};
75-
76-
/**
77-
* Look up a branch's most recent commit.
78-
*
79-
* @param {String|Ref} name Branch name, e.g. "master" or Branch Ref
58+
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
59+
* or Branch Ref
8060
* @param {Function} callback
8161
* @return {Commit}
8262
*/
83-
Repository.prototype.getBranchCommit = function(name, callback) {
63+
Repository.prototype.getReferenceCommit = function(name, callback) {
8464
var repository = this;
8565

86-
return this.getBranch(name).then(function(reference) {
66+
return this.getReference(name).then(function(reference) {
8767
return repository.getCommit(reference.target()).then(function(commit) {
8868
if (typeof callback === "function") {
8969
callback(null, commit);
@@ -94,6 +74,30 @@ Repository.prototype.getBranchCommit = function(name, callback) {
9474
}, callback);
9575
};
9676

77+
/**
78+
* Look up a branch. Alias for `getReference`
79+
*
80+
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
81+
* or Branch Ref
82+
* @param {Function} callback
83+
* @return {Ref}
84+
*/
85+
Repository.prototype.getBranch = function(name, callback) {
86+
return this.getReference(name, callback);
87+
};
88+
89+
/**
90+
* Look up a branch's most recent commit. Alias to `getReferenceCommit`
91+
*
92+
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
93+
* or Branch Ref
94+
* @param {Function} callback
95+
* @return {Commit}
96+
*/
97+
Repository.prototype.getBranchCommit = function(name, callback) {
98+
return this.getReferenceCommit(name, callback);
99+
};
100+
97101
/**
98102
* Gets the branch that HEAD currently points to
99103
* Is an alias to head()
@@ -106,14 +110,15 @@ Repository.prototype.getCurrentBranch = function() {
106110
/**
107111
* Lookup the reference with the given name.
108112
*
109-
* @param {String} name
113+
* @param {String|Ref} name Ref name, e.g. "master", "refs/heads/master"
114+
* or Branch Ref
110115
* @param {Function} callback
111116
* @return {Reference}
112117
*/
113118
Repository.prototype.getReference = function(name, callback) {
114119
var repository = this;
115120

116-
return Reference.lookup(this, name).then(function(reference) {
121+
return Reference.dwim(this, name).then(function(reference) {
117122
if (reference.isSymbolic()) {
118123
return reference.resolve().then(function(reference) {
119124
reference.repo = repository;
@@ -507,29 +512,47 @@ Repository.prototype.getRemote = function(remote, callback) {
507512
*
508513
* @param {String|Remote} remote
509514
*/
510-
Repository.prototype.fetch = function(remote) {
515+
Repository.prototype.fetch = function(
516+
remote,
517+
remoteCallbacks,
518+
ignoreCertErrors,
519+
callback)
520+
{
511521
var repo = this;
512522

513523
return repo.getRemote(remote).then(function(remote) {
514-
return remote.fetch(repo.defaultSignature());
515-
});
524+
remote.setCallbacks(remoteCallbacks);
525+
remote.checkCert(ignoreCertErrors ? 0 : 1);
526+
527+
return remote.fetch(repo.defaultSignature(), "Fetch from " + remote)
528+
.then(function() {
529+
if (typeof callback === "function") {
530+
callback();
531+
}
532+
});
533+
}, callback);
516534
};
517535

518536
/**
519537
* Fetches from all remotes
520538
*/
521-
Repository.prototype.fetchAll = function() {
539+
Repository.prototype.fetchAll = function(
540+
remoteCallbacks,
541+
ignoreCertErrors,
542+
callback)
543+
{
522544
var repo = this;
523545

524-
return repo.getRemotes(function(remotes) {
546+
return repo.getRemotes().then(function(remotes) {
525547
var fetchPromises = [];
526548

527549
remotes.forEach(function(remote) {
528-
fetchPromises.push(repo.fetch(remote));
550+
fetchPromises.push(
551+
repo.fetch(remote, remoteCallbacks, ignoreCertErrors, callback));
529552
});
530553

531554
return Promise.all(fetchPromises);
532-
});
555+
}, callback);
533556
};
534557

535558
/**

test/tests/remote.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,22 @@ describe("Remote", function() {
104104
});
105105

106106
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-
});
107+
return this.repository.fetch("origin", {
108+
credentials: function(url, userName) {
109+
return NodeGit.Cred.sshKeyFromAgent(userName);
110+
}
111+
}, true);
112+
});
113+
114+
it("can fetch from all remotes", function() {
115+
// Set a reasonable timeout here for the fetchAll test
116+
this.timeout(15000);
117+
118+
return this.repository.fetchAll({
119+
credentials: function(url, userName) {
120+
return NodeGit.Cred.sshKeyFromAgent(userName);
121+
}
122+
}, true);
113123
});
124+
114125
});

test/tests/repository.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe("Repository", function() {
4949

5050
it("can list remotes", function() {
5151
return this.repository.getRemotes().then(function(remotes) {
52-
assert.equal(remotes.count(), 1);
53-
assert.equal(remotes.strings(), "origin");
52+
assert.equal(remotes.length, 1);
53+
assert.equal(remotes[0], "origin");
5454
});
5555
});
5656

0 commit comments

Comments
 (0)