diff --git a/index.js b/index.js index 576e334..59d5be3 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,8 @@ const fs = require('fs'); const util = require('util'); -const find = require('findup-sync'); +const find = require('find-git-root'); +const path = require('path'); const readFile = util.promisify(fs.readFile); function branch(cwd, callback) { @@ -31,9 +32,10 @@ function parseBranch(buf) { } function gitHeadPath(cwd) { - const filepath = find('.git/HEAD', { cwd: cwd || process.cwd() }); + cwd = cwd || process.cwd(); + const filepath = path.join(find(cwd), 'HEAD'); if (!fs.existsSync(filepath)) { - throw new Error('.git/HEAD does not exist'); + throw new Error(`${path.relative(cwd, filepath)} does not exist`); } return filepath; } diff --git a/package.json b/package.json index 1a3724d..fd72724 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test": "mocha" }, "dependencies": { - "findup-sync": "^2.0.0" + "find-git-root": "^1.0.4" }, "devDependencies": { "gfc": "^2.0.1", diff --git a/test/test.js b/test/test.js index ff4518a..bf04550 100644 --- a/test/test.js +++ b/test/test.js @@ -1,18 +1,30 @@ 'use strict'; +const cp = require('child_process'); const gfc = require('gfc'); const path = require('path'); const util = require('util'); const assert = require('assert'); const rimraf = util.promisify(require('rimraf')); const branch = require('..'); -const fixtures = path.join(__dirname, 'fixtures'); +const fixturesBase = path.join(__dirname, 'fixtures'); +const fixtures = path.join(fixturesBase, 'git'); +const worktreeFixtures = path.join(fixturesBase, 'worktree'); + +const exec = util.promisify(cp.exec); +const createWorktree = async(gitDir) => exec(['git', 'worktree', 'add', '-b', 'some-branch', worktreeFixtures].join(' '), { cwd: gitDir }); + +const create = async() => { + await rimraf(fixturesBase); + await gfc(fixtures); + await createWorktree(fixtures); +}; + -const create = async() => await rimraf(fixtures).then(() => gfc(fixtures)); describe('git-branch', function() { beforeEach(() => create()); - afterEach(() => rimraf(fixtures)); + afterEach(() => rimraf(fixturesBase)); it('should get branch (sync)', () => assert.equal(branch.sync(fixtures), 'master')); it('should get branch (promise)', function() { @@ -25,4 +37,5 @@ describe('git-branch', function() { cb(); }); }); + it('should work with a git worktree', () => assert.strictEqual(branch.sync(worktreeFixtures), 'some-branch')); });