簡體   English   中英

如何讓git描述來自遠程存儲庫的輸出

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

我正在嘗試為我正在開發的基於buildroot的項目整理自動版本號生成。 目前有一個包含buildroot的元repo,用於從頭開始構建項目的包配置和腳本。 第二個存儲庫,包含作為buildroot包之一構建的特定於應用程序的源代碼。

隨着buildroot運行,它在指定的分支頭處執行包repo的淺層克隆。 我想要一種方法來運行git describe而不必完全克隆存儲庫。

目前,該項目能夠使用git describe生成相當准確的構建號。 我們使用主要版本和次要版本標記版本,然后我們需要在構建過程中添加標記以來的提交數量。

我已經成功地使用git ls-remote對它進行粗略的近似來生成SHA1哈希但是我無法獲得提交計數,有沒有人知道實現這一目的的方法?

因此,經過多次勞動和搜索,我放棄了遠程獲取它並將以下部分寫入我的Makefile以生成我想要的字符串。 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)

對於淺層克隆,您可以嘗試:

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)"

這是它的工作原理:

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至少取出兩個帶有分隔符的正則表達式. _ - . _ -將標點符號轉換為點。 sort -V | tail -1 sort -V | tail -1按升序對結果版本字符串進行排序,並獲取最新的標記。

git rev-list --count HEAD將計算自淺層克隆以來的所有提交。 它類似於git describe但是一旦淺層實際上接收到帶有標記的提交,它就無用了。

-g表示git

git show -s --abbrev-commit --abbrev=7 --pretty=format:%h將縮寫模擬為7個字符。

這是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