Functions/Helper/Get-IBHGitHubRepo.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
<#
    .SYNOPSIS
        Check the GitHub repository for its name.
 
    .DESCRIPTION
        This command will return the repository name if the current repository
        is a GitHub repository. If not, it will return an empty string.
 
    .OUTPUTS
        System.String. GitHub repository name.
 
    .EXAMPLE
        PS C:\> Get-IBHGitHubRepo
        Get the repository name of a GitHub repository.
 
    .LINK
        https://github.com/claudiospizzi/PSInvokeBuildHelper
#>

function Get-IBHGitHubRepo
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        # Root path of the git repository.
        [Parameter(Mandatory = $false)]
        [System.String]
        $Path
    )

    try
    {
        # Switch to the desired location, if specifed
        if ($PSBoundParameters.ContainsKey('Path'))
        {
            $locationStackName = [System.Guid]::NewGuid().Guid
            Push-Location -Path $Path -StackName $locationStackName
        }

        $url = git config --get remote.origin.url

        if ($url -match '^https:\/\/github\.com\/.*\/(?<repo>.*)\.git$' -or
            $url -match '^https:\/\/github\.com\/.*\/(?<repo>.*)$')
        {
            return $Matches['repo']
        }

        if ($url -match '^git@github\.com:.*\/(?<repo>.*)\.git$')
        {
            return $Matches['repo']
        }

        return ''
    }
    finally
    {
        # Go back to the original location
        if ($PSBoundParameters.ContainsKey('Path'))
        {
            Pop-Location -StackName $locationStackName
        }
    }
}