介绍
在很多git仓库中,发布版本中会在源码里面嵌入版本信息。比如 IDF 发布版本 v2.1,那如何将 v2.1 显示在源码中呢?
例如: esp-idf 下,esp-idf/make/project.mk 中嵌入了一下代码。
IDF_VER := $(shell cd ${IDF_PATH} && git describe --always --tags --dirty)
该命令会获取当前仓库当前的 commit 对应的最近的 tag。
git-describe - Describe a commit using the most recent tag reachable from it
–always
Show uniquely abbreviated commit object as fallback.
–tags
匹配远程的 refs/tag
Instead of using only the annotated tags, use any tag found in refs/tags namespace. This option enables matching a lightweight (non-annotated) tag.
–dirty
–dirty 意味着如果源码如果被修改了(git status),则会在版本后面加上 -dirty (默认),如版本为 v2.1 , 如果你修改了源码,则git describe 结果会成为 v2.1-dirty ,你也可以通过 --dirty= 来赋值新的字符串。

本文介绍了如何使用git-describe命令在Git仓库中获取最近的tag,并展示了如何在ESP8266项目中显示版本信息。当源码有修改时,-dirty标志会添加到版本号后。还提供了不同选项的使用示例,如获取commit距离tag的数量和简化commitID。

1387

被折叠的 条评论
为什么被折叠?



