2012-03-19 12 views
5

Sto provando ad eseguire un semplice comando git usando il seguente script python.cercando di eseguire il comando git usando lo script python

#!/usr/bin/python 

import commands 
import subprocess 
import os 
import sys 

pr = subprocess.Popen("/usr/bin/git log" , cwd = os.path.dirname('/ext/home/rakesh.kumar/workspace/myproject'), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 
(out, error) = pr.communicate() 


print "Error : " + str(error) 
print "out : " + str(out) 

ma sto ottenendo il seguente errore anche se sto facendo funzionare lo script Python nella stessa directory in cui git reposetory è.

Error : fatal: Not a git repository (or any of the parent directories): .git 

sospettavo il git potrebbe ottenuto correputed, ma i file git sono belle e comandi git funziona se eseguo il prompt dei comandi normali.

Ho provato a cercare in rete ma non ho potuto ottenere informazioni utili. Per favore aiutatelo sarà molto apprezzato.

+0

Cosa c'è di sbagliato nel codice che include 79 caratteri? –

risposta

4

Prova questo:

#!/usr/bin/python 

import commands 
import subprocess 
import os 
import sys 

pr = subprocess.Popen("/usr/bin/git log" , cwd = os.path.dirname('/ext/home/rakesh.kumar/workspace/myproject/'), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) 
(out, error) = pr.communicate() 


print "Error : " + str(error) 
print "out : " + str(out) 

percorso della directory dovrebbe avere '/' alla fine.

+0

Grazie per la pronta risposta Adam, ora funziona. – Rakesh

+0

Si noti che questo errore può verificarsi anche se si imposta la variabile di ambiente 'GIT_DIR' (in particolare su un percorso relativo, come può succedere in git hooks) –

5

Il problema è l'utilizzo del os.path.dirname():

os.path.dirname('/ext/home/rakesh.kumar/workspace/myproject') 

vi darà:

>>> os.path.dirname('/ext/home/rakesh.kumar/workspace/myproject') 
'/ext/home/rakesh.kumar/workspace' 

che, scommetto, non è ciò che si vuole.

Problemi correlati