Skip to content

Commit

Permalink
fix: normalize gitDir path to posix using normalize-path
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Aug 17, 2019
1 parent f77cefa commit f485e51
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"listr": "^0.14.3",
"log-symbols": "^3.0.0",
"micromatch": "^4.0.2",
"normalize-path": "^3.0.0",
"please-upgrade-node": "^3.1.1",
"string-argv": "^0.3.0",
"stringify-object": "^3.3.0"
Expand Down
4 changes: 2 additions & 2 deletions src/resolveGitDir.js
@@ -1,15 +1,15 @@
'use strict'

const execGit = require('./execGit')
const path = require('path')
const normalize = require('normalize-path')

module.exports = async function resolveGitDir(options = {}) {
try {
// git cli uses GIT_DIR to fast track its response however it might be set to a different path
// depending on where the caller initiated this from, hence clear GIT_DIR
delete process.env.GIT_DIR
const gitDir = await execGit(['rev-parse', '--show-toplevel'], options)
return path.resolve(gitDir)
return normalize(gitDir)
} catch (error) {
return null
}
Expand Down
8 changes: 5 additions & 3 deletions test/resolveGitDir.spec.js
@@ -1,4 +1,6 @@
import normalize from 'normalize-path'
import path from 'path'

import resolveGitDir from '../src/resolveGitDir'

/**
Expand All @@ -8,12 +10,12 @@ jest.unmock('execa')

describe('resolveGitDir', () => {
it('should resolve to current working dir when .git is in the same dir', async () => {
const expected = process.cwd()
const expected = normalize(process.cwd())
expect(await resolveGitDir()).toEqual(expected)
})

it('should resolve to the parent dir when .git is in the parent dir', async () => {
const expected = path.dirname(__dirname)
const expected = normalize(path.dirname(__dirname))
const processCwdBkp = process.cwd
process.cwd = () => __dirname
// path.resolve to strip trailing slash
Expand All @@ -22,7 +24,7 @@ describe('resolveGitDir', () => {
})

it('should resolve to the parent dir when .git is in the parent dir even when the GIT_DIR environment variable is set', async () => {
const expected = path.dirname(__dirname)
const expected = normalize(path.dirname(__dirname))
const processCwdBkp = process.cwd
process.cwd = () => __dirname
process.env.GIT_DIR = 'wrong/path/.git' // refer to https://github.com/DonJayamanne/gitHistoryVSCode/issues/233#issuecomment-375769718
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -4296,6 +4296,11 @@ normalize-path@^2.1.1:
dependencies:
remove-trailing-separator "^1.0.1"

normalize-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

npm-bundled@^1.0.1:
version "1.0.6"
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.6.tgz#e7ba9aadcef962bb61248f91721cd932b3fe6bdd"
Expand Down

0 comments on commit f485e51

Please sign in to comment.