Skip to content

Commit

Permalink
fix: use native git command to get top-level directory for repo
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jun 11, 2024
1 parent 6593870 commit e0386dc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 44 deletions.
23 changes: 3 additions & 20 deletions lib/resolveGitRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ const resolveGitConfigDir = async (gitDir) => {
return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim()
}

// exported for test
export const determineGitDir = (cwd, relativeDir) => {
// if relative dir and cwd have different endings normalize it
// this happens under windows, where normalize is unable to normalize git's output
if (relativeDir && relativeDir.endsWith(path.sep)) {
relativeDir = relativeDir.slice(0, -1)
}
if (relativeDir) {
// the current working dir is inside the git top-level directory
return cwd.substring(0, cwd.lastIndexOf(relativeDir))
} else {
// the current working dir is the top-level git directory
return cwd
}
}

/**
* Resolve git directory and possible submodule paths
*/
Expand All @@ -62,10 +46,9 @@ export const resolveGitRepo = async (cwd = process.cwd()) => {
debugLog('Unset GIT_WORK_TREE (was `%s`)', process.env.GIT_WORK_TREE)
delete process.env.GIT_WORK_TREE

// 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 = normalizePath(determineGitDir(normalizePath(cwd), gitRel))
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)
Expand Down
25 changes: 1 addition & 24 deletions test/unit/resolveGitRepo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { normalizePath } from '../../lib/normalizePath.js'
import { determineGitDir, resolveGitRepo } from '../../lib/resolveGitRepo.js'
import { resolveGitRepo } from '../../lib/resolveGitRepo.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand Down Expand Up @@ -45,27 +45,4 @@ describe('resolveGitRepo', () => {
const { gitDir } = await resolveGitRepo({ cwd: '/' }) // assume root is not a git directory
expect(gitDir).toEqual(null)
})

describe('determineGitDir', () => {
it('should resolve to current working dir when relative dir is empty', () => {
const cwd = process.cwd()
const relativeDir = undefined
const rootDir = determineGitDir(cwd, relativeDir)
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(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(normalizePath(rootDir)).toEqual(normalizePath(process.cwd()))
})
})
})

0 comments on commit e0386dc

Please sign in to comment.