我是GitPython新手,我正在尝试在提交中获取文件的内容。我能够从特定的提交中获取每个文件,但每次运行命令时都会收到一个错误。现在,我知道这个文件存在于GitPython中,但是每次我运行我的程序时,我都会得到以下错误:
returned non-zero exit status 1我正在使用Python2.7.6和Ubuntu 14.04。
我知道文件是存在的,因为我还直接从命令行进入Git,签出相应的提交,搜索文件并找到它。我还在上面运行cat命令,并显示文件内容。很多时候,当错误出现时,它会说所讨论的文件不存在。我试图使用GitPython完成每次提交,从每个提交中获取每个blob或文件,并对该文件的内容运行一个外部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访问文件的内容吗?例如,假设存在一个commit,并且该文件中有一个文件,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 different 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 the 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()发布于 2016-05-29 15:26:08
首先,当您像这样遍历提交历史记录时,文件将不会被签出。您所得到的只是文件名,可能会导致文件,也可能不会导致文件,但它肯定不会导致文件从不同的修订比当前签出。
然而,这是有解决办法的。请记住,原则上,您可以使用某些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 filehttps://stackoverflow.com/questions/36429482
复制相似问题