Skip to content

Commit 22c58ce

Browse files
author
John Haley
committed
Fixed fetch to be async and use callbacks
1 parent e3216b4 commit 22c58ce

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
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/repository.js

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,16 @@ 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" or Branch Ref
8059
* @param {Function} callback
8160
* @return {Commit}
8261
*/
83-
Repository.prototype.getBranchCommit = function(name, callback) {
62+
Repository.prototype.getReferenceCommit = function(name, callback) {
8463
var repository = this;
8564

86-
return this.getBranch(name).then(function(reference) {
65+
return this.getReference(name).then(function(reference) {
8766
return repository.getCommit(reference.target()).then(function(commit) {
8867
if (typeof callback === "function") {
8968
callback(null, commit);
@@ -112,8 +91,11 @@ Repository.prototype.getCurrentBranch = function() {
11291
*/
11392
Repository.prototype.getReference = function(name, callback) {
11493
var repository = this;
94+
var lookup = name.indexOf("refs/") === 0
95+
? Reference.lookup(this, name)
96+
: Reference.dwim(this, name);
11597

116-
return Reference.lookup(this, name).then(function(reference) {
98+
return lookup.then(function(reference) {
11799
if (reference.isSymbolic()) {
118100
return reference.resolve(function (error, reference) {
119101
reference.repo = repository;
@@ -507,29 +489,36 @@ Repository.prototype.getRemote = function(remote, callback) {
507489
*
508490
* @param {String|Remote} remote
509491
*/
510-
Repository.prototype.fetch = function(remote) {
492+
Repository.prototype.fetch = function(remote, remoteCallbacks, callback) {
511493
var repo = this;
512494

513495
return repo.getRemote(remote).then(function(remote) {
514-
return remote.fetch(repo.defaultSignature());
515-
});
496+
remote.setCallbacks(remoteCallbacks);
497+
498+
return remote.fetch(repo.defaultSignature(), "Fetch from " + remote)
499+
.then(function() {
500+
if (typeof callback === "function") {
501+
callback();
502+
}
503+
});
504+
}, callback);
516505
};
517506

518507
/**
519508
* Fetches from all remotes
520509
*/
521-
Repository.prototype.fetchAll = function() {
510+
Repository.prototype.fetchAll = function(remoteCallbacks, callback) {
522511
var repo = this;
523512

524-
return repo.getRemotes(function(remotes) {
513+
return repo.getRemotes().then(function(remotes) {
525514
var fetchPromises = [];
526515

527516
remotes.forEach(function(remote) {
528-
fetchPromises.push(repo.fetch(remote));
517+
fetchPromises.push(repo.fetch(remote, remoteCallbacks, callback));
529518
});
530519

531520
return Promise.all(fetchPromises);
532-
});
521+
}, callback);
533522
};
534523

535524
/**

0 commit comments

Comments
 (0)