如何为 Git 存储库启用 ident 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1792838/
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 07:24:23  来源:igfitidea点击:

How do I enable the ident string for a Git repository?

gitident

提问by pean

How do I enable ident $Id$on files in a Git repository?

如何启用ident $Id$Git 存储库中的文件?

回答by Jakub Nar?bski

Summary:The recommended way of embedding version information in a product is to use the build systemfor that; see below for details and alternate approaches.

总结:在产品中嵌入版本信息的推荐方式是使用构建系统;有关详细信息和替代方法,请参见下文。



In Git (and I think usually also in other VCS systems with atomic commits) there is no such thing like version of a single file.

在 Git 中(我认为通常也在其他具有原子提交的 VCS 系统中)没有像单个文件的版本这样的东西。

Git does support on-demand expansion of $Id:$keyword, but:

Git 确实支持按需扩展$Id:$关键字,但是:

  1. It is done on requestonly. You have to specify (perhaps using globbing pattern) that a file (or a set of files) has an identattributeset (in '.gitattributes' file in tree, or in '.git/info/attributes' for local repository settings).
  2. It expands to the SHA-1 of file contents(or to be more exact to $Id:<sha-1 of blob>$). The reason for this choice is that Git does not touch files that have not changed during branch switching or rewinding; if '$Id:$' expanded to revision info it would require to update everyversion-controlled file e.g. when switching branches.
  1. 它仅应要求进行。您必须指定(可能使用通配模式)一个文件(或一组文件)具有一个ident属性集(在树中的 '.gitattributes' 文件中,或在本地存储库设置的 '.git/info/attributes' 中)。
  2. 它扩展到文件内容的 SHA-1(或更确切地说是$Id:<sha-1 of blob>$)。之所以选择这个,是因为 Git 不会去接触在分支切换或回滚过程中没有改变的文件;如果 '$Id:$' 扩展为修订信息,则需要更新每个版本控制文件,例如在切换分支时。

Git supports quite a wide set of $Format:...$placeholders which expands to commit information (e.g. $Format:%H$replaced by a commit hash) but:

Git 支持相当广泛的$Format:...$占位符集,这些占位符扩展到提交信息(例如,$Format:%H$由提交哈希替换),但是:

  1. Expansion is done only when running git archive, in its output file.
  2. It is done on request, controlled via export-substattribute.
  1. 只有在其输出文件中运行git archive才会进行扩展。
  2. 它是根据请求完成的,通过export-subst属性控制。

The recommended way of embedding version information is to do it via the build system(in a build stage); see for example Git Makefileand GIT-VERSION-GENscript used by Makefile in the Git web interface for the git.git repository.

嵌入版本信息的推荐方式是通过构建系统(在构建阶段)进行;参见 Git Makefile和Makefile 在 Git Web 界面中使用的Git MakefileGIT-VERSION-GEN脚本,用于 git.git 存储库。

You can however (ab)use a clean/smudge filter driver (via filterattribute) to get CVS-like keyword expansion, expanding keywords on checkout, and cleaning them up on entering contents to the repository.

但是,您可以(ab)使用清理/涂抹过滤器驱动程序(通过filter属性)来获得类似 CVS 的关键字扩展,在结帐时扩展关键字,并在将内容输入到存储库时清理它们。

回答by hallski

You can do this by adding a pattern for which files you want this functionality followed by identin the .gitattributesfile. This will replace $Id$ with $Id:<40-digit SHA>$ on checkout of the file. Notice though that it won't give you a revision number of the file as in CVS/SVN.

您可以通过ident.gitattributes文件中添加您希望此功能的文件的模式来实现此目的。这将在签出文件时将 $Id$ 替换为 $Id:<40-digit SHA>$。请注意,它不会像在 CVS/SVN 中那样为您提供文件的修订号。

Example:

例子:

$ echo '*.txt ident' >> .gitattributes
$ echo '$Id$' > test.txt
$ git commit -a -m "test"

$ rm test.txt
$ git checkout -- test.txt
$ cat test.txt

Link to gitattributes(5) Manual Page

链接到gitattributes(5) 手册页

回答by A Parseeker

Git's ident does not do what $Id$ does in other versioning systems. As a kludge, use RCS along with git: RCS for individual file revisions and git to checkpoint the project as a whole. As I said, this is a kludge but it does kinda make sense (sometimes for some things).

Git 的 ident 不会做 $Id$ 在其他版本控制系统中所做的事情。顺便说一句,将 RCS 与 git: RCS 一起用于单个文件修订,并使用 git 来检查整个项目。正如我所说,这是一个混杂,但它确实有点道理(有时对于某些事情)。

回答by VonC

Jakub Nar?bskisuggested in his answer(more than 10 years ago ):

Jakub Nar?bski他的回答中建议(10 多年前):

You can however (ab)use a clean/smudge filter driver (via filter attribute) to get CVS-like keyword expansion, expanding keywords on checkout, and cleaning them up on entering contents to the repository.

但是,您可以(ab)使用清洁/涂抹过滤器驱动程序(通过过滤器属性)来获得类似 CVS 的关键字扩展,在结帐时扩展关键字,并在将内容输入到存储库时清理它们。

