在Powershell中返回Git提交ID [英] Return Git commit id in Powershell

查看:140
本文介绍了在Powershell中返回Git提交ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码git commit更改并返回commitid

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"


}

gitcommit函数中,我要获取正确的提交ID为7de234567f68fa8a3b40a95abc4d6d82a75d93. 但是我面临的问题是,当以字符串形式返回提交ID时,它将以 System类型出现.包含以下内容的数组

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

推荐答案

这里发生了一些事情:

  1. 您的函数正在返回对象数组,因为您没有取消某些命令的输出. 任何写入输出流的内容都会沿着管道传递并返回.
  2. 不需要return关键字.在PowerShell中,return的功能更像Write-Output $variable; break-将变量写入输出流,然后返回到父作用域.除非您想在其余代码运行之前停止执行该功能,否则不需要这样做.
  3. 为防止您的git commit命令使管道饱和,请将命令输出通过管道传输到Out-NullWrite-Host或其他流之一. 我在这里写了一个答案,其中详细介绍了重定向和输出流.或者,您可以将-q开关与git pushgit commit一起使用来禁止它们的输出,但是如果您仍然希望看到此内容,我建议改为将输出通过管道传递到Verbose流或Information流.信息,但不能以编程方式对其进行操作.
  4. 这不会给您造成问题,但是请不要使用括号()调用PowerShell函数.是的,将括号与静态方法或实例方法一起使用,但是将括号与函数一起使用最终会作为子表达式进行评估,这将有效地将多个参数组合为一个参数.
  5. 添加 [CmdletBinding()]属性允许您在cmdlet上调用自动参数.例如,gitcommit -Verbose将为gitcommit的执行启用详细流,您将能够看到否则为详细的输出.
  6. 考虑在每个git命令之后检查$LASTEXITCODE -eq 0-该命令不能保证成功执行,在这种情况下,您可能希望输出不同的内容.
  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"
}

这篇关于在Powershell中返回Git提交ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