adr*_*nne 5 windows git shell powershell scripting
我正在尝试创建别名,git commit并将消息记录到单独的文本文件中.但是,如果git commit返回"nothing to commit (working directory clean)",则不应将任何内容记录到单独的文件中.
这是我的代码.该git commit别名作品; 输出到文件的工作原理.但是,无论返回什么内容,它都会记录消息git commit.
function git-commit-and-log($msg)
{
$q = git commit -a -m $msg
$q
if ($q –notcontains "nothing to commit") {
$msg | Out-File w:\log.txt -Append
}
}
Set-Alias -Name gcomm -Value git-commit-and-log
Run Code Online (Sandbox Code Playgroud)
我正在使用PowerShell 3.
$q包含Git的标准输出的每一行的字符串数组.要使用-notcontains你需要匹配数组中项目的完整字符串,例如:
$q -notcontains "nothing to commit, working directory clean"
Run Code Online (Sandbox Code Playgroud)
如果要测试部分字符串匹配,请尝试-match运算符.(注意 - 它使用正则表达式并返回匹配的字符串.)
$q -match "nothing to commit"
Run Code Online (Sandbox Code Playgroud)
-match如果左操作数是一个数组,它将工作.所以你可以使用这个逻辑:
if (-not ($q -match "nothing to commit")) {
"there was something to commit.."
}
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用-like/ -notlike运算符.这些接受通配符并且不使用正则表达式.将返回匹配(或不匹配)的数组项.所以你也可以使用这个逻辑:
if (-not ($q -like "nothing to commit*")) {
"there was something to commit.."
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20105 次 |
| 最近记录: |