Skip to content

Instantly share code, notes, and snippets.

@PlagueHO
Created January 6, 2020 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PlagueHO/7a5784261a2d5d5d0753e64374dc60ff to your computer and use it in GitHub Desktop.
Save PlagueHO/7a5784261a2d5d5d0753e64374dc60ff to your computer and use it in GitHub Desktop.
Restores the Git Tags for DSC Community Resource modules that were backed up when running Convert-GetTagForDsc
<#
.SYNOPSIS
Restore a set of backed up Git tags from the backup file
created by Convert-GitTagForDsc.ps1 in the $ENV:Temp folder.
.PARAMETER BackupFile
The full path to the backup file created by Convert-GitTagForDsc.ps1.
.PARAMETER Remote
The name of the Git remote repository for your fork.
.PARAMETER Upstream
The name of the Git remote repository for the upstream fork.
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param (
[Parameter(Mandatory = $true)]
[System.String]
[ValidateScript({Test-Path -Path $_})]
$BackupFile,
[Parameter()]
[System.String]
$Remote = 'origin',
[Parameter()]
[System.String]
$Upstream = 'upstream'
)
function Test-GitTag {
[CmdletBinding()]
[OutputType([System.Boolean])]
param (
[Parameter()]
[System.String]
$Tag
)
$result = & git @('tag','-l',$Tag)
return -not ([System.String]::IsNullOrWhiteSpace($result))
}
Write-Verbose -Message "Checking out master branch."
& git @('checkout','master')
Write-Verbose -Message "Pulling latest tags from $Upstream"
& git @('pull',$Upstream,'master','--tags')
if ($PSCmdlet.ShouldProcess($Remote, "Push tags to $Remote master"))
{
Write-Verbose -Message "Pushing latest tags to $Remote"
& git @('push',$Remote,'master','--tags')
}
$tagRefs = Get-Content -Path $BackupFile
foreach ($tagRef in $tagRefs)
{
$hash, $ref = $tagRef -split ' '
$tag = ($ref -split '/')[2]
Write-Verbose -Message "Begin restoring tag '$tag'."
if (Test-GitTag -Tag $tag)
{
Write-Verbose -Message "Tag '$tag' already exists so skipping restore"
}
else
{
if ($PSCmdlet.ShouldProcess($tag, "Restoring tag '$tag' against '$hash'"))
{
Write-Verbose -Message "Creating tag '$tag' against '$hash'."
& git @('tag','-a',$tag,$hash)
}
}
}
if ($PSCmdlet.ShouldProcess($Remote, "Pushing '$Remote' tags"))
{
Write-Verbose -Message "Pushing '$Remote' tags."
& git @('push',$Remote,'--tag')
}
if ($PSCmdlet.ShouldProcess($Upstream, "Pushing '$Upstream' tags"))
{
Write-Verbose -Message "Pushing '$Upstream' tags."
& git @('push',$Upstream,'--tag')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment