【发布时间】:2016-07-25 13:52:46
【问题描述】:
我是 GitPython 的新手,我正在尝试在提交中获取文件的内容。我能够从特定的提交中获取每个文件,但是每次运行命令时都会出错。现在,我知道该文件存在于 GitPython 中,但每次运行程序时,都会出现以下错误:
returned non-zero exit status 1
我正在使用 Python 2.7.6 和 Ubuntu Linux 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 访问文件的内容? 例如,假设有一个提交并且它有一个文件 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()
【问题讨论】:
-
“我也直接从命令行进入 git,检查相应的提交,搜索文件,然后找到它” - 你的问题是,当你迭代GitPython不执行结帐。
-
提示1:既然Python很神奇,你可以用
for commit_num, commit in reversed(list(enumerate(commits))):代替i、len、commits[i]等等... -
提示 2:
re.search(r'\.java', entry.path)也将匹配像'my.name.is.java.txt'这样的字符串,它根本不必是 Java 源文件。为什么不干脆做entry.path.endswith('.java')?
标签: python-2.7 gitpython