Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Feb 26, 2016
0 parents commit a2f4641
Show file tree
Hide file tree
Showing 21 changed files with 705 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["nodejs-lts"]
}
14 changes: 14 additions & 0 deletions .editorconfig
@@ -0,0 +1,14 @@
root = true

[*]
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

[*.{json,*rc,yml}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
40 changes: 40 additions & 0 deletions .eslintrc
@@ -0,0 +1,40 @@
parser: babel-eslint

parserOptions:
ecmaVersion": 6,
sourceType: module

env:
node: true
es6: true

rules:
# Possible Errors
# https://github.com/eslint/eslint/tree/master/docs/rules#possible-errors
no-debugger: 2
no-dupe-args: 2
no-dupe-keys: 2
no-duplicate-case: 2
no-ex-assign: 2
no-extra-boolean-cast: 2
no-extra-semi: 2
no-func-assign: 2
no-invalid-regexp: 2
no-irregular-whitespace: 2
no-negated-in-lhs: 2
no-obj-calls: 2
no-proto: 2
no-unexpected-multiline: 2
no-unreachable: 2
use-isnan: 2
valid-typeof: 2

# Best Practices
# https://github.com/eslint/eslint/tree/master/docs/rules#best-practices
no-fallthrough: 2
no-redeclare: 2

# Variables
# http://eslint.org/docs/rules/#variables
no-undef: 2
no-unused-vars: 2
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
node_modules
lib
coverage
.nyc_output
34 changes: 34 additions & 0 deletions .travis.yml
@@ -0,0 +1,34 @@
sudo: false

language: node_js

matrix:
include:
- node_js: "4"
env: COVERALLS=1
- node_js: "5"

# This is a random private key used purely for testing.
before_script:
- echo -e "Host *\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
- echo -e "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkTcgXnHuqR0gbwegnr9Zxz4hTkjjV/SpgJNPJz7mo/HKN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DYwfG9og0tmz\nfzlruAgBAoGBAPfz73Pey86tNZEanhJhbX8gVjzy2hvyhT0paHg0q/H6c1VWOtUH\nOwZ3Ns2xAZqJhlDqCHnQYSCZDly042U/theP4N8zo1APb4Yg4qdmXF9QE1+2M03r\nkS6138gU/CSCLf8pCYa6pA/GmsaXxloeJGLvT4fzOZRsVav80/92XHRhAoGBAOu2\nmKh4Gr1EjgN9QNbk9cQTSFDtlBEqO/0pTepvL73UvNp/BAn4iYZFU4WnklFVBSWc\nL84Sc732xU12TAbTTUsa6E7W29pS8u7zVTxlIdQIIU5pzDyU1pNNk2kpxzte5p3Y\nPDtniPFsoYLWoH0LpsKL93t2pLAj+IOkE6f3XBq5AoGAIKaYo5N1FxQr952frx/x\nQUpK0N/R5Ng8v18SiLG26rhmM5iVSrQXC7TrHI7wfR8a9tC6qP/NqnM9NuwC/bQ0\nEEo7/GhaWxKNRwZRkmWiSFLNGk9t1hbtGU+N1lUdFtmloPIQdRNiw0kN3JTj474Q\nYI7O1EItFORnK6yxZfR6HEECgYEA1CT7MGUoa8APsMRCXyaiq15Pb8bjxK8mXquW\nHLEFXuzhLCW1FORDoj0y9s/iuKC0iS0ROX8R/J7k5NrbgikbH8WP36UxKkYNr1IC\nHOFImPTYRSKjVsL+fIUNb1DSp3S6SsYbL7v3XJJQqtlQiDq8U8x1aQFXJ9C4EoLR\nzhKrKsECgYBtU/TSF/TATZY5XtrN9O+HX1Fbz70Ci8XgvioheVI2fezOcXPRzDcC\nOYPaCMNKA5E8gHdg4s0TN7uDvKTJ+KhSg2V7gZ39A28dHrJaRX7Nz4k6t2uEBjX9\na1JidpAIbJ+3w7+hj6L299tVZvS+Y/6Dz/uuEQGXfJg/l/5CCvQPsA==\n-----END RSA PRIVATE KEY-----" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa*
- eval `ssh-agent -s`
- ssh-add ~/.ssh/id_rsa
- git config --global user.name travis
- git config --global user.email travis@locahost

after_success:
- if [ "x$COVERALLS" = "x1" ]; then npm run coveralls; fi

# It is necessary for nodegit
# https://github.com/nodegit/nodegit#getting-started
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- libstdc++-4.9-dev
- gcc-4.9
- g++-4.9
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Andrew Abramov <mail@blond.im>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
138 changes: 138 additions & 0 deletions README.md
@@ -0,0 +1,138 @@
nodegit-clone
=============

[![NPM Status][npm-img]][npm]
[![Travis Status][test-img]][travis]
[![Coverage Status][coverage-img]][coveralls]
[![Dependency Status][dependency-img]][david]

[npm]: https://www.npmjs.org/package/nodegit-clone
[npm-img]: https://img.shields.io/npm/v/nodegit-clone.svg

[travis]: https://travis-ci.org/blond/nodegit-clone
[test-img]: https://img.shields.io/travis/blond/nodegit-clone.svg?label=tests

[coveralls]: https://coveralls.io/r/blond/nodegit-clone
[coverage-img]: https://img.shields.io/coveralls/blond/nodegit-clone.svg

[david]: https://david-dm.org/blond/nodegit-clone
[dependency-img]: http://img.shields.io/david/blond/nodegit-clone.svg

Clone git repository with [nodegit](http://www.nodegit.org/).

Install
-------

```
$ npm install --save nodegit-clone
```

Usage
-----

```js
import clone from 'nodegit-clone';

clone('https://github.com/owner/repo')
.then(repo => {
// Access any repository methods here.
console.log(repo.path());
});

// path/to/repo/.git
```

API
---

### clone({ url, [localPath], [ghToken], [ssh] })

Returns a Promise, that resolves to instance of [Repository](http://www.nodegit.org/api/repository/).

#### url

Type: `string`

The URL to the repository.

**Note:** the following protocols are supported: `http`, `https`, `git` and `ssh`.

```js
clone('http://github.com/owner/repo');
clone('https://github.com/owner/repo');
clone('git://github.com/owner/repo.git');
clone('git@github.com:owner/repo.git');
```

#### localPath

Type: `string`

The Local path to store repository.

**Note:** if `localPath` is not specified then repository will be cloned to directory with repository name.

#### ghToken

Type: `string`

The GitHub personal OAuth token.

#### ssh

Type: `object`

The object with paths to ssh keys and passphrase.

GitHub Private Repositories
---------------------------

Before you can clone a repository, you'll need a GitHub OAuth application token. You can find more information on generating one here: [Creating an access token for command-line use](https://help.github.com/articles/creating-an-access-token-for-command-line-use/).

In this example we're going to clone one of our private test repositories from GitHub. This must be an https protocol URL for the clone to work.

```js
// Keep this value a secret. If you accidentally commit
// this key to a public GitHub repository they will immediately revoke it.
const GITHUB_TOKEN = '<GH_TOKEN>';

clone({
url: 'https://github.com/owner/private',
ghToken: GITHUB_TOKEN
});
```

SSH Keys
--------

Before you can clone a repository, you'll need SSH keys. You can find more information on generating them here: [Generating an SSH key](https://help.github.com/articles/generating-an-ssh-key/).

In this example we're going to clone one of our private test repositories. This must be an ssh protocol URL for the clone to work.

```js
clone({
url: 'git@github.com:owner/private.git',
ssh: {
publicKey: '/path/to/public-key',
privateKey: '/path/to/private-key'
}
});
```

For encrypted keys you should specify the `passphrase` option:

```js
clone({
url: 'git@github.com:owner/private.git',
ssh: {
publicKey: '/path/to/public-key',
privateKey: '/path/to/private-key',
passphrase: 'password'
}
});
```

License
-------

MIT © [Andrew Abramov](https://github.com/blond)
61 changes: 61 additions & 0 deletions package.json
@@ -0,0 +1,61 @@
{
"name": "nodegit-clone",
"version": "0.0.0",
"description": "Clone git repository with nodegit",
"license": "MIT",
"repository": "blond/nodegit-clone",
"author": "Andrew Abramov <mail@blond.im> (github.com/blond)",
"keywords": [
"git",
"clone",
"http",
"https",
"ssh",
"github",
"token"
],
"main": "lib/clone.js",
"files": [
"libs/**",
"index.js"
],
"dependencies": {
"is-ssh": "1.3.0",
"nodegit": "0.11.5",
"throw": "1.1.0"
},
"devDependencies": {
"ava": "0.12.0",
"babel-cli": "6.5.1",
"babel-eslint": "5.0.0",
"babel-preset-nodejs-lts": "1.2.1",
"babel-register": "6.5.2",
"coveralls": "2.11.6",
"es6-promisify": "3.0.0",
"eslint": "2.2.0",
"fs-extra": "0.26.5",
"nyc": "5.6.0",
"tempfile": "1.1.1"
},
"scripts": {
"test": "npm run lint && npm run cover",
"lint": "eslint .",
"unit-test": "ava",
"cover": "nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm run clean && npm run compile",
"compile": "babel src -d lib",
"clean": "rm -rf lib/"
},
"ava": {
"files": [
"test/**/*.js",
"!test/fixtures/**",
"!test/utils/**"
],
"verbose": true,
"require": [
"babel-register"
]
}
}
54 changes: 54 additions & 0 deletions src/clone.js
@@ -0,0 +1,54 @@
import os from 'os';

import git from 'nodegit';
import isSSH from 'is-ssh';

import init from './init';

/**
* Clones repository to `localPath`.
*
* @param {Object} opts Options or url of the repository.
* @param {String} opts.url The URL to the repository.
* @param {String} [opts.localPath] The Local path to store repository.
* @param {String} [opts.ghToken] The GitHub personal OAuth token.
* @param {Object} [opts.ssh] The object with paths to ssh keys and passphrase.
* @param {String} [opts.ssh.publicKey] The path to the public key of the credential.
* @param {String} [opts.ssh.privateKey] The path to the private key of the credential.
* @param {String} [opts.ssh.passphrase] The passphrase of the credential.
*
* @returns {Promise<Git.Repository>} A promise `Git.Repository` instance.
* @see [Git.Repository]{@link http://www.nodegit.org/api/repository/}
*/
export default function clone(opts) {
const { url, localPath, ghToken, ssh } = init(opts);

const cloneOpts = { fetchOpts: {} };
const callbacks = cloneOpts.fetchOpts.callbacks = {};

if (os.type() === 'Darwin'/* OS X */) {
// This is a required callback for OS X machines. There is a known issue
// with libgit2 being able to verify certificates from GitHub.
callbacks.certificateCheck = () => 1;
}

if (ghToken) {
callbacks.certificateCheck = () => 1;
callbacks.credentials = () => {
// In order to authorize the clone operation, we'll need to respond to a low-level callback
// that expects credentials to be passed.
// This function will respond back with the OAuth token.
return git.Cred.userpassPlaintextNew(ghToken, "x-oauth-basic");
};
} else if (ssh.publicKey || isSSH(url)) {
callbacks.credentials = (url, username) => {
// Forward user name to validate authentication.
return git.Cred.sshKeyFromAgent(username, ssh.publicKey, ssh.privateKey, ssh.passphrase);
};
}

// Convert NodeGit promise to ES Promise
return new Promise((resolve, reject) => {
git.Clone(url, localPath, cloneOpts).then(resolve, reject);
});
}
20 changes: 20 additions & 0 deletions src/init.js
@@ -0,0 +1,20 @@
import path from 'path';
import thr from 'throw';

import repoName from './repo-name';

/**
* Initializes options.
*
* @param {Object} opts
* @returns {String}
*/
export default function init(opts={}) {
const {
url = (typeof opts === 'string') ? opts : thr('You should specify url to repository.'),
localPath = repoName(url),
ghToken, ssh={}
} = opts;

return { url, localPath: path.resolve(localPath), ghToken, ssh };
}

0 comments on commit a2f4641

Please sign in to comment.