這就是我搜索最新標簽提交的方式:
tag=$(git describe --tags --abbrev=0)
base=$(git rev-parse $tag)
但是如果沒有標簽存在,它應該回傳 repo 的第一次提交。
first=$(git log --reverse --oneline)
我如何只獲得第一次提交 - 而不是串列,就像上面的命令一樣?我如何or為基地做一個?如果標簽存在,則回傳其提交,如果不存在,則回傳第一個提交。
uj5u.com熱心網友回復:
您將需要進行測驗。
該git tag命令不記錄退出狀態。手動測驗它表明它有一個,但您可能不希望依賴這個未記錄的功能。為了避免依賴退出狀態,我們可以改為檢查輸出:
tag=$(git describe --tags --abbrev=0)
if [ "x$tag" = x ]; then
# there is no tag
# so here we'll do something different
fi
在現代 shell 中,您不需要稍微愚蠢的"x$tag"技巧:
tag=$(git describe --tags --abbrev=0)
if [ "$tag" = "" ]; then ...; fi
例如。
要從當前提交獲取可訪問的根提交的哈希 ID,請使用--max-parents=0. 理想情況下,只有一個這樣的根,但根據存盤庫歷史的形狀,可能有多個,在這種情況下,您必須決定要對此做什么。避免這種可能性的一種方法是預先決定您想要通過僅遍歷第一親找到的任何根。
與git log需要添加--pretty=format或--format指令以僅提取哈希 ID 的 相比,git rev-list在此處使用通常更明智。rev-list "plumbing" 命令的一個小問題是你必須提供一個起點,而如果你不提供一個起點git log就會假設HEAD。這是次要的,因為寫入是微不足道的HEAD:
first=$(git rev-list --max-parents=0 --first-parent HEAD)
把這些放在一起,我們得到:
tag=$(git describe --tags --abbrev=0)
if [ "$tag" = "" ]; then
tag=$(git rev-list --max-parents=0 --first-parent HEAD)
fi
或更緊湊的形式:
tag=$(git describe --tags --abbrev=0)
[ "$tag" != "" ] || tag=$(git rev-list --max-parents=0 --first-parent HEAD)
實際上嘗試這個我們發現git describe在git describe步驟失敗并輸出空字串的存盤庫中這樣做:
$ git describe --tags --abbrev=0
fatal: No tags can describe '2808ac68000c62c3db379d73e3b7df292e333a57'.
Try --always, or create some tags.
$ echo $?
128
因此,“測驗”可以只是:沒有git describe成功? 在這種情況下,我們得到:
tag=$(git describe --tags --abbrev=0 ||
git rev-list --max-parents=0 --first-parent HEAD)
When run in my example repository I get:
$ tag=$(git describe --tags --abbrev=0 ||
> git rev-list --max-parents=0 --first-parent HEAD)
fatal: No tags can describe '2808ac68000c62c3db379d73e3b7df292e333a57'.
Try --always, or create some tags.
$ echo $tag
460cce09ff42c43a6826af9deaab0f7a8d1f6f44
To make the error message vanish, we can redirect it to /dev/null:
tag=$(git describe --tags --abbrev=0 2> /dev/null ||
git rev-list --max-parents=0 --first-parent HEAD)
Note that if you want to hide the error message, you have to do this redirection, no matter which of the testing methods you pick.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/313240.html
標籤:混帐
下一篇:如何在gerrit中推送功能分支
