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

Joa*_*ner 6 git bash git-plumbing

我可以使用很好的列出包含特定提交的所有分支git branch --list --contains.但正如关于如何列出所有分支的相关问题中所解释的,这是一个不应该在脚本中使用的瓷器命令.

后一个问题建议使用管道命令git for-each-ref,但这不支持--contains.

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

Von*_*onC 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日)由ÆvarArnfjörðBjarmason( )avar.
(由Junio C Hamano合并-gitster-提交d1d3d46,2017年4月11日)


原始答案

启动git 2.7(2015年第4季度),您将获得更完整的版本git for-each-ref,现在支持--contains

git for-each-ref --contains <SHA1>
Run Code Online (Sandbox Code Playgroud)

随着文档:

--contains [<object>]:
Run Code Online (Sandbox Code Playgroud)

仅列出包含指定提交的标记(如果未指定,则为HEAD).


提交4a71109,提交ee2bd06,提交f266c91,提交9d306b5,提交7c32834,提交35257aa,提交5afcb90,...,提交b2172fd(2015年7月7日),并提交af83baf(2015年7月9日)由KARTHIK纳亚克(KarthikNayak).
(通过合并JUNIOÇ滨野- gitster-提交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  
Run Code Online (Sandbox Code Playgroud)


jub*_*0bs 2

一种可能的解决方案是使用管道命令git-for-each-refgit 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 $?
Run Code Online (Sandbox Code Playgroud)

该脚本可从 GitHub 上的Jubobs/git-aliases获取。

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