从Powershell运行“Git-Clone”即使看起来有效也会出错

问题描述 投票:5回答:1

如何解释来自调用“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.
git powershell-v4.0
1个回答
2
投票

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

set GIT_REDIRECT_STDERR=2>&1

(或将该变量设置调整到您的脚本)

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

OP NealWalters添加in the comments

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

© www.soinside.com 2019 - 2024. All rights reserved.