git“svn info”的替代品可以包含在构build中以便追溯?

我正在寻找一个“svn info”的git替代品。

今天,我添加一些信息,SubVersion给我的“svn info”命令直接到我的版本,然后被推入到一个源文件,在启动时打印这个。 这样,我总是知道这个构build来自哪里以及如何重新获得它。

如果你有像URL,Repository Root,Repository UUID和Revision这样的“svn info”,你就可以在部署和构build系统之间build立良好的联系。 如果有人报告了一个错误,那么你知道这个软件来自哪里,并且由于这些信息被自动包含在内,所以人为错误的风险更小。

现在的问题是,我需要从git获得哪些信息,以便稍后确定构build的来源? 而且我如何使用这些信息切换回到那个版本?

(也许我需要添加一些关于“构build计算机”的信息,因为git是分布式的。)

感谢Johan


更新 :使用rev-parse是非常有用的,我有这样的东西:

cj@zap:~/git_test$ git rev-parse HEAD 72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8 

而随着这个神奇的数字,以后可以做

 cj@zap:~/git_test$ git checkout 72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8 

而我又回到了我所在的地方。


更新 :我认为,如果我从VonC提供的脚本中取出一些部分,并将它们放到我的构build文件中,我将得到我正在寻找的结果。


更新 :关于“git describe”的注释,在分支历史logging的早些时候需要一个真正的标签(标签-a)来完成这个工作,否则你会得到类似的东西。

 fatal: cannot describe '72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8' 

这个问题也在这里描述http://www.rockstarprogrammer.org/post/2008/oct/16/git-tag-does-wrong-thing-default/

但请注意,结账似乎无论如何,即使你是一个错误消息。

 git checkout 72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8 

正常情况下,你似乎创build了一个“ver1.0”标签,然后如果你继续工作,你会得到这样的东西:

 cj@zap:~/git_test$ git describe ver1.0-2-g4c7a057 cj@zap:~/git_test$ git tag -a ver2.0 cj@zap:~/git_test$ git describe ver2.0 cj@zap:~/git_test$ git commit . -m "something..." Created commit ac38a9d: something... 1 files changed, 1 insertions(+), 0 deletions(-) cj@zap:~/git_test$ git describe ver2.0-1-gac38a9d 

所以当你正确地使用描述的时候,它确实有效,并且可能会产生更多的人类可读的结果,而且可以是非常有用的。

感谢Johan

为了完成Charles的回答,你也可以制作一个脚本来显示“sn info”这样的信息,就像这个 (已经提到过那样 )

 #!/bin/bash # author: Duane Johnson # email: duane.johnson@gmail.com # date: 2008 Jun 12 # license: MIT # # Based on discussion at http://kerneltrap.org/mailarchive/git/2007/11/12/406496 pushd . >/dev/null # Find base of git directory while [ ! -d .git ] && [ ! `pwd` = "/" ]; do cd ..; done # Show various information about this git directory if [ -d .git ]; then echo "== Remote URL: `git remote -v`" echo "== Remote Branches: " git branch -r echo echo "== Local Branches:" git branch echo echo "== Configuration (.git/config)" cat .git/config echo echo "== Most Recent Commit" git log --max-count=1 echo echo "Type 'git log' for more commits, or 'git show' for full commit details." else echo "Not a git repository." fi popd >/dev/null 

这会产生像这样的东西:

 == Remote URL: origin git@github.com:canadaduane/my-project.git == Remote Branches: origin/work trunk trunk@1309 trunk@2570 trunk@8 == Local Branches: master * work == Configuration (.git/config) [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [svn-remote "svn"] url = svn+ssh://svn.my-project.com/srv/svn fetch = my-project/trunk:refs/remotes/trunk [remote "origin"] url = git@github.com:canadaduane/my-project.git fetch = refs/heads/*:refs/remotes/origin/* [github] user = canadaduane repo = my-project == Most Recent Commit commit b47dce8b4102faf1cedc8aa3554cb58d76e0cbc1 Author: Duane Johnson <duane.johnson@gmail.com> Date: Wed Jun 11 17:00:33 2008 -0600 Added changes to database schema that will allow decentralization from content pointers table type 'git log' for more, or 'git show' for full commit details. 

我知道答案已经被接受,但这可能有助于正在寻找远程和分支信息的人。

  git remote show origin 

在git中,提交id在一个项目中是独一无二的,甚至在整个distibuted的代码中也是如此。 你也可以签出一个提交ID,所以如果你想要一个标识符,而不是让你回到生成一个构build的代码的状态,你只需要提交ID。

 git rev-parse HEAD 

当然,你可能想要确定在工作树或索引中没有任何挂起的改变,所以你可能想检查没有输出到这样的东西:

 git diff --name-status HEAD 

或者只是使用退出代码:

 git diff --quiet HEAD 

您可能想要logging有关构build计算机的唯一的东西是环境因素,如工具链版本以及不是来自存储库的任何工具的状态。

如果您有一个中央主存储库,则可以logging该库的url,但是由于commit ID在项目的所有克隆中都是唯一的,因此它不是识别提交的关键信息。

您可以通过以下方式获得远程信息,例如我们在'svn info'中获得的信息:

 git remote -v
 git describe 

是你所需要的全部。 只要确保你已经创build了至less一个(适当的)标签。

我不知道这是否是你想要的,但是如果你想在编译期间embedded某种版本信息,请标记你的发行版本,并看看Git本身如何做(Linux内核使用相同的机制) Makefile和GIT-VERSION-GEN脚本(这两个链接都是在repo.or.cz上的gitweb)

GIT-VERSION-GEN反过来使用git-describe 。

这里是gitinfo.ps1(或纯粹主义者的Get-GitInfo.ps1),Duane Johnson的shell脚本的PowerShell版本:

 # From http://stackoverflow.com/a/924657/990504 # Duane Johnson's script translated to PowerShell by Jonathan Fischer 2015.04.25 Push-Location . # Find base of git directory while ( $true ) { if ( Test-Path -PathType container .git ) { # Show various information about this git directory Write-Output "== Remote URL: $(git remote -v)" Write-Output "`n== Remote Branches: " git branch -r Write-Output "`n== Local Branches:" git branch Write-Output "`n== Configuration (.git/config)" Get-Content .git/config Write-Output "`n== Most Recent Commit" git log --max-count=1 Write-Output "Type 'git log' for more commits, or 'git show' for full commit details." break } # Try parent directory. Set-Location .. # Stop if at root of drive. if ( (Get-Location).Path -match "[AZ]:\\$" ) { Write-Error "Not a Git repository." break } } # Note: even though the popd was not strictly necessary in the bash version # unless the script was sourced/dotted, PowerShell has the questionable behavior # where scripts modify the current directory (and environment!) of the calling # process. So we need the Pop-Location. Pop-Location