1+ import { sep } from "path" ;
2+ import * as tempy from "tempy" ;
3+ import { mkdirp } from "mkdirp" ;
4+ import { runCommand } from "@softwareventures/precise-commits/lib/utils" ;
15import {
26 getDiffForFile ,
3- resolveNearestGitDirectoryParent ,
7+ resolveGitWorkingTreePath ,
48 getModifiedFilenames ,
59 index
610} from "../src/git-utils" ;
@@ -10,7 +14,7 @@ const fixtures = readFixtures();
1014let testBed : TestBed ;
1115
1216describe ( "git-utils" , ( ) => {
13- describe ( "resolveNearestGitDirectoryParent ()" , ( ) => {
17+ describe ( "resolveGitWorkingTreePath ()" , ( ) => {
1418 beforeAll ( ( ) => {
1519 testBed = new TestBed ( ) ;
1620 } ) ;
@@ -26,11 +30,21 @@ describe("git-utils", () => {
2630 /**
2731 * The tmpFile should resolve to its own .git directory
2832 */
29- expect ( await resolveNearestGitDirectoryParent ( tmpFile . directoryPath ) ) . toEqual (
33+ expect ( await resolveGitWorkingTreePath ( tmpFile . directoryPath ) ) . toEqual (
3034 tmpFile . directoryPath
3135 ) ;
3236 } ) ;
3337 } ) ;
38+
39+ it ( `should resolve the correct working tree path in a working tree created by git worktree` , async ( ) => {
40+ await tempy . directory . task ( async worktreePath => {
41+ await runCommand ( "git" , [ "worktree" , "add" , "-B" , "worktree" , worktreePath ] ) ;
42+ const subdirPath = `${ worktreePath } ${ sep } subdir` ;
43+ await mkdirp ( subdirPath ) ;
44+ expect ( await resolveGitWorkingTreePath ( subdirPath ) ) . toEqual ( worktreePath ) ;
45+ await runCommand ( "git" , [ "worktree" , "remove" , worktreePath ] ) ;
46+ } ) ;
47+ } ) ;
3448 } ) ;
3549
3650 describe ( "getDiffForFile()" , ( ) => {
@@ -46,9 +60,7 @@ describe("git-utils", () => {
4660 it ( fixture . fixtureName , async ( ) => {
4761 await testBed . prepareFixtureInTmpDirectory ( fixture ) ;
4862 const tmpFile = testBed . getTmpFileForFixture ( fixture ) ;
49- const gitDirectoryParent = await resolveNearestGitDirectoryParent (
50- tmpFile . directoryPath
51- ) ;
63+ const gitDirectoryParent = await resolveGitWorkingTreePath ( tmpFile . directoryPath ) ;
5264 const diff = await getDiffForFile (
5365 gitDirectoryParent ,
5466 tmpFile . path ,
@@ -73,9 +85,7 @@ describe("git-utils", () => {
7385 it ( fixture . fixtureName , async ( ) => {
7486 await testBed . prepareFixtureInTmpDirectory ( fixture ) ;
7587 const tmpFile = testBed . getTmpFileForFixture ( fixture ) ;
76- const gitDirectoryParent = await resolveNearestGitDirectoryParent (
77- tmpFile . directoryPath
78- ) ;
88+ const gitDirectoryParent = await resolveGitWorkingTreePath ( tmpFile . directoryPath ) ;
7989 const fileNames = await getModifiedFilenames (
8090 gitDirectoryParent ,
8191 tmpFile . initialCommitSHA ,
0 commit comments