git_describe

介绍

在很多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= 来赋值新的字符串。

Describe the working tree. It means describe HEAD and appends (-dirty by default) if the working tree is dirty.

获取版本说明一

git describe --always --tags --dirty

返回: v3.1-dev-374-gb0f7ff5

说明:

  • v3.1-dev: 当前分支最近的 tag
  • 374 代表在 v3.1-dev tag 后有 374 次提交(commit)
  • -g 代表 git
  • b0f7ff5 代表最近的 commitID
  • git describe --always 会获取最近的 commitID

获取版本说明二

git log --abbrev-commit --pretty=oneline -1 | cut -c 1-7

返回: b0f7ff5

说明:

  • --abbrev-commit: 使用简化的 commitID
  • --pretty=oneline: 每个 commit 只保留第一行
  • -1 取第一个 commitID 行
  • cut -c 1-7 取 commitID 行中 commitID 号

你可能感兴趣的:(git)