1

I am using the below code to git commit the change and return the commitid

function gitcommit()
{

   git commit -a -m "message"
   $commitid= git rev-parse HEAD
   git push -q
  $comid=$commitid
   return $comid
}

function main()
{

  $commitid= gitcommit() 
  Write-Host "Commit id is $commitid"


}

In the gitcommit function I am to get the correct commit id as 7de234567f68fa8a3b40a95abc4d6d82a75d93. But the problem I am facing is that, while returning the commit id as a string it is coming as a type System. Array with the below content

 On branch master, Your branch is up to date with 'origin/master'., , nothing to commit, working tree clean, On branch master, Your branch is up to date with 'origin/master'., , nothing to commit, working tree clean,7de234567f68fa8a3b40a95abc4d6d82a75d93
codewario
  • 19,553
  • 20
  • 90
  • 159
mystack
  • 4,910
  • 10
  • 44
  • 75

1 Answers1

3

There are a few things going on here:

  1. Your function is returning an array of objects because you aren't suppressing the output of some of your commands. Anything written to the output stream is passed and returned along the pipeline.
  2. The return keyword is not required. In PowerShell, return functions more like Write-Output $variable; break - it writes the variable to the output stream and then returns to the parent scope. This is not required unless you want to stop execution of the function before the rest of the code runs.
  3. To prevent your git commit command from saturating the pipeline, pipe the command output to Out-Null, Write-Host, or one of the other streams. I've written an answer here that goes into great detail about redirection and output streams. Alternatively, you can use the -q switch with git push and git commit to suppress their output, but I would recommend instead to pipe the output to the Verbose stream or Information stream if you want to still see this information, but not act on it programmatically.
  4. This isn't causing a problem for you here, but don't invoke PowerShell functions with parentheses (). Yes, use parentheses with static or instance methods, but using them with functions ends up evaluating as a subexpression instead, which will effectively combine multiple arguments into a single one.
  5. Adding the [CmdletBinding()] attribute to your function definition allows you to invoke automatic parameters on your cmdlet. For example, gitcommit -Verbose will enable the verbose stream for that execution of gitcommit, and you would be able to see the otherwise verbose output.
  6. Consider checking $LASTEXITCODE -eq 0 after each git command - the command isn't guaranteed to succeed and you may want to output something different in that case.

Taking the above points into account, your code can be improved like so:

function gitcommit
{
   [CmdletBinding()] 
   git commit -a -m "message" | Write-Verbose
   if( $LASTEXITCODE -ne 0 ){
     Write-Warning "``git commit`` failed with exit code ${LASTEXITCODE}"
     return # Return now because the commit failed
   }

   git rev-parse HEAD # This does not need to be returned because it outputs to the output stream
   git push | Write-Verbose
   if( $LASTEXITCODE -ne 0 ){
     Write-Warning "``git push`` failed with exit code ${LASTEXITCODE}"
   }
}

function main()
{

  $commitid= gitcommit
  Write-Host "Commit id is $commitid"
}
codewario
  • 19,553
  • 20
  • 90
  • 159