diff --git a/src/GitUtils.ps1 b/src/GitUtils.ps1 index 63bc05c4a..0dffea0c8 100644 --- a/src/GitUtils.ps1 +++ b/src/GitUtils.ps1 @@ -31,6 +31,14 @@ function Get-GitDirectory { return $gitDirPath } + # Handle the worktree case where .git is a file + if (Test-Path -LiteralPath $gitDirPath -PathType Leaf) { + $gitDirPath = Invoke-Utf8ConsoleCommand { git rev-parse --git-dir 2>$null } + if ($gitDirPath) { + return $gitDirPath + } + } + $headPath = Join-Path $currentDir.FullName HEAD if (Test-Path -LiteralPath $headPath -PathType Leaf) { $refsPath = Join-Path $currentDir.FullName refs diff --git a/test/Get-GitDirectory.Tests.ps1 b/test/Get-GitDirectory.Tests.ps1 index 69318f835..350d0a522 100644 --- a/test/Get-GitDirectory.Tests.ps1 +++ b/test/Get-GitDirectory.Tests.ps1 @@ -3,6 +3,7 @@ Describe 'Get-GitDiretory Tests' { Context "Test normal repository" { BeforeAll { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] $origPath = Get-Location } AfterAll { @@ -29,8 +30,47 @@ Describe 'Get-GitDiretory Tests' { } } - Context "Test bare repository" { + Context 'Test worktree' { + BeforeEach { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] + $origPath = Get-Location + $temp = [System.IO.Path]::GetTempPath() + $repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName()) + $worktreePath = Join-Path $temp ([IO.Path]::GetRandomFileName()) + + git init $repoPath + Set-Location $repoPath + 'foo' > .\README.md + git add .\README.md + # Quoting is a hack due to our use of the global:git function and how it converts args for invoke-expression + git commit -m "`"initial commit.`"" + + if (Test-Path $worktreePath) { + Remove-Item $worktreePath -Recurse -Force + } + New-Item $worktreePath -ItemType Directory > $null + git worktree add -b test-worktree $worktreePath master + } + AfterEach { + Set-Location $origPath + if (Test-Path $repoPath) { + Remove-Item $repoPath -Recurse -Force + } + if (Test-Path $worktreePath) { + Remove-Item $worktreePath -Recurse -Force + } + } + + It 'Returns the correct dir when under a worktree' { + Set-Location $worktreePath + $worktreeBaseName = Split-Path $worktreePath -Leaf + Get-GitDirectory | Should BeExactly (MakeGitPath $repoPath\.git\worktrees\$worktreeBaseName) + } + } + + Context 'Test bare repository' { BeforeAll { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] $origPath = Get-Location $temp = [System.IO.Path]::GetTempPath() $bareRepoName = "test.git" diff --git a/test/Shared.ps1 b/test/Shared.ps1 index c68ee00b6..0b07e90a4 100644 --- a/test/Shared.ps1 +++ b/test/Shared.ps1 @@ -15,10 +15,15 @@ function global:git { } } -# Force the posh-git prompt to be installed. Could be runnng on dev system where -# user has customized the prompt. -$module = Import-Module $moduleManifestPath -ArgumentList $true,$true -Force -PassThru - function MakeNativePath([string]$Path) { $Path -replace '\\|/', [System.IO.Path]::DirectorySeparatorChar } + +function MakeGitPath([string]$Path) { + $Path -replace '\\', '/' +} + +# Force the posh-git prompt to be installed. Could be runnng on dev system where +# user has customized the prompt. +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] +$module = Import-Module $moduleManifestPath -ArgumentList $true,$true -Force -PassThru diff --git a/test/TabExpansion.Tests.ps1 b/test/TabExpansion.Tests.ps1 index f77c1671f..0e4ad203c 100644 --- a/test/TabExpansion.Tests.ps1 +++ b/test/TabExpansion.Tests.ps1 @@ -119,6 +119,7 @@ Describe 'TabExpansion Tests' { } Context 'Add/Reset/Checkout TabExpansion Tests' { BeforeEach { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] $origPath = Get-Location $temp = [System.IO.Path]::GetTempPath() $repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())