Skip to content

feat: git worktree support #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "mocha"
},
"dependencies": {
"findup-sync": "^2.0.0"
"find-git-root": "^1.0.4"
},
"devDependencies": {
"gfc": "^2.0.1",
Expand Down
19 changes: 16 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -25,4 +37,5 @@ describe('git-branch', function() {
cb();
});
});
it('should work with a git worktree', () => assert.strictEqual(branch.sync(worktreeFixtures), 'some-branch'));
});