简体   繁体   English

Git如何管理版本号?

[英]How to manage the version number in Git?

Let's imagine the blerp command line tool maintained on .让我们想象一下在维护的blerp命令行工具。 This tool has the (hidden) --version option which returns its version (let's say 0.1.2 ) and another --commit which returns the commit number from which it was built.这个工具有(隐藏的) --version选项,它返回它的版本(比方说0.1.2 )和另一个--commit ,它返回--commit它的提交号。

Both the version and the commit number are hard-coded on the code base.版本和提交号都是在代码库上硬编码的。

Now I make a bugfix then commit and rebuild my program.现在我做了一个错误修正,然后提交并重建我的程序。 I will still see 0.1.2 although this new version differ from the original 0.1.2.我仍然会看到0.1.2 ,虽然这个新版本从原来的0.1.2不同。 Only the commit will tell me that it is not the same 0.1.2.只有提交会告诉我它与 0.1.2 不同。 Is that bugfix worth a different version number?那个错误修复值得不同的版本号吗?

One solution is that each time I make a commit, I increase the hard-coded version number (which implies to always modify a minimum of 2 files for each commit).一种解决方案是,每次提交时,我都会增加硬编码版本号(这意味着每次提交至少要修改 2 个文件)。 This is a binding solution and it does not work when the developers are working on different active branches.这是一个绑定解决方案,当开发人员在不同的活动分支上工作时它不起作用。 If Bob works on feature foo from the version 0.1.2 and Alice works on feature bar from the same version.如果 Bob 在0.1.2版本的功能foo上工作,而 Alice 在相同版本的功能bar上工作。 How do they increase their version number?他们如何增加他们的版本号? Bob can use the odd and Alice the even. Bob 可以使用奇数,Alice 可以使用偶数。 What if Eve works on a third feature?如果 Eve 从事第三个功能会怎样?

Another solution can be to use the Git tags to automatically generate the version number.另一种解决方案是使用 Git 标签自动生成版本号。 A script can find the closest tag starting with v such as v0.1.2 and use the tag name as the version number plus the first n digits of the current commit ( v0.1.2 (build 4acd21) ).脚本可以找到最接近的以v开头的标签,例如v0.1.2并使用标签名称作为版本号加上当前提交的前 n 位数字( v0.1.2 (build 4acd21) )。 This works well if the working directory is clean.如果工作目录是干净的,这很有效。 One can imagine to add a * before the build number to indicate the working directory is not clean.可以想象在内部版本号前添加一个*来表示工作目录不干净。 The main problem with this solution is if somebody export the sources, it won't be able to build blerp .此解决方案的主要问题是,如果有人导出源代码,它将无法构建blerp

What possible alternative can solve this issue?什么可能的替代方案可以解决这个问题?

Alexey Kiselev and Dario already hinted towards the answer, but I will try to explain it in detail. Alexey Kiselev 和 Dario 已经暗示了答案,但我将尝试详细解释。

Versioning Schemes版本控制方案

There are two types of versioning schemes:有两种类型的版本控制方案:

  1. Internal version number: This can be incremented many times in a day (eg revision control number)内部版本号:这可以在一天内增加多次(例如修订控制号)
  2. Released version: This changes less often (eg semantic versioning)已发布版本:此更改较少(例如语义版本控制)

People use different schemes as per their need, but semantic versioning is fairly widely used and authored by Tom Preston-Werner, co-founder of GitHub.人们根据自己的需要使用不同的方案,但语义版本控制得到了相当广泛的使用,由 GitHub 联合创始人 Tom Preston-Werner 撰写。

Semantic Versioning语义版本控制

Semantic versioning follows the pattern of XYZ语义版本控制遵循XYZ模式

Or more readable would be [major].[minor].[patch]-[build/beta/rc]或者更具可读性的是[major].[minor].[patch]-[build/beta/rc]

Eg 1.2.0-beta例如1.2.0-beta

major or X can be incremented if there are major changes in software, like backward-incompatible API release.如果软件发生重大变化,例如向后不兼容的 API 版本,则major or X可以递增。

minor or Y is incremented if backward compatible APIs are introduced.如果引入了向后兼容的 API,则minor or Y会增加。

