Search code examples
gitpowershell-4.0

Running "Git-Clone" from Powershell giving errors even when it seems to work


How do I interpret the errors coming out of this PowerShell script that calls "Git Clone" (actually using GitLab). And can I clone an empty directory and cause it to work without getting errors?

$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

Output:

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.

In the larger script where I want to use this code, I include this:

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

so that it stops on the first error.

Even when I run it on a non-empty project, I see red and errors similar to above (took the --local and --verbose off this run):

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

If I run from windows command prompt, it runs nicely with some stats:

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.

Solution

  • As I mention in "PowerShell Capture Git Output", since a Git command can output information message on stderr (instead of stdout), try (with Gti 2.16+)

    set GIT_REDIRECT_STDERR=2>&1
    

    (or adapt that variable setting to your script)

    That might avoid your script to stop on the first "error", which is actually not an error

    The OP NealWalters adds in the comments:

    I ended up using the wrapper function Invoke-Git from "Git clone: Redirect stderr to stdout but keep errors being written to stderr"