GitPython: how can I access the contents of a file in a commit in GitPython?

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{}
    }

. . , .

 #! usr/bin/env python

 __author__ = 'rahkeemg'

 from git import *
 import git, json, subprocess, re


 git_dir = '/home/rahkeemg/Documents/GitRepositories/WhereHows'


 # make an instance of the repository from specified path
 repo = Repo(path=git_dir)

 heads = repo.heads  # obtain the differnet repositories
 master = heads.master  # get the master repository

 print master

 # get all of the commits on the master branch
 commits = list(repo.iter_commits(master))

 cmd = ['java', '-classpath', '/home/rahkeemg/workspace/CSCI499_Java/bin/:/usr/local/lib/*:', 'java_gram.mainJava']

 # start at the very 1st commit, or start at commit 0
 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())

            #add the current file, or blob, to the list for the command to run
            cmd.append(current_file) 
            print entry.abspath

            try:

                #This is scenario where I pass arguments into command as a string
                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)


                # scenario where I pass arguments into command as a list
                j_response = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)

            except subprocess.CalledProcessError as e:
                 print "Error on file: ", current_file

            #Use pop on list to remove the last string, which is the selected file at the moment, to make place for the next file.  
            cmd.pop()
+4
1

, , , . , , , , , , , , , , , .

. , , git, GitPython.

, , " " :

git show <treeish>:<file>

GitPython:

file_contents = repo.git.show('{}:{}'.format(commit.hexsha, entry.path))

, . , tempfile:

f = tempfile.NamedTemporaryFile(delete=False)
f.write(file_contents)
f.close()

# at this point file with name f.name contains contents of
#   the file from path entry.path at revision commit.hexsha
# your program launch goes here, use f.name as filename to be read

os.unlink(f.name) # delete the temp file
+4

All Articles