Functions/Find-GitRepository.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function Find-GitRepository
{
    <#
    .SYNOPSIS
    Searches a directory and its parents for a Git repository.
 
    .DESCRIPTION
    The `Find-GitRepository` function searches a directory for a Git repository and returns a `LibGit2Sharp.Repository` object representing that repository. If a repository isn't found, it looks up the directory tree until it finds one (i.e. it looks at the directories parent directory, then that directory's parent, then that directory's parent, etc. until it finds a repository or gets to the root directory. If it doesn't find one, nothing is returned.
 
    With no parameters, looks in the current directory and up its directory tree. If given a path with the `Path` parameter, it looks in that directory then up its directory tree.
 
    The repository object that is returned contains resources that don't get automatically removed from memory by .NET. To avoid memory leaks, you must call its `Dispose()` method when you're done using it.
 
    .OUTPUTS
    LibGit2Sharp.Repository.
 
    .EXAMPLE
    Find-GitRepository
 
    Demonstrates how to find the Git repository of the current directory.
 
    .EXAMPLE
    Find-GitRepository -Path 'C:\Projects\LibGit2.PowerShell\LibGit2\bin'
 
    Demonstrates how to find the Git repository that a specific directory is a part of. In this case, a `LibGit2Sharp.Repository` object is returned for the repository at `C:\Projects\LibGit2.PowerShell`.
    #>

    [CmdletBinding()]
    [OutputType([LibGit2Sharp.Repository])]
    param(
        [string]
        # The path to start searching.
        $Path = (Get-Location).ProviderPath,

        [Switch]
        # Write an error if a repository isn't found. Usually, no error is written and nothing is returned when a repository isn't found.
        $Verify
    )

    Set-StrictMode -Version 'Latest'

    if( -not $Path )
    {
        $Path = (Get-Location).ProviderPath
    }

    $Path = Resolve-Path -Path $Path -ErrorAction Ignore | Select-Object -ExpandProperty 'ProviderPath'
    if( -not $Path )
    {
        Write-Error -Message ('Can''t find a repository in ''{0}'' because it does not exist.' -f $PSBoundParameters['Path'])
        return
    }
    
    $startedAt = $Path

    while( $Path -and -not [LibGit2Sharp.Repository]::IsValid($Path) )
    {
        $Path = Split-Path -Parent -Path $Path
    }

    if( -not $Path )
    {
        if( $Verify )
        {
            Write-Error -Message ('Path ''{0}'' not in a Git repository.' -f $startedAt)
        }
        return
    }

    return Get-GitRepository -RepoRoot $Path
}