19

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.
NealWalters
  • 17,197
  • 42
  • 141
  • 251
  • 1
    I'm experimenting with the --Quiet flag, which might work, but not sure why I'm getting any errors that need to be suppressed with that flag. – NealWalters Jan 25 '19 at 21:00

1 Answers1

10

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"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    This is an environmental variable? From Win command prompt I tried: set GIT_REDIRECT_STDERR=2>&1, then typed in set and didn't see the variable created. I also tried in Powershell, it says: + set GIT_REDIRECT_STDERR=2>&1 + ~ The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string. My Git Version: 2.16.1.windows.1 – NealWalters Feb 11 '19 at 21:39
  • 1
    @NealWalters Yes it is: mode details in https://stackoverflow.com/a/47232450/6309 and https://github.com/git/git/commit/1a172e4ac1719a068c76384bd077ee65d915ebea: `GIT_REDIRECT_STDERR="2>&1"` – VonC Feb 11 '19 at 21:59
  • 1
    I ended up using the wrapper function Invoke-Git from the link above: https://stackoverflow.com/questions/34820975/git-clone-redirect-stderr-to-stdout-but-keep-errors-being-written-to-stderr/47232450#47232450 – NealWalters Feb 15 '19 at 15:33
  • 1
    @NealWalters Great! I have included your comment in the answer for more visibility. – VonC Feb 15 '19 at 16:27