I am new to GitPython and I am trying to get the contents of a file in a commit. I can get each file from a specific commit, but I get an error message every time I run the command. Now I know that the file exists in GitPython, but every time I run my program, I get the following error:
returned non-zero exit status 1
I am using Python 2.7.6 and Ubuntu Linux 14.04.
I know that the file exists, since I also go directly to git from the command line, check the corresponding commit, search for the file and search for it. I also run the cat command on it and the contents of the file are displayed. Many times, when an error occurs, it says that this file does not exist. What I'm trying to do is to go through each commit using GitPython, get every blob or file from every single commit, and run an external Java program in the contents of this file. The Java program is designed to return a string in Python. To grab the string returned from my Java code, I also use subprocess.check_output. Any help would be appreciated.
I tried passing the command as a list:
cmd = ['java', '-classpath', '/home/rahkeemg/workspace/CSCI499_Java/bin/:/usr/local/lib/*:', 'java_gram.mainJava','absolute/path/to/file']
subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
And I also tried passing the command as a string:
subprocess.check_output('java -classpath /home/rahkeemg/workspace/CSCI499_Java/bin/:/usr/local/lib/*: java_gram.mainJava {file}'.format(file=entry.abspath.strip()), shell=True)
GitPython?
, , , foo.java
:
foo.java
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class foo{
public static void main(String[] args) throws Exception{}
}
.
. , .
__author__ = 'rahkeemg'
from git import *
import git, json, subprocess, re
git_dir = '/home/rahkeemg/Documents/GitRepositories/WhereHows'
repo = Repo(path=git_dir)
heads = repo.heads
master = heads.master
print master
commits = list(repo.iter_commits(master))
cmd = ['java', '-classpath', '/home/rahkeemg/workspace/CSCI499_Java/bin/:/usr/local/lib/*:', 'java_gram.mainJava']
for i in range(len(commits) - 1, 0, -1):
commit = commits[i]
commit_num = len(commits) - 1 - i
print commit_num, ": ", commit.hexsha, '\n', commit.message, '\n'
for entry in commit.tree.traverse():
if re.search(r'\.java', entry.path):
current_file = str(entry.abspath.strip())
cmd.append(current_file)
print entry.abspath
try:
print subprocess.check_output('java -classpath /home/rahkeemg/workspace/CSCI499_Java/bin/:/usr/local/lib/*: java_gram.mainJava {file}'.format(file=entry.abspath.strip()), shell=True)
j_response = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
except subprocess.CalledProcessError as e:
print "Error on file: ", current_file
cmd.pop()