Skip to content

Commit

Permalink
refactor: move normalizePath calls to top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 2, 2023
1 parent 172c613 commit 5b3cf0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/resolveGitRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const debugLog = debug('lint-staged:resolveGitRepo')
*/
const resolveGitConfigDir = async (gitDir) => {
// Get the real path in case it's a symlink
const defaultDir = normalizePath(await fs.realpath(path.join(gitDir, '.git')))
const defaultDir = await fs.realpath(path.join(gitDir, '.git'))
const stats = await fs.lstat(defaultDir)
// If .git is a directory, use it
if (stats.isDirectory()) return defaultDir
Expand All @@ -33,10 +33,10 @@ export const determineGitDir = (cwd, relativeDir) => {
}
if (relativeDir) {
// the current working dir is inside the git top-level directory
return normalizePath(cwd.substring(0, cwd.lastIndexOf(relativeDir)))
return cwd.substring(0, cwd.lastIndexOf(relativeDir))
} else {
// the current working dir is the top-level git directory
return normalizePath(cwd)
return cwd
}
}

Expand All @@ -56,7 +56,7 @@ export const resolveGitRepo = async (cwd = process.cwd()) => {
// read the path of the current directory relative to the top-level directory
// don't read the toplevel directly, it will lead to an posix conform path on non posix systems (cygwin)
const gitRel = normalizePath(await execGit(['rev-parse', '--show-prefix'], { cwd }))
const gitDir = determineGitDir(normalizePath(cwd), gitRel)
const gitDir = normalizePath(determineGitDir(normalizePath(cwd), gitRel))
const gitConfigDir = normalizePath(await resolveGitConfigDir(gitDir))

debugLog('Resolved git directory to be `%s`', gitDir)
Expand Down
6 changes: 3 additions & 3 deletions test/unit/resolveGitRepo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ describe('resolveGitRepo', () => {
const cwd = process.cwd()
const relativeDir = undefined
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalizePath(cwd))
expect(normalizePath(rootDir)).toEqual(normalizePath(cwd))
})

it('should resolve to parent dir when relative dir is child', () => {
const relativeDir = 'bar'
const cwd = process.cwd() + path.sep + 'bar'
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalizePath(process.cwd()))
expect(normalizePath(rootDir)).toEqual(normalizePath(process.cwd()))
})

it('should resolve to parent dir when relative dir is child and child has trailing dir separator', () => {
const relativeDir = 'bar' + path.sep
const cwd = process.cwd() + path.sep + 'bar'
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalizePath(process.cwd()))
expect(normalizePath(rootDir)).toEqual(normalizePath(process.cwd()))
})
})
})

0 comments on commit 5b3cf0c

Please sign in to comment.