Module/HelperFunctions/GitHelperFunctions.ps1

function PullGitRepository {
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$SourceBranchName
    )

    try {
        git fetch origin
        Write-Host "Pulling source repository ($SourceBranchName) to current folder"        
        git checkout $SourceBranchName
        git pull
        if (-not $?) {
            throw "Error with git pull!"
        }
    } 
    catch 
    {
    }
}

function CreateGitBranch {
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$BranchName
    )

    try {
        git fetch origin;
        git show-ref --quiet "remotes/origin/$BranchName";
    
        if ($?) {
            $CurrenctBranch = git rev-parse --abbrev-ref HEAD;
    
            if ($CurrenctBranch -ne $BranchName) {
                Write-Host ("Performing a checkout for remote DEV branch {0} " -f $BranchName) -ForegroundColor Green;
                git checkout $BranchName

                if ($?) {
                    Write-Host ("Branch {0} was successfully checked out" -f $BranchName) -ForegroundColor Green;
                    }
                else {
                    Write-Host ("Branch {0} was not checked out {1}" -f $BranchName, $LASTEXITCODE) -ForegroundColor Green;
                    }
                }
            }
        else {
            Write-Host ("Remote develop branch {0} doesn't exists, please associate a new development branch to your Work Item in DevOps." -f $BranchName) -ForegroundColor Red
            break;
            }
        }
    catch {
        Write-Host "Error: $_" -ForegroundColor Red
        break;
    }
}

function CloneOrPullRepository {
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$Repository,
        [Parameter(Mandatory=$true, Position=1)]
        [string]$LocalProjectFolder,
        [Parameter(Mandatory=$true, Position=2)]
        [string]$SourceBranchName
    )

    if (-not $Repository -gt 1) {
        Write-Host("No repository specified. Using default container setup.")
        return
    }    

    if (-not (Test-Path -PathType Container $LocalProjectFolder))
    {
        try 
        {
            Write-Host(("Cloning repository {0}." -f $Repository))
            git clone $Repository $LocalProjectFolder                
            # $? Contains the execution status of the last operation.
            # It contains TRUE if the last operation succeeded and FALSE if it failed.
            if (-not $?) {
                throw "Error with git clone!"
            }
            Write-Host("Repository cloned.")
        } 
        catch {
            Write-Host("Check spelling and verify permission to repository.")
            exit 
        }
    }
    else
    {
        Write-Host("Pulling repository to folder $LocalProjectFolder")
        try {
            Set-Location -Path $LocalProjectFolder
            PullGitRepository -SourceBranchName $SourceBranchName >$null 2>&1;
            if (-not $?) {
                throw "Error with git pull!"
            }
            Write-Host("Repository pulled.")
        } 
        catch 
        {
            Write-Host("Check spelling and verify permission to repository.")
            exit 
        }
    }
}