diff --git a/README.md b/README.md index e31eca0..336eadb 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,15 @@ A simple Node.js wrapper for the Git CLI. The API is based on repo = git "path/to/repo" # => # + +Clone a repository: + + git = require 'gift' + + git.clone "git@host:path/to/remote/repo.git", "path/to/local/clone/repo", (err, _repo) -> + repo = _repo + # => # + ## Repo ### `Repo#path` `String` - The path to the repository. diff --git a/src/index.coffee b/src/index.coffee index 42ea7fb..1e4aa8c 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -18,3 +18,15 @@ Git.init = (path, callback) -> , (err, stdout, stderr) -> return callback err if err return callback err, (new Repo path) + +# Public: Clone a git repository. +# +# repository - The repository to clone from. +# path - The directory to clone into. +# callback - Receives `(err, repo)`. +# +Git.clone = (repository, path, callback) -> + bash = "git clone #{repository} #{path}" + exec bash, (err, stdout, stderr) -> + return callback err if err + return callback err, (new Repo path) \ No newline at end of file diff --git a/test/index.test.coffee b/test/index.test.coffee index 784bf44..25342b3 100644 --- a/test/index.test.coffee +++ b/test/index.test.coffee @@ -1,6 +1,7 @@ should = require 'should' git = require '../src' Repo = require '../src/repo' +{exec} = require 'child_process' describe "git", -> describe "()", -> @@ -8,3 +9,19 @@ describe "git", -> it "returns a Repo", -> repo.should.be.an.instanceof Repo + + describe "clone()", -> + @timeout 30000 + repo = null + newRepositoryDir = "#{__dirname}/fixtures/clone" + before (done) -> + git.clone "git@github.com:sentientwaffle/gift.git", newRepositoryDir, (err, _repo) -> + repo = _repo + done err + it "clone a repository", (done) -> + repo.should.be.an.instanceof Repo + repo.remote_list (err, remotes) -> + remotes.should.have.length 1 + done() + after (done) -> + exec "rm -rf #{newRepositoryDir}", done \ No newline at end of file