-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds remote, and super simple pull and push (see #14)
- Loading branch information
thisconnect
committed
May 9, 2016
1 parent
2740a5b
commit b6042cb
Showing
5 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|
||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |