简体   繁体   English

如何让git描述来自远程存储库的输出

[英]How to get git describe like output from a remote repository

I'm trying to sort out automatic version number generation for a buildroot based project I'm working on. 我正在尝试为我正在开发的基于buildroot的项目整理自动版本号生成。 At the moment there is a meta-repo that contains buildroot, the package configurations and scripts to build the project from scratch. 目前有一个包含buildroot的元repo,用于从头开始构建项目的包配置和脚本。 And a second repository that contains the application specific source code that's built as one of the buildroot packages. 第二个存储库,包含作为buildroot包之一构建的特定于应用程序的源代码。

As buildroot runs it performs a shallow clone of the package repo at a specified branch head. 随着buildroot运行,它在指定的分支头处执行包repo的浅层克隆。 I want a way to run git describe on this without having to do a full clone of the repository. 我想要一种方法来运行git describe而不必完全克隆存储库。

At the moment the project is able to generate a fairly accurate build number using git describe. 目前,该项目能够使用git describe生成相当准确的构建号。 We tag versions with major and minor versions, we then want the number of commits since the tag to be added during the build process. 我们使用主要版本和次要版本标记版本,然后我们需要在构建过程中添加标记以来的提交数量。

I've managed to hack together a rough approximation for it using git ls-remote to generate the SHA1 hash but I can't get the commit count, does anyone know of a way to achieve this? 我已经成功地使用git ls-remote对它进行粗略的近似来生成SHA1哈希但是我无法获得提交计数,有没有人知道实现这一目的的方法?

So after much toiling and searching I gave up on getting it remotely and wrote the following sections into my Makefile in order to generate the string I wanted. 因此,经过多次劳动和搜索,我放弃了远程获取它并将以下部分写入我的Makefile以生成我想要的字符串。 GIT_SITE is the address of the remote repository, GIT_BRANCH is the name of the branch that is being cloned, eg releaseCandidate/4.0 GIT_SITE是远程存储库的地址, GIT_BRANCH是要克隆的分支的名称,例如releaseCandidate/4.0

GIT_COMMIT_NUMBER = $(shell git clone $(GIT_SITE) -n -q temp && cd temp && git rev-list --count master..origin/$(GIT_BRANCH) && cd .. && rm -fr temp)

# This was easier to do, pulls the hash from the remote repo and cuts it down to be 7 characters long (same as would come out of git describe)
GIT_HASH = $(shell git ls-remote $(GIT_SITE) $(GIT_VERSION) | cut -f 1 | awk '{print substr($$0,1,7) }')

# This cuts down the branch name given to us by buildroot so that we cas use it in the build string generation
GIT_BRANCH_FLAG = $(shell echo $(GIT_BRANCH) | cut -f 1 -d '/')
GIT_BRANCH_VERSION = $(shell echo $(GIT_BRANCH) | cut -f 2 -d '/')

# Finally we put those bits together to get the outputs we want
VERSION ?= $(GIT_BRANCH_VERSION)
GIT_DESCRIBE ?= $(GIT_BRANCH_VERSION)-$(GIT_COMMIT_NUMBER)-g$(GIT_HASH)-$(GIT_BRANCH_FLAG)

For a shallow clone, you could try: 对于浅层克隆,您可以尝试:

echo "$(git ls-remote -q --tags --refs | grep -Pio '(\d+(\.|_|-|$)){2}[\w\.-]*' | perl -pe 's|[[:punct:]]|.|g' | sort -V | tail -1)-$(git rev-list --count HEAD)-g$(git show -s --abbrev-commit --abbrev=7 --pretty=format:%h)"

This is how it works: 这是它的工作原理:

git ls-remote -q --tags --refs | grep -Pio '(\\d+(\\.|_|-|$)){2}[\\w\\.-]*' | perl -pe 's|[[:punct:]]|.|g' | sort -V | tail -1 git ls-remote -q --tags --refs | grep -Pio '(\\d+(\\.|_|-|$)){2}[\\w\\.-]*' | perl -pe 's|[[:punct:]]|.|g' | sort -V | tail -1 will tease out, at minimum , a regex pattern of two numbers with a separator of . _ - git ls-remote -q --tags --refs | grep -Pio '(\\d+(\\.|_|-|$)){2}[\\w\\.-]*' | perl -pe 's|[[:punct:]]|.|g' | sort -V | tail -1至少取出两个带有分隔符的正则表达式. _ - . _ - converting that punctuation to dots. . _ -将标点符号转换为点。 sort -V | tail -1 sort -V | tail -1 sorts the resultant version strings in ascending order and grabs the, most recent, tag. sort -V | tail -1按升序对结果版本字符串进行排序,并获取最新的标记。

git rev-list --count HEAD Will count out all commits since the shallow clone. git rev-list --count HEAD将计算自浅层克隆以来的所有提交。 It's analogous to git describe but useless once the shallow actually picks up a commit with a tag in it. 它类似于git describe但是一旦浅层实际上接收到带有标记的提交,它就无用了。

-g means git -g表示git

git show -s --abbrev-commit --abbrev=7 --pretty=format:%h Simulates the abbreviation to 7 characters. git show -s --abbrev-commit --abbrev=7 --pretty=format:%h将缩写模拟为7个字符。

Here's an example of vbam: 这是vbam的一个例子:

$ git ls-remote -q --tags --refs
f7f67ff6f93e836309f43f13f4ccc286156d64c7    refs/tags/Beta-3
6ecab805e00e99c37bd23c425835ca35507cb593    refs/tags/Keyboardfixes
47d56f4fade1b95d5ea40ed0a820949fb23311fa    refs/tags/VBA-M_Beta_2
9f5b04786381fcc2f9cac7f426d01bc3f74da80c    refs/tags/c7c6ad6
2e8bee117d33d63745c5dd095254986e1985fde4    refs/tags/throttle
1eb768578bc0c4fa17396f573a4b37a652f12acd    refs/tags/v2.0.0
55fa9c69c0ebef70131ae385a094cb37fcaea057    refs/tags/v2.0.1
92ba676632f65c687c32bd655584dc54598d325e    refs/tags/v2.0.2
7aa5d9398e761e121c724a420a1aba04c80154cf    refs/tags/v2.1.0
931fda459a3d003c94eabdac5b0a60ef8f570ae1    refs/tags/waylandplus

$ git describe --abbrev=7 --tags
v2.1.0-99-g3cb3842

$ echo "$(git ls-remote -q --tags --refs | grep -Pio '(\d+(\.|_|-|$)){2}[\w\.-]*' | perl -pe 's|[[:punct:]]|.|g' | sort -V | tail -1)-$(git rev-list --count HEAD)-g$(git show -s --abbrev-commit --abbrev=7 --pretty=format:%h)"
2.1.0-155-g3cb3842

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

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