我試圖做一個別名git commit
它也記錄到一個單獨的文本文件的消息。但是,如果git commit
返回"nothing to commit (working directory clean)"
,它不應該記錄任何東西到單獨的文件。字符串比較在PowerShell函數中不起作用 - 我做錯了什麼?
這是我的代碼。 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
我使用PowerShell的3
」$ q包含每行git stdout的字符串數組。「只有當git生成多行輸出時。如果git只輸出一行到stdout,那麼$ q將包含一個單一的字符串,而不是一個數組(我在我的回答中提到的東西)。 – 2013-04-28 19:34:31
從OPs git commit commit(在我的機器上嘗試它)返回多行。 – 2013-04-28 21:20:06
我不使用該工具,因此我無法對此發表評論。但是我只想指出,這個特定的答案不能籠統地用於捕獲命令行工具輸出的所有情況。 – 2013-04-28 21:35:23