Skip to content

Commit

Permalink
Merge pull request #408 from dahlbyk/rkeithhill/is407-worktree-prompt…
Browse files Browse the repository at this point in the history
…-broken

Fix #407 worktree has .git file not folder.
  • Loading branch information
dahlbyk authored Feb 9, 2017
2 parents bf42720 + e150024 commit 8e34464
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion test/Get-GitDirectory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Describe 'Get-GitDiretory Tests' {
Context "Test normal repository" {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$origPath = Get-Location
}
AfterAll {
Expand All @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions test/Shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 8e34464

Please sign in to comment.