/ / GitPython:GitPythonのコミットでファイルのコンテンツにアクセスする方法-java、linux、python-2.7、gitpython

GitPython:GitPythonのコミットでファイルの内容にアクセスするにはどうしたらいいですか? - java、linux、python-2.7、gitpython

私はGitPythonを初めて使用し、コミット内のファイルの内容。特定のコミットから各ファイルを取得できますが、コマンドを実行するたびにエラーが発生します。これで、ファイルがGitPythonに存在することがわかりましたが、プログラムを実行するたびに、次のエラーが表示されます。

 returned non-zero exit status 1

使っています Python 2.7.6 そして Ubuntu Linux 14.04。

コマンドラインからgitに直接アクセスし、それぞれのコミットをチェックアウトし、ファイルを検索して見つけるため、ファイルが存在することを知っています。私も走る ネコ コマンドとファイルの内容が表示されます。 多くの場合、エラーが表示されると、問題のファイルが存在しないと表示されます。私がやろうとしていることは、GitPythonで各コミットを実行し、個々のコミットからすべてのブロブまたはファイルを取得し、そのファイルのコンテンツで外部Javaプログラムを実行することです。 Javaプログラムは、文字列をPythonに返すように設計されています。 Javaコードから返された文字列をキャプチャするために、私も使用しています subprocess.check_output。どんな助けも大歓迎です。

コマンドをリストとして渡してみました:

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)

また、コマンドを文字列として渡すことも試みました。

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からファイルのコンテンツにアクセスすることは可能ですか? たとえば、コミットがあり、1つのファイルがあるとします 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()

回答:

回答№1は6

まず、コミットをトラバースするときこのような履歴では、ファイルはチェックアウトされません。取得できるのはファイル名だけで、ファイルにつながっているかどうかはわかりませんが、現在チェックアウトされているリビジョンとは異なるリビジョンのファイルにはつながりません。

ただし、これには解決策があります。原則として、あなたができることは何でも git コマンドは、GitPythonで実行できます。

特定のリビジョンからファイルの内容を取得するには、次を実行できます。 私はそのページから取りました

git show <treeish>:<file>

したがって、GitPythonの場合:

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

ただし、それでもファイルがディスクに表示されません。ファイルの実際のパスが必要な場合は、次を使用できます。 一時ファイル

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