lint-staged 15.2.5 → 15.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/resolveGitRepo.js +7 -50
- package/package.json +1 -1
package/lib/resolveGitRepo.js
CHANGED
|
@@ -1,54 +1,10 @@
|
|
|
1
|
-
import fs from 'node:fs/promises'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
1
|
import debug from 'debug'
|
|
5
2
|
|
|
6
3
|
import { execGit } from './execGit.js'
|
|
7
|
-
import { readFile } from './file.js'
|
|
8
4
|
import { normalizePath } from './normalizePath.js'
|
|
9
5
|
|
|
10
6
|
const debugLog = debug('lint-staged:resolveGitRepo')
|
|
11
7
|
|
|
12
|
-
/**
|
|
13
|
-
* Resolve path to the .git directory, with special handling for
|
|
14
|
-
* submodules and worktrees
|
|
15
|
-
*/
|
|
16
|
-
const resolveGitConfigDir = async (gitDir) => {
|
|
17
|
-
// Get the real path in case it's a symlink
|
|
18
|
-
const defaultDir = path.join(gitDir, '.git')
|
|
19
|
-
const stats = await fs.lstat(defaultDir)
|
|
20
|
-
|
|
21
|
-
// If .git is a directory, use it
|
|
22
|
-
if (stats.isDirectory()) {
|
|
23
|
-
return defaultDir
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// If .git is a symlink, return the real location
|
|
27
|
-
if (stats.isSymbolicLink()) {
|
|
28
|
-
return await fs.realpath(gitDir)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Otherwise .git is a file containing path to real location
|
|
32
|
-
const file = (await readFile(defaultDir)).toString()
|
|
33
|
-
return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// exported for test
|
|
37
|
-
export const determineGitDir = (cwd, relativeDir) => {
|
|
38
|
-
// if relative dir and cwd have different endings normalize it
|
|
39
|
-
// this happens under windows, where normalize is unable to normalize git's output
|
|
40
|
-
if (relativeDir && relativeDir.endsWith(path.sep)) {
|
|
41
|
-
relativeDir = relativeDir.slice(0, -1)
|
|
42
|
-
}
|
|
43
|
-
if (relativeDir) {
|
|
44
|
-
// the current working dir is inside the git top-level directory
|
|
45
|
-
return cwd.substring(0, cwd.lastIndexOf(relativeDir))
|
|
46
|
-
} else {
|
|
47
|
-
// the current working dir is the top-level git directory
|
|
48
|
-
return cwd
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
8
|
/**
|
|
53
9
|
* Resolve git directory and possible submodule paths
|
|
54
10
|
*/
|
|
@@ -62,13 +18,14 @@ export const resolveGitRepo = async (cwd = process.cwd()) => {
|
|
|
62
18
|
debugLog('Unset GIT_WORK_TREE (was `%s`)', process.env.GIT_WORK_TREE)
|
|
63
19
|
delete process.env.GIT_WORK_TREE
|
|
64
20
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const gitDir = normalizePath(determineGitDir(normalizePath(cwd), gitRel))
|
|
69
|
-
const gitConfigDir = normalizePath(await resolveGitConfigDir(gitDir))
|
|
70
|
-
|
|
21
|
+
const gitDir = normalizePath(
|
|
22
|
+
await execGit(['rev-parse', '--path-format=absolute', '--show-toplevel'], { cwd })
|
|
23
|
+
)
|
|
71
24
|
debugLog('Resolved git directory to be `%s`', gitDir)
|
|
25
|
+
|
|
26
|
+
const gitConfigDir = normalizePath(
|
|
27
|
+
await execGit(['rev-parse', '--path-format=absolute', '--absolute-git-dir'], { cwd })
|
|
28
|
+
)
|
|
72
29
|
debugLog('Resolved git config directory to be `%s`', gitConfigDir)
|
|
73
30
|
|
|
74
31
|
return { gitDir, gitConfigDir }
|