The criticism is (by Arioch 'Thein the comments)

批评是(由Arioch '评论中

filter driver would be really great if it actually had any power.
As of know it only seems to get onlyfilename as a parameter: that is even less than identfilter, which gets the BLOB SHA1 as a parameter.

如果过滤器驱动程序真的有任何功能,它会非常棒。
据了解,它似乎只将文件名作为参数:这甚至小于ident过滤器,后者将 BLOB SHA1 作为参数

With Git 2.27 (Q2 2020), git content filters are powerless no more!

使用 Git 2.27(2020 年第二季度),git 内容过滤器不再无能为力!

Git 2.27 provides more information(e.g. the object of the tree-ish in which the blob being converted appears, in addition to its path, which has already been given) to smudge/clean conversion filters.

Git 2.27 提供了更多信息(例如,除了已经给出的路径之外,正在转换的 blob 出现的树状对象的对象)来涂抹/清理转换过滤器

See commit 0c0f8a7, commit 4cf76f6, commit 3f26785, commit dfc8cdc, commit 13e7ed6, commit c397aac, commit ab90eca(16 Mar 2020), and commit a860476(10 Mar 2020) by brian m. carlson (``).
(Merged by Junio C Hamano -- gitster--in commit 4e4baee, 27 Mar 2020)

请参阅提交 0c0f8a7提交 4cf76f6提交 3f26785提交 dfc8cdc提交 13e7ed6提交 c397aac提交 ab90eca(2020 年 3 月 16 日),并在 b2020年 3 月 10 日之前提交 a860476(2020 年 3 月 10 日)。卡尔森 (``).
(由Junio C gitsterHamano合并-- --commit 4e4baee,2020 年 3 月 27 日)

convert: permit passing additional metadata to filter processes

Signed-off-by: brian m. carlson

There are a variety of situations where a filter process can make use of some additional metadata.

For example, some people find the ident filter too limiting and would like to include the commit or the branch in their smudged files.

This information isn't available during checkout as HEAD hasn't been updated at that point, and it wouldn't be available in archives either.

Let's add a way to pass this metadata down to the filter.

We pass the blob we're operating on, the treeish (preferring the commit over the tree if one exists), and the ref we're operating on.

Note that we won't pass this information in all cases, such as when renormalizing or when we're performing diffs, since it doesn't make sense in those cases.

The data we currently get from the filter process looks like the following:

command=smudge
pathname=git.c
0000

With this change, we'll get data more like this:

command=smudge
pathname=git.c
refname=refs/tags/v2.25.1
treeish=c522f061d551c9bb8684a7c3859b2ece4499b56b
blob=7be7ad34bd053884ec48923706e70c81719a8660
0000

There are a couple things to note about this approach.

For operations like checkout, treeish will always be a commit, since we cannot check out individual trees, but for other operations, like archive, we can end up operating on only a particular tree, so we'll provide only a tree as the treeish.

Similar comments apply for refname, since there are a variety of cases in which we won't have a ref.

convert: 允许传递额外的元数据来过滤进程

签字人:brian m. 卡尔森

在多种情况下,过滤过程可以使用一些额外的元数据。

例如,有些人发现 ident 过滤器限制太多,并希望在他们的污迹文件中包含提交或分支

此信息在结帐期间不可用,因为此时 HEAD 尚未更新,并且在档案中也不可用。

让我们添加一种方法来将此元数据传递给过滤器。

我们传递我们正在操作的 blob、树状(如果存在树,则更喜欢提交而不是树)和我们正在操作的 ref。

请注意,我们不会在所有情况下都传递此信息,例如在重新规范化或执行差异时,因为在这些情况下它没有意义。

我们目前从过滤过程中获得的数据如下所示:

command=smudge
pathname=git.c
0000

通过这一变化,我们将获得更像这样的数据:

command=smudge
pathname=git.c
refname=refs/tags/v2.25.1
treeish=c522f061d551c9bb8684a7c3859b2ece4499b56b
blob=7be7ad34bd053884ec48923706e70c81719a8660
0000

关于这种方法有几点需要注意。

对于像 checkout 这样的操作,treeish 将始终是一个提交,因为我们不能检出单个树,但是对于其他操作,比如存档,我们最终只能对特定的树进行操作,所以我们将只提供一棵树作为 treeish .

类似的注释适用于 refname,因为在许多情况下我们不会有 ref。

And:

和:

convert: provide additional metadata to filters

Signed-off-by: brian m. carlson

Now that we have the codebase wired up to pass any additional metadata to filters, let's collect the additional metadata that we'd like to pass.

The two main places we pass this metadata are checkouts and archives.
In these two situations, reading HEAD isn't a valid option, since HEAD isn't updated for checkouts until after the working tree is written and archives can accept an arbitrary tree.

In other situations, HEAD will usually reflect the refname of the branch in current use.

We pass a smaller amount of data in other cases, such as git cat-file, where we can really only logically know about the blob.

convert:为过滤器提供额外的元数据

签字人:brian m. 卡尔森

现在我们已经连接了代码库以将任何额外的元数据传递给过滤器,让我们收集我们想要传递的额外元数据。

我们传递这个元数据的两个主要地方是 checkouts 和 archives
在这两种情况下,读取 HEAD 不是一个有效的选项,因为在写入工作树并且档案可以接受任意树之前,HEAD 不会更新以进行检出。

在其他情况下,HEAD 通常会反映当前使用的分支的 refname。

我们在其他情况下传递较少量的数据,例如git cat-file,我们实际上只能从逻辑上了解 blob。