Skip to content

Instantly share code, notes, and snippets.

@yofreke
Forked from anonymous/test.js
Last active February 2, 2016 00:58
Show Gist options
  • Save yofreke/513c762f41ccd28c2973 to your computer and use it in GitHub Desktop.
Save yofreke/513c762f41ccd28c2973 to your computer and use it in GitHub Desktop.
example.js (functional)
'use strict';
// Helper Methods Used
var fs = require('fs');
var NodeGit = require('NodeGit');
var cloneOpts = {
callbacks: {
certificateCheck: function() {
// github will fail cert check on some OSX machines
// this overrides that check
return 1;
},
credentials: function(url, username) {
console.log('creds for', username);
if (username) {
return NodeGit.Cred.sshKeyNew(username, '/Users/me/.ssh/id_rsa.pub', '/Users/me/.ssh/id_rsa', '');
} else {
return NodeGit.Cred.userpassPlaintextNew('MYUSER', 'MYPASS');
}
}
}
};
class GitClient {
static clone(options) {
return NodeGit.Clone(options.remote, options.local, {
checkoutBranch: options.branch,
callbacks: cloneOpts.callbacks
});
}
static addAndCommit(repo, filesArray, msg) {
return repo.createCommitOnHead(
filesArray,
NodeGit.Signature.create('test-service', 'test-service@devfactory.com', new Date().getTime(), 0),
NodeGit.Signature.create('test-service', 'test-service@devfactory.com', new Date().getTime(), 0),
msg || 'Anonymous Commit by the Test-Service'
);
}
static push(repo, repoName, remoteName, refs) {
return repo.getRemote(remoteName || 'origin')
.then(remote => {
return remote.push(
refs || [`refs/heads/${repoName || 'master'}:refs/heads/${repoName || 'master'}`],
cloneOpts
).then(function () {
console.log('success', arguments);
});
});
}
}
// Unit/Integration Test
// it('should push', () => {
let repo;
console.log('starting clone');
return GitClient.clone({
branch: 'master',
remote: 'https://github.com/yofreke/testrepo.git',
local: 'test-git-module/test-push',
path: 'src',
cwd: 'test-git-module'
}).then(r => {
repo = r;
let contents = new Date().getTime() + 'UDPATE';
console.log('got repo, writing contents:', contents);
fs.mkdirSync('test-git-module/test-push/1');
fs.writeFileSync('test-git-module/test-push/1/test.json', contents);
// add the file
return GitClient.addAndCommit(repo, ['1/test.json'], 'Committing');
}).then(() => {
console.log('commit done, pushing');
return GitClient.push(repo, 'master');
})
.then({} => {
console.log('done');
process.exit(0);
})
.catch(err => {
console.error('uncaught!', err);
process.exit(1);
});
//});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment