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)