Skip to content

Commit d7b217c

Browse files
committed
Merge pull request nodegit#457 from mattyclarkson/remote-push
Remote push
2 parents a963050 + 2fb16f3 commit d7b217c

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

generate/input/descriptor.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,11 @@
13941394
"isAsync": true,
13951395
"return": {
13961396
"isErrorCode": true
1397+
},
1398+
"args": {
1399+
"opts": {
1400+
"isOptional": true
1401+
}
13971402
}
13981403
},
13991404
"git_remote_set_callbacks": {

lib/remote.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var lookupWrapper = require("./util/lookupWrapper");
44

55
var Remote = NodeGit.Remote;
66
var setCallbacks = Remote.prototype.setCallbacks;
7+
var push = Remote.push;
78

89
/**
910
* Retrieves the remote by name
@@ -21,4 +22,21 @@ Remote.prototype.setCallbacks = function(callbacks) {
2122
return setCallbacks.call(this, callbacks);
2223
};
2324

25+
/**
26+
* Pushes to a remote
27+
*
28+
* @async
29+
* @param {Array} refSpecs The ref specs that should be pushed
30+
* @param {PushOptions} options Options for the checkout
31+
* @param {Signature} signature The identity to use for the reflog of the
32+
* updated references
33+
* @param {String} message The message to use for the update reflog messages
34+
* @return {Number} error code
35+
*/
36+
Remote.push = function(refSpecs, options, signature, message) {
37+
options = normalizeOptions(options, NodeGit.PushOptions);
38+
39+
return push.call(this, refSpecs, options, signature, message);
40+
};
41+
2442
module.exports = Remote;

test/tests/remote.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,43 @@ describe("Remote", function() {
171171
});
172172
});
173173

174+
it("cannot push to a repository", function() {
175+
this.timeout(5000);
176+
var repo = this.repository;
177+
var branch = "should-not-exist";
178+
return Remote.lookup(repo, "origin")
179+
.then(function(remote) {
180+
remote.setCallbacks({
181+
credentials: function(url, userName) {
182+
if (url.indexOf("https") === -1) {
183+
return NodeGit.Cred.sshKeyFromAgent(userName);
184+
} else {
185+
return NodeGit.Cred.userpassPlaintextNew(userName, "");
186+
}
187+
},
188+
certificateCheck: function() {
189+
return 1;
190+
}
191+
});
192+
return remote;
193+
})
194+
.then(function(remote) {
195+
var ref = "refs/heads/" + branch;
196+
var refs = [ref + ":" + ref];
197+
var signature = repo.defaultSignature();
198+
return remote.push(refs, null, signature,
199+
"Pushed '" + branch + "' for test");
200+
})
201+
.then(function() {
202+
return Promise.reject(
203+
new Error("should not be able to push to the repository"));
204+
}, function(err) {
205+
if (err.message.indexOf(401) === -1) {
206+
return Promise.reject(
207+
new Error("failed to return unauthorized status code"));
208+
} else {
209+
return Promise.resolve();
210+
}
211+
});
212+
});
174213
});

0 commit comments

Comments
 (0)