Skip to content

Commit

Permalink
adds remote, and super simple pull and push (see #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisconnect committed May 9, 2016
1 parent 2740a5b commit b6042cb
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ exports.commit = require('./commit.js');
exports.diff = require('./diff.js');
exports.log = require('./log.js');
exports.status = require('./status.js');
exports.remote = require('./remote.js');
exports.push = require('./push.js');
exports.pull = require('./pull.js');
35 changes: 35 additions & 0 deletions lib/pull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var Git = require('nodegit');

function sshKeyFromAgent(url, username){
return Git.Cred.sshKeyFromAgent(username);
}

module.exports = function(repo, o){
o = o || {};

if (!o.remote){
o.remote = 'origin';
}

return repo.getRemote(o.remote)
.then(function(remote){
return repo.fetch(remote, {
callbacks: {
credentials: sshKeyFromAgent,
certificateCheck: function() {
return 1;
}
}
});
})
.then(function(){
return repo.getCurrentBranch();
})
.then(function(current){
var from = o.remote + '/' + current.shorthand();
return repo.mergeBranches(current, from);
});

};
35 changes: 35 additions & 0 deletions lib/push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var Git = require('nodegit');

function sshKeyFromAgent(url, username){
return Git.Cred.sshKeyFromAgent(username);
}

module.exports = function(repo, o){
o = o || {};

if (!o.remote){
o.remote = 'origin';
}

return repo.getRemote(o.remote)
.then(function(remote){
// if no refspec option
// no all, mirror or tags?
// use remote.*.push config
// else push.default config
// else push current branch to corresponding upstream branch

// remote.getRefspec does not work yet :(
// var refspec = remote.getRefspec(0);
// var refspec = '+refs/heads/*:refs/remotes/origin/*';
var refspec = 'refs/heads/master:refs/heads/master';
return remote.push(refspec, {
callbacks: {
credentials: sshKeyFromAgent
}
});
});

};
58 changes: 58 additions & 0 deletions lib/remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var Git = require('nodegit');
var debug = require('debug')('kit:remote');


module.exports = function(repo, o){
o = o || {};

if (o.verbose){
return Git.Remote.list(repo)
.then(function(names){
return Promise.all(names.map(function(name){
return Git.Remote.lookup(repo, name);
}))
.then(function(remotes){
return remotes.map(function(remote){
return {
name: remote.name(),
fetch: remote.url(),
push: remote.pushurl()
};
});
});
});
}

return {

add: function(name, url){
return Git.Remote.create(repo, name, url);
},

remove: function(name){
return Git.Remote.delete(repo, name);
},

list: function(){
return Git.Remote.list(repo);
},

update: function(name, url){
return Git.Remote.lookup(repo, name)
.then(function(remote){
remote.setUrl(url);
remote.save();
})
.catch(function(error){
return Git.Remote.create(name, url);
})
.then(function(){
return repo;
});
}

};

};
112 changes: 112 additions & 0 deletions test/test-remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var git = require('../');

var test = require('ava');
var files = require('fildes-extra');
var resolve = require('path').resolve;

var local1 = resolve(__dirname, './repos/local1');
var local2 = resolve(__dirname, './repos/local2');
var remote1 = resolve(__dirname, './repos/remote1');
var remote2 = resolve(__dirname, './repos/remote2');


test.serial('remote setup (local and dummy remote)', function(t){
return Promise.all([
files.rmdir(local1),
files.rmdir(local2),
files.rmdir(remote1),
files.rmdir(remote2)
])
.then(function(){
return Promise.all([
git.init(local1),
git.init(local2),
git.init(remote1, { bare: 1 }),
git.init(remote2, { bare: 1 })
])
})
.catch(function(err){
t.fail(err);
});
});

test.serial('remote add', function(t){
return git.open(local1)
.then(function(repo){
var remote = git.remote(repo);
return remote.add('origin', remote1)
.then(function(){
return remote.add('remote2', remote2);
})
.then(function(){
return git.remote(repo, {
verbose: true
})
.then(function(remotes){
t.is(remotes.length, 2, 'has 2 remotes');
})
})
})
.catch(function(err){
t.fail(err);
});
});

test.serial('remote remove', function(t){
return git.open(local2)
.then(function(repo){
var remote = git.remote(repo);
return remote.add('upstream', remote1)
.then(function(){
return remote.add('remote2', remote2);
})
.then(function(){
return remote.remove('remote2');
})
.then(function(){
return git.remote(repo, {
verbose: true
})
.then(function(remotes){
t.is(remotes.length, 1, 'has 1 remote');
})
})
})
.catch(function(err){
t.fail(err);
});
});


test.serial('push', function(t){
var filepath = resolve(local1, 'new-file.txt');

return git.open(local1)
.then(function(repo){
return files.writeFile(filepath, 'a new file!\n')
.then(function(){
return git.commit(repo, {
'message': 'added a new file'
});
})
.then(function(oid){
return git.push(repo);
})
})
.catch(function(err){
t.fail(err);
});
});


test.serial('pull', function(t){
return git.open(local2)
.then(function(repo){
return git.pull(repo, {
remote: 'upstream'
});
})
.catch(function(err){
t.fail(err);
});
});

0 comments on commit b6042cb

Please sign in to comment.