├── .gitignore ├── .jshintrc ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── .gitconfig ├── gitconfig1.ini ├── gitconfig2.ini └── gitconfig3.ini └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | .DS_Store 3 | node_modules/ 4 | .idea 5 | npm-debug.log 6 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "laxcomma": true 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | sudo: false 4 | 5 | node_js: 6 | - 0.10 7 | - 0.12 8 | - iojs 9 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | // Generated on 2013-09-11 using generator-nodejs 0.0.0 2 | module.exports = function (grunt) { 3 | grunt.initConfig({ 4 | pkg: grunt.file.readJSON('package.json'), 5 | complexity: { 6 | generic: { 7 | src: ['app/**/*.js'], 8 | options: { 9 | errorsOnly: false, 10 | cyclometric: 6, // default is 3 11 | halstead: 16, // default is 8 12 | maintainability: 100 // default is 100 13 | } 14 | } 15 | }, 16 | jshint: { 17 | all: [ 18 | 'Gruntfile.js', 19 | 'app/**/*.js', 20 | 'test/**/*.js' 21 | ], 22 | options: { 23 | jshintrc: '.jshintrc' 24 | } 25 | }, 26 | mochacli: { 27 | all: ['test/**/*.js'], 28 | options: { 29 | reporter: 'spec', 30 | ui: 'tdd' 31 | } 32 | }, 33 | watch: { 34 | js: { 35 | files: ['**/*.js'], 36 | tasks: ['default'], 37 | options: { 38 | nospawn: true 39 | } 40 | } 41 | } 42 | }); 43 | 44 | grunt.loadNpmTasks('grunt-complexity'); 45 | grunt.loadNpmTasks('grunt-contrib-jshint'); 46 | grunt.loadNpmTasks('grunt-contrib-watch'); 47 | grunt.loadNpmTasks('grunt-mocha-cli'); 48 | 49 | grunt.registerTask('test', ['complexity', 'jshint', 'mochacli', 'watch']); 50 | grunt.registerTask('ci', ['complexity', 'jshint', 'mochacli']); 51 | grunt.registerTask('default', ['test']); 52 | }; 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Eugene Ware 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 1. Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | 2. Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | 3. Neither the name of Eugene Ware nor the names of its contributors 12 | may be used to endorse or promote products derived from this software 13 | without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY EUGENE WARE ''AS IS'' AND ANY 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL EUGENE WARE BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-config 2 | 3 | A simple way to extract out all the contents of a .gitconfig file and return as JSON 4 | 5 | [![build status](https://secure.travis-ci.org/eugeneware/git-config.png)](http://travis-ci.org/eugeneware/git-config) 6 | 7 | ## Installation 8 | 9 | This module is installed via npm: 10 | 11 | ``` bash 12 | $ npm install git-config 13 | ``` 14 | 15 | ## Example Usage 16 | 17 | ### Asynchronous 18 | 19 | ``` js 20 | var gitConfig = require('git-config'); 21 | gitConfig(function (err, config) { 22 | if (err) return done(err); 23 | expect(config.user.name).to.equal('Eugene Ware'); 24 | expect(config.user.email).to.equal('eugene@noblesamurai.com'); 25 | expect(config.github.user).to.equal('eugeneware'); 26 | done(); 27 | }); 28 | ``` 29 | 30 | Explicitly give a gitconfig file: 31 | 32 | ``` js 33 | var gitConfig = require('git-config'); 34 | gitConfig('/my/path/.gitconfig1', function (err, config) { 35 | if (err) return done(err); 36 | expect(config.user.name).to.equal('Eugene Ware'); 37 | expect(config.user.email).to.equal('eugene@noblesamurai.com'); 38 | expect(config.github.user).to.equal('eugeneware'); 39 | done(); 40 | }); 41 | ``` 42 | 43 | ### Synchronous 44 | 45 | ``` js 46 | var gitConfig = require('git-config'); 47 | var config = gitConfig.sync(); // can pass explit file if you want as well 48 | ``` 49 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var parser = require('iniparser'), 2 | path = require('path'); 3 | 4 | module.exports = function (gitConfigPath, cb) { 5 | if (typeof cb === 'undefined') { 6 | cb = gitConfigPath; 7 | gitConfigPath = path.join( 8 | process.env.HOME || process.env.USERPROFILE, '.gitconfig'); 9 | } 10 | parser.parse(gitConfigPath, cb); 11 | }; 12 | 13 | module.exports.sync = function (gitConfigPath) { 14 | if (typeof gitConfigPath === 'undefined') { 15 | gitConfigPath = path.join( 16 | process.env.HOME || process.env.USERPROFILE, '.gitconfig'); 17 | } 18 | var results = {}; 19 | try { 20 | results = parser.parseSync(gitConfigPath); 21 | } catch (err) { } 22 | return results; 23 | }; 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "git-config", 3 | "version": "0.0.7", 4 | "description": "A simple way to extract out all the contents of a .gitconfig file and return as JSON", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node_modules/.bin/grunt ci" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/eugeneware/git-config" 12 | }, 13 | "keywords": [], 14 | "author": "Eugene Ware ", 15 | "license": "BSD-3-Clause", 16 | "dependencies": { 17 | "iniparser": "~1.0.5" 18 | }, 19 | "devDependencies": { 20 | "expect.js": "^0.3.1", 21 | "grunt": "~0.4.1", 22 | "grunt-cli": "~0.1.9", 23 | "grunt-complexity": "^0.3.0", 24 | "grunt-contrib-jshint": "^0.11.2", 25 | "grunt-contrib-watch": "^0.6.1", 26 | "grunt-mocha-cli": "^1.13.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/fixtures/.gitconfig: -------------------------------------------------------------------------------- 1 | [color] 2 | diff = auto 3 | status = auto 4 | branch = auto 5 | [gui] 6 | fontdiff = -family \"Lucida Console\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0 7 | [user] 8 | name = Eugene Ware 9 | email = eugene@noblesamurai.com 10 | [branch] 11 | autosetuprebase = always 12 | [push] 13 | default = tracking 14 | [github] 15 | user = eugeneware 16 | token = notthistoken 17 | -------------------------------------------------------------------------------- /test/fixtures/gitconfig1.ini: -------------------------------------------------------------------------------- 1 | [color] 2 | diff = auto 3 | status = auto 4 | branch = auto 5 | [gui] 6 | fontdiff = -family \"Lucida Console\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0 7 | [user] 8 | name = Eugene Ware 9 | email = eugene@noblesamurai.com 10 | [branch] 11 | autosetuprebase = always 12 | [push] 13 | default = tracking 14 | [github] 15 | user = eugeneware 16 | token = notthistoken 17 | -------------------------------------------------------------------------------- /test/fixtures/gitconfig2.ini: -------------------------------------------------------------------------------- 1 | [color] 2 | diff = auto 3 | status = auto 4 | branch = auto 5 | [gui] 6 | fontdiff = -family \"Lucida Console\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0 7 | [user] 8 | name = Fred Flintstone 9 | email = fred@flintstone.com 10 | [branch] 11 | autosetuprebase = always 12 | [push] 13 | default = tracking 14 | [github] 15 | user = fredflintstone 16 | token = fredstoken 17 | -------------------------------------------------------------------------------- /test/fixtures/gitconfig3.ini: -------------------------------------------------------------------------------- 1 | [core] 2 | repositoryformatversion = 0 3 | filemode = true 4 | bare = false 5 | logallrefupdates = true 6 | ignorecase = true 7 | precomposeunicode = false 8 | [remote "origin"] 9 | url = git@github.com:eugeneware/git-config.git 10 | fetch = +refs/heads/*:refs/remotes/origin/* 11 | [branch "master"] 12 | remote = origin 13 | merge = refs/heads/master 14 | rebase = true 15 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var expect = require('expect.js'), 2 | fs = require('fs'), 3 | path = require('path'), 4 | gitConfig = require('..'); 5 | 6 | function fixture(name) { 7 | return fs.readFileSync(fixturePath(name), { encoding: 'utf8' }); 8 | } 9 | 10 | function fixturePath(name) { 11 | return path.join(__dirname, 'fixtures', name); 12 | } 13 | 14 | describe('git-config', function() { 15 | it('should be able to parse a .gitconfig file', function(done) { 16 | gitConfig(fixturePath('gitconfig1.ini'), function (err, config) { 17 | if (err) return done(err); 18 | expect(config.user.name).to.equal('Eugene Ware'); 19 | expect(config.user.email).to.equal('eugene@noblesamurai.com'); 20 | expect(config.github.user).to.equal('eugeneware'); 21 | done(); 22 | }); 23 | }); 24 | 25 | it('should be able to look for .gitconfig in the usual places', function(done) { 26 | process.env.HOME = fixturePath(''); 27 | gitConfig(function (err, config) { 28 | if (err) return done(err); 29 | expect(config.user.name).to.equal('Eugene Ware'); 30 | expect(config.user.email).to.equal('eugene@noblesamurai.com'); 31 | expect(config.github.user).to.equal('eugeneware'); 32 | done(); 33 | }); 34 | }); 35 | 36 | it('should be able to parse synchronously', function(done) { 37 | process.env.HOME = fixturePath(''); 38 | 39 | var config = gitConfig.sync(); 40 | expect(config.user.name).to.equal('Eugene Ware'); 41 | expect(config.user.email).to.equal('eugene@noblesamurai.com'); 42 | expect(config.github.user).to.equal('eugeneware'); 43 | 44 | var config2 = gitConfig.sync(fixturePath('gitconfig1.ini')); 45 | expect(config2.user.name).to.equal('Eugene Ware'); 46 | expect(config2.user.email).to.equal('eugene@noblesamurai.com'); 47 | expect(config2.github.user).to.equal('eugeneware'); 48 | done(); 49 | }); 50 | 51 | it('should be able to pass in a config path synchronously', function(done) { 52 | var config = gitConfig.sync(fixturePath('gitconfig2.ini')); 53 | expect(config.user.name).to.equal('Fred Flintstone'); 54 | expect(config.user.email).to.equal('fred@flintstone.com'); 55 | expect(config.github.user).to.equal('fredflintstone'); 56 | done(); 57 | }); 58 | 59 | it('should be able to pass in a config path asynchronously', function(done) { 60 | gitConfig(fixturePath('gitconfig2.ini'), function (err, config) { 61 | if (err) return done(err); 62 | expect(config.user.name).to.equal('Fred Flintstone'); 63 | expect(config.user.email).to.equal('fred@flintstone.com'); 64 | expect(config.github.user).to.equal('fredflintstone'); 65 | done(); 66 | }); 67 | }); 68 | 69 | it('should be able to get the origin URL', function(done) { 70 | gitConfig(fixturePath('gitconfig3.ini'), function (err, config) { 71 | expect(config['remote "origin"'].url).to.equal( 72 | 'git@github.com:eugeneware/git-config.git'); 73 | if (err) return done(err); 74 | done(); 75 | }); 76 | }); 77 | }); 78 | --------------------------------------------------------------------------------