Git Powershell dynamic<;选项卡>;完成

Git Powershell dynamic<;选项卡>;完成,git,powershell,Git,Powershell,^在投票结束之前,请理解上述问题的“答案”是使用另一个模块,而不是如何自己实现它 我有一组powershell模块,用来帮助我处理git工作流(想想) 这里有一个例子 Function Merge-FromBranch([string] $branch){ # ... } Set-Alias mfb Merge-FromBranch 我希望能够使用回购协议中存在的分支完成分支名称的制表符 现在获取本地分支名称很容易 > git branch 但我需要做的是 > mfb

^在投票结束之前,请理解上述问题的“答案”是使用另一个模块,而不是如何自己实现它

我有一组powershell模块,用来帮助我处理git工作流(想想)

这里有一个例子

Function Merge-FromBranch([string] $branch){
    # ...
}

Set-Alias mfb Merge-FromBranch
我希望能够使用回购协议中存在的分支完成分支名称的制表符

现在获取本地分支名称很容易

> git branch
但我需要做的是

> mfb fea<tab>
mfb有限元分析 以通过要素分支进行制表

如何将其连接起来,以便在模块上完成
$branch
参数


我知道我可以通过管道将
git分支
的输出传输到另一个方法(
git分支| do stuff
),但我不知道如何将其集成到模块的制表符完成中。

我不熟悉
git分支
及其输出内容。我希望它只能是分支名称,每行一个值,没有任何标题等。如果不是,那么您需要修改示例以首先清理它

如果我相信,这对PS3+应该有效:

function Merge-FromBranch {

[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)

            # Generate and set the ValidateSet 
            $arrSet = & git branch
            $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 { Write-Host "Chosen Branch is: $Branch" }

}

示例修改自:

为了与posh git的做法保持一致,并创建了自己的
TabExpansion
方法,我现在为自定义命令提供了适当的制表符完成

#备份现有的TabExpansion,这将允许我们扩展它而不是替换它。
复制函数:\tab扩展函数:\OriginalTabExpansion
#创建我们自己的自定义扩展方法
函数选项卡扩展($line,$lastWord){
$LineBlocks=[regex]::拆分($line,[|;]'))
$lastBlock=$LineBlocks[-1]
开关-正则表达式($lastBlock){
#取决于时髦的Git
“^$(获取别名模式“mfb | mtb | rb | db | ub | urb | co | pull”)(.*”{gittab扩展$lastBlock}
默认值{if(测试路径函数:\OriginalTabExpansion){OriginalTabExpansion$line$lastWord}
}
}
函数扩展($lastBlock){
开关-正则表达式($lastBlock){
“(?\S*)$”{gitbranks$匹配项['cmd']$true}
}   
}
函数脚本:GitBranchs($filter,$includeHEAD=$false){
$prefix=$null
if($filter-match“^(?\S*\.{2,3})(?*”){
$prefix=$matches['from']
$filter=$matches['to']
}
$branchs=@(git branch--no color | foreach{if($\-match“^\*?\s*(?*)){$matches['ref']})+
@(git分支——无颜色-r | foreach{if($|-match“^(?\S+(:->.+)?”){$matches['ref']})+
@(如果($includeHEAD){‘HEAD’、‘FETCH_HEAD’、‘ORIG_HEAD’、‘MERGE_HEAD’})
$Branchs|
其中{$\-ne'(无分支)'-和$\-like“$filter*”}|
foreach{$prefix+$\}
}

您不使用或我误解了您的需求有什么原因吗?我们也使用posh git,但我们的自定义命令添加了更多的附加功能。如果分支集是静态的,您可以在参数定义中使用ValidateSet(),如果我没记错的话,它应该为您提供制表符完成功能。但是我不知道如果你想让它动态的话,是否有可能在运行中创建一个场景。编辑:也许这个链接有帮助:posh git通过。我现在只是想让它适应我的需要。DynamicParam是我研究过的东西,但它肯定会导致很多臃肿的模块。经过相当多的挖掘,我想出了
TabExpansion
的东西,它被烘焙到Powershell中。我基本上是剽窃了在豪华吉托基已经做过的事情只要你没有100个这样的参数,我就不会认为它们臃肿,但这只是我的观点。要保持PowerShell ISE中的模块干净,您可以使用类似以下的区域
#region dynamicRAM-分支此代码#endregion
。那真是太棒了!但我有一个问题:当将它放入自定义函数文件时,它似乎不知道函数:\TabExpansion。为此,我决定将它放在posh git的GitTabExpansion.ps1的末尾。我无法想象这是正确的方法,所以如果你也有答案,那就太好了:)@x_mtd你是对的,因为这不是正确的方法。您希望它位于导入的模块中。我们怎么做。