PowerShell.PowerLibrary.Git.psm1

#region Variables
[psobject]$ErrorActionPreferenceValues = `
@{
    Stop = "Stop";
    Continue = "Continue";
}
#endregion

#region Functions

FUNCTION Get-GitRepositoryClone
{
    <#
    .Synopsis
        This function runs git command to Generate Difference Check
    .DESCRIPTION
        This function runs git command to Generate Difference Check
    .PARAMETER RepositoryServerPath
        Repository Server Path.
    .PARAMETER RepositoryLocalPath
        Repository Local Path.
    #>


    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline=$true)]
        [Alias('Git')]
        [ValidateNotNullOrEmpty()]
        [string]
        $RepositoryServerPath,

        [Parameter(Mandatory = $true, ValueFromPipeline=$true)]
        [Alias('Workarea')]
        [ValidateNotNullOrEmpty()]
        [string]
        $RepositoryLocalPath
    );

    BEGIN
    {
        $ErrorActionPreference = $ErrorActionPreferenceValues.Stop;
    }

    PROCESS
    {
        TRY
        {
            Write-Host "***GIT***Started***" -ForegroundColor Gray;
            Write-Host $RepositoryServerPath -NoNewline -ForegroundColor Yellow;
            Write-Host " >>> " -NoNewline -ForegroundColor White;
            Write-Host $RepositoryLocalPath -ForegroundColor Yellow;
            CD $RepositoryLocalPath;
            git clone $RepositoryServerPath;
            Write-Host "***GIT***Ended***" -ForegroundColor Gray;
            Write-Host;
        }
        CATCH [System.Exception]
        {
            Log-Error -Exception $_.Exception.Message -Message "An error has occurred when trying to clone the repository from GIT.`r`nGIT might be offline.";
            break;
        }
    }

    END
    {
        $ErrorActionPreference = $ErrorActionPreferenceValues.Continue;
    }
}

FUNCTION Get-GitRepositoriesClone
{
    <#
    .Synopsis
        This function runs git command to Generate Difference Check
    .DESCRIPTION
        This function runs git command to Generate Difference Check
    .PARAMETER GitRepositories
        List of all repositories to be cloned.
    #>


    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline=$true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [System.Collections.Specialized.StringDictionary]
        $GitRepositories,

        [Parameter(Mandatory = $false, ValueFromPipeline=$true, Position = 1)]
        [Alias('Progress')]
        [switch]
        $WithProgress
    );

    Write-Host "Get Git Repositories Clone" -ForegroundColor Cyan;
    IF($GitRepositories -eq $null)
    {
        Log-Error '$GitRepositories Cannot be null. Override $GitRepositories in your context';
    }
    IF($WithProgress)
    {
        $Total = $GitRepositories.Count;
        $Index = 0;
        $Progress = 0;
        IF($Total -gt 0) 
        {
            $Progress = $Index / $Total * 100;
        }
        Write-Progress -Activity "Getting Git Repositories Clone" -Status "----" -PercentComplete 0;
    }
    FOREACH($GitRepository in $GitRepositories)
    {
        IF($WithProgress)
        {
            IF($Total -gt 0) 
            {
                $Progress = $Index / $Total * 100;
            }
            Write-Progress -Activity "Getting Git Repositories Clone" -Status $TfsProject.Name -PercentComplete $Progress;
        }

        Get-GitRepositoryClone -RepositoryServerPath $GitRepository.Name -RepositoryLocalPath $GitRepository.Value;
        
        IF($WithProgress)
        {
            $Index ++;
        }
    }
    IF($WithProgress)
    {
        Write-Progress -Activity "Getting Git Repositories Clone" -Status "Completed" -PercentComplete 100 -Completed:$true;
    }
    Write-Host "Getting Git Repositories Clone: " -NoNewline -ForegroundColor Cyan;
    Write-Host "Completed`n" -ForegroundColor White;
}

FUNCTION Get-GitBranchRemote
{
    <#
    .Synopsis
        This function runs git command to List remote branches
    .DESCRIPTION
        This function runs git command to List remote branches
    #>


    git branch -r;
}

FUNCTION Get-GitDiffNameOnly
{
    <#
    .Synopsis
        This function runs git command to Generate Difference Check
    .DESCRIPTION
        This function runs git command to Generate Difference Check
    .PARAMETER Source
        Source Branch Name.
    .PARAMETER Target
        Target Branch Name.
    #>


    param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position= 0)]
        [string]
        $Source,
        
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position= 1)]
        [string]
        $Target
    );

    git diff --name-only $Source $Target
}

FUNCTION Set-GitAssumeUnchanged
{
    <#
    .Synopsis
        This function runs git command to assume or discard changes
    .DESCRIPTION
        This function runs git command to assume or discard changes
    .PARAMETER Target
        Path to the file full name.
    #>


    param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position= 0)]
        [string]
        $Target
    );

    BEGIN
    {

    }

    PROCESS
    {
        git update-index --assume-unchanged $Target
    }

    END
    {

    }    
}
#endregion