Public/Get-GitLastCommit.ps1

function Get-GitLastCommit {
<#
    .Synopsis
      Returns the full SHA1 commit hash for the most recent commit of the current branch
    .DESCRIPTION
      Will return an empty string if there is an error.
    .PARAMETER path
        Optional
        Default: Null
 
        The path to the folder or file that you want to get the most recent commit from. Needs to be relative to the root of the repository.
 
    .PARAMETER masterBranch
        Optional
        Default: $false
 
        This is kind of hacky so forgive me. If this switch is true, this will get the most recent commit hash from the local master branch, if false, it will look at the HEAD (or the current branch)
        I tried to parameterize this so you could choose which branch to get the commit, but I was getting errors which i believe was jsut the way I was formatting the invocation of the git.exe
        
    .EXAMPLE
        Get the most recent commit hash of the entire repository, for the current branch
 
        Get-GitLastCommit
 
        Output: dcef3a70cc28d9dfa058c8f86183cef6e78a6df5
 
    .EXAMPLE
        Get the most recent commit hash of a specific directory (named SSAS/SSAS_TabularModels), for the current branch
 
        Get-GitLastCommit "SSAS/SSAS_TabularModels"
 
        Output: 798bbd1d8d27b99ea27ff2e38e3ae86f4b02c317
 
    .EXAMPLE
        Get the most recent commit hash of a specific file in a subdirectory (named SSAS/SSAS_TabularModels\.gitgnore on windows...), for the current branch
 
        Get-GitLastCommit "SSAS/SSAS_TabularModels/.gitignore"
 
        Output: 6cb5d7854d5b81b407d475972ee602de9c7ddca3
 
    .EXAMPLE
        Get the most recent commit hash of a specific file in a subdirectory (named SSAS_TabularModels\.gitgnore on windows...), for the master branch
 
        Get-GitLastCommit "SSAS_TabularModels/.gitignore" -masterBranch
 
        Output: 6cb5d7854d5b81b407d475972ee602de9c7ddca3
    #>

  [CmdletBinding(SupportsShouldProcess = $true)]
  param([Parameter(Position = 0)] [string]$path = $null
    ,[Parameter(Position = 1)] [switch]$masterBranch = $false
  )

  function HandleGitOutput([Parameter(ValueFromPipeline)] $output){
    Write-Log "$($output.stdout)" Verbose
    
    if (-not [string]::IsNullOrEmpty($output.stderr)){
        Write-Log "$($output.stderr)" Warning
        Write-Log "There was an error of some type. See warning above for more info" Error -ErrorAction Stop
    }
    Write-Output $output.stdout
  }
  $origLocation = $(Get-Location)
  Write-Verbose "Current Location: $origLocation"

  $oldErrorAction = $ErrorActionPreference
  $ErrorActionPreference = "Stop"
  try {
    if (!([string]::IsNullOrEmpty($path))) {

      $path = $path -replace "\\","/"
      #This is crappy code... I was getting an error when I tried to parameterize this to allow you to specify what branch you want to get the last commit from. The error was :Path $path exists on disk but not in the index.
      #When I outputted the call I am making, I was able to execute it on the command line just fine. Come find out the parameter that was located where "master" or "head" are is what was causing it. For the meantime, this works for my purposes.
      #If theuser specifies, then get the commit from master. If not, get the commit of the current branch (HEAD)
      if ($masterBranch) {
        Write-Verbose "git rev-parse master^:""$path"""
        $gitLogOutput = Start-MyProcess -EXEPath "git" -options "rev-parse master:'""'$path'""'" -ErrorAction Stop | HandleGitOutput 
      }
      else {
        Write-Verbose "git log -n 1 --pretty=format:%H -- ""$path"""
        $gitLogOutput = Start-MyProcess -EXEPath "git" -options "log -n 1 --pretty=format:%H -- ""$path""" -ErrorAction Stop | HandleGitOutput 
      }
    }
    else {
      if ($masterBranch) {
        Write-Verbose "git rev-parse master^"
        $gitLogOutput = Start-MyProcess -EXEPath "git" -options 'log -n 1 --pretty="%H" -- "."' -ErrorAction Stop | HandleGitOutput 
      }
      else {
        Write-Verbose 'git log --format="%H" -n 1'
        $gitLogOutput = Start-MyProcess -EXEPath "git" -options 'log --format="%H" -n 1' -ErrorAction Stop | HandleGitOutput 
      }

    }
  }
  catch {
     $ex = $_.Exception
    $line = $_.InvocationInfo.ScriptLineNumber
    $scriptName = Split-Path $_.InvocationInfo.ScriptName -Leaf
    $msg = $ex.Message
    Write-Log "Error in script $scriptName at line $line, error message: $msg" Warning
    $gitLogOutput = ''
  }
  $ErrorActionPreference = $oldErrorAction
  Write-Output $gitLogOutput
} Export-ModuleMember -Function Get-GitLastCommit