从Powershell运行“ Git-Clone”即使在看起来可行的情况下也会出现错误

时间:2019-01-25 20:52:23

标签: git powershell-v4.0

如何解释此调用“ Git Clone”(实际上是使用GitLab)的PowerShell脚本的错误。我可以克隆一个空目录并使其正常运行而不会出现错误吗?

$path1 = "d:\GitPath" 
$path2 = "${path1}\mycompany-mygroup-nealtestautomation01-map"
$gitLabHttp = "http://git.mycompany.com/MyGroup/nealtestscriptaddedproject.git"
$gitLabHttp = "http://git.mycompany.com/MyGroup/mycompany-gwcustomers-nealtestautomation01-map.git" 
rd $path2 
cd $path1
git clone $gitLabHttp --local --verbose

输出:

git : Cloning into 'mycompany-mygroup-nealtestautomation01210-map'...
At D:\Scripts\GitCloneTest.ps1:7 char:1
+ git clone $gitLabHttp --local --verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cloning into '...mation01-map'...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

warning: --local is ignored
warning: You appear to have cloned an empty repository.

在我要使用此代码的较大脚本中,都包含以下内容:

$ErrorActionPreference = "Stop"  #Special Poweshell Syntax to Stop on First Error 

,以便在出现第一个错误时停止。

即使我在非空项目上运行它,我也看到红色和类似于上面的错误(取消此运行的--local和--verbose):

git : Cloning into 'xxx.yyy.CanInvToOutZZZ210.Map'...
At D:\Scripts\GitCloneTest2.ps1:5 char:1
+ git clone $gitLabHttp
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cloning into 'E...Intl210.Map'...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

如果我是从Windows命令提示符运行的,则它的某些统计信息运行得很好:

D:\GitGWCustomerMaps\Test2>git clone myrealurlhidden 
Cloning into 'XXX.YYY.CanInvToOutZZZZ210.Map'...
remote: Counting objects: 29, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 29 (delta 9), reused 0 (delta 0)
Unpacking objects: 100% (29/29), done.

1 个答案:

答案 0 :(得分:2)

正如我在“ PowerShell Capture Git Output”中提到的那样,由于Git命令可以在stderr(instead of stdout)上输出信息消息,请尝试(使用Gti 2.16 +)

set GIT_REDIRECT_STDERR=2>&1

(或使该变量设置适应您的脚本)

这样可以避免您的脚本在第一个“错误”上停止,而实际上这并不是一个错误

OP NealWalters添加in the comments

  

我最终使用了“ Git clone: Redirect stderr to stdout but keep errors being written to stderr”中的包装函数 Invoke-Git