是否可以在git标签期间自动替换自定义字符串? [英] Is it possible to automatically replace custom string during git tag?

查看:174
本文介绍了是否可以在git标签期间自动替换自定义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

放置在文件中的VERSION变量

VERSION variable placed in a file

即: VERSION ='1.0.0'

i.e.: VERSION='1.0.0'

我希望在创建新标签(发行版)时,git会以某种方式将此VERSION变量的值替换为发行版/标签名称.

I want that when a new tag (release) created, somehow git shall replace the value of this VERSION variable to the release/tag name.

有可能吗?

推荐答案

不是git本身,不是.

Not in git itself, no.

您需要在此处记录两件事:

You need to make note of two things here:

  1. A git commit通过获取索引/暂存区中的所有内容并将其转换为提交来制作新快照. 1

  1. A git commit makes a new snapshot, by taking whatever is in the index / staging-area and turning that into a commit.1

这意味着,经过一定的提交,将提取到工作树中的内容就是当时索引中的内容.要制作新的,不同的,已保存的树,必须更改索引,然后进行新的提交.

This means that what will be extracted to a work-tree, given some commit, is whatever was in the index at the time. To make a new, different, saved tree, you must alter the index and then make a new commit.

标签不是提交.

标签有两种形式:轻量级"标签(仅是名称以refs/tags/开头的引用)和带注释的"标签,其中git在存储库中添加了带注释的标签对象-该标签对象必须指向一个已经存在的提交对象 2 ,然后是一个使用名称的新轻量级标记,指向带注释的标记对象.

Tags come in two forms: "lightweight" tags, which are simply references whose names start with refs/tags/, and "annotated" tags, where git adds an annotated-tag object to the repository—this tag object necessarily points to an already-existing commit object2—and then a new lightweight tag using the name, pointing to the annotated-tag object.

给出标签名称,即以refs/tags/开头的引用,该标签在指向提交时为轻量级",而在指向带注释的标签对象时为带注释",然后该对象指向一个犯罪.无论哪种方式,标签都指向提交-在您制作标签时已经存在的某些提交.

Given a tag name, i.e., a reference starting with refs/tags/, that tag is "lightweight" when it points to a commit, and "annotated" when it points to an annotated-tag object, which then points to a commit. Either way, a tag points to a commit—some commit that already existed at the time you made the tag.

但是如果您有时尝试,您可能会发现,您会得到所需的东西

因此,任何实际存储在git中的文件都必须在提交时进行更改(并添加到索引/分段区域).因此,您可以编写一个小程序:

But if you try sometimes, you just might find, you get what you need

Therefore, any file actually stored in git, has to be changed (and added to the index / staging-area) at the time the commit is made. You can thus write a little program that:

  • 更新版本文件;
  • 提交更新;和
  • 标记提交

这将达到您想要的结果.

which will achieve the result you want.

或者,您可以设计系统,以使版本文件不是通过git维护的,而是由构建系统创建的,该构建系统会以您喜欢的任何方式从git中提取版本信息.

Alternatively, you can design your system so that the version file is not maintained through git, and instead is created by the build system, which extracts the version information from git in whatever manner you like.

有关更多信息,请参见以下有关使用git describe的问题:

See these SO questions about using git describe for more information:

  • Deriving application build version from `git describe` - how to get a relatively straightforward string?
  • "git describe" ignores a tag
  • How can I use "git describe --match" sensibly in combination with a shallow clone?
  • Git: How to get the name of checked out tag when 2/more tags on same commit

1 如果新提交与当前提交相同,并且新提交不是合并提交,则除非您用--allow-empty强制执行,否则git commit拒绝进行提交,除非您强制执行

1If the new commit would be identical to the current one, and the new commit is not a merge commit, git commit refuses to make the commit unless you force it, with --allow-empty.

2 从技术上讲,轻量级标签或带注释的标签内的哈希可以指向除提交对象以外的其他内容.过去,我已经将此用于一些特殊的技巧,并且它可以工作.但这通常不是git要做的.

2Technically, a lightweight tag, or the hash inside an annotated tag, can point to something other than a commit object. I have used this for some special hacks in the past, and it works. It's not something git will normally do though.

这篇关于是否可以在git标签期间自动替换自定义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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