2015-08-06 5 views
6

Posso elencare all branches containing a certain commit utilizzando git branch --list --contains bene. Ma come spiegato nel relativo question on how to list all branches, questo è un comando di porcellana che non dovrebbe essere usato negli script.git scripting: come elencare tutti i rami git contenenti un commit

L'ultima domanda suggerisce di utilizzare il comando idraulico git for-each-ref, ma che non supporta --contains.

Qual è l'interfaccia idraulica corretta per elencare tutti i rami che contengono un certo commit.

+1

Nota: con git 2.7 (Q4 2015), 'git for-each-Ref --contains' sarà una realtà. Vedere [la mia risposta sotto] (http://stackoverflow.com/a/32988289/6309) – VonC

risposta

2

Una possibile soluzione che utilizza l'impianto idraulico comandi git-for-each-ref e git merge-base (quest'ultimo suggerito da Joachim se stesso):

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

Lo script è disponibile presso Jubobs/git-aliases su GitHub.

(edit: grazie a coredump per avermi mostrato how to get rid of that nasty eval.)

+1

sembra simile a quello che ho finito, ma ho usato 'git merge-base --is-ancestor' invece di' [- n "$ (git rev-list $ rev^.. $ ref)"] '. –

+0

@JoachimBreitner Buona chiamata, usando 'git merge-base --is-ancestor'. Meno hacky, più robusto. Non lo sapevo nemmeno (grazie!). Pubblica la tua risposta o fammi sapere se dovrei modificare il mio. – Jubobs

+0

Il mio non usa fantasiose cose 'eval' (sfiducia personale), quindi aggiorna il tuo se pensi che sia un miglioramento. –

5

Aggiornamento 18 mesi più tardi (aprile 2017): con Git 2.13 (Q2 2017), git for-each-ref --no-contains <SHA1> è finalmente supportato!

Vedi commit 7505769, commit 783b829, commit ac3f5a3, commit 1e0c3b6, commit 6a33814, commit c485b24, commit eab98ee, commit bf74804 (24 Mar 2017), commit 7ac04f1, commit 682b29f, commit 4612edc, commit b643827 (23 Mar 2017), e commit 17d6c74, commit 8881d35, commit b084060, commit 0488792 (21 Mar 2017) di Ævar Arnfjörð Bjarmason (avar).
(fusa per Junio C Hamano -- gitster -- in commit d1d3d46 11 Apr 2017)


risposta originale

partire git 2.7 (Q4 2015) si otterrà una versione più completa di git for-each-ref, che ora il supporto il --contains

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

Con il d oc:

--contains [<object>]: 

Solo Lista tag che contengono la specificato commit (testa, se non specificato).


Vedi commit 4a71109, commit ee2bd06, commit f266c91, commit 9d306b5, commit 7c32834, commit 35257aa, commit 5afcb90, ..., commit b2172fd (7 Luglio 2015), e commit af83baf (9 luglio 2015) per Karthik Nayak (KarthikNayak).
(fusa per Junio C Hamano -- gitster -- in commit 9958dd8, 5 ottobre 2015)

Alcune caratteristiche da "git tag -l" e "git branch -l" sono stati resi a disposizione "git for-each-ref", in modo che alla fine il unificata implementazione può essere condivisa su tutti e tre, in una serie di follow-up o due.

* 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