From 119adb29854cabddbfcf0469d7c8a0126184a5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Mon, 10 Jun 2024 18:30:04 +0300 Subject: [PATCH] fix: use native git command to get .git directory --- .changeset/stale-ants-hope.md | 5 +++++ lib/resolveGitRepo.js | 34 ++++------------------------------ 2 files changed, 9 insertions(+), 30 deletions(-) create mode 100644 .changeset/stale-ants-hope.md diff --git a/.changeset/stale-ants-hope.md b/.changeset/stale-ants-hope.md new file mode 100644 index 00000000..87ec07a0 --- /dev/null +++ b/.changeset/stale-ants-hope.md @@ -0,0 +1,5 @@ +--- +'lint-staged': patch +--- + +Use native "git rev-parse" commands to determine git repo root directory and the .git config directory, instead of using custom logic. This hopefully makes path resolution more robust on non-POSIX systems. diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index 8f043b65..2a4879ea 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -1,38 +1,10 @@ -import fs from 'node:fs/promises' -import path from 'node:path' - import debug from 'debug' import { execGit } from './execGit.js' -import { readFile } from './file.js' import { normalizePath } from './normalizePath.js' const debugLog = debug('lint-staged:resolveGitRepo') -/** - * Resolve path to the .git directory, with special handling for - * submodules and worktrees - */ -const resolveGitConfigDir = async (gitDir) => { - // Get the real path in case it's a symlink - const defaultDir = path.join(gitDir, '.git') - const stats = await fs.lstat(defaultDir) - - // If .git is a directory, use it - if (stats.isDirectory()) { - return defaultDir - } - - // If .git is a symlink, return the real location - if (stats.isSymbolicLink()) { - return await fs.realpath(gitDir) - } - - // Otherwise .git is a file containing path to real location - const file = (await readFile(defaultDir)).toString() - return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim() -} - /** * Resolve git directory and possible submodule paths */ @@ -49,9 +21,11 @@ export const resolveGitRepo = async (cwd = process.cwd()) => { const gitDir = normalizePath( await execGit(['rev-parse', '--path-format=absolute', '--show-toplevel'], { cwd }) ) - const gitConfigDir = normalizePath(await resolveGitConfigDir(gitDir)) - debugLog('Resolved git directory to be `%s`', gitDir) + + const gitConfigDir = normalizePath( + await execGit(['rev-parse', '--path-format=absolute', '--absolute-git-dir'], { cwd }) + ) debugLog('Resolved git config directory to be `%s`', gitConfigDir) return { gitDir, gitConfigDir }