git 脚本:如何列出包含提交的所有 git 分支

问题描述 投票:0回答:2

我可以使用 git branch --list --contains 列出

包含某个提交的所有分支
就可以了。但正如相关的有关如何列出所有分支的问题中所解释的,这是一个瓷器命令,不应在脚本中使用。

后一个问题建议使用管道命令

git for-each-ref
,但这不支持
--contains

列出包含特定提交的所有分支的正确管道接口是什么。

git bash git-plumbing
2个回答
5
投票

18个月后(2017年4月)更新:使用Git 2.13(2017年第2季度),

git for-each-ref --no-contains <SHA1>
终于得到支持了!

参见 提交 7505769提交 783b829提交 ac3f5a3提交 1e0c3b6提交 6a33814提交 c485b24提交 eab98ee提交 bf74804(2017 年 3 月 24 日),提交 7ac04f1 提交 682b29f提交 4612edc提交 b643827(2017 年 3 月 23 日)、提交 17d6c74提交 8881d35提交 b084060提交 0488792(2017 年 3 月 21 日),作者:Ævar Arnfjörð Bjarmason (

avar
).
(由 Junio C Hamano --
gitster
--
合并于 commit d1d3d46,2017 年 4 月 11 日)


原答案

从 git 2.7(2015 年第 4 季度)开始,您将获得更完整的

git for-each-ref
版本,现在支持
--contains

git for-each-ref --contains <SHA1>

与医生:

--contains [<object>]:

仅列出包含指定提交的标签(如果未指定则为 HEAD)。


参见 提交 4a71109提交 ee2bd06提交 f266c91提交 9d306b5提交 7c32834提交 35257aa提交 5afcb90 ,...,提交 b2172fd(2015 年 7 月 7 日) ,并提交 af83baf(2015 年 7 月 9 日),作者:Karthik Nayak (

KarthikNayak
)
(由 Junio C Hamano --
gitster
--
合并于 commit 9958dd8,2015 年 10 月 5 日)

git tag -l
”和“
git branch -l
”的一些功能已经完成 可供“
git for-each-ref
”使用,以便最终统一 后续实施可以在所有三个之间共享 系列或两个。

* kn/for-each-tag-branch:
  for-each-ref: add '--contains' option
  ref-filter: implement '--contains' option
  parse-options.h: add macros for '--contains' option
  parse-option: rename parse_opt_with_commit()
  for-each-ref: add '--merged' and '--no-merged' options
  ref-filter: implement '--merged' and '--no-merged' options
  ref-filter: add parse_opt_merge_filter()
  for-each-ref: add '--points-at' option
  ref-filter: implement '--points-at' option  

2
投票

使用管道命令

git-for-each-ref
git merge-base
(后者由 Joachim 本人建议)的一种可能的解决方案:

#!/bin/sh

# git-branchesthatcontain.sh
#
# List the local branches that contain a specific revision
#
# Usage: git branchthatcontain <rev>
#
# To make a Git alias called 'branchesthatcontain' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.branchesthatcontain \
#       '!sh git-branchesthatcontain.sh'

if [ $# -ne 1 ]; then
    printf "%s\n\n" "usage: git branchesthatcontain <rev>"
    exit 1
fi

rev=$1

git for-each-ref --format='%(refname:short)' refs/heads | \
    while read ref; do
        if git merge-base --is-ancestor "$rev" "$ref"; then
            printf "%s\n" "$ref"
        fi;
    done

exit $?

该脚本可在 GitHub 上的 jub0bs/git-aliases 获取。

(编辑:感谢coredump向我展示了如何摆脱那个讨厌的

eval
。)

© www.soinside.com 2019 - 2024. All rights reserved.