ProductivityTools.ManageGitRepositories.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
function PerformSingleActiononDirectory([string] $Directory,$scriptblock) { Push-Location $directories=Get-ChildItem $Directory | ?{ $_.PSIsContainer } foreach($dir in $directories) { $directoryPath=$dir.FullName $scriptblock.Invoke($directoryPath) } Pop-Location } function CheckIfGitRepository() { if($(Test-Path ".git") -eq $false) { throw "Directory '$Directory' is not git repository" } } function Push-GitRepository() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) Push-Location cd $Directory CheckIfGitRepository $status=git status if ($($status -Like "*Your branch is ahead*").Count -gt 0) { Write-Host "Git repository $directoryPath - ahead of, pushing" -ForegroundColor Red git push --all $remote } if ($($status -Like "*nothing to commit, working tree clean*").Count -gt 0) { Write-Host "Git repository $directoryPath - working tree clean" -ForegroundColor Green } else { Write-Host "Git repository $directoryPath - dirty, pushing" -ForegroundColor Red git checkout -b "AutoCommitBranch" git add . git commit -m "Automatic Commit" $remote=git remote git push -u $remote AutoCommitBranch #this needs to be done to make branch track remote git push --all $remote } Pop-Location } function Push-GitRepositories([string] $Directory) { PerformSingleActiononDirectory $Directory {Param($x) Push-GitRepository $x} } function Get-AutoCommitRepository() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) cd $Directory CheckIfGitRepository $branches=git branch -a if ([bool]($branches -like "*AutoCommitBranch*")) { Write-Host "Git repository '$Directory' has AutoCommitBranch" -ForegroundColor Red } else { Write-Host "Git repository '$Directory' - no AutoCommitBranch exists" -ForegroundColor Green } } function Get-AutoCommitRepositories() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) PerformSingleActiononDirectory $Directory {Param($x) Get-AutoCommitRepository $x} } function Pull-GitRepository() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) Push-Location cd $Directory CheckIfGitRepository $x=git pull if ($x='Already up to date.') { Write-Host "Repository $directoryPath - up to date" -ForegroundColor Green } else { $x } Pop-Location } function Pull-GitRepositories([string] $Directory) { PerformSingleActiononDirectory $Directory {Param($x) Pull-GitRepository $x} } function Get-GitRepositoryStatus() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) Push-Location cd $Directory CheckIfGitRepository $status=git status -u if ($status -contains "nothing to commit, working tree clean") { Write-Host "Git repository $directoryPath - working tree clean" -ForegroundColor Green } else { Write-Host "Git repository $directoryPath - dirty" -ForegroundColor Red } } function Get-GitRepositoriesStatus() { Param( [Parameter(Mandatory=$true)] [string] $Directory ) PerformSingleActiononDirectory $Directory {Param($x) Get-GitRepositoryStatus $x} } Export-ModuleMember Push-GitRepository Export-ModuleMember Push-GitRepositories Export-ModuleMember Get-AutoCommitRepository Export-ModuleMember Get-AutoCommitRepositories Export-ModuleMember Pull-GitRepository Export-ModuleMember Pull-GitRepositories Export-ModuleMember Get-GitRepositoryStatus Export-ModuleMember Get-GitRepositoriesStatus |