From a3fade4d42afecf5bc41768a70eac4485ba83099 Mon Sep 17 00:00:00 2001 From: Moret84 Date: Fri, 24 Mar 2023 03:11:50 +0100 Subject: [PATCH] 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. --- src/utils/git.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/utils/git.ts b/src/utils/git.ts index bba4948..9214031 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -26,11 +26,17 @@ export const getOpenCommitIgnore = (): Ignore => { }; export const getStagedFiles = async (): Promise => { - 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 [];