git的脚本:如何列出一个包含所有混帐分支机构提交

问题描述:

我可以列出all含有一定犯使用分支 git的分支--list --contains 就好了。但在相关的question如何列出所有分支机构的,这是不应该在脚本中使用的瓷器命令。

I can list all branches containing a certain commit using git branch --list --contains just fine. But as explained in the related question on how to list all branches, this is a porcelain command that should not be used in scripts.

后一个问题建议使用管道命令混帐的for-each-REF ,但不支持 - 包含

The latter question suggests to use the plumbing command git for-each-ref, but that does not support --contains.

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

What is the correct plumbing interface to list all branches that contain a certain commit.

使用管道命令的一个可能的解决方案混帐的for-each-REF git的合并基础(后者由约阿希姆自己的建议):

One possible solution using the plumbing commands git-for-each-ref and git merge-base (the latter suggested by Joachim himself):

#!/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 $?

该脚本可在 Jubobs /混帐别名在GitHub上。

(编辑:感谢核心转储以显示我的如何摆脱那个讨厌评估 。)

( thanks to coredump for showing me how to get rid of that nasty eval.)