Skip to content

Instantly share code, notes, and snippets.

@philippdolder
Last active August 29, 2015 14:10
Show Gist options
  • Save philippdolder/add59168884bafca48a3 to your computer and use it in GitHub Desktop.
Save philippdolder/add59168884bafca48a3 to your computer and use it in GitHub Desktop.
git-tfs workflow
function Tfs-UpdateFeature {
[CmdletBinding()]
param ()
DynamicParam {
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$FeatureParameter = New-DynamicParameter -ParameterName "Feature" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Position 0 -Aliases @("b","f") -Mandatory
$BranchParameter = New-DynamicParameter -ParameterName "MainBranch" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Position 1
$RuntimeParameterDictionary.Add("Feature", $FeatureParameter)
$RuntimeParameterDictionary.Add("MainBranch", $BranchParameter)
$RuntimeParameterDictionary
}
begin {
$Feature = $PsBoundParameters["Feature"]
$MainBrach = $PsBoundParameters["MainBranch"]
# set default value
if (!$MainBrach) {
$MainBrach = "master"
}
}
process {
$shouldStash = git status --porcelain
if ($shouldStash) {
git stash save -u
}
git checkout $MainBrach
git tfs pull
git checkout $Feature
git rebase $MainBrach
if ($shouldStash) {
git stash pop
}
}
}
function Tfs-StartFeature {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[string] $Feature,
[parameter(Mandatory=$false)]
[string] $MainBranch = "master"
)
git checkout $MainBranch
git tfs pull
git checkout -b $Feature
}
function Tfs-CommitFeature {
[CmdletBinding()]
param ()
DynamicParam {
New-SingleDynamicParameter -ParameterName "Feature" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Aliases "b"
}
begin {
$Feature = $PsBoundParameters["Feature"]
}
process {
if ($Feature) {
git checkout $Feature
}
git tfs checkintool
}
}
function Tfs-RemoveFeature {
[CmdletBinding()]
param ()
DynamicParam {
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$FeatureParameter = New-DynamicParameter -ParameterName "Feature" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Position 0 -Aliases @("b","f") -Mandatory
$BranchParameter = New-DynamicParameter -ParameterName "MainBranch" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Position 1
$RuntimeParameterDictionary.Add("Feature", $FeatureParameter)
$RuntimeParameterDictionary.Add("MainBranch", $BranchParameter)
$RuntimeParameterDictionary
}
begin {
$Feature = $PsBoundParameters["Feature"]
$MainBrach = $PsBoundParameters["MainBranch"]
# set default value
if (!$MainBrach) {
$MainBrach = "master"
}
}
process {
git checkout $MainBrach
git branch -D $Feature
}
}
function BranchExists {
param (
[parameter(Mandatory=$true)]
[string] $branch)
$branches = (git branch --list) | ForEach-Object { $_.Substring(2) }
return $branches.Contains($branch)
}
function Tfs-ValidateBranch {
[CmdletBinding()]
param ()
DynamicParam {
# Set the dynamic parameters' name
$ParameterName = 'Branch'
# Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
$AliasAttribute = New-Object System.Management.Automation.AliasAttribute("b")
$AttributeCollection.Add($AliasAttribute)
# Generate and set the ValidateSet
$arrSet = (git branch --list) | ForEach-Object { $_.Substring(2) }
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin {
# Bind the parameter to a friendly variable
$Branch = $PsBoundParameters[$ParameterName]
}
process {
# Your code goes here
Write-Host "yeah $Branch exists"
}
}
function Tfs-Validate-Branch2 {
[CmdletBinding()]
param ()
DynamicParam {
New-DynamicParameter -ParameterName "Branch" -AllowedValues {(git branch --list) | ForEach-Object { $_.Substring(2) }} -Aliases "b"
}
begin {
$Branch = $PsBoundParameters["Branch"]
}
process {
# Your code goes here
Write-Host "yeah $Branch exists"
}
}
function New-SingleDynamicParameter {
param(
[parameter(Mandatory=$true, Position=0)]
[string] $ParameterName,
[parameter(Mandatory=$true, Position=1)]
[scriptblock] $AllowedValues,
[parameter(Mandatory=$false, Position=2)]
[string] $Position = 0,
[parameter(Mandatory=$false, Position=3, ValueFromRemainingArguments=$true)]
[string[]] $Aliases,
[switch] $Mandatory
)
# Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $Mandatory
$ParameterAttribute.Position = $Position
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
if ($Aliases) {
$AliasAttribute = New-Object System.Management.Automation.AliasAttribute($Aliases)
$AttributeCollection.Add($AliasAttribute)
}
# Set the ValidateSet
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute(Invoke-Command $AllowedValues)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
$RuntimeParameterDictionary
}
function New-DynamicParameter {
param(
[parameter(Mandatory=$true, Position=0)]
[string] $ParameterName,
[parameter(Mandatory=$true, Position=1)]
[scriptblock] $AllowedValues,
[parameter(Mandatory=$false, Position=2)]
[string] $Position = 0,
[parameter(Mandatory=$false, Position=3, ValueFromRemainingArguments=$true)]
[string[]] $Aliases,
[switch] $Mandatory
)
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $Mandatory
$ParameterAttribute.Position = $Position
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
if ($Aliases) {
$AliasAttribute = New-Object System.Management.Automation.AliasAttribute($Aliases)
$AttributeCollection.Add($AliasAttribute)
}
# Set the ValidateSet
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute(Invoke-Command $AllowedValues)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameter
}
@philippdolder
Copy link
Author

you need PowerShell 3.0 for the Dynamic Parameters to work (http://www.microsoft.com/en-us/download/details.aspx?id=40855)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment