Git Product home page Git Product logo

parse-git-config's Introduction

parse-git-config NPM version NPM monthly downloads NPM total downloads Linux Build Status

Parse .git/config into a JavaScript object. sync or async.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save parse-git-config

Usage

const parse = require('parse-git-config');

// sync
console.log(parse.sync());

// using async/await
(async () => console.log(await parse()))();

Options

cwd

The starting directory to search from.

Type: string

Default: process.cwd() (current working directory)

path

Either the absolute path to .git config, or the path relative to the current working directory.

Type: string

Default: .git/config

Examples config object

Parsed config object will look something like:

{ core:
   { repositoryformatversion: '0',
     filemode: true,
     bare: false,
     logallrefupdates: true,
     ignorecase: true,
     precomposeunicode: true },
  'remote "origin"':
   { url: 'https://github.com/jonschlinkert/parse-git-config.git',
     fetch: '+refs/heads/*:refs/remotes/origin/*' },
  'branch "master"': { remote: 'origin', merge: 'refs/heads/master', ... } }

API

Asynchronously parse a .git/config file. If only the callback is passed, the .git/config file relative to process.cwd() is used.

Params

  • options {Object|String|Function}: Options with cwd or path, the cwd to use, or the callback function.
  • callback {Function}: callback function if the first argument is options or cwd.
  • returns {Object}

Example

parse((err, config) => {
  if (err) throw err;
  // do stuff with config
});

// or, using async/await
(async () => {
  console.log(await parse());
  console.log(await parse({ cwd: 'foo' }));
  console.log(await parse({ cwd: 'foo', path: 'some/.git/config' }));
})();

Synchronously parse a .git/config file. If no arguments are passed, the .git/config file relative to process.cwd() is used.

Params

  • options {Object|String}: Options with cwd or path, or the cwd to use.
  • returns {Object}

Example

console.log(parse.sync());
console.log(parse.sync({ cwd: 'foo' }));
console.log(parse.sync({ cwd: 'foo', path: 'some/.git/config' }));

Returns an object with only the properties that had ini-style keys converted to objects.

Params

  • config {Object}: The parsed git config object.
  • returns {Object}

Example

const config = parse.sync({ path: '/path/to/.gitconfig' });
const obj = parse.expandKeys(config);

.expandKeys examples

Converts ini-style keys into objects:

Example 1

const parse = require('parse-git-config');
const config = { 
  'foo "bar"': { doStuff: true },
  'foo "baz"': { doStuff: true } 
};

console.log(parse.expandKeys(config));

Results in:

{ 
  foo: { 
    bar: { doStuff: true }, 
    baz: { doStuff: true } 
  } 
}

Example 2

const parse = require('parse-git-config');
const config = {
  'remote "origin"': { 
    url: 'https://github.com/jonschlinkert/normalize-pkg.git',
    fetch: '+refs/heads/*:refs/remotes/origin/*' 
  },
  'branch "master"': { 
    remote: 'origin', 
    merge: 'refs/heads/master' 
  },
  'branch "dev"': { 
    remote: 'origin', 
    merge: 'refs/heads/dev', 
    rebase: true 
  }
};

console.log(parse.expandKeys(config));

Results in:

{
  remote: {
    origin: {
      url: 'https://github.com/jonschlinkert/normalize-pkg.git',
      fetch: '+refs/heads/*:refs/remotes/origin/*'
    }
  },
  branch: {
    master: {
      remote: 'origin',
      merge: 'refs/heads/master'
    },
    dev: {
      remote: 'origin',
      merge: 'refs/heads/dev',
      rebase: true
    }
  }
}

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
66 jonschlinkert
4 doowb
1 daviwil
1 LexSwed
1 sam3d
1 suarasaur

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on November 20, 2018.

parse-git-config's People

Contributors

daviwil avatar doowb avatar jonschlinkert avatar junosuarez avatar sam3d avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parse-git-config's Issues

TypeError: Path must be a string. Received null; at index.js:116:15

error output

% ❯ verb                                                                                               November 04, 06:02:12
[06:06:55] starting verb
[06:06:59] ✔ running tasks: [ 'readme' ]
path.js:7
    throw new TypeError('Path must be a string. Received ' + inspect(path));
    ^

TypeError: Path must be a string. Received null
    at assertPath (path.js:7:11)
    at Object.resolve (path.js:1146:7)
    at Function.resolve (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/parse-git-config/index.js:116:15)
    at Function.parseSync [as sync] (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/parse-git-config/index.js:79:24)
    at Schema.module.exports (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/expand-pkg/lib/expanders/git.js:13:36)
    at Schema.normalizeField (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/map-schema/index.js:557:16)
    at Schema.update (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/map-schema/index.js:246:8)
    at Schema.module.exports (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/expand-pkg/lib/expanders/owner.js:14:10)
    at Schema.normalizeField (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/map-schema/index.js:557:16)
    at Schema.normalize (/home/charlike/.nvm/versions/node/v7.0.0/lib/node_modules/verb-generate-readme/node_modules/map-schema/index.js:497:27)

just found it, because just dont have .git/ dir.

Does not support multiple values

Git config supports multiple values for the same key. A real-world example:

git config --add url.https://github.com/.insteadof [email protected]:
git config --add url.https://github.com/.insteadof ssh://github.com/

parse-git-config doesn't seem to support this; it only parses the last value, dropping earlier ones.

resolveConfigPath bugs

When calling parse('./directory'), the string is supposed to be interpreted as cwd.
https://github.com/jonschlinkert/parse-git-config/blob/master/index.js#L36

Yet that value is passed to resolveConfigPath('./directory') which treats it as the type instead of cwd.
https://github.com/jonschlinkert/parse-git-config/blob/master/index.js#L58
https://github.com/jonschlinkert/parse-git-config/blob/master/index.js#L89
https://github.com/jonschlinkert/parse-git-config/blob/master/index.js#L108

Additionally, this call to configPath passes the type but omits cwd. So when you call resolveConfigPath({cwd, type}) -- or pass the same options object to parse -- it will not do the right thing.
https://github.com/jonschlinkert/parse-git-config/blob/master/index.js#L110

Support . in branch names

replicate:
create a branch named "2.0"

...
'branch "master"': { remote: 'origin', merge: 'refs/heads/master' },
  'branch "2': { '0"': { remote: 'origin', merge: 'refs/heads/2.0' }
...

Expected result would be "2.0"

Support 'include' directives

The include directive is useful to include another git configuration file in the current one. For example, I use it to store private information like access tokens in it, which allows me to commit my .gitconfig to a public repository.

Not that ~ is resolved in the include path, so applying untildify on the path will be wise.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.