patch or Z is incremented after a bug fix. patch or Z在错误修复后增加。

How do we achieve this using Git?我们如何使用 Git 实现这一目标?

By using tags:通过使用标签:

Tags in Git can be used to add a version number. Git 中的标签可用于添加版本号。

git tag -a "v1.5.0-beta" -m "version v1.5.0-beta"

adds a version tag of v1.5.0-beta to your current Git repository.将 v1.5.0-beta 的版本标签添加到您当前的 Git 存储库。 Every new commit after this will auto-increment tag by appending commit number and commit hash.此后的每个新提交都将通过附加提交编号和提交哈希来自动增加标签。 This can be viewed using the git describe command.这可以使用git describe命令查看。

v1.5.0-beta-1-g0c4f33f here -1- is the commit number and 0c4f33f the abbreviation of commit's hash. v1.5.0-beta-1-g0c4f33f这里-1-是提交号, 0c4f33f是提交哈希的缩写。 The g prefix stands for "git" . g前缀代表"git"

Complete details can be viewed using:可以使用以下方法查看完整的详细信息:

git show v1.5.0-beta

Please, have a look at git describe command.请看一下git describe命令。 This command shows you the latest tag and an amount of commits made after the tag was set.此命令向您显示最新标签以及在设置标签后所做的提交量。 It also possible to show the dirtiness of the repository.还可以显示存储库的肮脏程度。

As you mentioned this command wouldn't work without the git repository (.git folder) and git installed.正如您所提到的,如果没有 git 存储库(.git 文件夹)和 git 安装,此命令将无法工作。 But it's almost unimaginable developer today without git but with all other tools installed.但是今天没有 git 但安装了所有其他工具的开发人员几乎是不可想象的。

Revision numbers should be maintained by you, not by git.修订号应该由你维护,而不是由 git 维护。 As, opposed to SVN, you don't have a incremental revision number growing on each commit, there's no way out of the box to git to contextualize what your version is.与 SVN 不同的是,每次提交时您都不会增加增量修订号,因此 git 无法开箱即用地将您的版本置于上下文中。

As you say, versioning issues are usually solved in git using branch and tags (like semantic versioning pattern).正如您所说,版本控制问题通常在 git 中使用branchtags (如语义版本控制模式)来解决。

The better way is to use git to track only changes in the codebase, ignoring (using .gitignore file) builds files and maintaining a clean repository.更好的方法是使用git仅跟踪代码库中的更改,忽略(使用.gitignore文件)构建文件并维护一个干净的存储库。

Builds results ( pre/compiled files, executables files, distribution files, zips, exe... ) could depend of environments variables (platform, system arch, etc) and should be keep separate in a registry.构建结果(预/编译文件、可执行文件、分发文件、zip、exe... )可能取决于环境变量(平台、系统架构等),并且应该在注册表中分开保存。

If the codebase very big and hard to maintain, maybe you should considerate to divide it in smaller components (or git submodule ) to avoid cross-dependencies at development time.如果代码库非常大且难以维护,也许您应该考虑将其划分为较小的组件(或git submodule )以避免开发时的交叉依赖。

  1. Create file in project dir (or where you want) build_number and put it value 1 in this file在项目目录(或您想要的位置)build_number 中创建文件并将其值 1 放在此文件中

  2. Go to git dir and create file called "pre-commit" in .git/hooks/ (without .sample)转到 git dir 并在 .git/hooks/ 中创建名为“pre-commit”的文件(不带 .sample)

  3. Put this code there把这段代码放在那里

#!/bin/sh #!/bin/sh

currentbuildnumber= cat build_number let "currentbuildnumber++" printf $currentbuildnumber > build_number currentbranch= git branch | tr -cd "[:alpha:]" currentbuildnumber= cat build_number let "currentbuildnumber++" printf $currentbuildnumber > build_number currentbranch= git branch | tr -cd "[:alpha:]" git branch | tr -cd "[:alpha:]" git log $currentbranch --pretty=format:"%h - %an, %ar : %s, Build: $currentbuildnumber" git branch | tr -cd "[:alpha:]" git log $currentbranch --pretty=format:"%h - %an, %ar : %s, Build: $currentbuildnumber"

Working for me :) enjoy!为我工作:) 享受!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM