可以包含在构建中以实现可追溯性的“svn 信息”的 Git 替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:31:31  来源:igfitidea点击:

Git alternatives to "svn info" that can be included in a build for traceability?

git

提问by Johan

I'm looking for a Git alternatives to "svn info".

我正在寻找“svn info”的 Git 替代品。

Today I added some information that Subversion gives me with the "svn info" command right into my build, and that is then pushed into a source file that prints this during startup. That way I always know where that build came from and how to get it back again.

今天,我在我的构建中添加了 Subversion 通过“svn info”命令给我的一些信息,然后将这些信息推送到一个源文件中,该文件在启动期间打印出来。这样我总是知道构建来自哪里以及如何重新获取它。

If you have "svn info" like URL, Repository Root, Repository UUID and the Revision, you have a good link between what is deployed and the buildsystem. And if someone reports a bug you know where that software came from, and since that information was automatically included, the risk of human error is smaller.

如果你有像 URL、Repository Root、Repository UUID 和修订版这样的“svn 信息”,那么你在部署的内容和构建系统之间有一个很好的链接。如果有人报告错误,您就知道该软件的来源,并且由于该信息是自动包含的,因此人为错误的风险较小。

Now the question is, what information do I need to get from Git so I can later identify where that build came from? And how do I use that information to switch back to exactly that version?

现在的问题是,我需要从 Git 获取哪些信息,以便稍后确定构建的来源?我如何使用该信息切换回该版本?

(Maybe I need to add some information about the "build computer" as well since Git is distributed.)

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



Update: Using rev-parse was really useful, and I got something like this:

更新:使用 rev-parse 真的很有用,我得到了这样的东西:

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

And with that magic number it is later possible to do:

有了这个神奇的数字,以后可以这样做:

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

And I am back where I was.

而我又回到了原来的地方。



Update: I think that if I take some parts from that scripts VonC provided and put them into my buildfile I will get the result I was looking for.

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



Update:

更新

A note on "git describe". You need a real tag (tag -a) earlier in you branch history to make this work or you will get something like this.

关于“git describe”的注释。您需要在分支历史记录的早期使用一个真正的标签 (tag -a) 才能完成这项工作,否则您将得到类似的结果。

fatal: cannot describe '72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8'

The problem is also described in Git Tag Does the Wrong Thing by Default.

该问题也在Git Tag 默认情况下执行错误的事情中有所描述。

But please note that a checkout seems to work anyway, even though that was an error message.

但请注意,结帐似乎仍然有效,即使这是一条错误消息。

git checkout 72ce5f3e13c61f76fde5c58cefc85eed91b6f1f8

The normal thing though seems to be that you create something like a "ver1.0" tag, and then if you continue to work you get something like this:

正常的事情似乎是你创建了一个类似“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

So when you use describecorrectly it does work and may produce a more human-readable results, and it can be really useful as well.

因此,当您describe正确使用它时,它确实有效,并且可能会产生更易读的结果,而且它也非常有用。

采纳答案by VonC

To complete Charles's answer, you also can make a script displaying "sn info" like information, like this one(already mentioned there)

要完成查尔斯的回答,您还可以制作一个显示“sn info”之类的信息的脚本,就像这个(已经在那里提到

#!/bin/bash

# author: Duane Johnson
# email: [email protected]
# 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 --no-pager log  -n1
  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

Which would produce something like:

这会产生类似的东西:

== Remote URL: origin [email protected]: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 = [email protected]:canadaduane/my-project.git
  fetch = refs/heads/*:refs/remotes/origin/*
[github]
  user = canadaduane
  repo = my-project

== Most Recent Commit
commit b47dce8b4102faf1cedc8aa3554cb58d76e0cbc1
Author: Duane Johnson <[email protected]>
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.

回答by Webghost

I know the answer is already accepted but this may help someone who is looking for remote and branch information.

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

 git remote show origin

回答by CB Bailey

In git, the commit id is unique in a project even across distibuted code. You can also checkout a commit id, so if you want an identifier than will enable you to get back to the state of the code that generated a build you just need the commit id.

在 git 中,即使跨分布式代码,提交 ID 在项目中也是唯一的。您还可以检出提交 id,因此如果您想要一个标识符而不是使您能够返回生成构建的代码状态,您只需要提交 id。

git rev-parse HEAD

Of course, you probably want to be sure that there aren't any pending changes in the working tree or index so you might want to check that there's no output to something like this:

当然,您可能希望确保工作树或索引中没有任何挂起的更改,因此您可能需要检查是否没有类似以下内容的输出:

git diff --name-status HEAD

or just use the exit code of:

或仅使用以下退出代码:

git diff --quiet HEAD

The only things that you might want to record about the build machine are environmental factors such as tool chain versions and what state any tools that didn't come from the repository were in.

您可能想要记录的关于构建机器的唯一事情是环境因素,例如工具链版本以及不是来自存储库的任何工具所处的状态。

If you have a central master repository you could record the url of that, although as the commit id is unique across all clones of the project it's not critical information for identifying the commit.

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

回答by Nandan A

You can get the remote info like we get in 'svn info' by:

您可以通过以下方式获取远程信息,就像我们在“svn info”中获取的信息一样:

git remote -v

回答by Dustin

git describe

is all you need. Just make sure you've created at least one (proper) tag.

是你所需要的全部。只要确保您已经创建了至少一个(正确的)标签。

回答by Jakub Nar?bski

I don't know if this is what you want, but if you want to embed some kind of version info during build time, tag your point releases, and take a look how Git itself does it (Linux kernel uses the same mechanism) using Makefileand GIT-VERSION-GENscript (both links are to gitweb at repo.or.cz).

我不知道这是否是你想要的,但是如果你想在构建时嵌入某种版本信息,标记你的点发布,并看看 Git 本身是如何做到的(Linux 内核使用相同的机制)使用MakefileGIT-VERSION-GEN脚本(两个链接都指向 repo.or.cz 上的 gitweb)

GIT-VERSION-GEN in turn uses git-describe.

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

回答by Jonathan Fischer

Here's gitinfo.ps1 (or Get-GitInfo.ps1 for the purists), a PowerShell version of Duane Johnson's shell script:

这是 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 "[A-Z]:\$" )
  {
    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

回答by hkaraoglu

You can create an aliasin git.

您可以在git 中创建别名

git config --global alias.info 'rev-parse HEAD'

Then you can use same command in git,

然后你可以在git中使用相同的命令,

git info