Skip to content

Commit

Permalink
locked down the signature of Repo#sync with unit tests and an accurat…
Browse files Browse the repository at this point in the history
…e representation in the README
  • Loading branch information
notatestuser committed Mar 17, 2013
1 parent eed147c commit 4b4c803
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ Commit some changes.
### `Repo#checkout(treeish, callback)`
`git checkout <treeish>`

### `Repo#sync([remote_name, ]branch, callback)`
### `Repo#sync([[remote, ]branch, ]callback)`
Sync the current branch with the remote, keeping all local changes intact.

The following steps are carried out: `stash`, `pull`, `push`, `stash pop`. If there were no changes to stash, the last `stash pop` is not executed.

* `remote_name` - `String`
* `branch` - `String`
* `callback` - Receives `(err)`.
* `remote` - `String` (defaults to `origin`).
* `branch` - `String` (defaults to `master`).
* `callback` - Receives `(err)`.


## Commit
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
, "devDependencies":
{ "should": "1.2.x"
, "mocha": "1.x.x"
, "sinon": "1.6.x"
, "coffee-script": "1.6.x"
}

Expand Down
18 changes: 13 additions & 5 deletions src/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,26 @@ module.exports = class Repo

# Public: Sync the current branch with the remote.
#
# Arguments: ([[remote_name, ]branch_name, ]callback)
#
# remote_name - String (optional).
# branch_name - String.
# callback - Receives `(err)`.
#
sync: (remote_name, branch, callback) ->
[remote_name, callback, branch] = ['origin', branch, remote_name] if !callback
[remote_name, callback, branch] = ['origin', remote_name, []] if !branch
sync: (remote_name, branch_name, callback) ->

# handle 'curried' arguments
[remote, branch] = [remote_name, branch_name] if typeof callback is "function"
[remote, branch, callback] = ["origin", remote_name, branch_name] if typeof branch_name is "function"
[remote, branch, callback] = ["origin", "master", remote_name] if typeof remote_name is "function"

@status (err, status) =>
return callback err if err
@git "stash", {}, ["save"], (err) =>
return callback err if err
@git "pull", {}, [remote_name, branch], (err) =>
@git "pull", {}, [remote, branch], (err) =>
return callback err if err
@git "push", {}, [remote_name, branch], (err) =>
@git "push", {}, [remote, branch], (err) =>
return callback err if err
if not status?.clean
@git "stash", {}, ["pop"], (err) =>
Expand Down
38 changes: 38 additions & 0 deletions test/repo.test.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
should = require 'should'
sinon = require 'sinon'
fs = require 'fs'
fixtures = require './fixtures'
git = require '../src'
Expand All @@ -13,6 +14,43 @@ Status = require '../src/status'
{exec} = require 'child_process'

describe "Repo", ->
describe "#sync", ->
describe "when passed curried arguments", ->
repo = fixtures.branched
remote = branch = ""

before ->
sinon.stub repo, "git", (command, opts, args, callback) ->
if command is "pull"
remote = args[0]
branch = args[1]
callback? null
sinon.stub repo, "status", (callback) ->
callback? null, clean: no

after ->
repo.git.restore()
repo.status.restore()

it "passes through the correct parameters when nothing is omitted", (done) ->
repo.sync "github", "my-branch", ->
remote.should.eql "github"
branch.should.eql "my-branch"
done()

it "passes through the correct parameters when remote_name is omitted", (done) ->
repo.sync "my-branch", ->
remote.should.eql "origin"
branch.should.eql "my-branch"
done()

it "passes through the correct parameters when remote_name and branch are omitted", (done) ->
repo.sync ->
remote.should.eql "origin"
branch.should.eql "master"
done()


describe "#identify", ->
describe "when asked to set the identity's name and email", ->
repo = fixtures.branched
Expand Down

0 comments on commit 4b4c803

Please sign in to comment.