【问题标题】:Running "Git-Clone" from Powershell giving errors even when it seems to work从 Powershell 运行“Git-Clone”,即使它似乎可以工作,也会出现错误
【发布时间】:2019-01-25 20:52:22
【问题描述】:

我如何解释这个调用“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.

【问题讨论】:

  • 我正在尝试使用 --Quiet 标志,它可能会起作用,但不确定为什么我会收到任何需要使用该标志抑制的错误。

标签: git powershell-4.0


【解决方案1】:

正如我在“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

【讨论】:

  • 这是一个环境变量?从 Win 命令提示符我尝试:设置 GIT_REDIRECT_STDERR=2>&1,然后输入 set 并没有看到创建的变量。我也在 Powershell 中尝试过,它说: + set GIT_REDIRECT_STDERR=2>&1 + ~ 不允许使用与号 (&) 字符。 & 运算符保留供将来使用;用双引号 ("&") 将 & 号括起来以将其作为字符串的一部分传递。我的 Git 版本:2.16.1.windows.1
  • @NealWalters 是的:stackoverflow.com/a/47232450/6309github.com/git/git/commit/… 中的模式详细信息:GIT_REDIRECT_STDERR="2>&1"
  • 我最终使用了上面链接中的包装函数 Invoke-Git:stackoverflow.com/questions/34820975/…
  • @NealWalters 太棒了!我已将您的评论包含在答案中以提高知名度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多