2011-10-27 16 views
9

Voglio fermarmi accidentalmente a dare qualcosa al master branch a meno che non ne sia sicuro. Così ho provato questo script per determinare quale ramo sono attivo, ma c'è un problema. Quando creo un nuovo ramo git rendimenti nome-rev maestro anche se io sono sull'altro ramoCome usare git hook pre-commit per fermare i commit al master

$ git branch 
    ignore 
    master 
* set_support 
$ git name-rev --name-only HEAD 
master 

Questo è il mio script.

#!/bin/sh 
# Check to see if we are on master branch. Stop accidental commits 
if [ "`git name-rev --name-only HEAD`" == "master" ] 
then 
    if [ -f i_want_to_commit_to_master ] 
    then 
     rm i_want_to_commit_to_master 
     exit 0 
    else 
     echo "Cannot commit to master branch Adrian" 
     echo "Remember to create file 'touch i_want_to_commit_to_master' to commit to master" 
    fi 
    exit 1 
fi 
exit 0 

Per Mark: ho ricostruito git contro l'ultimo tag stabile e gli stessi risultati. Funziona solo dopo che viene effettuato un commit sul nuovo ramo.

$ mkdir gittest 
$ cd gittest 
$ git init 
Initialized empty Git repository in /home/adrian/gittest/.git/ 
$ touch file1 
$ git add file1 
$ git commit 
[master (root-commit) 7c56424] New file 
0 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 file1 
$ git branch 
* master 
$ git checkout -b new_branch 
Switched to a new branch 'new_branch' 
$ git name-rev --name-only HEAD 
master 
$ git --version 
git version 1.7.7.1 
$ git branch 
    master 
* new_branch 
$ touch file2 
$ git add file2 
$ git commit 
[new_branch 1e038fb] new file 
0 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 file2 
$ git name-rev --name-only HEAD 
new_branch 
+0

Quale versione di git stai usando e su quale sistema operativo? I risultati di 'git branch' seguiti da' git name-rev HEAD' sembrano un bug (sorprendente), se veramente copiato e incollato accuratamente. –

+0

Costruisco git dalla fonte - ultima build era $ git descrivere v1.7.7-rc3 $ git --version git versione 1.7.7-RC3 $ uname -a Linux iceweasel.bluedreamer 2.6.40.3-0.fc15 .x86_64 # 1 SMP mar 16 ago 04:10:59 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux –

risposta

11

Questo comando è utilizzato per trovare un nome descrittivo di un commit. Quello che sta accadendo è che HEAD si sta risolvendo prima con lo sha1 del commit e poi viene determinato un nome. Sto indovinando che è arbitrariamente scegliere il master per il nome quando viene visualizzato per primo in cosa si troverebbe lo .

vorrei solo analizzare l'output di git branch nel test:

"`git branch | grep \* | cut -f2 -d' '` == "master" 

o un modo più diretto sarebbe:

$(git symbolic-ref HEAD 2>/dev/null) == "refs/heads/master" 
+1

Cool - Grazie Adam - Ho apportato una piccola modifica ma ora funziona se ["$ (git symbolic-ref HEAD 2>/dev/null) "==" refs/heads/master "] –

+0

Nice .. Modificherò la risposta a beneficio degli altri. –

8

In alternativa si può usare git rev-parse come suggerito in this answer. Quindi l'espressione if sarebbe:

"$(git rev-parse --abbrev-ref HEAD)" == "master" 
Problemi correlati