Skip to content

Commit

Permalink
Add basic support for reading git config
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric O'Connell committed Oct 5, 2013
1 parent 9503fc8 commit 9ef8c80
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
17 changes: 17 additions & 0 deletions src/config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = C = (repo, callback) ->
repo.git "config", {list: true}, (err, stdout, stderr) ->
config = new Config repo
config.parse stdout
callback err, config

C.Config = class Config
constructor: (@repo) ->

# Internal: Parse the config from stdout of a `git config` command
parse: (text)->
@values = {}
for line in text.split("\n")
if line.length == 0
continue
[key, value] = line.split('=')
@values[key] = value
22 changes: 11 additions & 11 deletions src/git.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fs = require 'fs'

module.exports = Git = (git_dir, dot_git) ->
dot_git ||= "#{git_dir}/.git"

git = (command, options, args, callback) ->
[callback, args] = [args, callback] if !callback
[callback, options] = [options, callback] if !callback
Expand All @@ -15,26 +15,26 @@ module.exports = Git = (git_dir, dot_git) ->
bash = "#{Git.bin} #{command} #{options} #{args}"
exec bash, {cwd: git_dir}, callback
return bash


# Public: Get a list of the remote names.
#
#
# callback - Receives `(err, names)`.
#
#
git.list_remotes = (callback) ->
fs.readdir "#{dot_git}/refs/remotes", (err, files) ->
callback err, (files || [])


# Public: Get the ref data string.
#
#
# type - Such as `remote` or `tag`.
# callback - Receives `(err, stdout)`.
#
#
git.refs = (type, options, callback) ->
[callback, options] = [options, callback] if !callback
prefix = "refs/#{type}s/"

git "show-ref", (err, text) ->
matches = []
for line in (text || "").split("\n")
Expand All @@ -43,7 +43,7 @@ module.exports = Git = (git_dir, dot_git) ->
if name.substr(0, prefix.length) == prefix
matches.push "#{name.substr(prefix.length)} #{id}"
return callback err, matches.join("\n")

return git


Expand Down
4 changes: 4 additions & 0 deletions src/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ _ = require 'underscore'
cmd = require './git'
Actor = require './actor'
Commit = require './commit'
Config = require './config'
Tree = require './tree'
Diff = require './diff'
Tag = require './tag'
Expand Down Expand Up @@ -184,6 +185,9 @@ module.exports = class Repo
status: (callback) ->
return Status(this, callback)

config: (callback) ->
return Config(this, callback)


# Public: Get the repository's tags.
#
Expand Down
36 changes: 36 additions & 0 deletions test/config.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
should = require 'should'
Config = require '../src/config'

GIT_CONFIG = """
user.name=John Doe
[email protected]
core.editor=pico
"""

GIT_CONFIG_DUPLICATE_KEYS = """
user.name=John Doe
[email protected]
core.editor=pico
[email protected]
core.editor=emacs
"""

describe "Config", ->
describe "()", ->
describe "when there are no overlapping keys", ->
config = new Config.Config 'mock repo'
config.parse GIT_CONFIG

it "read the keys and values", ->
config.values['user.name'].should.equal 'John Doe'
config.values['user.email'].should.equal '[email protected]'
config.values['core.editor'].should.equal 'pico'

describe "with overlapping keys", ->
config = new Config.Config 'mock repo'
config.parse GIT_CONFIG_DUPLICATE_KEYS

it "read the keys and values", ->
config.values['user.name'].should.equal 'John Doe'
config.values['user.email'].should.equal '[email protected]'
config.values['core.editor'].should.equal 'emacs'
14 changes: 7 additions & 7 deletions test/status.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ describe "Status", ->
repo = fixtures.status
status = new Status.Status repo
status.parse GIT_STATUS_CLEAN

it "is clean", ->
status.clean.should.be.true

describe "when there are changes", ->
repo = fixtures.status
status = new Status.Status repo
Expand All @@ -37,25 +37,25 @@ describe "Status", ->
repo = fixtures.status
status = new Status.Status repo
status.parse GIT_STATUS

it "has a modified staged file", ->
status.files["file.txt"].staged.should.be.true
status.files["file.txt"].type.should.eql "M"
status.files["file.txt"].tracked.should.be.true

it "has a modified unstaged file", ->
status.files["cheese.txt"].staged.should.be.false
status.files["cheese.txt"].type.should.eql "M"
status.files["cheese.txt"].tracked.should.be.true

it "has a deleted file", ->
status.files["crackers.txt"].staged.should.be.true
status.files["crackers.txt"].type.should.eql "D"
status.files["crackers.txt"].tracked.should.be.true

it "has an untracked file", ->
status.files["pickles.txt"].tracked.should.be.false
should.not.exist status.files["pickles.txt"].type

it "is not clean", ->
status.clean.should.be.false

0 comments on commit 9ef8c80

Please sign in to comment.