functions/Push-RegentSolutionChanges.ps1

function Push-RegentSolutionChanges {
  <#
    .SYNOPSIS
    Records all changes made to a CRM solution to both the local and central repository.
     
    .DESCRIPTION
    This is a wrapper function that stages all changes in the local repository, commits them with the specified message, and then pushes the changes to the remote repository. Depends on the changes already being unpacked by the solution packager.
    .EXAMPLE
    Commit the changes to the default branch specified in your config.
    Push-SolutionChanges -CommitMessage "Added regent_SurveyQuestion1 and regent_SurveyQuestion2 fields to the Person record"
 
    .EXAMPLE
    Commit the changes to a different branch than the one in your config
    Push-SolutionChanges -CommitMessage "...." -Branch my-crazyivanbranch
  #>

  param(
    # The message associated with these changes. Be specific! Rather than saying "Added fields to Person record", be sure to include which fields, and maybe a few words about the purpose.
    [Parameter(Mandatory = $true)]
    [string]$CommitMessage, 
    # The branch to commit to. If not provided, defaults to the branch in your config (which is desirable 99% of the time).
    [string]$Branch
  )

  # Git can sometimes yield non-0 exit codes when there are no actual errors, so have PowerShell try to skip over them
  $ErrorActionPreference = "Continue"

  $Config = Get-Config
  $RepositoryPath = $Config.RepositoryPath # The path to Git repository with the unpacked solutions

  if (-not $Branch) {
    $Branch = $Config.DefaultBranch
  }
  Write-Verbose 'Entering ExtractCustomizations.ps1'
  Write-Verbose "Repository Path = $RepositoryPath"
  Write-Verbose "Commit Message = $CommitMessage"

  # Switch to the Repo directory
  Push-Location $RepositoryPath

  # Stage changes
  git add -A

  # Commit the changes
  $commitOutput = & git commit -m "$env:USERNAME - $CommitMessage"
  Write-Host "Commit Output"
  Write-Host -ForegroundColor Yellow $commitOutput
  if (($lastexitcode -ne 0) -and ("$commitOutput" -notlike "*nothing to commit*")) {
    throw "Git commit failed with exit code: $lastexitcode"
  }
  else {
    Write-Host -ForegroundColor Green "Git commit successful"
  }

  #Push
  $pushOutput = & git push origin $branch --porcelain # Porcelain flag added to redirect stderr to stdout to avoid error warnings

  Write-Host "Push Output"
  Write-Host -ForegroundColor Yellow $pushOutput

  if (($lastexitcode -ne 0) -and ($pushOutput -notlike "*Everything up-to-date*")) {
    throw "Git push failed with exit code: $lastexitcode"
  }
  else {
    Write-Host -ForegroundColor Green "Git push successful"
  }

  Pop-Location
  Write-Verbose 'Leaving CommitChanges.ps1'
}