From 7b7e15e207d61d1bd9273f81095fe054302b42c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Kalm=C3=A1r?= Date: Sat, 9 Oct 2021 17:35:57 +0200 Subject: [PATCH 1/2] Fix #1027 by removing dangling chars --- lib/resolveGitRepo.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index 03d04ed55..7c92c3a06 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -26,11 +26,16 @@ const resolveGitConfigDir = async (gitDir) => { } const determineGitDir = (cwd, relativeDir) => { + // if relative dir and cwd have different endings normalize it + if (relativeDir.endsWith(path.sep) && !cwd.endsWith(path.sep)) { + relativeDir = relativeDir.slice(0, -1) + } if (relativeDir) { // the current working dir is inside the git top-level directory return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir))) } else { // the current working dir is the top-level git directory + return normalize(cwd) } } From 23def5d590bbf108764998dfbde79bada0dc68b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Kalm=C3=A1r?= Date: Sat, 9 Oct 2021 20:19:35 +0200 Subject: [PATCH 2/2] Add tests --- lib/resolveGitRepo.js | 7 +++++-- test/resolveGitRepo.spec.js | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index 7c92c3a06..79dfff61d 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -27,7 +27,8 @@ const resolveGitConfigDir = async (gitDir) => { const determineGitDir = (cwd, relativeDir) => { // if relative dir and cwd have different endings normalize it - if (relativeDir.endsWith(path.sep) && !cwd.endsWith(path.sep)) { + // 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) { @@ -35,7 +36,6 @@ const determineGitDir = (cwd, relativeDir) => { return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir))) } else { // the current working dir is the top-level git directory - return normalize(cwd) } } @@ -70,3 +70,6 @@ const resolveGitRepo = async (cwd = process.cwd()) => { } module.exports = resolveGitRepo + +// exported for test +module.exports.determineGitDir = determineGitDir diff --git a/test/resolveGitRepo.spec.js b/test/resolveGitRepo.spec.js index 46086ab41..e36e44559 100644 --- a/test/resolveGitRepo.spec.js +++ b/test/resolveGitRepo.spec.js @@ -1,7 +1,7 @@ import normalize from 'normalize-path' import path from 'path' -import resolveGitRepo from '../lib/resolveGitRepo' +import resolveGitRepo, { determineGitDir } from '../lib/resolveGitRepo' /** * resolveGitRepo runs execa, so the mock needs to be disabled for these tests @@ -48,4 +48,27 @@ 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(rootDir).toEqual(normalize(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(normalize(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(normalize(process.cwd())) + }) + }) })