├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── appveyor.yml ├── docs └── README.tmpl.md ├── index.js ├── package.json └── test └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | .vscode 5 | *.sublime-* 6 | 7 | # test related, or directories generated by tests 8 | test/actual 9 | actual 10 | coverage 11 | .nyc* 12 | 13 | # npm 14 | node_modules 15 | npm-debug.log 16 | 17 | # yarn 18 | yarn.lock 19 | yarn-error.log 20 | 21 | # misc 22 | _gh_pages 23 | _draft 24 | _drafts 25 | bower_components 26 | vendor 27 | temp 28 | tmp 29 | TODO.md 30 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '9' 9 | - '8' 10 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | const branch = require('{%= name %}'); 5 | ``` 6 | 7 | Optionally pass the cwd (current working directory) as the first argument. 8 | 9 | 10 | **Promise** 11 | 12 | ```js 13 | branch('some/path') 14 | .then(name => console.log('Branch:', name)) //=> 'master' 15 | .catch(console.error); 16 | ``` 17 | 18 | **Callback** 19 | 20 | ```js 21 | branch(function(err, name) { 22 | if (err) throw err; 23 | console.log('Branch:', name); //=> 'master' 24 | }); 25 | ``` 26 | 27 | **Sync** 28 | 29 | ```js 30 | console.log('Branch:', branch.sync()); //=> 'master' 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2018, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-branch [![NPM version](https://img.shields.io/npm/v/git-branch.svg?style=flat)](https://www.npmjs.com/package/git-branch) [![NPM monthly downloads](https://img.shields.io/npm/dm/git-branch.svg?style=flat)](https://npmjs.org/package/git-branch) [![NPM total downloads](https://img.shields.io/npm/dt/git-branch.svg?style=flat)](https://npmjs.org/package/git-branch) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/git-branch.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/git-branch) 2 | 3 | > Get the current branch from the local git repository. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save git-branch 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | const branch = require('git-branch'); 19 | ``` 20 | 21 | Optionally pass the cwd (current working directory) as the first argument. 22 | 23 | **Promise** 24 | 25 | ```js 26 | branch('some/path') 27 | .then(name => console.log('Branch:', name)) //=> 'Branch: master' 28 | .catch(console.error); 29 | ``` 30 | 31 | **Callback** 32 | 33 | ```js 34 | branch(function(err, name) { 35 | if (err) throw err; 36 | console.log('Branch:', name); //=> 'Branch: master' 37 | }); 38 | ``` 39 | 40 | **Sync** 41 | 42 | ```js 43 | console.log('Branch:', branch.sync()); //=> 'Branch: master' 44 | ``` 45 | 46 | ## About 47 | 48 |
49 | Contributing 50 | 51 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 52 | 53 |
54 | 55 |
56 | Running Tests 57 | 58 | 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: 59 | 60 | ```sh 61 | $ npm install && npm test 62 | ``` 63 | 64 |
65 | 66 |
67 | Building docs 68 | 69 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 70 | 71 | To generate the readme, run the following command: 72 | 73 | ```sh 74 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 75 | ``` 76 | 77 |
78 | 79 | ### Related projects 80 | 81 | You might also be interested in these projects: 82 | 83 | * [git-add-remote](https://www.npmjs.com/package/git-add-remote): API for adding git remotes. | [homepage](https://github.com/jonschlinkert/git-add-remote "API for adding git remotes.") 84 | * [git-user-name](https://www.npmjs.com/package/git-user-name): Get a user's name from git config at the project or global scope, depending on… [more](https://github.com/jonschlinkert/git-user-name) | [homepage](https://github.com/jonschlinkert/git-user-name "Get a user's name from git config at the project or global scope, depending on what git uses in the current context.") 85 | * [github-base](https://www.npmjs.com/package/github-base): JavaScript wrapper that greatly simplifies working with GitHub's API. | [homepage](https://github.com/jonschlinkert/github-base "JavaScript wrapper that greatly simplifies working with GitHub's API.") 86 | 87 | ### Contributors 88 | 89 | | **Commits** | **Contributor** | 90 | | --- | --- | 91 | | 19 | [jonschlinkert](https://github.com/jonschlinkert) | 92 | | 6 | [emilrowland](https://github.com/emilrowland) | 93 | 94 | ### Author 95 | 96 | **Jon Schlinkert** 97 | 98 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 99 | * [GitHub Profile](https://github.com/jonschlinkert) 100 | * [Twitter Profile](https://twitter.com/jonschlinkert) 101 | 102 | ### License 103 | 104 | Copyright © 2018, [Jon Schlinkert](http://github.com/https://github.com/jonschlinkert). 105 | Released under the [MIT License](LICENSE). 106 | 107 | *** 108 | 109 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 09, 2018._ 110 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: "9" 4 | - nodejs_version: "8" 5 | 6 | version: "{build}" 7 | build: off 8 | deploy: off 9 | 10 | install: 11 | - ps: Install-Product node $env:nodejs_version 12 | - npm install 13 | 14 | test_script: 15 | - node --version 16 | - npm --version 17 | - npm test 18 | -------------------------------------------------------------------------------- /docs/README.tmpl.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} 2 | 3 | > {%= description %} 4 | 5 | ## Install 6 | {%= include("install", {save: '--save'}) %} 7 | 8 | ## Usage 9 | 10 | ```js 11 | var branch = require('git-branch'); 12 | console.log(branch); 13 | ``` 14 | 15 | ## Author 16 | {%= contrib("jon") %} 17 | 18 | ## License 19 | {%= copyright() %} 20 | {%= license() %} 21 | 22 | *** 23 | 24 | {%= include("footer") %} -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const util = require('util'); 5 | const find = require('findup-sync'); 6 | const readFile = util.promisify(fs.readFile); 7 | 8 | function branch(cwd, callback) { 9 | if (typeof cwd === 'function') { 10 | callback = cwd; 11 | cwd = null; 12 | } 13 | 14 | const promise = readFile(gitHeadPath(cwd)).then(buf => parseBranch(buf)); 15 | 16 | if (typeof callback === 'function') { 17 | promise.then(res => callback(null, res)).catch(callback); 18 | return; 19 | } 20 | 21 | return promise; 22 | } 23 | 24 | branch.sync = function(cwd) { 25 | return parseBranch(fs.readFileSync(gitHeadPath(cwd))); 26 | }; 27 | 28 | function parseBranch(buf) { 29 | const match = /ref: refs\/heads\/([^\n]+)/.exec(buf.toString()); 30 | return match ? match[1] : null; 31 | } 32 | 33 | function gitHeadPath(cwd) { 34 | const filepath = find('.git/HEAD', { cwd: cwd || process.cwd() }); 35 | if (!fs.existsSync(filepath)) { 36 | throw new Error('.git/HEAD does not exist'); 37 | } 38 | return filepath; 39 | } 40 | 41 | /** 42 | * Expose `branch` 43 | */ 44 | 45 | module.exports = branch; 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "git-branch", 3 | "description": "Get the current branch from the local git repository.", 4 | "version": "2.0.1", 5 | "homepage": "https://github.com/jonschlinkert/git-branch", 6 | "author": "Jon Schlinkert (http://github.com/https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/git-branch", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/git-branch/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=8" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "findup-sync": "^2.0.0" 24 | }, 25 | "devDependencies": { 26 | "gfc": "^2.0.1", 27 | "gulp-format-md": "^1.0.0", 28 | "mocha": "^3.5.3", 29 | "rimraf": "^2.6.2" 30 | }, 31 | "keywords": [ 32 | "branch", 33 | "git" 34 | ], 35 | "verb": { 36 | "toc": false, 37 | "layout": "default", 38 | "tasks": [ 39 | "readme" 40 | ], 41 | "plugins": [ 42 | "gulp-format-md" 43 | ], 44 | "related": { 45 | "list": [ 46 | "git-add-remote", 47 | "git-user-name", 48 | "github-base" 49 | ] 50 | }, 51 | "lint": { 52 | "reflinks": true 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const gfc = require('gfc'); 4 | const path = require('path'); 5 | const util = require('util'); 6 | const assert = require('assert'); 7 | const rimraf = util.promisify(require('rimraf')); 8 | const branch = require('..'); 9 | const fixtures = path.join(__dirname, 'fixtures'); 10 | 11 | const create = async() => await rimraf(fixtures).then(() => gfc(fixtures)); 12 | 13 | describe('git-branch', function() { 14 | beforeEach(() => create()); 15 | afterEach(() => rimraf(fixtures)); 16 | 17 | it('should get branch (sync)', () => assert.equal(branch.sync(fixtures), 'master')); 18 | it('should get branch (promise)', function() { 19 | return branch(fixtures).then(res => assert.equal(res, 'master')); 20 | }); 21 | it('should get branch (async)', function(cb) { 22 | branch(fixtures, function(err, res) { 23 | if (err) return cb(err); 24 | assert.equal(res, 'master'); 25 | cb(); 26 | }); 27 | }); 28 | }); 29 | --------------------------------------------------------------------------------