fix(hook): install from subdirectory

fix #199
This commit is contained in:
Hiroki Osame
2023-05-03 23:24:34 +09:00
parent dda7f8d424
commit 7631c2fc0b
4 changed files with 36 additions and 14 deletions

View File

@@ -29,16 +29,16 @@ export default command({
parameters: ['<install/uninstall>'], parameters: ['<install/uninstall>'],
}, (argv) => { }, (argv) => {
(async () => { (async () => {
await assertGitRepo(); const gitRepoPath = await assertGitRepo();
const { installUninstall: mode } = argv._; const { installUninstall: mode } = argv._;
const hookExists = await fileExists(symlinkPath); const absoltueSymlinkPath = path.join(gitRepoPath, symlinkPath);
const hookExists = await fileExists(absoltueSymlinkPath);
if (mode === 'install') { if (mode === 'install') {
if (hookExists) { if (hookExists) {
// If the symlink is broken, it will throw an error // If the symlink is broken, it will throw an error
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
const realpath = await fs.realpath(symlinkPath).catch(() => {}); const realpath = await fs.realpath(absoltueSymlinkPath).catch(() => {});
if (realpath === hookPath) { if (realpath === hookPath) {
console.warn('The hook is already installed'); console.warn('The hook is already installed');
return; return;
@@ -46,16 +46,16 @@ export default command({
throw new KnownError(`A different ${hookName} hook seems to be installed. Please remove it before installing aicommits.`); throw new KnownError(`A different ${hookName} hook seems to be installed. Please remove it before installing aicommits.`);
} }
await fs.mkdir(path.dirname(symlinkPath), { recursive: true }); await fs.mkdir(path.dirname(absoltueSymlinkPath), { recursive: true });
if (isWindows) { if (isWindows) {
await fs.writeFile( await fs.writeFile(
symlinkPath, absoltueSymlinkPath,
windowsHook, windowsHook,
); );
} else { } else {
await fs.symlink(hookPath, symlinkPath, 'file'); await fs.symlink(hookPath, absoltueSymlinkPath, 'file');
await fs.chmod(symlinkPath, 0o755); await fs.chmod(absoltueSymlinkPath, 0o755);
} }
console.log(`${green('✔')} Hook installed`); console.log(`${green('✔')} Hook installed`);
return; return;
@@ -68,20 +68,20 @@ export default command({
} }
if (isWindows) { if (isWindows) {
const scriptContent = await fs.readFile(symlinkPath, 'utf8'); const scriptContent = await fs.readFile(absoltueSymlinkPath, 'utf8');
if (scriptContent !== windowsHook) { if (scriptContent !== windowsHook) {
console.warn('Hook is not installed'); console.warn('Hook is not installed');
return; return;
} }
} else { } else {
const realpath = await fs.realpath(symlinkPath); const realpath = await fs.realpath(absoltueSymlinkPath);
if (realpath !== hookPath) { if (realpath !== hookPath) {
console.warn('Hook is not installed'); console.warn('Hook is not installed');
return; return;
} }
} }
await fs.rm(symlinkPath); await fs.rm(absoltueSymlinkPath);
console.log(`${green('✔')} Hook uninstalled`); console.log(`${green('✔')} Hook uninstalled`);
return; return;
} }

View File

@@ -2,11 +2,13 @@ import { execa } from 'execa';
import { KnownError } from './error.js'; import { KnownError } from './error.js';
export const assertGitRepo = async () => { export const assertGitRepo = async () => {
const { stdout } = await execa('git', ['rev-parse', '--is-inside-work-tree'], { reject: false }); const { stdout, failed } = await execa('git', ['rev-parse', '--show-toplevel'], { reject: false });
if (stdout !== 'true') { if (failed) {
throw new KnownError('The current directory must be a Git repository!'); throw new KnownError('The current directory must be a Git repository!');
} }
return stdout;
}; };
const excludeFromDiff = (path: string) => `:(exclude)${path}`; const excludeFromDiff = (path: string) => `:(exclude)${path}`;

View File

@@ -1,3 +1,4 @@
import path from 'path';
import { testSuite, expect } from 'manten'; import { testSuite, expect } from 'manten';
import { import {
assertOpenAiToken, assertOpenAiToken,
@@ -22,6 +23,25 @@ export default testSuite(({ describe }) => {
await fixture.rm(); await fixture.rm();
}); });
test('installs from Git repo subdirectory', async () => {
const { fixture, aicommits } = await createFixture({
...files,
'some-dir': {
'file.txt': '',
},
});
await createGit(fixture.path);
const { stdout } = await aicommits(['hook', 'install'], {
cwd: path.join(fixture.path, 'some-dir'),
});
expect(stdout).toMatch('Hook installed');
expect(await fixture.exists('.git/hooks/prepare-commit-msg')).toBe(true);
await fixture.rm();
});
test('Commits', async () => { test('Commits', async () => {
const { fixture, aicommits } = await createFixture(files); const { fixture, aicommits } = await createFixture(files);
const git = await createGit(fixture.path); const git = await createGit(fixture.path);

View File

@@ -19,8 +19,8 @@ const createAicommits = (fixture: FsFixture) => {
args?: string[], args?: string[],
options?: Options, options?: Options,
) => execaNode(aicommitsPath, args, { ) => execaNode(aicommitsPath, args, {
...options,
cwd: fixture.path, cwd: fixture.path,
...options,
extendEnv: false, extendEnv: false,
env: { env: {
...homeEnv, ...homeEnv,