我需要为远程存储库获得与
git describe <hash>
相同的结果。
例如,给定哈希值 d96edbcf
,我想要得到类似 v0.54.0-1-gd96edbcf
的内容
我需要对多个存储库进行此操作,因此我正在寻找一种有效的方法来执行此操作,而不必每次都检查整个存储库。
获取哈希值不起作用,因为
git describe <hash>
说No names found, cannot describe anything
我会从
git ls-remote
开始。如果有问题的提交被标记或者它位于分支的头部,您可以轻松找到它而无需克隆。示例:
$ git ls-remote https://github.com/phdru/mimedecode.git | grep "^192c1d0"
192c1d01fb1dac29d06b60c521846da7971f3960 refs/tags/3.2.0^{}
不幸的是,如果提交不是在引用处,则没有通用方法可以在不克隆的情况下描述它。要么使用 GitHub/GitLab API,要么至少克隆一些东西。您可以尽可能少地克隆 — 仅提交,不复制 blob,不签出。
如果您知道提交所属的分支,您可以克隆单个分支:
$ git clone --bare --branch=master --filter=blob:none --single-branch https://github.com/phdru/mimedecode.git
$ cd mimedecode.git
$ git describe 192c1d0
3.2.0
$ git describe bf6c8b1
3.2.0-3-gbf6c8b1
$ cd ..
$ rm -rf mimedecode.git
如果您不知道某个分支,则需要克隆更多:
$ git clone --bare --filter=blob:none https://github.com/phdru/mimedecode.git
$ cd mimedecode.git
$ git describe 192c1d0
3.2.0
$ git describe bf6c8b1
3.2.0-3-gbf6c8b1
$ cd ..
$ rm -rf mimedecode.git