From c9db3283ba38a6e55f3fd9c4af9f27c7c54daaed Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Fri, 19 Feb 2021 11:23:15 +1100 Subject: [PATCH 1/2] feat: git worktree support fixes #10 --- index.js | 8 +++++--- package.json | 2 +- test/test.js | 19 ++++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 576e334..356d4b8 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')); }); From 6868a80d6c584e8dd3c4505c839f90d810114725 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Fri, 19 Feb 2021 11:26:41 +1100 Subject: [PATCH 2/2] fix: add missing semi --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 356d4b8..59d5be3 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ function parseBranch(buf) { } function gitHeadPath(cwd) { - cwd = cwd || process.cwd() + cwd = cwd || process.cwd(); const filepath = path.join(find(cwd), 'HEAD'); if (!fs.existsSync(filepath)) { throw new Error(`${path.relative(cwd, filepath)} does not exist`);