Skip to content

Commit

Permalink
fix(hook): install from subdirectory
Browse files Browse the repository at this point in the history
fix #199
  • Loading branch information
privatenumber committed May 3, 2023
1 parent dda7f8d commit 7631c2f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/commands/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ export default command({
parameters: ['<install/uninstall>'],
}, (argv) => {
(async () => {
await assertGitRepo();

const gitRepoPath = await assertGitRepo();
const { installUninstall: mode } = argv._;

const hookExists = await fileExists(symlinkPath);
const absoltueSymlinkPath = path.join(gitRepoPath, symlinkPath);
const hookExists = await fileExists(absoltueSymlinkPath);
if (mode === 'install') {
if (hookExists) {
// If the symlink is broken, it will throw an error
// 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) {
console.warn('The hook is already installed');

Check warning on line 43 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 43 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement

Check warning on line 43 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 43 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement
return;
}
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) {
await fs.writeFile(
symlinkPath,
absoltueSymlinkPath,
windowsHook,
);
} else {
await fs.symlink(hookPath, symlinkPath, 'file');
await fs.chmod(symlinkPath, 0o755);
await fs.symlink(hookPath, absoltueSymlinkPath, 'file');
await fs.chmod(absoltueSymlinkPath, 0o755);
}
console.log(`${green('✔')} Hook installed`);

Check warning on line 60 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 60 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement

Check warning on line 60 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 60 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement
return;
Expand All @@ -68,20 +68,20 @@ export default command({
}

if (isWindows) {
const scriptContent = await fs.readFile(symlinkPath, 'utf8');
const scriptContent = await fs.readFile(absoltueSymlinkPath, 'utf8');
if (scriptContent !== windowsHook) {
console.warn('Hook is not installed');

Check warning on line 73 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 73 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement

Check warning on line 73 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 73 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement
return;
}
} else {
const realpath = await fs.realpath(symlinkPath);
const realpath = await fs.realpath(absoltueSymlinkPath);
if (realpath !== hookPath) {
console.warn('Hook is not installed');

Check warning on line 79 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 79 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement

Check warning on line 79 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 79 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement
return;
}
}

await fs.rm(symlinkPath);
await fs.rm(absoltueSymlinkPath);
console.log(`${green('✔')} Hook uninstalled`);

Check warning on line 85 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 85 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement

Check warning on line 85 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement

Check warning on line 85 in src/commands/hook.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Unexpected console statement
return;
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { execa } from 'execa';
import { KnownError } from './error.js';

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!');
}

return stdout;
};

const excludeFromDiff = (path: string) => `:(exclude)${path}`;
Expand Down
20 changes: 20 additions & 0 deletions tests/specs/git-hook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import { testSuite, expect } from 'manten';
import {
assertOpenAiToken,
Expand All @@ -22,6 +23,25 @@ export default testSuite(({ describe }) => {
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 () => {
const { fixture, aicommits } = await createFixture(files);
const git = await createGit(fixture.path);
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const createAicommits = (fixture: FsFixture) => {
args?: string[],
options?: Options,
) => execaNode(aicommitsPath, args, {
...options,
cwd: fixture.path,
...options,
extendEnv: false,
env: {
...homeEnv,
Expand Down

0 comments on commit 7631c2f

Please sign in to comment.