我正在使用“git grep -e”来查找与内容中的模式匹配的文件。 我查看了jgit的索引,找不到“grep”,但最接近的是PatternMatchRevFilter。这类似于“git grep”正在做什么?
在官方JGit用户指南中,它显示“TODO谈论过滤器”。 :)有人有一个如何使用此过滤器的示例?
谢谢!
杰森
PS。这可能是一个单独的问题 - 如何为搜索指定分支?
答案 0 :(得分:4)
首先,PatternMatchRevFilter不是您想要的。 RevFilter用于在步行期间选择某些修订(提交)。所以它相当于git log --grep=pattern
。
你想要的是走一个单一版本的树并阅读blob内容。
还没有一个简单易用的API,相当于git grep
。您已合并了较低级别的API:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.TreeWalk;
public class Grep {
private final Repository repository;
private final Pattern pattern;
private final String revName;
public Grep(Repository repository, Pattern pattern, String revName) {
this.repository = repository;
this.pattern = pattern;
this.revName = revName;
}
public void grepPrintingResults() throws IOException {
ObjectReader objectReader = repository.newObjectReader();
try {
ObjectId commitId = repository.resolve(revName);
impl(objectReader, commitId);
} finally {
objectReader.release();
}
}
private void impl(ObjectReader objectReader, ObjectId commitId)
throws IOException {
TreeWalk treeWalk = new TreeWalk(objectReader);
RevWalk revWalk = new RevWalk(objectReader);
RevCommit commit = revWalk.parseCommit(commitId);
CanonicalTreeParser treeParser = new CanonicalTreeParser();
treeParser.reset(objectReader, commit.getTree());
int treeIndex = treeWalk.addTree(treeParser);
treeWalk.setRecursive(true);
while (treeWalk.next()) {
AbstractTreeIterator it = treeWalk.getTree(treeIndex,
AbstractTreeIterator.class);
ObjectId objectId = it.getEntryObjectId();
ObjectLoader objectLoader = objectReader.open(objectId);
if (!isBinary(objectLoader.openStream())) {
List<String> matchedLines = getMatchedLines(objectLoader
.openStream());
if (!matchedLines.isEmpty()) {
String path = it.getEntryPathString();
for (String matchedLine : matchedLines) {
System.out.println(path + ":" + matchedLine);
}
}
}
}
}
private List<String> getMatchedLines(InputStream stream) throws IOException {
BufferedReader buf = null;
try {
List<String> matchedLines = new ArrayList<String>();
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
buf = new BufferedReader(reader);
String line;
while ((line = buf.readLine()) != null) {
Matcher m = pattern.matcher(line);
if (m.find()) {
matchedLines.add(line);
}
}
return matchedLines;
} finally {
if (buf != null) {
buf.close();
}
}
}
private static boolean isBinary(InputStream stream) throws IOException {
try {
return RawText.isBinary(stream);
} finally {
try {
stream.close();
} catch (IOException e) {
// Ignore, we were just reading
}
}
}
}
像这样使用:
Grep grep = new Grep(repository, Pattern.compile("test"), "HEAD");
grep.grepPrintingResults();