|
| 1 | +var nodegit = require('../'); |
| 2 | +var path = require('path'); |
| 3 | +var Promise = require('nodegit-promise'); |
| 4 | +var promisify = require('promisify-node'); |
| 5 | +var fse = promisify(require('fs-extra')); |
| 6 | +fse.ensureDir = promisify(fse.ensureDir); |
| 7 | + |
| 8 | +var fileName = 'newFile.txt'; |
| 9 | +var fileContent = 'hello world'; |
| 10 | + |
| 11 | +var repoDir = '../../newRepo'; |
| 12 | + |
| 13 | +var repository; |
| 14 | + |
| 15 | +var signature = nodegit.Signature.create("Foo bar", "foo@bar.com", 123456789, 60); |
| 16 | + |
| 17 | +// Create a new repository in a clean directory, and add our first file |
| 18 | +fse.remove(path.resolve(__dirname, repoDir)) |
| 19 | +.then(function() { |
| 20 | + return fse.ensureDir(path.resolve(__dirname, repoDir)); |
| 21 | +}) |
| 22 | +.then(function() { |
| 23 | + return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0); |
| 24 | +}) |
| 25 | +.then(function(repo) { |
| 26 | + repository = repo; |
| 27 | + return fse.writeFile(path.join(repository.workdir(), fileName), fileContent); |
| 28 | +}) |
| 29 | + |
| 30 | +// Load up the repository index and make our initial commit to HEAD |
| 31 | +.then(function() { |
| 32 | + return repository.openIndex(); |
| 33 | +}) |
| 34 | +.then(function(index) { |
| 35 | + index.read(1); |
| 36 | + index.addByPath(fileName); |
| 37 | + index.write() |
| 38 | + |
| 39 | + return index.writeTree(); |
| 40 | +}) |
| 41 | +.then(function(oid) { |
| 42 | + return repository.createCommit('HEAD', signature, signature, 'initial commit', oid, []); |
| 43 | +}) |
| 44 | + |
| 45 | +// Add a new remote |
| 46 | +.then(function() { |
| 47 | + return nodegit.Remote.create(repository, "origin", "git@github.com:nodegit/push-example.git") |
| 48 | + .then(function(remote) { |
| 49 | + remote.connect(nodegit.Enums.DIRECTION.PUSH); |
| 50 | + |
| 51 | + var push; |
| 52 | + |
| 53 | + // We need to set the auth on the remote, not the push object |
| 54 | + remote.setCallbacks({ |
| 55 | + credentials: function(url, userName) { |
| 56 | + return nodegit.Cred.sshKeyFromAgent(userName); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // Create the push object for this remote |
| 61 | + return nodegit.Push.create(remote) |
| 62 | + .then(function(pushResult) { |
| 63 | + push = pushResult; |
| 64 | + |
| 65 | + // This just says what branch we're pushing onto what branch in the remote |
| 66 | + return push.addRefspec("refs/heads/master:refs/heads/master"); |
| 67 | + }).then(function() { |
| 68 | + // This is the call that performs the actual push |
| 69 | + return push.finish(); |
| 70 | + }).then(function() { |
| 71 | + // Check to see if the remote accepted our push request. |
| 72 | + return push.unpackOk(); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}).done(function() { |
| 76 | + console.log("Done!"); |
| 77 | +}) |
0 commit comments