Public/Get-GitRepositoryState.ps1


function Get-GitRepositoryState {
  [CmdletBinding()]
  [OutputType([string])]
  Param(
    [System.IO.DirectoryInfo]$item
  )

  $changed_text = "Changes to be committed"
  $untracked_files = "Untracked files"
  $changes_not_staged = "Changes not staged for commit:"
  
  $result = ""

  $isGitFolder = Get-IsGitRepository $item

  if ($isGitFolder) {

    $status = git -C $item.FullName status #-s
    $status = Get-IfNull $status ""

    switch ($status) {
      { [String]::IsNullOrEmpty($status) } {
        $result = "clean"
        Break
      }
      { $status -like "*$untracked_files*" } {
        $result = "untracked_files"
        # $dirty = $true
        Break
      }
      { $status -like "*$changed_text*" } {
        $result = "changed_text"
        # $dirty = $true
        Break
      }
      { $status -like "*$changes_not_staged*" } {
        $result = "changes_not_staged"
        # $dirty = $true
        Break
      }
      { $status -like "*No commits yet*" } {
        $result = "no_commits_yet"
        Break
      }
      { $status -like "*nothing to commit, working tree clean*" } {
        $result = "working_tree_clean"
        Break
      }          
      { $status -like "*Your branch is up to date with 'origin/*" } {
        $result = "up_to_date"
        Break
      }
          
    }
  }
  else {
    throw "ArgumentException: Directory is not a git repository"
  }
    
  return $result
}