Question

I have a task running a mercurial command that fails because mercurial is not returning 0 but 1.

When mercurial returns 1 does not mean it's an error but there is nothing to do. According to the documentation:

Returns 0 if push was successful, 1 if nothing to push.

How can I handle the return in my task so when the hg command returns 0 or 1 it won't break.

Here is the failing function

Function Invoke-HgBookmark([string]$bookmarkname)
{
    $repo = GetCredentials
    & $hg.file bookmark -f $bookmarkname
    Invoke-Expression "$($hg.file) push -B $bookmarkname $repo"
    if ($LASTEXITCODE -eq 0 -Or $LASTEXITCODE -eq 1)
    {
        return 0
    }
    else
    {
        return -1
    }
}

Psake is calling my function with this code:

try {
    $global:lastexitcode = 0                 
    & $cmd
   if ($lastexitcode -ne 0) {
        throw ("Exec: " + $errorMessage)
    }
    break
}
Was it helpful?

Solution

In the function, set $global:LastExitCode to the desired value before it exits. You have to use the global: prefix in order to modify the global variable. Otherwise PowerShell does a "copy-on-write" operation and modifies a variable with the same name that is local to the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top