Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Last active June 6, 2017 13:31
Show Gist options
  • Save WimObiwan/e4288fce35140412a111d2b7a53e11ce to your computer and use it in GitHub Desktop.
Save WimObiwan/e4288fce35140412a111d2b7a53e11ce to your computer and use it in GitHub Desktop.
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory = $True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[string]$Repository,
[Parameter(Mandatory = $True)]
[string]$Target,
[Parameter(Mandatory = $False)]
[int]$LastDays = 0
)
BEGIN {
$ErrorActionPreference = "Stop"
function Invoke-Git {
<#
.Synopsis
Wrapper function that deals with Powershell's peculiar error output when Git uses the error stream.
.Example
Invoke-Git ThrowError
$LASTEXITCODE
#>
[CmdletBinding()]
param(
[parameter(ValueFromRemainingArguments=$true)]
[string[]]$Arguments
)
$argumentstext = $([string]::Join(' ', ($Arguments | %{ "'$_'" })))
Write-Verbose "Executing Git: git.exe $argumentstext"
& {
[CmdletBinding()]
param(
[parameter(ValueFromRemainingArguments=$true)]
[string[]]$InnerArgs
)
git.exe $InnerArgs
} -ErrorAction SilentlyContinue -ErrorVariable fail @Arguments
$resultcode = $LASTEXITCODE
if ($fail) {
$fail.Exception
}
if ($resultcode -ne 0) {
Write-Error "Git.exe failed with exitcode $resultcode while executing: 'git.exe $argumentstext' on directory '$pwd'"
}
}
}
PROCESS {
$repo = Resolve-Path $Repository
Write-Verbose "Processing $repo"
pushd $repo
if (Test-Path $Target -PathType Container) {
$TargetFile = Join-Path $Target (Split-Path -Leaf $repo)
} else {
$TargetFile = $Target
}
if ([IO.Path]::GetExtension($TargetFile) -eq '') {
if ($LastDays -gt 0) {
$TargetFile += "-last-$LastDays-days.bundle"
} else {
$TargetFile += '.bundle'
}
}
if (Test-Path $TargetFile) {
$TargetFileBackup = "$TargetFile-previous"
if (Test-Path $TargetFileBackup) {
Remove-Item $TargetFileBackup -Force
}
Move-Item $TargetFile "$TargetFile-previous"
}
if ($PSCmdlet.ShouldProcess($repo, "Creating bundle '$TargetFile'")) {
Invoke-Git fetch --all
if ($LastDays -gt 0) {
$since = "--since=$LastDays.days.ago"
} else {
$since = ""
}
$maxcount = 20
$count = @(Invoke-Git log "$since" --all --pretty=oneline "-$maxcount").Count
if ($count -eq 0) {
Write-Warning "Skipping repository $repo because no commits are present"
} else {
if ($count -ge $maxcount) {
Write-Verbose "Repository $repo contains at least $count commits"
} else {
Write-Verbose "Repository $repo contains $count commits"
}
Invoke-Git bundle create "$TargetFile" "$since" --all
}
}
popd
}
END {
}
param
(
[Parameter(Mandatory = $False)]
[int]$LastDays = 0
)
[string[]]$repositories = @(
'C:\SourceGIT\ContactCentre'
'C:\SourceGIT\ContactCentre2'
'C:\SourceGIT\ContactCentre3'
'C:\SourceGIT\CTArchitect'
'C:\SourceGIT\CTArchitect2'
'C:\SourceGIT\CTArchitect3'
'C:\SourceGIT\CTScript'
'C:\SourceGIT\CTScript2'
'C:\SourceGIT\Internal'
'C:\SourceGIT\Projects'
'C:\SourceGIT\Utility'
)
$target = "U:\SourceGIT-$env:COMPUTERNAME"
cp C:\SourceGIT\backup.ps1 $target
$repositories | . "C:\SourceGIT\Internal\UsefulScripts\Backup-GitRepository.ps1" -Target $target -LastDays $LastDays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment