2010-11-12 43 views
1

J'essaye d'écrire un post-commit hook, j'ai un dépôt Git sur un lecteur mappé (V :), msysgit installé dans C: \ Git, et Python dans C: \ Python26. Je cours TortoiseGit sur Windows 7 64 bits.erreur git: ne peut pas apparaître .git/hooks/post-commit: Aucun fichier ou répertoire

Le script est:

#!C:/Python26/python 

import sys 
from subprocess import Popen, PIPE, call 

GIT_PATH = 'C:\Git\bin\git.exe' 
BRANCHES = ['master'] 
TRAC_ENV = 'C:\TRAC_ENV' 
REPO_NAME = 'core' 

def call_git(command, args): 
    return Popen([GIT_PATH, command] + args, stdout=PIPE).communicate()[0] 

def handle_ref(old, new, ref): 
    # If something else than the master branch (or whatever is contained by the 
    # constant BRANCHES) was pushed, skip this ref. 
    if not ref.startswith('refs/heads/') or ref[11:] not in BRANCHES: 
     return 

    # Get the list of hashs for commits in the changeset. 
    args = (old == '0' * 40) and [new] or [new, '^' + old] 
    pending_commits = call_git('rev-list', args).splitlines()[::-1] 

    call(["trac-admin", TRAC_ENV, "changeset", "added", REPO_NAME] + pending_commits) 

if __name__ == '__main__': 
    for line in sys.stdin: 
     handle_ref(*line.split()) 

Si je lance la commande « git commit ... » de la ligne de commande, il ne semble pas fonctionner même le script de crochet du tout.

+0

Donc ... l'erreur dans le titre de votre question se produit lorsque vous essayez de valider via TortoiseGit, et une validation de ligne de commande fonctionne correctement (juste sans exécuter le crochet), non? Est-ce que le hook est bien dans '.git/hooks/post-commit', et exécutable? – Cascabel

Répondre

1

Selon le githooks man page,

[The post-commit hook] is invoked by git-commit. It takes no parameter, and is invoked after a commit is made.

Il ne prend aucun paramètre. En Python, cela signifie que sys.argv [1:] sera une liste vide. La page de manuel ne dit pas quoi, si quelque chose, est envoyé à stdin, mais probablement rien. Vérifions cela.

J'ai fait un petit répertoire git et mettre cela en .git/crochets/post-commit:

#!/usr/bin/env python 
import sys 
def handle_ref(old, new, ref): 
    with open('/tmp/out','w') as f: 
     f.write(old,new,ref) 
if __name__ == '__main__': 
    with open('/tmp/out','w') as f: 
     f.write('post-commit running') 
    for line in sys.stdin: 
     handle_ref(*line.split()) 
     with open('/tmp/out','w') as f: 
      f.write('Got here') 

et a rendu exécutable.

Quand je fais un commettras je vois le/tmp/out fichier a été créé, mais son seul contenu sont

post-commit running 

Donc le script a couru, mais la boucle for line in sys.stdin: ne rien puisque rien est envoyé à sys .stdin.

Vous allez devoir générer les arguments à envoyer à handle_ref d'une autre manière, peut-être à travers un appel de sous-processus à une commande git.