PowerShell ISE在git checkout上抛出一个错误 [英] PowerShell ISE throws an error on git checkout
问题描述
在PowerShell中, git checkout
运行时没有任何错误信息。在ISE中,虽然 git checkout
剧照可以正常工作,但ISE会给出错误信息。
> git checkout master
您的分支在3次提交之前超过'origin / master'。
(使用git push发布你的本地提交)
git:切换到分支'master'
在行:1 char:1
+ git checkout master
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified :(切换到分支'master':String)[],RemoteException
+ FullyQualifiedErrorId :NativeCommandError
这不是一个主要问题,因为 git checkout code>仍然有效。不过,这很烦人,所以我想知道为什么ISE会在标准PowerShell不支持的时候抱怨,而且重要的是,我们该如何防止这种烦恼。
我已经看过为什么Powershell ISE会显示Powershell控制台没有显示错误?,这说明ISE只是显示正常shell正在经历的事情。这个答案并不能解释如何平息这种恼人的行为。
有几种方法可以避免这些错误,他们看起来或感觉自然。
第一个使用错误流重定向和一些逻辑错误:
$ out = git? 2>& 1
if($?){
$ out
} else {
$ out.Exception
}
$ c $第二个依赖于ErrorAction,它只适用于PowerShell构造,所以我们需要先构建一个:
$ b
& {
[CmdletBinding()]
param()
git?
} -ErrorAction SilentlyContinue -ErrorVariable失败
if($ fail){
$ fail.Exception
}
在我的 ISEGit 模块中,我使用后者来避免错误记录''泄漏'给最终用户。
最后,您可以'修复它'(好,排除...),确保您可以在end:
$(git?2>& 1)
或者我会投票反对,因为它会让你不知道任何实际的错误,设置全局 $ ErrorActionPreference
到 SilentlyContinue
- 虽然这与将错误流重定向到 $ null
没有区别。
In PowerShell, git checkout
runs without any error message. In the ISE, while git checkout
stills works, the ISE gives an error message.
> git checkout master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
git : Switched to branch 'master'
At line:1 char:1
+ git checkout master
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Switched to branch 'master':String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
This isn't a major problem, because git checkout
still works. It's annoying, though, so I'd like to know why the ISE complains when the standard PowerShell doesn't, and importantly, how can we prevent this annoyance.
I have looked at Why is Powershell ISE showing errors that Powershell console does not show?, which explains that the ISE is just displaying what the normal shell is experiencing. That answer does not explain how to quiet down this annoying behavior.
解决方案 There are few ways you can avoid these errors, none of them looks or feels 'natural'.
First one uses error stream redirection and some logic around errors:
$out = git ? 2>&1
if ($?) {
$out
} else {
$out.Exception
}
Second depends on the ErrorAction, that is available only for PowerShell constructs, so we need to build one first:
& {
[CmdletBinding()]
param()
git ?
} -ErrorAction SilentlyContinue -ErrorVariable fail
if ($fail) {
$fail.Exception
}
In my ISEGit module I use latter to avoid error records 'leaking' to end user in uncontrolled manner.
Finally you can 'fix it' (well, sort off...) by making sure you can a string in the end:
"$(git ? 2>&1 )"
Or something I would vote against as it will leave you unaware of any actual errors, setting global $ErrorActionPreference
to SilentlyContinue
- though this is not different from redirecting error stream to $null
.
这篇关于PowerShell ISE在git checkout上抛出一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!