Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #407 worktree has .git file not folder. #408

Merged
merged 2 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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