Skip to content

Commit

Permalink
Add checkout and branch commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Aug 13, 2014
1 parent 29e81cc commit 2312081
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/cli-option.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class CliOption
[@option, @args] = [option, args]
Util.checkArgs @args, [Array, String, Number, Boolean]
@args = [@args] unless _.isArray @args
@args = (arg.toString() for arg in @args)
@args = _.map @args, (a) -> if a == _.isBoolean(a) then '' else a.toString()

@hasArgs = true
@hasArgs = _.any @args, (a) -> a.length > 0

toString: ->
if @hasArgs
Expand Down
10 changes: 10 additions & 0 deletions src/git-util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ GitUtil =
pushUrl: /\s+Push URL: (.*?)\n/.exec(remoteStr)?[1]
headBranch: /\s+HEAD branch: (.*?)\n/.exec(remoteStr)?[1]

parseCurrentBranch: (branches) ->
branches = branches.trim().split '\n'
branch = _.find branches, (b) -> b[0] == '*'
if branch? then branch.substring(2) else undefined

parseBranches: (branches) ->
branches = branches.trimRight().split '\n'
_.map branches, (b) -> b.substring(2)


module.exports = GitUtil
31 changes: 31 additions & 0 deletions src/repository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ class Repository
command = new CliCommand(['git', 'remote', 'show'], name, options.cli)
Runner.execute command, @_createOptions(options)

currentBranch: (options, callback) ->
options = Util.setOptions options, callback
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
branch = GitUtil.parseCurrentBranch stdout
callback err, branch
command = new CliCommand(['git', 'branch'], options.cli)
Runner.execute command, @_createOptions(options)

branch: (branch, options, callback) ->
branch = [branch] if _.isString(branch)
if _.isArray(branch)
[options, hasName] = [Util.setOptions(options, callback), true]
else
[options, hasName] = [Util.setOptions(branch, options), false]
branch = [] unless hasName
if options.callback && !hasName
callback = options.callback
options.callback = (err, stdout, stderr) ->
branches = GitUtil.parseBranches stdout
callback err, branches
command = new CliCommand(['git', 'branch'], branch, options.cli)
Runner.execute command, @_createOptions(options)


checkout: (branch, options, callback) ->
options = Util.setOptions options, callback
command = new CliCommand(['git', 'checkout'], branch, options.cli)
Runner.execute command, @_createOptions(options)


workingDir: -> path.dirname @path

Expand Down
5 changes: 5 additions & 0 deletions test/cli-option-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ describe 'CliOption', ->
option = new CliOption('message', 'lorem ipsum')
expected = "--message='lorem ipsum'"
expect(option.toString()).to.be expected

it 'should ignore "true"', ->
option = new CliOption('verbose', true)
expected = "--verbose"
expect(option.toString()).to.be expected
69 changes: 50 additions & 19 deletions test/git-util-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,54 @@ describe 'GitUtil', ->
checkStats s, expected

describe '#parseRemote', ->
s = """
* remote origin
Fetch URL: git@github.com:tuvistavie/node-git-cli.git
Push URL: git@github.com:tuvistavie/node-git-cli.git
HEAD branch: master
Remote branch:
it 'should parse remote info', ->
s = """
* remote origin
Fetch URL: git@github.com:tuvistavie/node-git-cli.git
Push URL: git@github.com:tuvistavie/node-git-cli.git
HEAD branch: master
Remote branch:
master
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
"""
remoteInfo = GitUtil.parseRemote s
expected =
fetchUrl: 'git@github.com:tuvistavie/node-git-cli.git'
pushUrl: 'git@github.com:tuvistavie/node-git-cli.git'
headBranch: 'master'

expect(remoteInfo).to.eql expected

describe '#parseCurrentBranch', ->
it 'should parse current branch', ->
s = """
dev
facebook_share
master
mobile
* new_design
"""
currentBranch = GitUtil.parseCurrentBranch s
expect(currentBranch).to.be 'new_design'

it 'should return undefined when no current branch', ->
expect(GitUtil.parseCurrentBranch('')).to.be undefined

describe '#parseBranches', ->
it 'should parse branch list', ->
s = """
dev
facebook_share
master
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
"""
remoteInfo = GitUtil.parseRemote s
expected =
fetchUrl: 'git@github.com:tuvistavie/node-git-cli.git'
pushUrl: 'git@github.com:tuvistavie/node-git-cli.git'
headBranch: 'master'

expect(remoteInfo).to.eql expected
mobile
* new_design
"""
branches = GitUtil.parseBranches s
expected = ['dev', 'facebook_share', 'master', 'mobile', 'new_design']
expect(branches).to.eql expected
38 changes: 38 additions & 0 deletions test/repository-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,47 @@ describe 'Repository', ->
describe '#showRemote', ->
it 'should get remote info', (done) ->
testRepository.showRemote 'origin', (err, info) ->
expect(err).to.be null
expected =
pushUrl: baseRepository.path
fetchUrl: baseRepository.path
headBranch: 'master'
expect(info).to.eql expected
done()

describe '#currentBranch', ->
it 'should return current branch', (done) ->
testRepository.currentBranch (err, branch) ->
expect(err).to.be null
expect(branch).to.be 'master'
done()

describe '#branch', ->
it 'should list branches', (done) ->
testRepository.branch (err, branches) ->
expect(branches).to.eql ['master']
done()

it 'should create new branches', (done) ->
testRepository.branch 'foo', (err, branches) ->
testRepository.branch (err, branches) ->
expect(branches).to.eql ['foo', 'master']
done()


describe '#checkout', ->
it 'should do basic branch checkout', (done) ->
testRepository.currentBranch (err, branch) ->
testRepository.branch 'gh-pages', (err, branches) ->
expect(branch).to.be 'master'
testRepository.checkout 'gh-pages', (err) ->
expect(err).to.be null
testRepository.currentBranch (err, branch) ->
expect(branch).to.be 'gh-pages'
done()

it 'should work with -b flag', (done) ->
testRepository.checkout 'foo', { cli: { b: true } }, (err) ->
testRepository.currentBranch (err, branch) ->
expect(branch).to.be 'foo'
done()

0 comments on commit 2312081

Please sign in to comment.