GitHelpers.psm1

$Script:PSModuleRoot = $PSScriptRoot
# Imported from [D:\a\1\s\GitHelpers\Public]
# Get-GitRepositoryUrl.ps1
function Get-GitRepositoryUrl
{
    <#
    .SYNOPSIS
 
    Returns the git repository url.
 
    .DESCRIPTION
 
    Returns the git repository url from the .git\config.
 
    .PARAMETER Path
 
    The path to the local git repository.
 
    .INPUTS
 
    None, does not support pipline input.
 
    .OUTPUTS
 
    String, git repository url.
 
    .Example
 
    Get-GitRepositoryUrl -Path C:\git\GitHelpers
 
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Path
    )

    begin
    {

    }

    process
    {
        $pathItem = Get-Item -Path $Path
        $gitFolder = Get-ChildItem -Path $pathItem.FullName -Filter '.git' -Hidden
        If ($gitFolder)
        {
            $config = Get-ChildItem -Path $gitFolder.FullName -Filter 'config'
            $content = Get-Content -Path $config.FullName | Select-String -SimpleMatch 'url ='
            $content.ToString().Trim().Replace('url = ', '')
        }
        else
        {
            Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Unable to locate .git repository configuration at [$($pathItem.FullName)]"
            Get-GitRepositoryUrl -Path $pathItem.PSParentPath
        }
    }

    end
    {
    }
}
# Invoke-GitCommit.ps1
function Invoke-GitCommit
{
    <#
    .SYNOPSIS
 
    Commit git changes.
 
    .DESCRIPTION
 
    Commit git changes.
 
    .PARAMETER Comment
 
    The comment for the commit.
 
    .PARAMETER Path
 
    The path to the git repository to commit to.
 
    .PARAMETER All
 
    Include all tracked and untracked files in the commit.
 
    .PARAMETER WhatIf
 
    Switch, adds --dry-run to the git commit command.
 
    .INPUTS
 
    None, does not support the pipeline.
 
    .OUTPUTS
 
    String, Git output
 
    .EXAMPLE
 
    Invoke-GitCommit -Path c:\git\PLDeploy
 
    .EXAMPLE
 
    Commit
 
    .EXAMPLE
 
    Commit -WhatIf
 
    .EXAMPLE
 
    Commit
 
    .LINK
 
    https://git-scm.com/docs/git-commit
    #>

    [CmdletBinding()]
    [alias('Commit')]
    param
    (
        [Parameter(Position = 0)]
        [string]
        $Comment,

        [Parameter(Position = 1)]
        [string[]]
        $Path,

        [Parameter()]
        [switch]
        $All,

        [Parameter()]
        [switch]
        $WhatIf
    )

    begin
    {
    }

    process
    {
        If($All.IsPresent)
        {
            git add .
        }
        If (-not($Path))
        {
            $Path = Get-Location
        }
        If(-not($Comment))
        {
            $Comment = "Updates"
        }
        Foreach ($_path in $Path)
        {
            Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Processing [$_path]."
            If (Test-Path -Path $_path)
            {
                Set-Location -Path $_path
                If ($WhatIf.IsPresent)
                {
                    git commit --quiet --all --dry-run --m $Comment
                }
                else
                {
                    git commit --quiet --all --m $Comment
                }
            }
            else
            {
                Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate [$_path]"
            }
        }
    }

    end
    {
    }
}
# Invoke-GitPrune.ps1
function Invoke-GitPrune
{
    <#
    .SYNOPSIS
 
    Prune old git branches.
 
    .DESCRIPTION
 
    Prune old git branches.
    Prune all unreachable objects from the object database
 
    .PARAMETER Path
 
    The path to the local work space's directory.
 
    .PARAMETER Child
 
    Switch, gets child directories within the path.
 
    .INPUTS
 
    None, does not support the pipeline.
 
    .OUTPUTS
 
    String, Git output
 
    .EXAMPLE
 
    Invoke-GitPrune -Path c:\git\PLDeploy
 
    .EXAMPLE
 
    Invoke-GitPrune -Child -Path c:\git\
 
    .EXAMPLE
 
    Prune -Child -WhatIf
 
    .EXAMPLE
 
    Prune
 
    .LINK
 
    https://git-scm.com/docs/git-prune
    #>

    [CmdletBinding()]
    [alias('Prune')]
    param
    (
        [Parameter()]
        [string]
        $Path,

        [Parameter()]
        [switch]
        $Child,

        [Parameter()]
        [switch]
        $WhatIf
    )

    begin
    {
    }

    process
    {
        If(-not($Path))
        {
            $Paths = Get-Location
        }
        If($Child.IsPresent)
        {
            $Paths = (Get-ChildItem -Path $Path -Directory).FullName
        }
        Foreach ($_path in $Paths)
        {
            Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Processing [$_path]."
            If (Test-Path -Path $_path)
            {
                Set-Location -Path $_path
                If ($WhatIf.IsPresent)
                {
                    git remote prune origin --dry-run
                }
                else
                {
                    git remote prune origin
                }
            }
            else
            {
                Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate [$_path]"
            }
        }
    }

    end
    {
    }
}
# Invoke-GitPush.ps1
function Invoke-GitPush
{
    <#
    .SYNOPSIS
 
    Push git changes.
 
    .DESCRIPTION
 
    Push git changes to the remote.
 
    .PARAMETER Path
 
    The path to the git repository to push from.
 
    .PARAMETER WhatIf
 
    Switch, adds --dry-run to the git Push command.
 
    .INPUTS
 
    None, does not support the pipeline.
 
    .OUTPUTS
 
    String, Git output
 
    .EXAMPLE
 
    Invoke-GitPush -Path c:\git\PLDeploy
 
    .EXAMPLE
 
    Push
 
    .EXAMPLE
 
    Push -WhatIf
 
    .EXAMPLE
 
    Push
 
    .LINK
 
    https://git-scm.com/docs/git-Push
    #>

    [CmdletBinding()]
    [alias('Push')]
    param
    (
        [Parameter(Position = 0)]
        [string[]]
        $Path,

        [Parameter()]
        [switch]
        $WhatIf
    )

    begin
    {
    }

    process
    {
        If (-not($Path))
        {
            $Path = Get-Location
        }
        Foreach ($_path in $Path)
        {
            Write-Verbose "[$($MyInvocation.MyCommand.Name)]: Processing [$_path]."
            If (Test-Path -Path $_path)
            {
                Set-Location -Path $_path
                If ($WhatIf.IsPresent)
                {
                    git push --all --quiet --dry-run
                }
                else
                {
                    git push --all --quiet
                }
            }
            else
            {
                Write-Error "[$($MyInvocation.MyCommand.Name)]: Unable to locate [$_path]"
            }
        }
    }

    end
    {
    }
}
# New-GitBranch.ps1
function New-GitBranch
{
     <#
    .SYNOPSIS
 
    Creates a new git branch.
 
    .DESCRIPTION
 
    Creates a new git branch based on the current directory or the path provided.
 
    .PARAMETER Path
 
    The path to the local git repository, if one is not provided it will default to the current location.
 
    .PARAMETER Name
 
    The name of the branch, if one is not provided it will generate a random guid.
 
    .INPUTS
 
    None, does not support pipline input.
 
    .OUTPUTS
 
    Web Page, takes you to the TFS pull request page.
 
    .EXAMPLE
 
    New-GitBranch -Name 'myBranch' -Path 'C:\git\PLDeploy'
 
    .EXAMPLE
 
    Branch
 
    #>

    [CmdletBinding()]
    [alias('Branch')]
    param (
        [Parameter()]
        [string]
        $Path,

        [Parameter()]
        [string]
        $Name
    )

    begin
    {
    }

    process
    {
        If($Path)
        {
            Set-Location -Path $Path
        }
        If(-not($Name))
        {
            $Name = (New-Guid).Guid
        }
        git checkout -q -b $Name
    }

    end
    {
    }
}
# New-GitPullRequest.ps1
function New-GitPullRequest
{
     <#
    .SYNOPSIS
 
    Creates a new pull request.
 
    .DESCRIPTION
 
    Creates a new pull request based on the current directory or the path provided.
 
    .PARAMETER Path
 
    The path to the git repository.
 
    .INPUTS
 
    None, does not support pipline input.
 
    .OUTPUTS
 
    Web Page, takes you to the TFS pull request page.
 
    .EXAMPLE
 
    PR
 
    .EXAMPLE
 
    New-GitPullRequest -Path C:\git\GitHelpers
 
    #>

    [CmdletBinding()]
    [alias('PR')]
    param (
        [Parameter()]
        [string]
        $Path
    )

    begin
    {
    }

    process
    {
        If(-not($Path))
        {
            $Path = Get-Location
        }
        $uri = Get-GitRepositoryUrl -Path $Path

        If($uri -match 'azure.com')
        {
            Start-Process "$uri\pullrequests?_a=mine"
        }
        If($uri -match 'github.com')
        {
            Start-Process "$uri\pulls".Replace('.git','')
        }
    }

    end
    {
    }
}
# Imported from [D:\a\1\s\GitHelpers\Tests]