diff --git a/README.md b/README.md index f575b54..849e37d 100644 --- a/README.md +++ b/README.md @@ -193,9 +193,16 @@ Commit some changes. ### `Repo#remove(files, callback)` `git rm ` -### `Repo#checkout(treeish, callback)` +### `Repo#checkout(treeish, [options], callback)` `git checkout ` +Checkout a branch/commit/... + + * `treeish` - Branch or treeish to checkout. + * `options` - + - `b` - `Boolean` to create a new branch + * `callback` - Receives `(err)`. + ### `Repo#checkoutFile([files, options, ]callback)` Checkout some files. diff --git a/src/repo.coffee b/src/repo.coffee index 3c0e2be..7aa0fc4 100644 --- a/src/repo.coffee +++ b/src/repo.coffee @@ -365,8 +365,13 @@ module.exports = class Repo # Public: Checkout the treeish. - checkout: (treeish, callback) -> - @git "checkout", {}, treeish, callback + # + # options - The {Object} containing any of the options available to git checkout: + # :b - {Boolean) Creates a branch when it doesn't exist yet. + # + checkout: (treeish, options, callback) -> + [options, callback] = [{}, options] if !callback + @git "checkout", options, treeish, callback # Public: Clean the git repo by removing untracked files # diff --git a/test/repo.test.coffee b/test/repo.test.coffee index a0ce37b..e4b4de2 100644 --- a/test/repo.test.coffee +++ b/test/repo.test.coffee @@ -676,6 +676,51 @@ describe "Repo", -> status.files.should.not.have.a.property file status.files.should.not.have.a.property 'rawr.txt' + describe "#checkout", -> + repo = null + head = null + git_dir = __dirname + "/fixtures/junk_checkout" + + # given a fresh new repo + beforeEach (done) -> + fs.remove git_dir, (err) -> + return done err if err + fs.copy "#{__dirname}/fixtures/reset", "#{git_dir}", (err) -> + return done err if err + fs.rename "#{git_dir}/git.git", "#{git_dir}/.git", (err) -> + git.init git_dir, (err) -> + return done err if err + repo = git git_dir + done() + + after (done) -> + fs.remove git_dir, (err) -> + done err + + describe "an existing branch", -> + beforeEach (done) -> + repo.checkout "feature/foo", {b: true}, (err) -> + return done err if err? + repo.checkout "master", (err) -> + return done err if err? + repo.branch (err, _head) -> + head = _head + done err + + it "should succeed", -> + head.name.should.equal "master" + + describe "and create new branch", -> + beforeEach (done) -> + repo.checkout "feature/foo", {b: true}, (err) -> + return done err if err? + repo.branch (err, _head) -> + head = _head + done err + + it "should succeed", -> + head.name.should.equal "feature/foo" + describe "#checkoutFile", -> repo = null git_dir = __dirname + "/fixtures/junk_checkoutFile"