Skip to content

Commit dac1271

Browse files
authored
Merge pull request #496 from kykungz/resolve-top-level-git-dir
Fix git commands when executed from subdirectories
2 parents 4deb7bc + 7e60c68 commit dac1271

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

src/utils/git.ts

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execa } from 'execa';
22
import { readFileSync } from 'fs';
33
import ignore, { Ignore } from 'ignore';
4-
4+
import { join } from 'path';
55
import { outro, spinner } from '@clack/prompts';
66

77
export const assertGitRepo = async () => {
@@ -16,41 +16,44 @@ export const assertGitRepo = async () => {
1616
// (file) => `:(exclude)${file}`
1717
// );
1818

19-
export const getOpenCommitIgnore = (): Ignore => {
19+
export const getOpenCommitIgnore = async (): Promise<Ignore> => {
20+
const gitDir = await getGitDir();
21+
2022
const ig = ignore();
2123

2224
try {
23-
ig.add(readFileSync('.opencommitignore').toString().split('\n'));
25+
ig.add(
26+
readFileSync(join(gitDir, '.opencommitignore')).toString().split('\n')
27+
);
2428
} catch (e) {}
2529

2630
return ig;
2731
};
2832

2933
export const getCoreHooksPath = async (): Promise<string> => {
30-
const { stdout } = await execa('git', ['config', 'core.hooksPath']);
34+
const gitDir = await getGitDir();
35+
36+
const { stdout } = await execa('git', ['config', 'core.hooksPath'], {
37+
cwd: gitDir
38+
});
3139

3240
return stdout;
3341
};
3442

3543
export const getStagedFiles = async (): Promise<string[]> => {
36-
const { stdout: gitDir } = await execa('git', [
37-
'rev-parse',
38-
'--show-toplevel'
39-
]);
44+
const gitDir = await getGitDir();
4045

41-
const { stdout: files } = await execa('git', [
42-
'diff',
43-
'--name-only',
44-
'--cached',
45-
'--relative',
46-
gitDir
47-
]);
46+
const { stdout: files } = await execa(
47+
'git',
48+
['diff', '--name-only', '--cached', '--relative'],
49+
{ cwd: gitDir }
50+
);
4851

4952
if (!files) return [];
5053

5154
const filesList = files.split('\n');
5255

53-
const ig = getOpenCommitIgnore();
56+
const ig = await getOpenCommitIgnore();
5457
const allowedFiles = filesList.filter((file) => !ig.ignores(file));
5558

5659
if (!allowedFiles) return [];
@@ -59,12 +62,17 @@ export const getStagedFiles = async (): Promise<string[]> => {
5962
};
6063

6164
export const getChangedFiles = async (): Promise<string[]> => {
62-
const { stdout: modified } = await execa('git', ['ls-files', '--modified']);
63-
const { stdout: others } = await execa('git', [
64-
'ls-files',
65-
'--others',
66-
'--exclude-standard'
67-
]);
65+
const gitDir = await getGitDir();
66+
67+
const { stdout: modified } = await execa('git', ['ls-files', '--modified'], {
68+
cwd: gitDir
69+
});
70+
71+
const { stdout: others } = await execa(
72+
'git',
73+
['ls-files', '--others', '--exclude-standard'],
74+
{ cwd: gitDir }
75+
);
6876

6977
const files = [...modified.split('\n'), ...others.split('\n')].filter(
7078
(file) => !!file
@@ -74,16 +82,20 @@ export const getChangedFiles = async (): Promise<string[]> => {
7482
};
7583

7684
export const gitAdd = async ({ files }: { files: string[] }) => {
85+
const gitDir = await getGitDir();
86+
7787
const gitAddSpinner = spinner();
7888

7989
gitAddSpinner.start('Adding files to commit');
8090

81-
await execa('git', ['add', ...files]);
91+
await execa('git', ['add', ...files], { cwd: gitDir });
8292

8393
gitAddSpinner.stop(`Staged ${files.length} files`);
8494
};
8595

8696
export const getDiff = async ({ files }: { files: string[] }) => {
97+
const gitDir = await getGitDir();
98+
8799
const lockFiles = files.filter(
88100
(file) =>
89101
file.includes('.lock') ||
@@ -108,12 +120,20 @@ export const getDiff = async ({ files }: { files: string[] }) => {
108120
(file) => !file.includes('.lock') && !file.includes('-lock.')
109121
);
110122

111-
const { stdout: diff } = await execa('git', [
112-
'diff',
113-
'--staged',
114-
'--',
115-
...filesWithoutLocks
116-
]);
123+
const { stdout: diff } = await execa(
124+
'git',
125+
['diff', '--staged', '--', ...filesWithoutLocks],
126+
{ cwd: gitDir }
127+
);
117128

118129
return diff;
119130
};
131+
132+
export const getGitDir = async (): Promise<string> => {
133+
const { stdout: gitDir } = await execa('git', [
134+
'rev-parse',
135+
'--show-toplevel'
136+
]);
137+
138+
return gitDir;
139+
};

0 commit comments

Comments
 (0)