refactor(git.ts): use git rev-parse to get the root directory of the repository (#46)

The function getStagedFiles has been refactored to use git rev-parse to
get the root directory of the repository. This improves the reliability
of the function as it will work regardless of the current working
directory. The root directory is then passed to the git diff command to
get the list of staged files. With only the --relative flag, staged
diff(s) on files in a different same level directory as the current
working one would not be found by the command.
This commit is contained in:
Moret84
2023-03-24 03:11:50 +01:00
committed by GitHub
parent 83906fc704
commit a3fade4d42

View File

@@ -26,11 +26,17 @@ export const getOpenCommitIgnore = (): Ignore => {
};
export const getStagedFiles = async (): Promise<string[]> => {
const { stdout: files } = await execa('git', [
'diff',
'--name-only',
'--cached',
'--relative'
const { stdout: gitDir } = await execa("git", [
"rev-parse",
"--show-toplevel"
]);
const { stdout: files } = await execa("git", [
"diff",
"--name-only",
"--cached",
"--relative",
gitDir
]);
if (!files) return [];