Debug/Test-Git.ps1

# Remove-Folder C:\src\Beispiele @("node_modules", "npm-cache")

# . C:\src\vs\UIC\PowerShellScripts\Get-IsGitRepository.ps1
# . C:\src\vs\UIC\PowerShellScripts\Get-GitRemoteOrigin.ps1
# . C:\src\vs\UIC\PowerShellScripts\Get-IsLocalGitRepository.ps1
# . C:\src\vs\UIC\PowerShellScripts\Get-GitCommitRequired.ps1
# . C:\src\vs\UIC\PowerShellScripts\Get-GitPushRequired.ps1
# . C:\src\vs\UIC\PowerShellScripts\Get-GitRepositoryState.ps1

$ErrorActionPreference = 'Stop'

# https://github.com/microsoft/PowerShellForGitHub

class RepositoryInfo {
  # Optionally, add attributes to prevent invalid values
  [ValidateNotNullOrEmpty()][string]$Name
  [ValidateNotNullOrEmpty()][string]$State
  [ValidateNotNullOrEmpty()][System.IO.DirectoryInfo]$DirectoryInfo
  [string]$RemoteOrigin  
  [ValidateNotNullOrEmpty()][bool]$Commit
  [ValidateNotNullOrEmpty()][bool]$Push
  [ValidateNotNullOrEmpty()][bool]$Local
  [ValidateNotNullOrEmpty()][string]$FullName
}

function Test-GitFolder {
  [CmdletBinding()]
  [OutputType([RepositoryInfo[]])]
  Param(
    [string]$Path = $pwd
  )

  $result = @() #New-Object System.Collections.ArrayList

  Clear-Host

  $childs = Get-ChildItem $Path -Directory
  
  $changed_text = "Changes to be committed"
  $untracked_files = "Untracked files"
  $changes_not_staged = "Changes not staged for commit:"

  foreach ($item in $childs) {

    Write-Debug "------ $($item.FullName) ----------"

    if ($item -is [System.IO.DirectoryInfo]) {

      $checkHead = $false
      $isDirty = $false
      $pushRequired = $false

      $curDir = Join-Path $Path $item

      $isGitFolder = Get-IsGitRepository $item

      if ($isGitFolder) {

        Write-Debug "REPO: $curDir"

        $status = git -C $curDir status #-s
        $status = Get-IfNull $status ""

        switch ($status) {
          { [String]::IsNullOrEmpty($status) } {
            Write-Debug "C L E A N"
            $checkHead = $True
            Break
          }
          { $status -like "*$untracked_files*" } {
            Write-Debug "untracked_files"
            $isDirty = $true
            $checkHead = $false
            Break
          }
          { $status -like "*$changed_text*" } {
            Write-Debug "changed_text"
            $isDirty = $true
            $checkHead = $false
            Break
          }
          { $status -like "*$changes_not_staged*" } {
            Write-Debug "changes_not_staged"
            $isDirty = $true
            $checkHead = $false
            Break
          }
          { $status -like "*No commits yet*" } {
            Write-Debug "No commits yet"
            $isDirty = $false
            $checkHead = $false
            Break
          }
          { $status -like "*nothing to commit, working tree clean*" } {
            Write-Debug "nothing to commit, working tree clean"
            $isDirty = $false
            $checkHead = $true # < false
            Break
          }          
          { $status -like "*Your branch is up to date with 'origin/*" } {
            Write-Debug "Your branch is up to date with 'origin"
            
            $checkHead = $True
            Break
          }
          
          Default {
            $checkHead = $true
            Break
          }

        }

        if ($checkHead) {
          $head = git -C $curDir diff HEAD
          #Write-Debug "dirty by head"
          if (-not [string]::IsNullOrEmpty($head)) {       
            Write-Debug " -+-+-+- CHECK HEAD -+-+-+- "
            write-host "HEAD"
          }

        }
        
        $remoteOrigin = $(Get-GitRemoteOrigin $item)
        if ([string]::IsNullOrEmpty($remoteOrigin)) {       
          Write-Debug " -+-+-+- LOCAL REPO -+-+-+- "
        }
        else {
          $gitlog = git -C $curDir log --branches --not --remotes  
          Write-Debug "log: $gitlog"

          if (-not [string]::IsNullOrEmpty($gitlog)) {       
            write-host "gitlog: $gitlog"
            $pushRequired = $true
          }

          # $cherry = git -C $curDir cherry -v origin/master
          # Write-Debug "cherry: $cherry"
          # if (-not [string]::IsNullOrEmpty($cherry)) {
          # write-host "cherry: $cherry"
          # }

        }

        $test = Get-GitCommitRequired $item
        if ($test -ne $isDirty) {       
          write-host " ------------------------ U H A ++++++++++++++++ "
        }

        $test = Get-GitPushRequired $item
        if ($test -ne $pushRequired) {       
          write-host " ------------------------ U H A ++++++++++++++++ "
        }

        $currentRepo = [RepositoryInfo]@{
          Name          = $item.Name
          DirectoryInfo = $item
          State         = Get-GitRepositoryState $item
          Commit        = $isDirty
          Push          = $pushRequired
          RemoteOrigin  = $remoteOrigin
          Local         = [string]::IsNullOrEmpty($remoteOrigin)
          FullName      = $item.FullName
        }

        $result += $currentRepo

        Write-Debug "isDirty: $isDirty"

        # Write-Debug "status: $status"
        # if ([String]::IsNullOrEmpty($status)) {
        # Write-Debug "Ok"
        # }
        # else {
        # $result = git -C $curDir diff HEAD

        # Write-Debug "changed"
        # }

      }
      else {
        # check subfolders
        $sub = Test-GitFolder $curDir
        $result += $sub
      }
    }
  }

  return $result
}

Test-GitFolder C:\src\g-dotup | Format-Table -Property Name, State, Commit, Push, Local, RemoteOrigin, FullName # c:\temp
# git status