Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Test-LocalOrParentPath Broken

2 views
Skip to first unread message

Jeremy Skinner

unread,
Apr 5, 2010, 1:41:38 PM4/5/10
to posh...@googlegroups.com
I see that Test-LocalOrParentPath (used to find the .git directory) is broken if you're in a subdirectory. I've just committed a new version of Utils.ps1 to my fork that contains a fixed version based on Mark's original code:

function Test-LocalOrParentPath($path) {
    $checkIn = Get-Item .
    while ($checkIn -ne $NULL) {
        $pathToTest = [System.IO.Path]::Combine($checkIn.fullname, $path)
        if (Test-Path $pathToTest) {
            return $true
        } else {
            $checkIn = $checkIn.parent
        }
    }
    return $false
}

Jeremy

David Mohundro

unread,
Apr 5, 2010, 1:50:27 PM4/5/10
to posh...@googlegroups.com
I thought I tested it but I guess I didn't :)

Here's another version that is a little more similar to my first version (with the missing not check added):

function Test-LocalOrParentPath($path) {
    $done = $false
    do {
        if (Test-Path $path) { return $true }
        if (-not (Test-Path ..)) { return $false }
        $path = "..\$path"
    } while (!$done)
}

I'm fine with whatever, though.

Jeremy Skinner

unread,
Apr 5, 2010, 1:51:27 PM4/5/10
to posh...@googlegroups.com
I tried that first but it doesn't work - calling Test-Path .. always returns true.

Jeremy

David Mohundro

unread,
Apr 5, 2010, 1:53:48 PM4/5/10
to posh...@googlegroups.com
Huh, yeah, you're right - never mind then. I guess I just tested it on immediate subdirectories as opposed to multiple levels down. Sorry about that.

David
Reply all
Reply to author
Forward
0 new messages