我如何在提交时将Git提交ID填充到文件中? [英] How can I populate the Git commit ID into a file when I commit?

查看:209
本文介绍了我如何在提交时将Git提交ID填充到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Git钩子,它将填充我将在源代码中创建的文件(基本上是变量替换)的提交ID。这可能与Git?或者事实上,通过将变量解析为git id,我将改变sha 1,从而以鸡或鸡蛋问题结束。

解决方案

我曾用于类似情况的解决方案是这样的:


  1. 将字符串 $ Id $ 放在您想识别的文件的某个位置(例如 test.html <
  2. 在您的 .gitattributes 中),可能位于文件的注释或其他非功能部分中, / code>,使用 ident 关键字(例如 *。html ident )标记相关文件。

结果是,当 git checkout 将文件复制出来将对象数据库放到工作目录中,它扩展 $ Id $ 字符串以读取 $ Id:< sha-1 of file> $ git add 会在您想检入时反转该转换,所以对象数据库中该文件的版本只包含 $ Id $ ,而不是展开的表单。



这是一个开始,但不幸只要找到包含具有特定散列的文件的提交并不那么容易,也不一定是一对一的。因此,我还使用 export-subst 属性标记这些文件(例如 *。html ident export-subst in .gitattributes ),然后添加一个额外的字符串,如 $格式:%ci $($格式:%h $)
$ git checkout 和 git add <$ c>> / code>不会影响这些标签,因此,我的存储库中的版本始终具有该字符串。为了扩展这些标签,你必须使用 git archive 来创建一个特定版本的项目的tar-ball(或.zip),然后使用它要部署该版本 - 您将无法复制文件,或 make install 或其他任何内容,因为 git archive

我给出的两个标签展开为 YYYY-MM-DD HH: MM:SS + TZOFFSET(HASH),其中 HASH 在这种情况下是实际提交哈希,所以它更有用。



您可以在 git log $格式:$ 说明符c>帮助页面 - 漂亮格式说明符。


I would like to create Git hook(s) that will populate the commit id of the commit I am about to make into a file (basically variable substitution) in my source code. Is this possible with Git? Or is the fact that by resolving the variable to the git id, I am going to be changing the sha 1, thereby winding up with a "chicken or the egg" problem.

解决方案

The solution I have used for a similar situation is this:

  1. Put the string $Id$ somewhere in the file you want to have identified (e.g. test.html), probably within a comment or other non-functional section of the file where it won't cause issues.
  2. In your .gitattributes, flag the file in question with the ident keyword (e.g. *.html ident).

The result of this is that when git checkout copies the file out of the object database into your working directory, it expands the $Id$ string to read $Id: <sha-1 of file>$, and git add reverses that transformation when you want to check it in, so the versions of that file in your object database only ever contain $Id$, not the expanded forms.

That's a start, but unfortunately, finding the commit that contains a file with a specific hash is not so easy, and not necessarily one-to-one either. So, in addition, I also tag those files with the export-subst attribute (e.g. *.html ident export-subst in .gitattributes), and add an additional string, like $Format:%ci$ ($Format:%h$) somewhere in the file as well.

git checkout and git add don't affect these tags, though, so the versions in my repository always have exactly that string. In order to get those tags expanded, you have to use git archive to create a tar-ball (or .zip) of a specific version of your project, which you then use to deploy that version - you won't be able to just copy the files, or make install or whatever, since git archive is the only thing that will expand those tags.

The two tags I gave as an example expand to YYYY-MM-DD HH:MM:SS +TZOFFSET (HASH), where the HASH in this case is the actual commit hash, so it's more useful.

You can find other potentially usefull $Format:$ specifiers in the git log help page under the --pretty-format specifiers.

这篇关于我如何在提交时将Git提交ID填充到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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