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

发布于 2024-08-12 04:20:28 字数 49 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-08-19 04:20:28

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


在 Git 中(我认为通常在具有原子提交的其他 VCS 系统中也是如此),不存在诸如单个文件的版本之类的东西。

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

  1. 它仅根据请求完成。您必须指定(可能使用通配模式)一个文件(或一组文件)具有 ident 属性 设置(在树中的“.gitattributes”文件中,或在“.git/info/attributes”中用于本地存储库设置)。
  2. 它扩展为文件内容的 SHA-1(或更准确地说,扩展为$Id:$的 sha-1)。做出这种选择的原因是,Git 不会触及分支切换或回滚期间未更改的文件;如果“$Id:$”扩展到修订信息,则需要更新每个版本控制文件,例如在切换分支时。

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

  1. 仅当运行 git archive,在其输出文件中。
  2. 它是根据请求完成的,通过export-subst 属性控制。

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

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

Summary: The recommended way of embedding version information in a product is to use the build system for 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 does support on-demand expansion of $Id:$ keyword, but:

  1. It is done on request only. You have to specify (perhaps using globbing pattern) that a file (or a set of files) has an ident attribute set (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 every version-controlled file e.g. when switching branches.

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

  1. Expansion is done only when running git archive, in its output file.
  2. It is done on request, controlled via export-subst attribute.

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

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.

柏林苍穹下 2024-08-19 04:20:28

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

示例:

$ echo '*.txt ident' >> .gitattributes
$ echo '$Id

链接到 gitattributes(5) 手册页

> test.txt $ git commit -a -m "test" $ rm test.txt $ git checkout -- test.txt $ cat test.txt

链接到 gitattributes(5) 手册页

You can do this by adding a pattern for which files you want this functionality followed by ident in the .gitattributes file. 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.

Example:

$ echo '*.txt ident' >> .gitattributes
$ echo '$Id

Link to gitattributes(5) Manual Page

> test.txt $ git commit -a -m "test" $ rm test.txt $ git checkout -- test.txt $ cat test.txt

Link to gitattributes(5) Manual Page

想你只要分分秒秒 2024-08-19 04:20:28

Git 的 ident 不执行 $Id$ 在其他版本控制系统中执行的操作。作为一个组合,将 RCS 与 git 一起使用:RCS 用于单个文件修订,而 git 对整个项目进行检查点。正如我所说,这是一个拼凑,但它确实有点道理(有时对于某些事情)。

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).

旧街凉风 2024-08-19 04:20:28

Jakub Narębski他的回答(十多年前):

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

批评是(由Arioch 'The评论

如果过滤器驱动程序确实有任何功能,那就真的很棒了。
据了解,它似乎只获取文件名作为参数:这甚至比 ident 过滤器(获取 BLOB SHA1 作为参数)还要少

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

Git 2.27 为 smudge/clean 转换过滤器提供更多信息(例如,除了已经给出的路径之外,正在转换的 blob 出现的树形对象)

请参阅 提交 0c0f8a7提交 4cf76f6, 提交3f26785提交 dfc8cdc提交 13e7ed6, 提交 c397aac提交 ab90eca(2020 年 3 月 16 日) ,并提交 a860476(2020 年 3 月 10 日),作者:brian m.卡尔森(``)
(由 Junio C Hamano -- gitster -- 合并于 提交 4e4baee,2020 年 3 月 27 日)

convert:允许传递其他元数据到过滤器流程

签署人:brian m。卡尔森

过滤器进程可以在多种情况下使用一些附加元数据。

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

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

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

我们传递正在操作的 blob、树形结构(如果存在,则优先选择提交而不是树)以及正在操作的引用。

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

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

命令=涂抹
路径名=git.c
0000

通过此更改,我们将获得更像这样的数据:

命令=涂抹
路径名=git.c
refname=refs/tags/v2.25.1
树形=c522f061d551c9bb8684a7c3859b2ece4499b56b
斑点=7be7ad34bd053884ec48923706e70c81719a8660
0000

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

对于诸如签出之类的操作,treeish 将始终是一个提交,因为我们无法签出单个树,但对于其他操作(例如存档),我们最终只能在特定的树上进行操作,因此我们将只提供一棵树作为树状的。

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

和:

转换:向过滤器提供附加元数据< /h2>

签署人:brian m。卡尔森

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

我们传递此元数据的两个主要位置是签出和存档
在这两种情况下,读取 HEAD 不是一个有效的选项,因为只有在写入工作树并且档案可以接受任意树之后,HEAD 才会更新以进行签出。

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

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

Jakub Narębski suggested in his answer (more than 10 years ago ):

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.

The criticism is (by Arioch 'The in the comments)

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

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

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.

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)

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文