Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

fix(lib): getting .git from parent dir #74

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@
"contributions": [
"code"
]
},
{
"login": "standy",
"name": "standy",
"avatar_url": "https://avatars3.githubusercontent.com/u/750319?v=3",
"profile": "https://github.com/standy",
"contributions": [
"bug",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use code instead of bug

"test"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-27-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-28-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -192,7 +192,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/2112202?v=3" width="100px;"/><br /><sub>Shawn Erquhart</sub>](http://www.professant.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) | [<img src="https://avatars.githubusercontent.com/u/194482?v=3" width="100px;"/><br /><sub>Tushar Mathur</sub>](http://tusharm.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) | [<img src="https://avatars.githubusercontent.com/u/904007?v=3" width="100px;"/><br /><sub>Jason Dreyzehner</sub>](https://twitter.com/bitjson)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) | [<img src="https://avatars.githubusercontent.com/u/9654923?v=3" width="100px;"/><br /><sub>Abimbola Idowu</sub>](http://twitter.com/hisabimbola)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=hisabimbola) | [<img src="https://avatars.githubusercontent.com/u/2212006?v=3" width="100px;"/><br /><sub>Gleb Bahmutov</sub>](https://glebbahmutov.com/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) | [<img src="https://avatars.githubusercontent.com/u/332905?v=3" width="100px;"/><br /><sub>Dennis</sub>](http://dennis.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=ds82) | [<img src="https://avatars.githubusercontent.com/u/6425649?v=3" width="100px;"/><br /><sub>Matt Lewis</sub>](https://mattlewis.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=mattlewis92) |
| [<img src="https://avatars.githubusercontent.com/u/323761?v=3" width="100px;"/><br /><sub>Tom Vincent</sub>](https://tlvince.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tlvince) | [<img src="https://avatars.githubusercontent.com/u/615381?v=3" width="100px;"/><br /><sub>Anders D. Johnson</sub>](https://andrz.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) | [<img src="https://avatars.githubusercontent.com/u/1643758?v=3" width="100px;"/><br /><sub>James Zetlen</sub>](http://jameszetlen.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) | [<img src="https://avatars.githubusercontent.com/u/235784?v=3" width="100px;"/><br /><sub>Paul Bienkowski</sub>](http://opatut.de)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) | [<img src="https://avatars.githubusercontent.com/u/324073?v=3" width="100px;"/><br /><sub>Barney Scott</sub>](https://github.com/bmds)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) | [<img src="https://avatars.githubusercontent.com/u/5572221?v=3" width="100px;"/><br /><sub>Emmanuel Murillo Sánchez</sub>](https://github.com/Emmurillo)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) | [<img src="https://avatars.githubusercontent.com/u/968267?v=3" width="100px;"/><br /><sub>Hans Kristian Flaatten</sub>](https://starefossen.github.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [<img src="https://avatars.githubusercontent.com/u/1057324?v=3" width="100px;"/><br /><sub>Spyros Ioakeimidis</sub>](http://www.spyros.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [<img src="https://avatars.githubusercontent.com/u/126441?v=3" width="100px;"/><br /><sub>Matt Travi</sub>](https://matt.travi.org)<br />[🐛](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [<img src="https://avatars.githubusercontent.com/u/868301?v=3" width="100px;"/><br /><sub>Jonathan Garbee</sub>](http://jonathan.garbee.me)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) | [<img src="https://avatars.githubusercontent.com/u/2978876?v=3" width="100px;"/><br /><sub>Tobias Lins</sub>](https://lins.in)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=tobiaslins) | [<img src="https://avatars1.githubusercontent.com/u/680356?v=3" width="100px;"/><br /><sub>Max Claus Nunes</sub>](http://blog.maxcnunes.net/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=maxcnunes) |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [<img src="https://avatars.githubusercontent.com/u/1057324?v=3" width="100px;"/><br /><sub>Spyros Ioakeimidis</sub>](http://www.spyros.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [<img src="https://avatars.githubusercontent.com/u/126441?v=3" width="100px;"/><br /><sub>Matt Travi</sub>](https://matt.travi.org)<br />[🐛](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [<img src="https://avatars.githubusercontent.com/u/868301?v=3" width="100px;"/><br /><sub>Jonathan Garbee</sub>](http://jonathan.garbee.me)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) | [<img src="https://avatars.githubusercontent.com/u/2978876?v=3" width="100px;"/><br /><sub>Tobias Lins</sub>](https://lins.in)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=tobiaslins) | [<img src="https://avatars1.githubusercontent.com/u/680356?v=3" width="100px;"/><br /><sub>Max Claus Nunes</sub>](http://blog.maxcnunes.net/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=maxcnunes) | [<img src="https://avatars3.githubusercontent.com/u/750319?v=3" width="100px;"/><br /><sub>standy</sub>](https://github.com/standy)<br />[🐛](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Astandy) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=standy) |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
30 changes: 19 additions & 11 deletions lib/getGitFolder.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
'use strict';

var fs = require('fs');
var path = require('path');
var findParentDir = require('find-parent-dir');

module.exports = function getGitFolder() {
var gitDirLocation = './.git';
if (!fs.existsSync(gitDirLocation)) {
throw new Error('Cannot find file ' + gitDirLocation);
}
var dir = findParentDir.sync(process.cwd(), '.git');
if (!dir) throw new Error('Cannot find .git folder');

if (!fs.lstatSync(gitDirLocation).isDirectory()) {
var unparsedText = '' + fs.readFileSync(gitDirLocation);
gitDirLocation = unparsedText.substring('gitdir: '.length).trim();
}
var gitDir = path.join(dir, '.git');
var stats = fs.lstatSync(gitDir);

if (!stats.isDirectory()) {
// Expect following format
// git: pathToGit
var pathToGit = fs
.readFileSync(gitDir, 'utf-8')
.split(':')[1]
.trim();
gitDir = path.join(dir, pathToGit);

if (!fs.existsSync(gitDirLocation)) {
throw new Error('Cannot find file ' + gitDirLocation);
if (!fs.existsSync(gitDir)) {
throw new Error('Cannot find file ' + pathToGit);
}
}

return gitDirLocation;
return gitDir;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
},
"dependencies": {
"conventional-commit-types": "^2.0.0",
"find-parent-dir": "^0.3.0",
"findup": "0.1.5",
"semver-regex": "1.0.0"
},
Expand Down
26 changes: 16 additions & 10 deletions test/getGitFolder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var sinon = require('sinon');
var expect = require('chai').expect;
var fs = require('fs');
var resolve = require('path').resolve;
var findParentDir = require('find-parent-dir');
var getGitFolder = require('../lib/getGitFolder');

describe('handle .git as folder', function() {
Expand All @@ -20,7 +22,7 @@ describe('handle .git as folder', function() {
});

it('should return ./.git when .git is a directory', function() {
expect(getGitFolder()).to.be.equal('./.git');
expect(getGitFolder()).to.be.equal(resolve('.git'));
});

after(function() {
Expand All @@ -38,7 +40,7 @@ describe('handle .git as file', function() {
//Ensure that we say ./.git is a file, but ../../actual-folder is folder
return {
isDirectory: function() {
return location != './.git';
return location != resolve('.git');
}
}
});
Expand All @@ -48,7 +50,7 @@ describe('handle .git as file', function() {
});

it('should load gitdir from .git file', function() {
expect(getGitFolder()).to.be.equal('../../actual-folder');
expect(getGitFolder()).to.be.equal(resolve('../../actual-folder'));
});

after(function() {
Expand All @@ -60,43 +62,47 @@ describe('handle .git as file', function() {

describe('handle .git does not exist', function() {
before(function() {
sinon.stub(fs, "existsSync", function() {
return false;
sinon.stub(findParentDir, 'sync', function() {
return null;
})
});

it('should throw error when ./.git is missing', function() {
expect(getGitFolder).to.throw('Cannot find file ./.git');
expect(getGitFolder).to.throw('Cannot find .git folder');
});

after(function() {
fs.existsSync.restore();
findParentDir.sync.restore();
});
});

describe('handle .git gitdir: folder does not exist', function() {
before(function() {
sinon.stub(findParentDir, 'sync', function() {
return resolve('.');
});
sinon.stub(fs, "existsSync", function(dir) {
return './.git' == dir;
return dir == resolve('.git');
});
sinon.stub(fs, "lstatSync", function(location) {
//Ensure that we say ./.git is a file, but ../../actual-folder is folder
return {
isDirectory: function() {
return location != './.git';
return location != resolve('.git');
}
}
});
sinon.stub(fs, 'readFileSync', function() {
return 'gitdir: ../../actual-folder';
})
});
});

it('should throw error when ./.git is missing', function() {
expect(getGitFolder).to.throw('Cannot find file ../../actual-folder');
});

after(function() {
findParentDir.sync.restore();
fs.existsSync.restore();
fs.lstatSync.restore();
fs.readFileSync.restore();
Expand Down